DragMove 拖曳元素
讓元素相對原位置移動,保留原文件流占位。支援把手、邊界、軸向、網格吸附、鍵盤與 Esc 取消。
引入
import { HuiDragMove } from '@nscode/handy-ui'
import type { DragMovePosition, DragMoveProps, DragMoveMethods } from '@nscode/handy-ui'
範例
整塊拖曳與狀態插槽
不設定 handle 時,整塊內容都能拖曳。Tab 聚焦後用方向鍵移動。
<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 ? '正在拖曳' : '拖曳我,或用方向鍵移動'
}}</template>
</HuiDragMove>
</div>
</template>
只用標題欄拖曳
handle 在元件內尋找第一個匹配元素,並補齊聚焦、鍵盤與無障礙屬性。輸入框、按鈕、連結及可編輯區域預設不會開始拖曳。
<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)">
從標題欄拖曳
</div>
<HuiSpace direction="vertical" fill style="padding: 12px">
<HuiInput placeholder="輸入不會移動面板" />
<HuiButton>普通按鈕</HuiButton>
</HuiSpace>
</HuiDragMove>
</div>
</template>
視口、父元素與不限制
bounds 預設是 viewport;parent 按父元素矩形限制,false 不限制。位移不會改變版面配置佔位,範例預留了移動空間。
<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>
選擇器與元素邊界
選擇器在目前文件內查找,元素形式可用範本引用提供。邊界按拖曳開始時的實際矩形測量。
<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)"
>選擇器邊界</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 邊界</HuiDragMove
>
</div>
</HuiSpace>
</template>
軸向限制
both、x、y 都是物理方向,不隨 RTL 翻轉;鍵盤也遵守同樣的限制。
<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>
網格吸附
指標位移相對每次按下位置吸附,再限制於邊界內,因此最後位置未必落在網格。鍵盤維持 10px 或 1px 步長,不依網格取整。
<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>
雙向綁定與恢復原位
reset() 請求回到 { x: 0, y: 0 } 並發出 update:position,不受軸向、網格或邊界限制。受控模式仍由父元件決定是否接受。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDragMove, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { DragMoveMethods, DragMovePosition } from '@nscode/handy-ui'
const move = ref<DragMoveMethods>()
const position = ref<DragMovePosition>({ x: 20, y: 10 })
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><HuiButton @click="move?.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)"
>移動後觀察綁定值</HuiDragMove
>
</div>
</HuiSpace>
</template>
停用與外層標籤
<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>停用</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)"
>section 標籤;停用時不響應指標或鍵盤</HuiDragMove
>
</div>
</HuiSpace>
</template>
事件、鍵盤與 Esc
Tab 聚焦整塊元素或把手:方向鍵每次 10px,Shift+方向鍵每次 1px。指標拖曳中按 Esc 回到按下時的位置,這次 Esc 不會傳到外層彈層。
<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('開始', $event)"
@drag="log('移動', $event)"
@drag-end="(position, cancelled) => log('結束', position, cancelled)"
style="width: 220px; padding: 16px; background: var(--hui-fill-color-light)"
>拖曳後按 Esc,或聚焦後用方向鍵</HuiDragMove
>
</div>
<ul>
<li v-for="(item, index) in logs" :key="index">{{ item }}</li>
</ul>
</HuiSpace>
</template>
保留旋轉與版面配置樣式
位移使用獨立的 CSS translate,由元件優先管理;使用者原有的行內或類別 transform、width、height、top、left 都會保留。不要同時依賴業務自身的 translate,可將該變換移至外層容器。
<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)"
>旋轉保留,拖曳只改變 translate</HuiDragMove
>
</div>
</template>
無遮罩小浮窗與自訂排除區
cancel 會取代預設排除選擇器,須自行保留應排除的互動元素。空字串可允許按鈕充當把手。元件只負責移動,不管理視窗層級、關閉或焦點陷阱。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiDragMove, HuiButton, HuiSpace } from '@nscode/handy-ui'
const message = ref('拖曳標題欄,工具區不會開始拖曳。')
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>小浮窗</span
><span class="floating-tools"
><HuiButton size="small" @click="message = '已點擊工具按鈕'">工具</HuiButton></span
></HuiSpace
>
</div>
<p style="padding: 12px">{{ message }}</p>
</HuiDragMove>
</div>
</template>
位移自行保存與受控回填
未傳入 position 或只有更新監聽器時,從原點開始自行保存位移。傳入即受控,無監聽器也不自行改變;明確 undefined 以原點顯示。父元件可接受、改寫或拒絕請求,CSS 位移與 dragEnd 皆依實際位置。移除屬性恢復原內部位置,切換模式會警告並結束舊指標操作。更新事件使用獨立副本,監聽器修改請求物件不會篡改內部位置;reset 仍只請求原點,不修改父物件。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDragMove, HuiSpace } from '@nscode/handy-ui'
import type { DragMoveMethods, DragMovePosition } from '@nscode/handy-ui'
const moveRedraw = ref(0)
const moveLast = ref('尚無請求')
const moveBound = ref<DragMovePosition>({ x: 0, y: 0 })
const moveLocal = ref<DragMoveMethods>()
</script>
<template>
<HuiSpace class="drag-move-q1-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton class="move-redraw" @click="moveRedraw++">父重繪 {{ moveRedraw }}</HuiButton>
<HuiButton class="move-reset" @click="moveLocal?.reset()">恢復第一塊原位</HuiButton>
</HuiSpace>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 24px;
"
>
<div class="move-local">
<p>未傳位移,自行保存</p>
<div style="height: 90px">
<HuiDragMove
ref="moveLocal"
:bounds="false"
style="
width: 96px;
height: 52px;
background: var(--hui-fill-color-light);
"
>鍵盤移動</HuiDragMove
>
</div>
</div>
<div class="move-listener">
<p>僅監聽更新</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"
>鍵盤移動</HuiDragMove
>
</div>
</div>
<div class="move-empty">
<p>明確undefined,保持原點</p>
<div style="height: 90px">
<HuiDragMove
:position="undefined"
:bounds="false"
style="
width: 96px;
height: 52px;
background: var(--hui-fill-color-light);
"
>固定原點</HuiDragMove
>
</div>
</div>
<div class="move-fixed">
<p>固定20,10,父拒絕更新</p>
<div style="height: 90px">
<HuiDragMove
:position="{ x: 20, y: 10 }"
:bounds="false"
style="
width: 96px;
height: 52px;
background: var(--hui-fill-color-light);
"
>固定位置</HuiDragMove
>
</div>
</div>
<div class="move-bound">
<p>雙向綁定位移</p>
<div style="height: 90px">
<HuiDragMove
v-model:position="moveBound"
:bounds="false"
style="
width: 96px;
height: 52px;
background: var(--hui-fill-color-light);
"
>鍵盤移動</HuiDragMove
>
</div>
</div>
</div>
<p class="move-status">
綁定位置:{{ moveBound.x }},{{ moveBound.y }};最近請求:{{ moveLast }}
</p>
</HuiSpace>
</template>
Props
| 屬性 | 型別 | 預設值 | 說明 |
|---|---|---|---|
| position | DragMovePosition | { x: 0, y: 0 } | 傳入即受控(含undefined,按原點);未傳或僅監聽時從原點保存,支援v-model:position |
| handle | string | - | 元件內部第一個匹配的把手;不設時整塊可拖曳 |
| bounds | DragMoveBounds | 'viewport' | 視口、父元素、文件內選擇器、元素或 false |
| axis | 'x' | 'y' | 'both' | 'both' | 可移動的物理方向 |
| grid | [number, number] | - | 指標每軸的吸附間隔,必須大於 0 且有限 |
| cancel | string | 見下方 | 不開始拖曳的後代選擇器;空字串不排除 |
| disabled | boolean | false | 停用指標和鍵盤移動 |
| tag | string | 'div' | 外層原生標籤 |
預設 cancel:input, textarea, select, button, a[href], [contenteditable]:not([contenteditable="false"])。
Events
| 事件 | 參數 | 說明 |
|---|---|---|
| update:position | (position: DragMovePosition) | 請求更新位移,reset 和 Esc 恢復也觸發 |
| dragStart | (position: DragMovePosition) | 指標或鍵盤移動開始 |
| drag | (position: DragMovePosition) | 候選位移;Esc 恢復時也觸發,reset 不觸發 |
| dragEnd | (position: DragMovePosition, cancelled: boolean) | 回填後的實際結束位置;只有 Esc 取消為 true |
公開開始與結束事件宣告和實際發送統一為 dragStart、dragEnd。Vue範本仍可使用 @drag-start、@drag-end,渲染函式使用 onDragStart、onDragEnd,只發新宣告事件。最終實際位移、取消標記、通知順序與取消規則保持。
Slots
| 插槽 | 參數 | 說明 |
|---|---|---|
| default | DragMoveSlotProps | 內容;dragging 表示指標正在拖曳 |
方法
| 方法 | 參數 | 說明 |
|---|---|---|
| reset | 無 | 停止目前拖曳,請求回到原位並發出 update:position |
型別
公開方法使用 DragMoveExpose。舊 DragMoveMethods 保留為標記 deprecated 的直接別名至 1.0;方法名、參數與回傳值不變。
import type {
DragMovePosition,
DragMoveBounds,
DragMoveAxis,
DragMoveProps,
DragMoveEmits,
DragMoveSlotProps,
DragMoveMethods
} 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 DragMoveMethods {
reset: () => void
}