numori-ui 0.5.0 → 0.6.0
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/CHANGELOG.md +54 -0
- package/README.md +9 -1
- package/dist/components/Avatar.js +71 -19
- package/dist/components/Avatar.js.map +1 -1
- package/dist/components/Dropdown.js +3 -1
- package/dist/components/Dropdown.js.map +1 -1
- package/dist/components/Modal.js +61 -4
- package/dist/components/Modal.js.map +1 -1
- package/dist/components/Prompt.js +11 -6
- package/dist/components/Prompt.js.map +1 -1
- package/dist/components/Select.js +1 -1
- package/dist/components/Select.js.map +1 -1
- package/dist/components/Skeleton.js +122 -0
- package/dist/components/Skeleton.js.map +1 -0
- package/dist/components/SplitPane.js +264 -0
- package/dist/components/SplitPane.js.map +1 -0
- package/dist/components/Tree.js +417 -0
- package/dist/components/Tree.js.map +1 -0
- package/dist/components/VirtualList.js +292 -0
- package/dist/components/VirtualList.js.map +1 -0
- package/dist/components/index.js +29 -22
- package/dist/components/index.js.map +1 -1
- package/dist/icons/mdi.js +2 -0
- package/dist/icons/mdi.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +15 -11
- package/dist/index.js.map +1 -1
- package/dist/internal/focusTrap.js +163 -0
- package/dist/internal/focusTrap.js.map +1 -0
- package/dist/manifest.js +5 -1
- package/dist/manifest.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Avatar.vue +69 -18
- package/src/components/Dropdown.vue +4 -1
- package/src/components/Modal.vue +51 -3
- package/src/components/Prompt.vue +14 -3
- package/src/components/Select.vue +1 -1
- package/src/components/Skeleton.vue +91 -0
- package/src/components/SplitPane.vue +264 -0
- package/src/components/Tree.vue +428 -0
- package/src/components/VirtualList.vue +250 -0
- package/src/components/index.js +12 -1
- package/src/icons/mdi.js +2 -0
- package/src/internal/focusTrap.js +185 -0
- package/src/manifest.js +4 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div
|
|
3
|
+
ref="rootRef"
|
|
4
|
+
class="flex h-full w-full min-h-0 min-w-0"
|
|
5
|
+
:class="[isHorizontal ? 'flex-row' : 'flex-col', dragging ? 'select-none' : '']"
|
|
6
|
+
>
|
|
7
|
+
<div
|
|
8
|
+
:id="startId"
|
|
9
|
+
class="relative min-h-0 min-w-0 shrink-0 overflow-hidden"
|
|
10
|
+
:style="startStyle"
|
|
11
|
+
>
|
|
12
|
+
<slot name="start" :size="currentSize" />
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
<!-- The splitter: the WAI-ARIA "window splitter" pattern. A 1px rule with
|
|
16
|
+
a wider invisible hit area, focusable and operable from the keyboard. -->
|
|
17
|
+
<div
|
|
18
|
+
role="separator"
|
|
19
|
+
tabindex="0"
|
|
20
|
+
:aria-label="label"
|
|
21
|
+
:aria-controls="startId"
|
|
22
|
+
:aria-orientation="isHorizontal ? 'vertical' : 'horizontal'"
|
|
23
|
+
:aria-valuenow="Math.round(currentSize)"
|
|
24
|
+
:aria-valuemin="min"
|
|
25
|
+
:aria-valuemax="effectiveMax"
|
|
26
|
+
:data-dragging="dragging ? 'true' : undefined"
|
|
27
|
+
class="group relative z-10 shrink-0 touch-none focus-visible:outline-none"
|
|
28
|
+
:class="isHorizontal ? 'w-px cursor-col-resize' : 'h-px cursor-row-resize'"
|
|
29
|
+
@pointerdown="onPointerDown"
|
|
30
|
+
@keydown="onKeydown"
|
|
31
|
+
@dblclick="reset"
|
|
32
|
+
>
|
|
33
|
+
<span
|
|
34
|
+
aria-hidden="true"
|
|
35
|
+
class="absolute bg-gray-200 transition-colors group-hover:bg-primary-400 group-focus-visible:bg-primary-500 group-data-[dragging=true]:bg-primary-500 dark:bg-gray-800 dark:group-hover:bg-primary-500"
|
|
36
|
+
:class="
|
|
37
|
+
isHorizontal
|
|
38
|
+
? 'inset-y-0 left-0 w-px group-focus-visible:-left-px group-focus-visible:w-[3px]'
|
|
39
|
+
: 'inset-x-0 top-0 h-px group-focus-visible:-top-px group-focus-visible:h-[3px]'
|
|
40
|
+
"
|
|
41
|
+
/>
|
|
42
|
+
<span
|
|
43
|
+
aria-hidden="true"
|
|
44
|
+
class="absolute"
|
|
45
|
+
:class="isHorizontal ? 'inset-y-0 -left-1.5 w-3' : 'inset-x-0 -top-1.5 h-3'"
|
|
46
|
+
/>
|
|
47
|
+
</div>
|
|
48
|
+
|
|
49
|
+
<div class="relative min-h-0 min-w-0 flex-1 overflow-hidden">
|
|
50
|
+
<slot name="end" />
|
|
51
|
+
</div>
|
|
52
|
+
</div>
|
|
53
|
+
</template>
|
|
54
|
+
|
|
55
|
+
<script setup>
|
|
56
|
+
/**
|
|
57
|
+
* UiSplitPane — two panes divided by a draggable, keyboard-operable splitter.
|
|
58
|
+
*
|
|
59
|
+
* The `start` pane has an explicit size in pixels; the `end` pane takes the
|
|
60
|
+
* rest. Drag the splitter with a mouse, finger or pen, or focus it and use the
|
|
61
|
+
* arrow keys (Shift for bigger steps), Home and End for the limits, and Enter
|
|
62
|
+
* to toggle between the current size and `min`. Double-click resets to
|
|
63
|
+
* `defaultSize`.
|
|
64
|
+
*
|
|
65
|
+
* Follows the WAI-ARIA window splitter pattern: the splitter is a focusable
|
|
66
|
+
* `role="separator"` whose `aria-valuenow` is the start pane's size and whose
|
|
67
|
+
* `aria-controls` points at it, so assistive technology can report and change
|
|
68
|
+
* the split.
|
|
69
|
+
*
|
|
70
|
+
* Bind `v-model` to persist the size (for example to local storage); leave it
|
|
71
|
+
* unbound and the component keeps its own state. `update:modelValue` fires on
|
|
72
|
+
* every change while dragging, and `resize-end` once when a drag or key press
|
|
73
|
+
* finishes — persist on the latter to avoid a write per pixel.
|
|
74
|
+
*
|
|
75
|
+
* The component fills its parent (`h-full w-full`), so give the parent a
|
|
76
|
+
* size. Panes can nest: put a second `UiSplitPane` in `#end` for three columns.
|
|
77
|
+
*
|
|
78
|
+
* @example Message list beside a reader
|
|
79
|
+
* <UiSplitPane v-model="listWidth" :min="280" :max="640" label="Resize message list">
|
|
80
|
+
* <template #start><MessageList /></template>
|
|
81
|
+
* <template #end><MessageReader /></template>
|
|
82
|
+
* </UiSplitPane>
|
|
83
|
+
*
|
|
84
|
+
* @example Stacked
|
|
85
|
+
* <UiSplitPane direction="vertical" :default-size="240" label="Resize preview">
|
|
86
|
+
* <template #start>Top</template>
|
|
87
|
+
* <template #end>Bottom</template>
|
|
88
|
+
* </UiSplitPane>
|
|
89
|
+
*
|
|
90
|
+
* @emits {number} update:modelValue — The start pane's new size, in pixels.
|
|
91
|
+
* @emits {number} resize-end — A drag or keyboard adjustment finished, with the final size.
|
|
92
|
+
*
|
|
93
|
+
* @slot start — The sized pane. Scope: `{ size }`.
|
|
94
|
+
* @slot end — The flexible pane.
|
|
95
|
+
*/
|
|
96
|
+
import { computed, onBeforeUnmount, onMounted, ref, useId } from 'vue'
|
|
97
|
+
|
|
98
|
+
const props = defineProps({
|
|
99
|
+
/**
|
|
100
|
+
* Size of the start pane in pixels, for `v-model`. Leave unset to let the
|
|
101
|
+
* component manage it from `defaultSize`.
|
|
102
|
+
* @type {number}
|
|
103
|
+
* @default undefined
|
|
104
|
+
*/
|
|
105
|
+
modelValue: { type: Number, default: undefined },
|
|
106
|
+
/**
|
|
107
|
+
* Initial size in pixels when uncontrolled, and the size double-click resets to.
|
|
108
|
+
* @type {number}
|
|
109
|
+
* @default 320
|
|
110
|
+
*/
|
|
111
|
+
defaultSize: { type: Number, default: 320 },
|
|
112
|
+
/**
|
|
113
|
+
* Smallest start pane size in pixels.
|
|
114
|
+
* @type {number}
|
|
115
|
+
* @default 160
|
|
116
|
+
*/
|
|
117
|
+
min: { type: Number, default: 160 },
|
|
118
|
+
/**
|
|
119
|
+
* Largest start pane size in pixels. The pane is also never allowed to push
|
|
120
|
+
* the end pane below `endMin`.
|
|
121
|
+
* @type {number}
|
|
122
|
+
* @default 800
|
|
123
|
+
*/
|
|
124
|
+
max: { type: Number, default: 800 },
|
|
125
|
+
/**
|
|
126
|
+
* Smallest size the end pane may be squeezed to, in pixels.
|
|
127
|
+
* @type {number}
|
|
128
|
+
* @default 240
|
|
129
|
+
*/
|
|
130
|
+
endMin: { type: Number, default: 240 },
|
|
131
|
+
/**
|
|
132
|
+
* Layout direction. `horizontal` puts the panes side by side.
|
|
133
|
+
* @type {string}
|
|
134
|
+
* @default 'horizontal'
|
|
135
|
+
* @values 'horizontal' | 'vertical'
|
|
136
|
+
*/
|
|
137
|
+
direction: { type: String, default: 'horizontal' },
|
|
138
|
+
/**
|
|
139
|
+
* Arrow-key step in pixels. Shift multiplies it by five.
|
|
140
|
+
* @type {number}
|
|
141
|
+
* @default 16
|
|
142
|
+
*/
|
|
143
|
+
step: { type: Number, default: 16 },
|
|
144
|
+
/**
|
|
145
|
+
* Accessible name for the splitter.
|
|
146
|
+
* @type {string}
|
|
147
|
+
* @default 'Resize panes'
|
|
148
|
+
*/
|
|
149
|
+
label: { type: String, default: 'Resize panes' },
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
const emit = defineEmits(['update:modelValue', 'resize-end'])
|
|
153
|
+
|
|
154
|
+
const startId = `${useId()}-start`
|
|
155
|
+
const rootRef = ref(null)
|
|
156
|
+
const isHorizontal = computed(() => props.direction !== 'vertical')
|
|
157
|
+
|
|
158
|
+
// ── Size state (controlled or uncontrolled) ──
|
|
159
|
+
const internalSize = ref(props.defaultSize)
|
|
160
|
+
const currentSize = computed(() => props.modelValue ?? internalSize.value)
|
|
161
|
+
|
|
162
|
+
// The container's extent along the split axis, so `max` can respect `endMin`.
|
|
163
|
+
const extent = ref(0)
|
|
164
|
+
const effectiveMax = computed(() => {
|
|
165
|
+
if (!extent.value) return props.max
|
|
166
|
+
return Math.max(props.min, Math.min(props.max, extent.value - props.endMin))
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
const clamp = (value) => Math.round(Math.min(effectiveMax.value, Math.max(props.min, value)))
|
|
170
|
+
|
|
171
|
+
const startStyle = computed(() => {
|
|
172
|
+
const size = `${clamp(currentSize.value)}px`
|
|
173
|
+
return isHorizontal.value ? { width: size } : { height: size }
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
function setSize(value) {
|
|
177
|
+
const next = clamp(value)
|
|
178
|
+
if (next === currentSize.value) return
|
|
179
|
+
internalSize.value = next
|
|
180
|
+
emit('update:modelValue', next)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const finish = () => emit('resize-end', clamp(currentSize.value))
|
|
184
|
+
|
|
185
|
+
// ── Pointer dragging ──
|
|
186
|
+
const dragging = ref(false)
|
|
187
|
+
let dragOrigin = 0
|
|
188
|
+
let sizeOrigin = 0
|
|
189
|
+
|
|
190
|
+
function onPointerDown(event) {
|
|
191
|
+
if (event.button !== 0) return
|
|
192
|
+
event.preventDefault()
|
|
193
|
+
event.currentTarget.setPointerCapture?.(event.pointerId)
|
|
194
|
+
event.currentTarget.focus({ preventScroll: true })
|
|
195
|
+
dragging.value = true
|
|
196
|
+
dragOrigin = isHorizontal.value ? event.clientX : event.clientY
|
|
197
|
+
sizeOrigin = clamp(currentSize.value)
|
|
198
|
+
event.currentTarget.addEventListener('pointermove', onPointerMove)
|
|
199
|
+
event.currentTarget.addEventListener('pointerup', onPointerUp, { once: true })
|
|
200
|
+
event.currentTarget.addEventListener('pointercancel', onPointerUp, { once: true })
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function onPointerMove(event) {
|
|
204
|
+
if (!dragging.value) return
|
|
205
|
+
const position = isHorizontal.value ? event.clientX : event.clientY
|
|
206
|
+
setSize(sizeOrigin + position - dragOrigin)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function onPointerUp(event) {
|
|
210
|
+
event.currentTarget.removeEventListener('pointermove', onPointerMove)
|
|
211
|
+
event.currentTarget.removeEventListener('pointerup', onPointerUp)
|
|
212
|
+
event.currentTarget.removeEventListener('pointercancel', onPointerUp)
|
|
213
|
+
if (!dragging.value) return
|
|
214
|
+
dragging.value = false
|
|
215
|
+
finish()
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ── Keyboard ──
|
|
219
|
+
let sizeBeforeCollapse = null
|
|
220
|
+
|
|
221
|
+
function onKeydown(event) {
|
|
222
|
+
const decrease = isHorizontal.value ? 'ArrowLeft' : 'ArrowUp'
|
|
223
|
+
const increase = isHorizontal.value ? 'ArrowRight' : 'ArrowDown'
|
|
224
|
+
const amount = event.shiftKey ? props.step * 5 : props.step
|
|
225
|
+
const now = clamp(currentSize.value)
|
|
226
|
+
let next = null
|
|
227
|
+
if (event.key === decrease) next = now - amount
|
|
228
|
+
else if (event.key === increase) next = now + amount
|
|
229
|
+
else if (event.key === 'Home') next = props.min
|
|
230
|
+
else if (event.key === 'End') next = effectiveMax.value
|
|
231
|
+
else if (event.key === 'Enter') {
|
|
232
|
+
if (now > props.min) {
|
|
233
|
+
sizeBeforeCollapse = now
|
|
234
|
+
next = props.min
|
|
235
|
+
} else {
|
|
236
|
+
next = sizeBeforeCollapse ?? props.defaultSize
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (next === null) return
|
|
240
|
+
event.preventDefault()
|
|
241
|
+
setSize(next)
|
|
242
|
+
finish()
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function reset() {
|
|
246
|
+
setSize(props.defaultSize)
|
|
247
|
+
finish()
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ── Track the container size ──
|
|
251
|
+
let observer = null
|
|
252
|
+
onMounted(() => {
|
|
253
|
+
const measure = () => {
|
|
254
|
+
const rect = rootRef.value?.getBoundingClientRect()
|
|
255
|
+
if (rect) extent.value = isHorizontal.value ? rect.width : rect.height
|
|
256
|
+
}
|
|
257
|
+
measure()
|
|
258
|
+
if (typeof ResizeObserver !== 'undefined' && rootRef.value) {
|
|
259
|
+
observer = new ResizeObserver(measure)
|
|
260
|
+
observer.observe(rootRef.value)
|
|
261
|
+
}
|
|
262
|
+
})
|
|
263
|
+
onBeforeUnmount(() => observer?.disconnect())
|
|
264
|
+
</script>
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div
|
|
3
|
+
role="tree"
|
|
4
|
+
:aria-label="label || undefined"
|
|
5
|
+
class="flex flex-col gap-px"
|
|
6
|
+
@keydown="onKeydown"
|
|
7
|
+
>
|
|
8
|
+
<div
|
|
9
|
+
v-for="row in rows"
|
|
10
|
+
:key="row.node.id"
|
|
11
|
+
:ref="(el) => setRowRef(row.node.id, el)"
|
|
12
|
+
role="treeitem"
|
|
13
|
+
:aria-level="row.level"
|
|
14
|
+
:aria-setsize="row.setSize"
|
|
15
|
+
:aria-posinset="row.posInSet"
|
|
16
|
+
:aria-expanded="row.hasChildren ? (row.expanded ? 'true' : 'false') : undefined"
|
|
17
|
+
:aria-selected="row.node.id === modelValue ? 'true' : 'false'"
|
|
18
|
+
:aria-disabled="row.node.disabled ? 'true' : undefined"
|
|
19
|
+
:tabindex="row.node.id === focusId ? 0 : -1"
|
|
20
|
+
:draggable="draggable && !row.node.disabled ? 'true' : undefined"
|
|
21
|
+
class="group flex w-full items-center gap-2 rounded-lg py-1.5 pr-3 text-left text-sm font-medium transition-colors select-none focus-visible:outline-2 focus-visible:outline-offset-[-2px] focus-visible:outline-primary-500 dark:focus-visible:outline-primary-400"
|
|
22
|
+
:class="rowClass(row)"
|
|
23
|
+
:style="{ paddingLeft: `${8 + (row.level - 1) * indent}px` }"
|
|
24
|
+
@click="onRowClick(row)"
|
|
25
|
+
@focus="focusId = row.node.id"
|
|
26
|
+
@contextmenu="emit('contextmenu', { node: row.node, event: $event })"
|
|
27
|
+
@dragstart="onDragStart(row, $event)"
|
|
28
|
+
@dragend="onDragEnd"
|
|
29
|
+
@dragover="onDragOver(row, $event)"
|
|
30
|
+
@dragleave="onDragLeave(row, $event)"
|
|
31
|
+
@drop="onDrop(row, $event)"
|
|
32
|
+
>
|
|
33
|
+
<!-- Disclosure. Not a separate tab stop: the row itself carries
|
|
34
|
+
aria-expanded and answers Left / Right. -->
|
|
35
|
+
<span
|
|
36
|
+
class="-my-1 inline-flex size-5 shrink-0 items-center justify-center rounded text-gray-400 dark:text-gray-500"
|
|
37
|
+
:class="
|
|
38
|
+
row.hasChildren
|
|
39
|
+
? 'hover:bg-gray-200 hover:text-gray-700 dark:hover:bg-gray-700 dark:hover:text-gray-200'
|
|
40
|
+
: ''
|
|
41
|
+
"
|
|
42
|
+
aria-hidden="true"
|
|
43
|
+
@click.stop="row.hasChildren && toggle(row.node.id)"
|
|
44
|
+
>
|
|
45
|
+
<UiIcon
|
|
46
|
+
v-if="row.hasChildren"
|
|
47
|
+
name="mdi:chevron-right"
|
|
48
|
+
class="size-4 transition-transform motion-reduce:transition-none"
|
|
49
|
+
:class="row.expanded ? 'rotate-90' : ''"
|
|
50
|
+
/>
|
|
51
|
+
</span>
|
|
52
|
+
<slot
|
|
53
|
+
name="item"
|
|
54
|
+
:node="row.node"
|
|
55
|
+
:level="row.level"
|
|
56
|
+
:expanded="row.expanded"
|
|
57
|
+
:selected="row.node.id === modelValue"
|
|
58
|
+
>
|
|
59
|
+
<UiIcon
|
|
60
|
+
v-if="row.node.icon"
|
|
61
|
+
:name="row.node.icon"
|
|
62
|
+
class="size-[18px] shrink-0"
|
|
63
|
+
:class="
|
|
64
|
+
row.node.id === modelValue
|
|
65
|
+
? 'text-primary-600 dark:text-primary-400'
|
|
66
|
+
: 'text-gray-400 group-hover:text-gray-600 dark:text-gray-500 dark:group-hover:text-gray-300'
|
|
67
|
+
"
|
|
68
|
+
/>
|
|
69
|
+
<span class="min-w-0 flex-1 truncate">{{ row.node.label }}</span>
|
|
70
|
+
<span
|
|
71
|
+
v-if="row.node.badge !== undefined && row.node.badge !== null && row.node.badge !== ''"
|
|
72
|
+
class="ml-auto inline-flex min-w-5 items-center justify-center rounded-full px-1.5 text-xs font-semibold tabular-nums"
|
|
73
|
+
:class="
|
|
74
|
+
row.node.id === modelValue
|
|
75
|
+
? 'bg-primary-100 text-primary-700 dark:bg-primary-400/20 dark:text-primary-200'
|
|
76
|
+
: 'bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-300'
|
|
77
|
+
"
|
|
78
|
+
>{{ row.node.badge }}</span
|
|
79
|
+
>
|
|
80
|
+
</slot>
|
|
81
|
+
</div>
|
|
82
|
+
</div>
|
|
83
|
+
</template>
|
|
84
|
+
|
|
85
|
+
<script setup>
|
|
86
|
+
/**
|
|
87
|
+
* UiTree — a nested, collapsible, keyboard-navigable tree (folders, outlines,
|
|
88
|
+
* file browsers), following the WAI-ARIA tree view pattern.
|
|
89
|
+
*
|
|
90
|
+
* Nodes are plain objects:
|
|
91
|
+
* `{ id, label, icon?, badge?, disabled?, children?: Node[] }`.
|
|
92
|
+
* Only expanded branches are rendered, as a flat run of rows; each row carries
|
|
93
|
+
* `aria-level`, `aria-setsize` and `aria-posinset`, so assistive technology
|
|
94
|
+
* hears the structure without nested groups in the DOM.
|
|
95
|
+
*
|
|
96
|
+
* Keyboard (one tab stop for the whole tree, with a roving tabindex):
|
|
97
|
+
* Up / Down move between visible rows; Right expands, or moves to the first
|
|
98
|
+
* child; Left collapses, or moves to the parent; Home / End jump to the ends;
|
|
99
|
+
* Enter or Space selects; `*` expands every sibling; typing a few letters
|
|
100
|
+
* jumps to the next row whose label starts with them.
|
|
101
|
+
*
|
|
102
|
+
* Selection is `v-model` (a single node id). Expansion is `v-model:expanded`
|
|
103
|
+
* (an array of ids), or uncontrolled from `default-expanded`.
|
|
104
|
+
*
|
|
105
|
+
* Drag and drop is opt-in. `draggable` lets rows be picked up; `droppable`
|
|
106
|
+
* (true, or a predicate `(node, event) => boolean`) lets rows accept a drop —
|
|
107
|
+
* from this tree or from anywhere else, such as messages dragged out of a list.
|
|
108
|
+
* Hovering a collapsed branch during a drag opens it. The tree does not move
|
|
109
|
+
* anything itself: it emits `drop` and leaves the data to you.
|
|
110
|
+
*
|
|
111
|
+
* @example Mail folders
|
|
112
|
+
* <UiTree
|
|
113
|
+
* v-model="folderId"
|
|
114
|
+
* v-model:expanded="openFolders"
|
|
115
|
+
* :nodes="[
|
|
116
|
+
* { id: 'inbox', label: 'Inbox', icon: 'mdi:inbox', badge: 3 },
|
|
117
|
+
* { id: 'work', label: 'Work', icon: 'mdi:folder-outline', children: [
|
|
118
|
+
* { id: 'work/2026', label: '2026', icon: 'mdi:folder-outline' },
|
|
119
|
+
* ] },
|
|
120
|
+
* ]"
|
|
121
|
+
* label="Folders"
|
|
122
|
+
* droppable
|
|
123
|
+
* @drop="({ node, event }) => moveMessages(event.dataTransfer, node.id)"
|
|
124
|
+
* />
|
|
125
|
+
*
|
|
126
|
+
* @emits {string | number} update:modelValue — A node was selected (its id).
|
|
127
|
+
* @emits {Array<string | number>} update:expanded — The set of expanded ids changed.
|
|
128
|
+
* @emits {object} select — A node was selected (the node object).
|
|
129
|
+
* @emits {{ node: object, event: DragEvent, sourceId: string | number | null }} drop —
|
|
130
|
+
* Something was dropped on a row. `sourceId` is set when it was a row of this tree.
|
|
131
|
+
* @emits {{ node: object, event: MouseEvent }} contextmenu — A row was right-clicked.
|
|
132
|
+
*
|
|
133
|
+
* @slot item — Replaces a row's icon, label and badge (the disclosure arrow
|
|
134
|
+
* stays). Scope: `{ node, level, expanded, selected }`.
|
|
135
|
+
*/
|
|
136
|
+
import { computed, onBeforeUnmount, ref } from 'vue'
|
|
137
|
+
import UiIcon from '../icons/Icon.vue'
|
|
138
|
+
|
|
139
|
+
const props = defineProps({
|
|
140
|
+
/**
|
|
141
|
+
* The root nodes.
|
|
142
|
+
* @type {Array<{ id: string | number, label: string, icon?: string, badge?: string | number, disabled?: boolean, children?: Array<object> }>}
|
|
143
|
+
* @default []
|
|
144
|
+
*/
|
|
145
|
+
nodes: { type: Array, default: () => [] },
|
|
146
|
+
/**
|
|
147
|
+
* Id of the selected node, for `v-model`.
|
|
148
|
+
* @type {string | number | null}
|
|
149
|
+
* @default null
|
|
150
|
+
*/
|
|
151
|
+
modelValue: { type: [String, Number], default: null },
|
|
152
|
+
/**
|
|
153
|
+
* Ids of expanded nodes, for `v-model:expanded`. Leave unset to let the
|
|
154
|
+
* tree manage expansion itself.
|
|
155
|
+
* @type {Array<string | number>}
|
|
156
|
+
* @default undefined
|
|
157
|
+
*/
|
|
158
|
+
expanded: { type: Array, default: undefined },
|
|
159
|
+
/**
|
|
160
|
+
* Ids expanded initially when `expanded` is not bound.
|
|
161
|
+
* @type {Array<string | number>}
|
|
162
|
+
* @default []
|
|
163
|
+
*/
|
|
164
|
+
defaultExpanded: { type: Array, default: () => [] },
|
|
165
|
+
/**
|
|
166
|
+
* Accessible name for the tree.
|
|
167
|
+
* @type {string}
|
|
168
|
+
* @default ''
|
|
169
|
+
*/
|
|
170
|
+
label: { type: String, default: '' },
|
|
171
|
+
/**
|
|
172
|
+
* Indentation per level, in pixels.
|
|
173
|
+
* @type {number}
|
|
174
|
+
* @default 16
|
|
175
|
+
*/
|
|
176
|
+
indent: { type: Number, default: 16 },
|
|
177
|
+
/**
|
|
178
|
+
* Let rows be dragged.
|
|
179
|
+
* @type {boolean}
|
|
180
|
+
* @default false
|
|
181
|
+
*/
|
|
182
|
+
draggable: { type: Boolean, default: false },
|
|
183
|
+
/**
|
|
184
|
+
* Let rows accept drops: `true` for every enabled row, or a predicate
|
|
185
|
+
* `(node, event) => boolean`.
|
|
186
|
+
* @type {boolean | Function}
|
|
187
|
+
* @default false
|
|
188
|
+
*/
|
|
189
|
+
droppable: { type: [Boolean, Function], default: false },
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
const emit = defineEmits(['update:modelValue', 'update:expanded', 'select', 'drop', 'contextmenu'])
|
|
193
|
+
|
|
194
|
+
/** MIME type a dragged row carries, so drops can tell tree rows from other data. */
|
|
195
|
+
const DRAG_TYPE = 'application/x-numori-tree-node'
|
|
196
|
+
|
|
197
|
+
// ── Expansion (controlled or uncontrolled) ──
|
|
198
|
+
const internalExpanded = ref(new Set(props.defaultExpanded))
|
|
199
|
+
const expandedSet = computed(() =>
|
|
200
|
+
props.expanded ? new Set(props.expanded) : internalExpanded.value,
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
function setExpanded(next) {
|
|
204
|
+
internalExpanded.value = next
|
|
205
|
+
emit('update:expanded', [...next])
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function toggle(id, force) {
|
|
209
|
+
const next = new Set(expandedSet.value)
|
|
210
|
+
const open = force ?? !next.has(id)
|
|
211
|
+
if (open === next.has(id)) return
|
|
212
|
+
if (open) next.add(id)
|
|
213
|
+
else next.delete(id)
|
|
214
|
+
setExpanded(next)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// ── Flatten the visible part of the tree ──
|
|
218
|
+
const rows = computed(() => {
|
|
219
|
+
const out = []
|
|
220
|
+
const walk = (nodes, level, parentId) => {
|
|
221
|
+
nodes.forEach((node, i) => {
|
|
222
|
+
const hasChildren = Array.isArray(node.children) && node.children.length > 0
|
|
223
|
+
const expanded = hasChildren && expandedSet.value.has(node.id)
|
|
224
|
+
out.push({
|
|
225
|
+
node,
|
|
226
|
+
level,
|
|
227
|
+
parentId,
|
|
228
|
+
hasChildren,
|
|
229
|
+
expanded,
|
|
230
|
+
setSize: nodes.length,
|
|
231
|
+
posInSet: i + 1,
|
|
232
|
+
})
|
|
233
|
+
if (expanded) walk(node.children, level + 1, node.id)
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
walk(props.nodes, 1, null)
|
|
237
|
+
return out
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
// ── Focus (roving tabindex) ──
|
|
241
|
+
const userFocusId = ref(null)
|
|
242
|
+
const focusId = computed({
|
|
243
|
+
get() {
|
|
244
|
+
const ids = rows.value.map((r) => r.node.id)
|
|
245
|
+
if (userFocusId.value !== null && ids.includes(userFocusId.value)) return userFocusId.value
|
|
246
|
+
if (props.modelValue !== null && ids.includes(props.modelValue)) return props.modelValue
|
|
247
|
+
return rows.value.find((r) => !r.node.disabled)?.node.id ?? null
|
|
248
|
+
},
|
|
249
|
+
set(id) {
|
|
250
|
+
userFocusId.value = id
|
|
251
|
+
},
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
const rowEls = new Map()
|
|
255
|
+
const setRowRef = (id, el) => {
|
|
256
|
+
if (el) rowEls.set(id, el)
|
|
257
|
+
else rowEls.delete(id)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function focusRow(id) {
|
|
261
|
+
if (id === null || id === undefined) return
|
|
262
|
+
focusId.value = id
|
|
263
|
+
rowEls.get(id)?.focus()
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function select(row) {
|
|
267
|
+
if (row.node.disabled) return
|
|
268
|
+
emit('update:modelValue', row.node.id)
|
|
269
|
+
emit('select', row.node)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function onRowClick(row) {
|
|
273
|
+
focusId.value = row.node.id
|
|
274
|
+
select(row)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// ── Keyboard ──
|
|
278
|
+
let typeahead = ''
|
|
279
|
+
let typeaheadTimer = null
|
|
280
|
+
|
|
281
|
+
function onKeydown(event) {
|
|
282
|
+
const list = rows.value
|
|
283
|
+
const index = list.findIndex((r) => r.node.id === focusId.value)
|
|
284
|
+
if (index === -1) return
|
|
285
|
+
const row = list[index]
|
|
286
|
+
|
|
287
|
+
const enabledFrom = (start, step) => {
|
|
288
|
+
for (let i = start; i >= 0 && i < list.length; i += step) {
|
|
289
|
+
if (!list[i].node.disabled) return list[i].node.id
|
|
290
|
+
}
|
|
291
|
+
return null
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
switch (event.key) {
|
|
295
|
+
case 'ArrowDown':
|
|
296
|
+
event.preventDefault()
|
|
297
|
+
focusRow(enabledFrom(index + 1, 1))
|
|
298
|
+
return
|
|
299
|
+
case 'ArrowUp':
|
|
300
|
+
event.preventDefault()
|
|
301
|
+
focusRow(enabledFrom(index - 1, -1))
|
|
302
|
+
return
|
|
303
|
+
case 'Home':
|
|
304
|
+
event.preventDefault()
|
|
305
|
+
focusRow(enabledFrom(0, 1))
|
|
306
|
+
return
|
|
307
|
+
case 'End':
|
|
308
|
+
event.preventDefault()
|
|
309
|
+
focusRow(enabledFrom(list.length - 1, -1))
|
|
310
|
+
return
|
|
311
|
+
case 'ArrowRight':
|
|
312
|
+
event.preventDefault()
|
|
313
|
+
if (row.hasChildren && !row.expanded) toggle(row.node.id, true)
|
|
314
|
+
else if (row.expanded) focusRow(list[index + 1]?.node.id)
|
|
315
|
+
return
|
|
316
|
+
case 'ArrowLeft':
|
|
317
|
+
event.preventDefault()
|
|
318
|
+
if (row.expanded) toggle(row.node.id, false)
|
|
319
|
+
else focusRow(row.parentId)
|
|
320
|
+
return
|
|
321
|
+
case 'Enter':
|
|
322
|
+
case ' ':
|
|
323
|
+
event.preventDefault()
|
|
324
|
+
select(row)
|
|
325
|
+
return
|
|
326
|
+
case '*': {
|
|
327
|
+
event.preventDefault()
|
|
328
|
+
const next = new Set(expandedSet.value)
|
|
329
|
+
for (const sibling of list) {
|
|
330
|
+
if (sibling.parentId === row.parentId && sibling.hasChildren) next.add(sibling.node.id)
|
|
331
|
+
}
|
|
332
|
+
setExpanded(next)
|
|
333
|
+
return
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Type-ahead: printable characters, no modifiers.
|
|
338
|
+
if (event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
|
|
339
|
+
typeahead += event.key.toLowerCase()
|
|
340
|
+
clearTimeout(typeaheadTimer)
|
|
341
|
+
typeaheadTimer = setTimeout(() => (typeahead = ''), 500)
|
|
342
|
+
const ordered = [...list.slice(index + 1), ...list.slice(0, index + 1)]
|
|
343
|
+
// A single repeated letter cycles through matches; a longer prefix
|
|
344
|
+
// searches from the current row.
|
|
345
|
+
const search = typeahead.length > 1 ? [row, ...ordered] : ordered
|
|
346
|
+
const match = search.find(
|
|
347
|
+
(r) => !r.node.disabled && String(r.node.label).toLowerCase().startsWith(typeahead),
|
|
348
|
+
)
|
|
349
|
+
if (match) focusRow(match.node.id)
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// ── Drag and drop ──
|
|
354
|
+
const draggingId = ref(null)
|
|
355
|
+
const dropTargetId = ref(null)
|
|
356
|
+
let expandTimer = null
|
|
357
|
+
|
|
358
|
+
function onDragStart(row, event) {
|
|
359
|
+
if (!props.draggable || row.node.disabled) return
|
|
360
|
+
draggingId.value = row.node.id
|
|
361
|
+
event.dataTransfer.effectAllowed = 'move'
|
|
362
|
+
event.dataTransfer.setData(DRAG_TYPE, String(row.node.id))
|
|
363
|
+
event.dataTransfer.setData('text/plain', String(row.node.label))
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function onDragEnd() {
|
|
367
|
+
draggingId.value = null
|
|
368
|
+
dropTargetId.value = null
|
|
369
|
+
clearTimeout(expandTimer)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function accepts(row, event) {
|
|
373
|
+
if (!props.droppable || row.node.disabled) return false
|
|
374
|
+
if (draggingId.value !== null && draggingId.value === row.node.id) return false
|
|
375
|
+
return typeof props.droppable === 'function' ? !!props.droppable(row.node, event) : true
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function onDragOver(row, event) {
|
|
379
|
+
if (!accepts(row, event)) return
|
|
380
|
+
event.preventDefault()
|
|
381
|
+
if (event.dataTransfer) event.dataTransfer.dropEffect = 'move'
|
|
382
|
+
if (dropTargetId.value !== row.node.id) {
|
|
383
|
+
dropTargetId.value = row.node.id
|
|
384
|
+
clearTimeout(expandTimer)
|
|
385
|
+
if (row.hasChildren && !row.expanded) {
|
|
386
|
+
expandTimer = setTimeout(() => toggle(row.node.id, true), 700)
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function onDragLeave(row, event) {
|
|
392
|
+
if (event.currentTarget.contains(event.relatedTarget)) return
|
|
393
|
+
if (dropTargetId.value === row.node.id) {
|
|
394
|
+
dropTargetId.value = null
|
|
395
|
+
clearTimeout(expandTimer)
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function onDrop(row, event) {
|
|
400
|
+
if (!accepts(row, event)) return
|
|
401
|
+
event.preventDefault()
|
|
402
|
+
const raw = event.dataTransfer?.getData(DRAG_TYPE)
|
|
403
|
+
const source = raw ? (draggingId.value ?? raw) : null
|
|
404
|
+
dropTargetId.value = null
|
|
405
|
+
clearTimeout(expandTimer)
|
|
406
|
+
emit('drop', { node: row.node, event, sourceId: source })
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
onBeforeUnmount(() => {
|
|
410
|
+
clearTimeout(expandTimer)
|
|
411
|
+
clearTimeout(typeaheadTimer)
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
// ── Appearance ──
|
|
415
|
+
function rowClass(row) {
|
|
416
|
+
const selected = row.node.id === props.modelValue
|
|
417
|
+
return [
|
|
418
|
+
selected
|
|
419
|
+
? 'bg-primary-50 text-primary-700 dark:bg-primary-400/10 dark:text-primary-200'
|
|
420
|
+
: 'text-gray-700 hover:bg-gray-100 hover:text-gray-900 dark:text-gray-300 dark:hover:bg-gray-800 dark:hover:text-gray-100',
|
|
421
|
+
row.node.disabled ? 'pointer-events-none opacity-40' : 'cursor-pointer',
|
|
422
|
+
dropTargetId.value === row.node.id
|
|
423
|
+
? 'ring-2 ring-inset ring-primary-400 dark:ring-primary-500'
|
|
424
|
+
: '',
|
|
425
|
+
draggingId.value === row.node.id ? 'opacity-50' : '',
|
|
426
|
+
].join(' ')
|
|
427
|
+
}
|
|
428
|
+
</script>
|