@m3ui-vue/m3ui-vue 0.8.4 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/{MMenuItem-DiilFeDO.js → MMenuItem-BmPLAyv5.js} +64 -62
- package/dist/{MMenuItem-DiilFeDO.js.map → MMenuItem-BmPLAyv5.js.map} +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MChatBubble.vue.d.ts +4 -2
- package/dist/components/MCheckbox.vue.d.ts +2 -0
- package/dist/components/MDragDropList.vue.d.ts +19 -6
- package/dist/components/MFileUpload.vue.d.ts +16 -4
- package/dist/components/MKanban.vue.d.ts +15 -6
- package/dist/components/MTable.vue.d.ts +8 -0
- package/dist/components/MTransferList.vue.d.ts +9 -3
- package/dist/components/_MTransferPane.vue.d.ts +39 -0
- package/dist/composables/useLocale.d.ts +24 -0
- package/dist/locales/index.js +175 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +3600 -2696
- package/dist/m3ui.js.map +1 -1
- package/dist/rich-text-editor.js +2 -2
- package/dist/styles.css +1 -1
- package/dist/{useLocale-DbREg9Zn.js → useLocale-4afAzuuP.js} +26 -2
- package/dist/useLocale-4afAzuuP.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MChatBubble.test.ts +33 -0
- package/src/__tests__/MDragDropList.test.ts +194 -0
- package/src/__tests__/MFileUpload.test.ts +78 -0
- package/src/__tests__/MKanban.test.ts +150 -0
- package/src/__tests__/MMenu.test.ts +84 -0
- package/src/__tests__/MPullToRefresh.test.ts +67 -0
- package/src/__tests__/MRating.test.ts +72 -0
- package/src/__tests__/MTransferList.test.ts +76 -0
- package/src/components/MChatBubble.vue +107 -20
- package/src/components/MCheckbox.vue +3 -0
- package/src/components/MContextMenu.vue +10 -1
- package/src/components/MDragDropList.vue +485 -56
- package/src/components/MFileUpload.vue +56 -20
- package/src/components/MKanban.vue +720 -78
- package/src/components/MMenu.vue +10 -1
- package/src/components/MNavigationRail.vue +35 -23
- package/src/components/MPullToRefresh.vue +101 -35
- package/src/components/MRating.vue +163 -67
- package/src/components/MResult.vue +19 -9
- package/src/components/MTable.vue +10 -3
- package/src/components/MTransferList.vue +133 -130
- package/src/components/_MTransferPane.vue +182 -0
- package/src/composables/useLocale.ts +68 -0
- package/src/locales/de.ts +34 -0
- package/src/locales/es.ts +34 -0
- package/src/locales/fr.ts +34 -0
- package/src/locales/ja.ts +34 -0
- package/src/locales/ko.ts +34 -0
- package/src/locales/pt.ts +34 -0
- package/src/locales/zh.ts +34 -0
- package/dist/useLocale-DbREg9Zn.js.map +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import { ref,
|
|
2
|
+
import { ref, onBeforeUpdate, onUpdated, onMounted, onBeforeUnmount } from 'vue'
|
|
3
3
|
import MIcon from './MIcon.vue'
|
|
4
|
+
import { useLocale } from '../composables/useLocale'
|
|
4
5
|
|
|
5
6
|
export interface DragDropItem {
|
|
6
7
|
id: string | number
|
|
@@ -11,8 +12,16 @@ const props = withDefaults(
|
|
|
11
12
|
defineProps<{
|
|
12
13
|
modelValue: DragDropItem[]
|
|
13
14
|
handle?: boolean
|
|
15
|
+
disabled?: boolean
|
|
16
|
+
/**
|
|
17
|
+
* Drops the row's own look (padding, min height, hover/dragged state layer, background)
|
|
18
|
+
* so the slot draws the whole row, e.g. one card per item. Dragging, the lift shadow
|
|
19
|
+
* (as a drop-shadow that follows your content's shape) and keyboard support remain.
|
|
20
|
+
* With `handle`, bind the slot's `handleProps` onto your own grip element.
|
|
21
|
+
*/
|
|
22
|
+
bare?: boolean
|
|
14
23
|
}>(),
|
|
15
|
-
{ handle: false },
|
|
24
|
+
{ handle: false, disabled: false, bare: false },
|
|
16
25
|
)
|
|
17
26
|
|
|
18
27
|
const emit = defineEmits<{
|
|
@@ -20,92 +29,512 @@ const emit = defineEmits<{
|
|
|
20
29
|
reorder: [{ from: number; to: number; items: DragDropItem[] }]
|
|
21
30
|
}>()
|
|
22
31
|
|
|
23
|
-
const
|
|
24
|
-
const overIndex = ref<number | null>(null)
|
|
32
|
+
const t = useLocale()
|
|
25
33
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
defineSlots<{
|
|
35
|
+
default?: (props: {
|
|
36
|
+
item: DragDropItem
|
|
37
|
+
index: number
|
|
38
|
+
/** True while this row is lifted (pointer drag, drop settle, or keyboard grab). */
|
|
39
|
+
dragging: boolean
|
|
40
|
+
/** With `handle`: spread onto your own grip (`v-bind="handleProps"`), needed in `bare` mode. */
|
|
41
|
+
handleProps: Record<string, unknown>
|
|
42
|
+
}) => unknown
|
|
43
|
+
}>()
|
|
44
|
+
|
|
45
|
+
/*
|
|
46
|
+
* Pointer Events, not native HTML5 drag-and-drop: native DnD never fires for touch on most
|
|
47
|
+
* mobile browsers, can't be styled (the browser draws its own translucent screenshot), and
|
|
48
|
+
* gives no way to animate the other rows out of the way.
|
|
49
|
+
*
|
|
50
|
+
* Motion follows the library convention (see CLAUDE.md): every row's translateY is a spring
|
|
51
|
+
* stepped in one rAF loop, held in plain variables and written straight to `style.transform`
|
|
52
|
+
* — never a CSS transition, never a reactive :style binding. Standard `DefaultSpatial`
|
|
53
|
+
* (dampingRatio 0.9, stiffness 700), same scheme as MTabs' indicator.
|
|
54
|
+
*
|
|
55
|
+
* M3 has no reorderable-list component, but ListTokens defines the dragged item:
|
|
56
|
+
* ItemDraggedContainerElevation = Level4, ItemDraggedContainerExpressiveShape = CornerLarge
|
|
57
|
+
* (16dp), DraggedStateLayerOpacity = 0.16, ItemOneLineContainerHeight = 56dp,
|
|
58
|
+
* ItemLeading/TrailingSpace = 16dp.
|
|
59
|
+
*/
|
|
60
|
+
const DAMPING = 0.9
|
|
61
|
+
const STIFFNESS = 700
|
|
62
|
+
|
|
63
|
+
type Id = string | number
|
|
64
|
+
type Spring = { pos: number; vel: number; target: number }
|
|
65
|
+
|
|
66
|
+
const rootEl = ref<HTMLElement | null>(null)
|
|
67
|
+
const rowEls = new Map<Id, HTMLElement>()
|
|
68
|
+
const springs = new Map<Id, Spring>()
|
|
69
|
+
|
|
70
|
+
function setRowEl(id: Id, el: unknown) {
|
|
71
|
+
if (el) rowEls.set(id, el as HTMLElement)
|
|
72
|
+
else {
|
|
73
|
+
rowEls.delete(id)
|
|
74
|
+
springs.delete(id)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function springOf(id: Id): Spring {
|
|
79
|
+
let s = springs.get(id)
|
|
80
|
+
if (!s) {
|
|
81
|
+
s = { pos: 0, vel: 0, target: 0 }
|
|
82
|
+
springs.set(id, s)
|
|
31
83
|
}
|
|
84
|
+
return s
|
|
32
85
|
}
|
|
33
86
|
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
overIndex.value = index
|
|
87
|
+
function write(id: Id, s: Spring) {
|
|
88
|
+
const el = rowEls.get(id)
|
|
89
|
+
if (el) el.style.transform = Math.abs(s.pos) < 0.01 ? '' : `translateY(${s.pos}px)`
|
|
38
90
|
}
|
|
39
91
|
|
|
40
|
-
|
|
41
|
-
|
|
92
|
+
// Reactive only for classes (lifted look); per-frame state stays in `drag`/`springs`.
|
|
93
|
+
const draggingId = ref<Id | null>(null)
|
|
94
|
+
const settlingId = ref<Id | null>(null)
|
|
95
|
+
const grabbedId = ref<Id | null>(null)
|
|
96
|
+
const announcement = ref('')
|
|
97
|
+
|
|
98
|
+
interface DragState {
|
|
99
|
+
id: Id
|
|
100
|
+
from: number
|
|
101
|
+
to: number
|
|
102
|
+
pointerId: number
|
|
103
|
+
startX: number
|
|
104
|
+
startY: number
|
|
105
|
+
active: boolean
|
|
106
|
+
fromHandle: boolean
|
|
107
|
+
longPress?: ReturnType<typeof setTimeout>
|
|
108
|
+
tops: number[]
|
|
109
|
+
heights: number[]
|
|
110
|
+
gap: number
|
|
42
111
|
}
|
|
112
|
+
let drag: DragState | null = null
|
|
43
113
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
114
|
+
let raf = 0
|
|
115
|
+
let last = 0
|
|
116
|
+
function kick() {
|
|
117
|
+
if (raf) return
|
|
118
|
+
last = performance.now()
|
|
119
|
+
raf = requestAnimationFrame(step)
|
|
120
|
+
}
|
|
121
|
+
function step(now: number) {
|
|
122
|
+
raf = 0
|
|
123
|
+
const dt = Math.min((now - last) / 1000, 1 / 30)
|
|
124
|
+
last = now
|
|
125
|
+
let moving = false
|
|
126
|
+
for (const [id, s] of springs) {
|
|
127
|
+
// The row under the pointer follows it directly; update() sets its pos.
|
|
128
|
+
if (drag?.active && id === drag.id) {
|
|
129
|
+
write(id, s)
|
|
130
|
+
continue
|
|
131
|
+
}
|
|
132
|
+
const accel = -STIFFNESS * (s.pos - s.target) - 2 * DAMPING * Math.sqrt(STIFFNESS) * s.vel
|
|
133
|
+
s.vel += accel * dt
|
|
134
|
+
s.pos += s.vel * dt
|
|
135
|
+
if (Math.abs(s.pos - s.target) < 0.5 && Math.abs(s.vel) < 5) {
|
|
136
|
+
s.pos = s.target
|
|
137
|
+
s.vel = 0
|
|
138
|
+
} else {
|
|
139
|
+
moving = true
|
|
140
|
+
}
|
|
141
|
+
write(id, s)
|
|
50
142
|
}
|
|
143
|
+
if (moving || drag?.active) {
|
|
144
|
+
last = now
|
|
145
|
+
raf = requestAnimationFrame(step)
|
|
146
|
+
} else {
|
|
147
|
+
settlingId.value = null
|
|
148
|
+
}
|
|
149
|
+
}
|
|
51
150
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
151
|
+
const isLifted = (id: Id) => draggingId.value === id || settlingId.value === id || grabbedId.value === id
|
|
152
|
+
|
|
153
|
+
// ---------- Pointer dragging ----------
|
|
154
|
+
|
|
155
|
+
const INTERACTIVE = 'input, textarea, select, button, a, label, [contenteditable]'
|
|
156
|
+
let prevUserSelect = ''
|
|
157
|
+
|
|
158
|
+
function onPointerDown(e: PointerEvent, index: number, fromHandle: boolean) {
|
|
159
|
+
if (props.disabled || drag || e.button !== 0) return
|
|
160
|
+
if (props.handle && !fromHandle) return
|
|
161
|
+
// Let the row's own controls (checkboxes, switches, links...) work normally.
|
|
162
|
+
if (!fromHandle && (e.target as Element).closest(INTERACTIVE)) return
|
|
163
|
+
const item = props.modelValue[index]
|
|
164
|
+
if (!item) return
|
|
165
|
+
drag = {
|
|
166
|
+
id: item.id,
|
|
167
|
+
from: index,
|
|
168
|
+
to: index,
|
|
169
|
+
pointerId: e.pointerId,
|
|
170
|
+
startX: e.clientX,
|
|
171
|
+
startY: e.clientY,
|
|
172
|
+
active: false,
|
|
173
|
+
fromHandle,
|
|
174
|
+
tops: [],
|
|
175
|
+
heights: [],
|
|
176
|
+
gap: 0,
|
|
177
|
+
}
|
|
178
|
+
// A whole row dragged by touch needs a long press first, so a plain swipe still scrolls
|
|
179
|
+
// the page. The handle is `touch-action: none`, so it starts right away.
|
|
180
|
+
if (e.pointerType === 'touch' && !fromHandle) drag.longPress = setTimeout(activate, 350)
|
|
181
|
+
window.addEventListener('pointermove', onPointerMove)
|
|
182
|
+
window.addEventListener('pointerup', onPointerUp)
|
|
183
|
+
window.addEventListener('pointercancel', onPointerCancel)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function activate() {
|
|
187
|
+
if (!drag || drag.active) return
|
|
188
|
+
clearTimeout(drag.longPress)
|
|
189
|
+
drag.longPress = undefined
|
|
190
|
+
// offsetTop ignores transforms, so this is the natural layout even mid-animation.
|
|
191
|
+
const els = props.modelValue.map((it) => rowEls.get(it.id))
|
|
192
|
+
if (els.some((el) => !el)) return cancelPress()
|
|
193
|
+
drag.tops = els.map((el) => el!.offsetTop)
|
|
194
|
+
drag.heights = els.map((el) => el!.offsetHeight)
|
|
195
|
+
drag.gap = els.length > 1 ? drag.tops[1]! - drag.tops[0]! - drag.heights[0]! : 0
|
|
196
|
+
drag.active = true
|
|
197
|
+
draggingId.value = drag.id
|
|
198
|
+
springOf(drag.id).vel = 0
|
|
199
|
+
prevUserSelect = document.body.style.userSelect
|
|
200
|
+
document.body.style.userSelect = 'none'
|
|
201
|
+
window.getSelection()?.removeAllRanges()
|
|
202
|
+
kick()
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function onPointerMove(e: PointerEvent) {
|
|
206
|
+
if (!drag || e.pointerId !== drag.pointerId) return
|
|
207
|
+
const dy = e.clientY - drag.startY
|
|
208
|
+
if (!drag.active) {
|
|
209
|
+
const dist = Math.hypot(e.clientX - drag.startX, dy)
|
|
210
|
+
if (drag.longPress !== undefined) {
|
|
211
|
+
// Moved before the long press fired: it's a scroll, not a drag.
|
|
212
|
+
if (dist > 8) cancelPress()
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
if (dist < 4) return
|
|
216
|
+
activate()
|
|
217
|
+
if (!drag?.active) return
|
|
218
|
+
}
|
|
219
|
+
update(dy)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function update(rawDy: number) {
|
|
223
|
+
if (!drag) return
|
|
224
|
+
const { tops, heights, from, gap } = drag
|
|
225
|
+
const n = tops.length
|
|
226
|
+
// Keep the lifted row within the list's own bounds.
|
|
227
|
+
const minDy = tops[0]! - tops[from]!
|
|
228
|
+
const maxDy = tops[n - 1]! + heights[n - 1]! - heights[from]! - tops[from]!
|
|
229
|
+
const dy = Math.min(maxDy, Math.max(minDy, rawDy))
|
|
230
|
+
const center = tops[from]! + dy + heights[from]! / 2
|
|
231
|
+
let to = from
|
|
232
|
+
// Inclusive: the clamp above stops the row exactly at the first/last row's midpoint,
|
|
233
|
+
// so a strict comparison would make the two ends unreachable.
|
|
234
|
+
for (let i = from + 1; i < n; i++) if (center >= tops[i]! + heights[i]! / 2) to = i
|
|
235
|
+
for (let i = from - 1; i >= 0; i--) if (center <= tops[i]! + heights[i]! / 2) to = i
|
|
236
|
+
drag.to = to
|
|
237
|
+
const shift = heights[from]! + gap
|
|
238
|
+
props.modelValue.forEach((it, i) => {
|
|
239
|
+
const s = springOf(it.id)
|
|
240
|
+
if (i === from) {
|
|
241
|
+
s.pos = dy
|
|
242
|
+
return
|
|
243
|
+
}
|
|
244
|
+
s.target = from < to && i > from && i <= to ? -shift : from > to && i >= to && i < from ? shift : 0
|
|
245
|
+
})
|
|
246
|
+
kick()
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function removeListeners() {
|
|
250
|
+
window.removeEventListener('pointermove', onPointerMove)
|
|
251
|
+
window.removeEventListener('pointerup', onPointerUp)
|
|
252
|
+
window.removeEventListener('pointercancel', onPointerCancel)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function cancelPress() {
|
|
256
|
+
if (drag) clearTimeout(drag.longPress)
|
|
257
|
+
drag = null
|
|
258
|
+
removeListeners()
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function onPointerUp(e: PointerEvent) {
|
|
262
|
+
if (!drag || e.pointerId !== drag.pointerId) return
|
|
263
|
+
if (!drag.active) return cancelPress()
|
|
264
|
+
finishDrag(drag.to)
|
|
265
|
+
}
|
|
55
266
|
|
|
267
|
+
function onPointerCancel(e: PointerEvent) {
|
|
268
|
+
if (!drag || e.pointerId !== drag.pointerId) return
|
|
269
|
+
if (!drag.active) return cancelPress()
|
|
270
|
+
finishDrag(drag.from)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function finishDrag(to: number) {
|
|
274
|
+
if (!drag) return
|
|
275
|
+
const { id, from } = drag
|
|
276
|
+
drag = null
|
|
277
|
+
removeListeners()
|
|
278
|
+
draggingId.value = null
|
|
279
|
+
settlingId.value = id
|
|
280
|
+
document.body.style.userSelect = prevUserSelect
|
|
281
|
+
// Everything heads home; if the parent applies the new order, onUpdated re-bases each
|
|
282
|
+
// row so it springs from where it is on screen into its new slot instead.
|
|
283
|
+
for (const s of springs.values()) s.target = 0
|
|
284
|
+
if (to !== from) commit(from, to)
|
|
285
|
+
kick()
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function commit(from: number, to: number) {
|
|
289
|
+
const items = [...props.modelValue]
|
|
290
|
+
const moved = items.splice(from, 1)[0]!
|
|
291
|
+
items.splice(to, 0, moved)
|
|
56
292
|
emit('update:modelValue', items)
|
|
57
|
-
emit('reorder', { from
|
|
58
|
-
|
|
293
|
+
emit('reorder', { from, to, items })
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// While a row is actually being dragged, stop the page from scrolling under the finger.
|
|
297
|
+
// Must be a non-passive touchmove: touch-action can't be changed mid-gesture.
|
|
298
|
+
function onTouchMove(e: TouchEvent) {
|
|
299
|
+
if (drag?.active && e.cancelable) e.preventDefault()
|
|
300
|
+
}
|
|
301
|
+
function onContextMenu(e: Event) {
|
|
302
|
+
// Long-pressing a row on touch would otherwise open the browser's context menu.
|
|
303
|
+
if (drag) e.preventDefault()
|
|
59
304
|
}
|
|
60
305
|
|
|
61
|
-
|
|
62
|
-
|
|
306
|
+
// ---------- FLIP: animate any reorder of the rendered list ----------
|
|
307
|
+
// Covers a pointer drop, keyboard moves, and the parent changing the array itself.
|
|
308
|
+
|
|
309
|
+
let beforeTops: Map<Id, { natural: number; visual: number }> | null = null
|
|
310
|
+
onBeforeUpdate(() => {
|
|
311
|
+
if (drag?.active) return
|
|
312
|
+
beforeTops = new Map()
|
|
313
|
+
for (const [id, el] of rowEls) {
|
|
314
|
+
beforeTops.set(id, { natural: el.offsetTop, visual: el.offsetTop + (springs.get(id)?.pos ?? 0) })
|
|
315
|
+
}
|
|
316
|
+
})
|
|
317
|
+
onUpdated(() => {
|
|
318
|
+
const before = beforeTops
|
|
319
|
+
beforeTops = null
|
|
320
|
+
if (before) {
|
|
321
|
+
let changed = false
|
|
322
|
+
for (const [id, el] of rowEls) {
|
|
323
|
+
const b = before.get(id)
|
|
324
|
+
if (!b || Math.abs(b.natural - el.offsetTop) < 0.5) continue
|
|
325
|
+
const s = springOf(id)
|
|
326
|
+
s.pos = b.visual - el.offsetTop
|
|
327
|
+
s.target = 0
|
|
328
|
+
write(id, s)
|
|
329
|
+
changed = true
|
|
330
|
+
}
|
|
331
|
+
if (changed) kick()
|
|
332
|
+
}
|
|
333
|
+
// A keyboard move re-inserts the focused row in the DOM, which can drop focus.
|
|
334
|
+
if (grabbedId.value !== null) focusRow(grabbedId.value)
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
// ---------- Keyboard ----------
|
|
338
|
+
|
|
339
|
+
let grabOrigin: DragDropItem[] | null = null
|
|
340
|
+
let grabFromIndex = 0
|
|
341
|
+
|
|
342
|
+
function say(template: string, index?: number) {
|
|
343
|
+
announcement.value = template
|
|
344
|
+
.replace('{pos}', String((index ?? 0) + 1))
|
|
345
|
+
.replace('{total}', String(props.modelValue.length))
|
|
63
346
|
}
|
|
64
347
|
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
|
|
348
|
+
function focusRow(id: Id) {
|
|
349
|
+
const row = rowEls.get(id)
|
|
350
|
+
const target = props.handle ? row?.querySelector<HTMLElement>('[data-drag-handle]') : row
|
|
351
|
+
if (target && document.activeElement !== target) target.focus({ preventScroll: true })
|
|
68
352
|
}
|
|
69
353
|
|
|
70
|
-
function
|
|
71
|
-
|
|
72
|
-
if (
|
|
73
|
-
|
|
354
|
+
function onKeydown(e: KeyboardEvent, index: number) {
|
|
355
|
+
// Only keys aimed at the row/handle itself, not at a control inside the slot.
|
|
356
|
+
if (props.disabled || e.target !== e.currentTarget) return
|
|
357
|
+
const item = props.modelValue[index]
|
|
358
|
+
if (!item) return
|
|
359
|
+
const grabbed = grabbedId.value === item.id
|
|
360
|
+
|
|
361
|
+
if (e.key === ' ' || e.key === 'Enter') {
|
|
362
|
+
e.preventDefault()
|
|
363
|
+
if (!grabbed) {
|
|
364
|
+
grabbedId.value = item.id
|
|
365
|
+
grabOrigin = [...props.modelValue]
|
|
366
|
+
grabFromIndex = index
|
|
367
|
+
say(t.reorderGrabbed, index)
|
|
368
|
+
} else {
|
|
369
|
+
grabbedId.value = null
|
|
370
|
+
grabOrigin = null
|
|
371
|
+
settlingId.value = item.id
|
|
372
|
+
kick()
|
|
373
|
+
say(t.reorderDropped, index)
|
|
374
|
+
}
|
|
375
|
+
} else if (grabbed && (e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
|
|
376
|
+
e.preventDefault()
|
|
377
|
+
const to = e.key === 'ArrowUp' ? index - 1 : index + 1
|
|
378
|
+
if (to < 0 || to >= props.modelValue.length) return
|
|
379
|
+
commit(index, to)
|
|
380
|
+
say(t.reorderMoved, to)
|
|
381
|
+
} else if (grabbed && e.key === 'Escape') {
|
|
382
|
+
e.preventDefault()
|
|
383
|
+
cancelKeyboard(index)
|
|
384
|
+
}
|
|
74
385
|
}
|
|
386
|
+
|
|
387
|
+
function cancelKeyboard(currentIndex: number) {
|
|
388
|
+
const origin = grabOrigin
|
|
389
|
+
const id = grabbedId.value
|
|
390
|
+
grabbedId.value = null
|
|
391
|
+
grabOrigin = null
|
|
392
|
+
if (origin && currentIndex !== grabFromIndex) {
|
|
393
|
+
emit('update:modelValue', origin)
|
|
394
|
+
emit('reorder', { from: currentIndex, to: grabFromIndex, items: origin })
|
|
395
|
+
}
|
|
396
|
+
if (id !== null) settlingId.value = id
|
|
397
|
+
kick()
|
|
398
|
+
say(t.reorderCancelled)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function onBlur(e: FocusEvent, index: number) {
|
|
402
|
+
// Tabbing away while grabbed drops the row where it is.
|
|
403
|
+
if (e.target !== e.currentTarget) return
|
|
404
|
+
const item = props.modelValue[index]
|
|
405
|
+
if (!item || grabbedId.value !== item.id) return
|
|
406
|
+
// Focus can briefly leave while Vue moves the row; onUpdated restores it.
|
|
407
|
+
requestAnimationFrame(() => {
|
|
408
|
+
const row = rowEls.get(item.id)
|
|
409
|
+
if (grabbedId.value === item.id && row && !row.contains(document.activeElement)) {
|
|
410
|
+
grabbedId.value = null
|
|
411
|
+
grabOrigin = null
|
|
412
|
+
}
|
|
413
|
+
})
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function handlePropsFor(index: number, id: Id): Record<string, unknown> {
|
|
417
|
+
if (!props.handle) return {}
|
|
418
|
+
return {
|
|
419
|
+
'data-drag-handle': '',
|
|
420
|
+
role: 'button',
|
|
421
|
+
tabindex: props.disabled ? -1 : 0,
|
|
422
|
+
'aria-label': t.reorderHandle,
|
|
423
|
+
'aria-pressed': grabbedId.value === id,
|
|
424
|
+
'aria-disabled': props.disabled || undefined,
|
|
425
|
+
style: { touchAction: 'none', cursor: props.disabled ? 'default' : draggingId.value === id ? 'grabbing' : 'grab' },
|
|
426
|
+
onPointerdown: (e: PointerEvent) => {
|
|
427
|
+
e.stopPropagation()
|
|
428
|
+
onPointerDown(e, index, true)
|
|
429
|
+
},
|
|
430
|
+
onKeydown: (e: KeyboardEvent) => onKeydown(e, index),
|
|
431
|
+
onBlur: (e: FocusEvent) => onBlur(e, index),
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
onMounted(() => {
|
|
436
|
+
rootEl.value?.addEventListener('touchmove', onTouchMove, { passive: false })
|
|
437
|
+
})
|
|
438
|
+
onBeforeUnmount(() => {
|
|
439
|
+
rootEl.value?.removeEventListener('touchmove', onTouchMove)
|
|
440
|
+
cancelAnimationFrame(raf)
|
|
441
|
+
raf = 0
|
|
442
|
+
if (drag) {
|
|
443
|
+
clearTimeout(drag.longPress)
|
|
444
|
+
if (drag.active) document.body.style.userSelect = prevUserSelect
|
|
445
|
+
}
|
|
446
|
+
drag = null
|
|
447
|
+
removeListeners()
|
|
448
|
+
})
|
|
75
449
|
</script>
|
|
76
450
|
|
|
77
451
|
<template>
|
|
78
|
-
<div
|
|
452
|
+
<div
|
|
453
|
+
ref="rootEl"
|
|
454
|
+
class="relative flex flex-col"
|
|
455
|
+
:class="disabled ? 'opacity-[0.38]' : ''"
|
|
456
|
+
role="list"
|
|
457
|
+
@contextmenu="onContextMenu"
|
|
458
|
+
>
|
|
79
459
|
<div
|
|
80
460
|
v-for="(item, index) in modelValue"
|
|
81
461
|
:key="item.id"
|
|
82
|
-
:
|
|
83
|
-
|
|
462
|
+
:ref="(el) => setRowEl(item.id, el)"
|
|
463
|
+
role="listitem"
|
|
464
|
+
class="group relative outline-none"
|
|
84
465
|
:class="[
|
|
85
|
-
|
|
86
|
-
|
|
466
|
+
bare
|
|
467
|
+
? ['transition-[filter] duration-150', isLifted(item.id) ? 'm3-dd-bare-lifted z-10' : '']
|
|
468
|
+
: [
|
|
469
|
+
'flex min-h-14 items-center gap-4 px-4 py-2 transition-[box-shadow,background-color,border-radius] duration-150',
|
|
470
|
+
isLifted(item.id) ? 'z-10 rounded-lg bg-surface shadow-elevation-4' : 'rounded-none',
|
|
471
|
+
],
|
|
472
|
+
!handle && !disabled
|
|
473
|
+
? [
|
|
474
|
+
'cursor-grab touch-pan-y select-none [-webkit-touch-callout:none]',
|
|
475
|
+
bare
|
|
476
|
+
? 'focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-secondary'
|
|
477
|
+
: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-secondary',
|
|
478
|
+
]
|
|
479
|
+
: '',
|
|
480
|
+
draggingId === item.id ? 'cursor-grabbing' : '',
|
|
87
481
|
]"
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
@
|
|
91
|
-
@
|
|
92
|
-
@
|
|
93
|
-
role="option"
|
|
482
|
+
:tabindex="!handle && !disabled ? 0 : undefined"
|
|
483
|
+
:aria-grabbed="!handle ? grabbedId === item.id : undefined"
|
|
484
|
+
@pointerdown="onPointerDown($event, index, false)"
|
|
485
|
+
@keydown="!handle && onKeydown($event, index)"
|
|
486
|
+
@blur="!handle && onBlur($event, index)"
|
|
94
487
|
>
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
488
|
+
<!-- State layer: 8% hover, 16% while dragged (DraggedStateLayerOpacity) -->
|
|
489
|
+
<span
|
|
490
|
+
v-if="!bare"
|
|
491
|
+
aria-hidden="true"
|
|
492
|
+
class="pointer-events-none absolute inset-0 rounded-[inherit] bg-on-surface transition-opacity duration-150"
|
|
493
|
+
:class="
|
|
494
|
+
isLifted(item.id)
|
|
495
|
+
? 'opacity-[0.16]'
|
|
496
|
+
: !disabled && !handle
|
|
497
|
+
? 'opacity-0 group-hover:opacity-[0.08]'
|
|
498
|
+
: 'opacity-0'
|
|
499
|
+
"
|
|
500
|
+
/>
|
|
501
|
+
|
|
502
|
+
<button
|
|
503
|
+
v-if="handle && !bare"
|
|
504
|
+
type="button"
|
|
505
|
+
data-drag-handle
|
|
506
|
+
class="relative -ml-2 flex h-10 w-10 shrink-0 touch-none items-center justify-center rounded-full text-on-surface-variant outline-none transition-colors hover:bg-on-surface/8 focus-visible:ring-2 focus-visible:ring-secondary"
|
|
507
|
+
:class="disabled ? 'cursor-default' : draggingId === item.id ? 'cursor-grabbing' : 'cursor-grab'"
|
|
508
|
+
:aria-label="t.reorderHandle"
|
|
509
|
+
:aria-pressed="grabbedId === item.id"
|
|
510
|
+
:disabled="disabled"
|
|
511
|
+
@pointerdown.stop="onPointerDown($event, index, true)"
|
|
512
|
+
@keydown="onKeydown($event, index)"
|
|
513
|
+
@blur="onBlur($event, index)"
|
|
100
514
|
>
|
|
101
|
-
<MIcon name="drag_indicator" :size="
|
|
102
|
-
</
|
|
515
|
+
<MIcon name="drag_indicator" :size="24" />
|
|
516
|
+
</button>
|
|
103
517
|
|
|
104
|
-
<div class="min-w-0 flex-1">
|
|
105
|
-
<slot
|
|
106
|
-
|
|
518
|
+
<div :class="bare ? '' : 'relative min-w-0 flex-1 text-body-large text-on-surface'">
|
|
519
|
+
<slot
|
|
520
|
+
:item="item"
|
|
521
|
+
:index="index"
|
|
522
|
+
:dragging="isLifted(item.id)"
|
|
523
|
+
:handle-props="handlePropsFor(index, item.id)"
|
|
524
|
+
>
|
|
525
|
+
<span>{{ item.id }}</span>
|
|
107
526
|
</slot>
|
|
108
527
|
</div>
|
|
109
528
|
</div>
|
|
529
|
+
|
|
530
|
+
<div class="sr-only" aria-live="assertive" aria-atomic="true">{{ announcement }}</div>
|
|
110
531
|
</div>
|
|
111
532
|
</template>
|
|
533
|
+
|
|
534
|
+
<style scoped>
|
|
535
|
+
/* Elevation level 4 as a drop-shadow, so in `bare` mode it follows whatever shape the slot
|
|
536
|
+
draws (rounded card, pill...) instead of the row's own box. */
|
|
537
|
+
.m3-dd-bare-lifted {
|
|
538
|
+
filter: drop-shadow(0 2px 3px rgb(0 0 0 / 0.3)) drop-shadow(0 6px 10px rgb(0 0 0 / 0.15));
|
|
539
|
+
}
|
|
540
|
+
</style>
|