DragMove
Move an element relative to its original position while preserving its space in normal flow. Supports handles, bounds, axes, grids, keyboard movement, and Escape cancellation.
Import
import { HuiDragMove } from '@nscode/handy-ui'
import type { DragMovePosition, DragMoveProps, DragMoveExpose } from '@nscode/handy-ui'
Examples
Whole element and slot state
Without handle, the entire element is draggable. Focus it with Tab and use arrow keys to move.
<script setup lang="ts">
import { HuiDragMove } from '@nscode/handy-ui'
</script>
<template>
<div style="min-height: 180px; padding: 16px; border: 1px dashed var(--hui-border-color)">
<HuiDragMove
bounds="parent"
style="width: 180px; padding: 16px; background: var(--hui-fill-color-light)"
>
<template #default="{ dragging }">{{
dragging ? 'Dragging' : 'Drag me or use arrow keys'
}}</template>
</HuiDragMove>
</div>
</template>
Title handle
handle selects the first matching descendant. The component adds focus, keyboard, and accessibility support. Inputs, buttons, links, and editable areas do not start dragging by default.
<script setup lang="ts">
import { HuiDragMove, HuiInput, HuiButton, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<div style="min-height: 230px; border: 1px dashed var(--hui-border-color)">
<HuiDragMove
handle=".move-title"
bounds="parent"
style="width: 260px; border: 1px solid var(--hui-border-color); background: var(--hui-bg-color)"
>
<div class="move-title" style="padding: 12px; background: var(--hui-fill-color-light)">
Drag this title
</div>
<HuiSpace direction="vertical" fill style="padding: 12px">
<HuiInput placeholder="Typing does not move the panel" />
<HuiButton>Normal button</HuiButton>
</HuiSpace>
</HuiDragMove>
</div>
</template>
Viewport, parent, and no bounds
bounds defaults to viewport. Use parent for the parent rectangle or false for unrestricted movement. Translation preserves the original layout space.
<script setup lang="ts">
import { HuiDragMove, HuiSpace } from '@nscode/handy-ui'
import type { DragMoveBounds } from '@nscode/handy-ui'
const boundaries: DragMoveBounds[] = ['viewport', 'parent', false]
</script>
<template>
<HuiSpace direction="vertical" fill>
<div
v-for="boundary in boundaries"
:key="String(boundary)"
style="height: 130px; border: 1px dashed var(--hui-border-color)"
>
<HuiDragMove
:bounds="boundary"
style="width: 160px; padding: 16px; background: var(--hui-fill-color-light)"
>bounds={{ String(boundary) }}</HuiDragMove
>
</div>
</HuiSpace>
</template>
Selector and element bounds
Selectors resolve in the current document. A template reference can supply an element. Bounds use the actual rectangle measured at the start of each drag.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDragMove, HuiSpace } from '@nscode/handy-ui'
const boundary = ref<HTMLElement>()
</script>
<template>
<HuiSpace direction="vertical" fill>
<div
id="drag-move-selector-boundary"
style="height: 140px; border: 1px dashed var(--hui-border-color)"
>
<HuiDragMove
bounds="#drag-move-selector-boundary"
style="width: 180px; padding: 16px; background: var(--hui-fill-color-light)"
>Selector bounds</HuiDragMove
>
</div>
<div ref="boundary" style="height: 140px; border: 1px dashed var(--hui-border-color)">
<HuiDragMove
:bounds="boundary ?? false"
style="width: 180px; padding: 16px; background: var(--hui-fill-color-light)"
>HTMLElement bounds</HuiDragMove
>
</div>
</HuiSpace>
</template>
Axis constraints
both, x, and y use physical directions, including keyboard movement. RTL does not reverse them.
<script setup lang="ts">
import { HuiDragMove, HuiSpace } from '@nscode/handy-ui'
import type { DragMoveAxis } from '@nscode/handy-ui'
const axes: DragMoveAxis[] = ['both', 'x', 'y']
</script>
<template>
<HuiSpace direction="vertical" fill>
<div
v-for="axis in axes"
:key="axis"
style="height: 120px; border: 1px dashed var(--hui-border-color)"
>
<HuiDragMove
:axis="axis"
bounds="parent"
style="width: 140px; padding: 16px; background: var(--hui-fill-color-light)"
>axis={{ axis }}</HuiDragMove
>
</div>
</HuiSpace>
</template>
Grid snapping
Pointer deltas snap relative to each press, then clamp to bounds. The final value can fall between grid lines. Keyboard steps remain 10px or 1px without grid rounding.
<script setup lang="ts">
import { HuiDragMove, HuiSpace } from '@nscode/handy-ui'
const grids: [number, number][] = [
[20, 20],
[40, 10]
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<div
v-for="grid in grids"
:key="grid.join(',')"
style="height: 130px; border: 1px dashed var(--hui-border-color)"
>
<HuiDragMove
:grid="grid"
bounds="parent"
style="width: 160px; padding: 16px; background: var(--hui-fill-color-light)"
>grid=[{{ grid.join(', ') }}]</HuiDragMove
>
</div>
</HuiSpace>
</template>
Binding and reset
reset() requests { x: 0, y: 0 } and emits update:position. It ignores axes, grids, and bounds. In controlled mode, the parent still decides whether to accept the request.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDragMove, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { DragMoveExpose, DragMovePosition } from '@nscode/handy-ui'
const move = ref<DragMoveExpose>()
const position = ref<DragMovePosition>({ x: 20, y: 10 })
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><HuiButton @click="move?.reset()">Reset</HuiButton
><span>x={{ position.x }}, y={{ position.y }}</span></HuiSpace
>
<div style="height: 170px; border: 1px dashed var(--hui-border-color)">
<HuiDragMove
ref="move"
v-model:position="position"
bounds="parent"
style="width: 180px; padding: 16px; background: var(--hui-fill-color-light)"
>Move and watch the binding</HuiDragMove
>
</div>
</HuiSpace>
</template>
Disabled and tag
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDragMove, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const disabled = ref(true)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace><span>Disabled</span><HuiSwitch v-model="disabled" /></HuiSpace>
<div style="height: 150px; border: 1px dashed var(--hui-border-color)">
<HuiDragMove
tag="section"
:disabled="disabled"
bounds="parent"
style="width: 220px; padding: 16px; background: var(--hui-fill-color-light)"
>A section; disabled blocks pointer and keyboard movement</HuiDragMove
>
</div>
</HuiSpace>
</template>
Events, keyboard, and Escape
Focus the element or handle with Tab. Arrow keys move by 10px; Shift+arrow moves by 1px. Escape during a pointer drag restores the position at the press and prevents that Escape from reaching an outer overlay.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDragMove, HuiSpace } from '@nscode/handy-ui'
import type { DragMovePosition } from '@nscode/handy-ui'
const logs = ref<string[]>([])
const log = (name: string, position: DragMovePosition, cancelled = false) => {
logs.value = [
`${name}: ${position.x}, ${position.y}; cancelled=${cancelled}`,
...logs.value
].slice(0, 5)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<div style="height: 150px; border: 1px dashed var(--hui-border-color)">
<HuiDragMove
bounds="parent"
@drag-start="log('Start', $event)"
@drag="log('Move', $event)"
@drag-end="(position, cancelled) => log('End', position, cancelled)"
style="width: 220px; padding: 16px; background: var(--hui-fill-color-light)"
>Drag and press Escape, or focus and use arrow keys</HuiDragMove
>
</div>
<ul>
<li v-for="(item, index) in logs" :key="index">{{ item }}</li>
</ul>
</HuiSpace>
</template>
Preserving transforms and layout
The component manages the independent CSS translate property with priority over the user-supplied inline value. Inline and class-based transform, width, height, top, and left remain intact. Put any additional application translation on a wrapper.
<script setup lang="ts">
import { HuiDragMove } from '@nscode/handy-ui'
</script>
<template>
<div style="height: 190px; padding: 24px; border: 1px dashed var(--hui-border-color)">
<HuiDragMove
bounds="parent"
style="width: 180px; padding: 16px; transform: rotate(-4deg); background: var(--hui-fill-color-light)"
>Rotation stays; dragging only changes translate</HuiDragMove
>
</div>
</template>
Panel without a mask and custom exclusions
cancel replaces the default exclusion selector, so include interactive elements that should stay excluded. An empty string allows a button to serve as a handle. The component manages movement; window stacking, closing, and focus trapping belong to the application.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDragMove, HuiButton, HuiSpace } from '@nscode/handy-ui'
const message = ref('Drag the title. The toolbar does not start dragging.')
const floatingPosition = ref({ x: 24, y: 20 })
</script>
<template>
<div style="height: 260px; position: relative; border: 1px dashed var(--hui-border-color)">
<HuiDragMove
handle=".floating-title"
cancel=".floating-tools, button, input"
bounds="parent"
v-model:position="floatingPosition"
style="position: absolute; width: 280px; border: 1px solid var(--hui-border-color); border-radius: var(--hui-border-radius); background: var(--hui-bg-color); box-shadow: var(--hui-shadow)"
>
<div class="floating-title" style="padding: 12px; background: var(--hui-fill-color-light)">
<HuiSpace fill style="justify-content: space-between"
><span>Floating panel</span
><span class="floating-tools"
><HuiButton size="small" @click="message = 'Toolbar button clicked'"
>Tool</HuiButton
></span
></HuiSpace
>
</div>
<p style="padding: 12px">{{ message }}</p>
</HuiDragMove>
</div>
</template>
Internal position and controlled updates
Omitting position starts at the origin and saves movement, including listener-only usage. Supplying position makes it controlled even without a listener; explicit undefined means the origin. The parent accepts, transforms or rejects requests; displayed translation and dragEnd reflect the actual position. Removing the prop restores saved internal position. Control mode changes warn in development and stop old pointer sessions. Requests use copied objects so listeners cannot mutate saved internal position. Reset still requests the origin without changing the controlled parent value.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDragMove, HuiSpace } from '@nscode/handy-ui'
import type { DragMoveExpose, DragMovePosition } from '@nscode/handy-ui'
const moveRedraw = ref(0)
const moveLast = ref('No request')
const moveBound = ref<DragMovePosition>({ x: 0, y: 0 })
const moveLocal = ref<DragMoveExpose>()
</script>
<template>
<HuiSpace class="drag-move-q1-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton class="move-redraw" @click="moveRedraw++"
>Parent render {{ moveRedraw }}</HuiButton
>
<HuiButton class="move-reset" @click="moveLocal?.reset()">Reset the first block</HuiButton>
</HuiSpace>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 24px;
"
>
<div class="move-local">
<p>Omitted position</p>
<div style="height: 90px">
<HuiDragMove
ref="moveLocal"
:bounds="false"
style="
width: 96px;
height: 52px;
background: var(--hui-fill-color-light);
"
>Keyboard movement</HuiDragMove
>
</div>
</div>
<div class="move-listener">
<p>Update listener only</p>
<div style="height: 90px">
<HuiDragMove
:bounds="false"
style="
width: 96px;
height: 52px;
background: var(--hui-fill-color-light);
"
@update:position="moveLast = $event.x + ',' + $event.y"
>Keyboard movement</HuiDragMove
>
</div>
</div>
<div class="move-empty">
<p>Explicit undefined keeps the origin</p>
<div style="height: 90px">
<HuiDragMove
:position="undefined"
:bounds="false"
style="
width: 96px;
height: 52px;
background: var(--hui-fill-color-light);
"
>Fixed origin</HuiDragMove
>
</div>
</div>
<div class="move-fixed">
<p>Fixed 20,10 rejects updates</p>
<div style="height: 90px">
<HuiDragMove
:position="{ x: 20, y: 10 }"
:bounds="false"
style="
width: 96px;
height: 52px;
background: var(--hui-fill-color-light);
"
>Fixed position</HuiDragMove
>
</div>
</div>
<div class="move-bound">
<p>Two-way position binding</p>
<div style="height: 90px">
<HuiDragMove
v-model:position="moveBound"
:bounds="false"
style="
width: 96px;
height: 52px;
background: var(--hui-fill-color-light);
"
>Keyboard movement</HuiDragMove
>
</div>
</div>
</div>
<p class="move-status">
Bound position: {{ moveBound.x }},{{ moveBound.y }};Latest request: {{ moveLast }}
</p>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| position | DragMovePosition | { x: 0, y: 0 } | Supplied props are controlled, including undefined at the origin; omitted props save position; supports v-model:position |
| handle | string | - | First matching descendant; the whole element is draggable when omitted |
| bounds | DragMoveBounds | 'viewport' | Viewport, parent, document selector, element, or false |
| axis | 'x' | 'y' | 'both' | 'both' | Physical movement directions |
| grid | [number, number] | - | Pointer grid spacing per axis; positive finite numbers |
| cancel | string | See below | Excluded descendants; an empty string excludes nothing |
| disabled | boolean | false | Disable pointer and keyboard movement |
| tag | string | 'div' | Native root tag |
Default cancel: input, textarea, select, button, a[href], [contenteditable]:not([contenteditable="false"]).
Events
| Event | Parameters | Description |
|---|---|---|
| update:position | (position: DragMovePosition) | Position request; also emitted by reset and Escape restoration |
| dragStart | (position: DragMovePosition) | Pointer or keyboard movement starts |
| drag | (position: DragMovePosition) | Candidate position; emitted on Escape restoration, but not reset |
| dragEnd | (position: DragMovePosition, cancelled: boolean) | Actual position after parent feedback; only Escape sets cancelled to true |
The public start and end events are declared and emitted as dragStart and dragEnd. Vue templates can still use @drag-start and @drag-end; render functions use onDragStart and onDragEnd. Only the new declared events are emitted. The final position, cancelled flag, event order and cancellation behavior remain unchanged.
Slots
| Slot | Parameters | Description |
|---|---|---|
| default | DragMoveSlotProps | Content; dragging indicates an active pointer drag |
Methods
| Method | Parameters | Description |
|---|---|---|
| reset | None | Stop the current drag and request the original position through update:position |
Types
Public method refs use DragMoveExpose. The old DragMoveMethods name remains a deprecated direct alias until 1.0; methods, arguments, and return values are unchanged.
import type {
DragMovePosition,
DragMoveBounds,
DragMoveAxis,
DragMoveProps,
DragMoveEmits,
DragMoveSlotProps,
DragMoveExpose
} from '@nscode/handy-ui'
interface DragMovePosition {
x: number
y: number
}
type DragMoveBounds = 'parent' | 'viewport' | false | string | HTMLElement
type DragMoveAxis = 'x' | 'y' | 'both'
interface DragMoveSlotProps {
dragging: boolean
}
interface DragMoveExpose {
reset: () => void
}
/** @deprecated 请使用DragMoveExpose,将在1.0移除。 */
type DragMoveMethods = DragMoveExpose