RDraggableCard
The component uses Naive UI Card and Interact.js. It teleports the card to an application-managed container outside the original parent's layout flow. With a title, drag the header. Otherwise, drag the content. Resizing is outside the component's interaction model.
| Prop | Default | Description |
|---|---|---|
dad | true | Enables dragging. |
restrictionElement | 'body' | Movement boundary. Accepts a CSS selector, an element, or a target Vue can unwrap. Selectors use the first match. |
restrictRectOptions | Default boundary, endOnly: true | Overrides the Interact.js restriction. |
defaultPosition | { x: 0, y: 0 } | Offset relative to the boundary or a named preset. |
padding | None | { x, y } spacing from the boundary on initial placement. |
width / zIndex | 600 / unspecified | Card width and stacking order. |
animation | false | Enables the entrance transition. |
Position presets are top-left, top-right, bottom-left, bottom-right, center, top-center, and bottom-center. Replacing defaultPosition, padding, or the boundary reference repositions the card. Toggling dad keeps its current position. Drag events and position persistence are internal. Use a selector that exists in the layout and a boundary with a definite size. Drag DOM and interaction initialize after browser mount. Disabling dragging or unmounting releases listeners, and the last card to unmount removes the shared container. The component inherits Card props and slots. Its entrance transition honors reduced motion.
Interactive example
View example source
import { useI18n } from '@/hooks'
import RDraggableCard from '@ray-template/ui/draggable-card'
import type { DefaultPosition } from '@ray-template/ui/draggable-card'
import { NButton, NCard, NFlex, NSelect, NSwitch, NTag } from 'naive-ui'
import DemoPage from './components/DemoPage'
const positions = [
'center',
'top-center',
'bottom-center',
'top-left',
'top-right',
'bottom-left',
'bottom-right',
] as const
const options = positions.map((value) => ({ label: value, value }))
export default defineComponent({
name: 'DraggableCardDemo',
setup() {
const { t } = useI18n('demoDetails')
const target = useTemplateRef<HTMLElement>('target')
const position = ref<DefaultPosition>('center')
const draggable = ref(true)
const scope = ref<'bounded' | 'body' | null>(null)
return () => (
<DemoPage title={t('drag.title')} description={t('drag.intro')}>
<NCard title={t('drag.stage')}>
<div class="r-demo-card-stack">
<NFlex align="center">
<NSelect
value={position.value as string}
options={options}
style={{ width: '150px' }}
aria-label={t('drag.position')}
onUpdateValue={(value: DefaultPosition) => {
position.value = value
}}
/>
<NSwitch
v-model:value={draggable.value}
aria-label={t('drag.enable')}
/>
<NTag bordered={false}>
{draggable.value ? t('drag.enable') : t('drag.locked')}
</NTag>
<NButton
type="primary"
onClick={() => {
scope.value = scope.value === 'bounded' ? null : 'bounded'
}}
>
{t('drag.bounded')}
</NButton>
<NButton
onClick={() => {
scope.value = scope.value === 'body' ? null : 'body'
}}
>
{t('drag.body')}
</NButton>
</NFlex>
<div ref="target" class="r-demo-stage r-demo-stage--drag">
<span>{t('drag.bounds')}</span>
</div>
</div>
</NCard>
{scope.value && target.value && (
<RDraggableCard
restrictionElement={
scope.value === 'bounded' ? target.value : 'body'
}
width="min(220px, calc(100vw - 56px))"
defaultPosition={position.value}
title={t(`drag.${scope.value}`)}
closable
dad={draggable.value}
onClose={() => {
scope.value = null
}}
>
<NTag bordered={false}>
{draggable.value ? t('drag.enable') : t('drag.locked')}
</NTag>
</RDraggableCard>
)}
</DemoPage>
)
},
})import { RDraggableCard } from '@/components'
export const renderFloatingHelp = () => (
<RDraggableCard
title="Help"
width={320}
defaultPosition="bottom-right"
padding={{ x: 24, y: 24 }}
>
Drag the header to move this card.
</RDraggableCard>
)