numori-ui 0.5.0 → 0.7.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.
Files changed (54) hide show
  1. package/CHANGELOG.md +85 -0
  2. package/README.md +11 -3
  3. package/dist/components/Avatar.js +71 -19
  4. package/dist/components/Avatar.js.map +1 -1
  5. package/dist/components/ChipsInput.js +423 -0
  6. package/dist/components/ChipsInput.js.map +1 -0
  7. package/dist/components/Drawer.js +236 -0
  8. package/dist/components/Drawer.js.map +1 -0
  9. package/dist/components/Dropdown.js +4 -2
  10. package/dist/components/Dropdown.js.map +1 -1
  11. package/dist/components/Modal.js +61 -4
  12. package/dist/components/Modal.js.map +1 -1
  13. package/dist/components/Prompt.js +11 -6
  14. package/dist/components/Prompt.js.map +1 -1
  15. package/dist/components/Select.js +1 -1
  16. package/dist/components/Select.js.map +1 -1
  17. package/dist/components/Skeleton.js +122 -0
  18. package/dist/components/Skeleton.js.map +1 -0
  19. package/dist/components/SplitPane.js +264 -0
  20. package/dist/components/SplitPane.js.map +1 -0
  21. package/dist/components/Toast.js +62 -16
  22. package/dist/components/Toast.js.map +1 -1
  23. package/dist/components/Tree.js +417 -0
  24. package/dist/components/Tree.js.map +1 -0
  25. package/dist/components/VirtualList.js +292 -0
  26. package/dist/components/VirtualList.js.map +1 -0
  27. package/dist/components/index.js +93 -82
  28. package/dist/components/index.js.map +1 -1
  29. package/dist/icons/mdi.js +3 -0
  30. package/dist/icons/mdi.js.map +1 -1
  31. package/dist/index.d.ts +6 -0
  32. package/dist/index.js +49 -43
  33. package/dist/index.js.map +1 -1
  34. package/dist/internal/focusTrap.js +163 -0
  35. package/dist/internal/focusTrap.js.map +1 -0
  36. package/dist/manifest.js +7 -1
  37. package/dist/manifest.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/components/Avatar.vue +69 -18
  40. package/src/components/ChipsInput.vue +387 -0
  41. package/src/components/Drawer.vue +213 -0
  42. package/src/components/Dropdown.vue +4 -1
  43. package/src/components/Modal.vue +51 -3
  44. package/src/components/Prompt.vue +14 -3
  45. package/src/components/Select.vue +1 -1
  46. package/src/components/Skeleton.vue +91 -0
  47. package/src/components/SplitPane.vue +264 -0
  48. package/src/components/Toast.vue +57 -13
  49. package/src/components/Tree.vue +428 -0
  50. package/src/components/VirtualList.vue +250 -0
  51. package/src/components/index.js +18 -1
  52. package/src/icons/mdi.js +3 -0
  53. package/src/internal/focusTrap.js +185 -0
  54. package/src/manifest.js +6 -0
@@ -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>
@@ -0,0 +1,250 @@
1
+ <template>
2
+ <div
3
+ ref="scrollerRef"
4
+ class="relative min-h-0 overflow-y-auto overscroll-contain focus-visible:outline-none"
5
+ :role="role || undefined"
6
+ :aria-label="label || undefined"
7
+ :aria-multiselectable="role === 'listbox' && multiselectable ? 'true' : undefined"
8
+ :tabindex="focusable ? 0 : undefined"
9
+ @scroll.passive="onScroll"
10
+ >
11
+ <slot v-if="items.length === 0" name="empty" />
12
+ <!-- One spacer as tall as the whole list keeps the scrollbar honest; only
13
+ the rows near the viewport are rendered inside it. -->
14
+ <div v-else class="relative w-full" :style="{ height: `${totalHeight}px` }">
15
+ <div
16
+ v-for="row in visibleRows"
17
+ :key="row.key"
18
+ :role="itemRole || undefined"
19
+ :aria-setsize="items.length"
20
+ :aria-posinset="row.index + 1"
21
+ class="absolute inset-x-0"
22
+ :style="{ top: `${row.index * itemHeight}px`, height: `${itemHeight}px` }"
23
+ >
24
+ <slot :item="row.item" :index="row.index" />
25
+ </div>
26
+ </div>
27
+ </div>
28
+ </template>
29
+
30
+ <script setup>
31
+ /**
32
+ * UiVirtualList — a scrolling list that renders only the rows near the
33
+ * viewport, so ten thousand items cost about as much as thirty.
34
+ *
35
+ * Rows have a fixed height (`itemHeight`). That is a deliberate restriction:
36
+ * it makes the position of every row a multiplication rather than a
37
+ * measurement, so scrolling never jumps and `scrollToIndex` is exact. Lists
38
+ * with variable-height content (an inbox with optional snippets, say) should
39
+ * pick one height per density setting.
40
+ *
41
+ * Accessibility: because most rows are not in the DOM, each rendered row
42
+ * carries `aria-setsize` and `aria-posinset`, so a screen reader still
43
+ * announces "item 412 of 9,000". The container and row roles default to
44
+ * `list` / `listitem`; for a selectable list set `role="listbox"` and
45
+ * `item-role="option"`, and put `aria-selected` on your row content's root via
46
+ * the slot (or pass `item-role=""` and supply roles yourself).
47
+ *
48
+ * Keyboard navigation between rows is the caller's job — what Up and Down mean
49
+ * differs between a picker and an inbox — but `scrollToIndex()` is exposed so
50
+ * the active row can be kept in view.
51
+ *
52
+ * @example
53
+ * <UiVirtualList :items="messages" :item-height="72" label="Messages" @end-reached="loadMore">
54
+ * <template #default="{ item }"><MessageRow :message="item" /></template>
55
+ * <template #empty><UiEmptyState title="Nothing here" /></template>
56
+ * </UiVirtualList>
57
+ *
58
+ * @emits {{ start: number, end: number }} range — The rendered index range changed
59
+ * (end exclusive). Useful for prefetching what is about to be seen.
60
+ * @emits {void} end-reached — Scrolled within `endThreshold` rows of the bottom.
61
+ * @emits {Event} scroll — The list scrolled.
62
+ *
63
+ * @slot default — One row. Scope: `{ item, index }`.
64
+ * @slot empty — Shown when `items` is empty.
65
+ */
66
+ import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
67
+
68
+ const props = defineProps({
69
+ /**
70
+ * The rows to render.
71
+ * @type {Array<any>}
72
+ * @default []
73
+ */
74
+ items: { type: Array, default: () => [] },
75
+ /**
76
+ * Height of every row in pixels.
77
+ * @type {number}
78
+ * @default 48
79
+ */
80
+ itemHeight: { type: Number, default: 48 },
81
+ /**
82
+ * Property used as each row's key, or a function `(item, index) => key`.
83
+ * Falls back to the index when the property is missing.
84
+ * @type {string | Function}
85
+ * @default 'id'
86
+ */
87
+ itemKey: { type: [String, Function], default: 'id' },
88
+ /**
89
+ * Extra rows rendered above and below the viewport, so fast scrolling does
90
+ * not show blank space.
91
+ * @type {number}
92
+ * @default 8
93
+ */
94
+ overscan: { type: Number, default: 8 },
95
+ /**
96
+ * Viewport height assumed before the list has been measured (and on the
97
+ * server), in pixels.
98
+ * @type {number}
99
+ * @default 800
100
+ */
101
+ initialHeight: { type: Number, default: 800 },
102
+ /**
103
+ * How many rows from the bottom `end-reached` fires.
104
+ * @type {number}
105
+ * @default 5
106
+ */
107
+ endThreshold: { type: Number, default: 5 },
108
+ /**
109
+ * ARIA role of the scroll container. Empty for none.
110
+ * @type {string}
111
+ * @default 'list'
112
+ */
113
+ role: { type: String, default: 'list' },
114
+ /**
115
+ * ARIA role of each row wrapper. Empty for none.
116
+ * @type {string}
117
+ * @default 'listitem'
118
+ */
119
+ itemRole: { type: String, default: 'listitem' },
120
+ /**
121
+ * Accessible name for the list.
122
+ * @type {string}
123
+ * @default ''
124
+ */
125
+ label: { type: String, default: '' },
126
+ /**
127
+ * With `role="listbox"`, announce that several rows can be selected.
128
+ * @type {boolean}
129
+ * @default false
130
+ */
131
+ multiselectable: { type: Boolean, default: false },
132
+ /**
133
+ * Make the scroll container itself a tab stop, so keyboard users can scroll
134
+ * it. Turn off when your rows manage focus themselves.
135
+ * @type {boolean}
136
+ * @default true
137
+ */
138
+ focusable: { type: Boolean, default: true },
139
+ })
140
+
141
+ const emit = defineEmits(['range', 'end-reached', 'scroll'])
142
+
143
+ const scrollerRef = ref(null)
144
+ const scrollTop = ref(0)
145
+ const viewport = ref(props.initialHeight)
146
+
147
+ const totalHeight = computed(() => props.items.length * props.itemHeight)
148
+
149
+ const range = computed(() => {
150
+ const first = Math.floor(scrollTop.value / props.itemHeight)
151
+ const visible = Math.ceil(viewport.value / props.itemHeight) + 1
152
+ const start = Math.max(0, first - props.overscan)
153
+ const end = Math.min(props.items.length, first + visible + props.overscan)
154
+ return { start, end }
155
+ })
156
+
157
+ const keyOf = (item, index) => {
158
+ if (typeof props.itemKey === 'function') return props.itemKey(item, index)
159
+ const key = item && typeof item === 'object' ? item[props.itemKey] : undefined
160
+ return key ?? index
161
+ }
162
+
163
+ const visibleRows = computed(() => {
164
+ const rows = []
165
+ for (let index = range.value.start; index < range.value.end; index++) {
166
+ const item = props.items[index]
167
+ rows.push({ item, index, key: keyOf(item, index) })
168
+ }
169
+ return rows
170
+ })
171
+
172
+ watch(range, (next, previous) => {
173
+ if (previous && next.start === previous.start && next.end === previous.end) return
174
+ emit('range', { ...next })
175
+ })
176
+
177
+ // ── End detection ──
178
+ // Fires once per arrival at the bottom; scrolling away and back re-arms it.
179
+ let endArmed = true
180
+ const checkEnd = () => {
181
+ const remaining =
182
+ props.items.length - Math.ceil((scrollTop.value + viewport.value) / props.itemHeight)
183
+ if (remaining <= props.endThreshold) {
184
+ if (endArmed && props.items.length > 0) {
185
+ endArmed = false
186
+ emit('end-reached')
187
+ }
188
+ } else {
189
+ endArmed = true
190
+ }
191
+ }
192
+
193
+ // New items arriving (a page loaded) re-arms the trigger.
194
+ watch(
195
+ () => props.items.length,
196
+ () => {
197
+ endArmed = true
198
+ checkEnd()
199
+ },
200
+ )
201
+
202
+ function onScroll(event) {
203
+ scrollTop.value = event.target.scrollTop
204
+ checkEnd()
205
+ emit('scroll', event)
206
+ }
207
+
208
+ /**
209
+ * Scroll so the row at `index` is visible.
210
+ *
211
+ * @param {number} index
212
+ * @param {'auto' | 'start' | 'center' | 'end'} [align='auto'] `auto` scrolls
213
+ * only as far as needed, and not at all if the row is already in view.
214
+ */
215
+ function scrollToIndex(index, align = 'auto') {
216
+ const el = scrollerRef.value
217
+ if (!el || index < 0 || index >= props.items.length) return
218
+ const top = index * props.itemHeight
219
+ const bottom = top + props.itemHeight
220
+ const view = el.clientHeight
221
+ let target = null
222
+ if (align === 'start') target = top
223
+ else if (align === 'end') target = bottom - view
224
+ else if (align === 'center') target = top - (view - props.itemHeight) / 2
225
+ else if (top < el.scrollTop) target = top
226
+ else if (bottom > el.scrollTop + view) target = bottom - view
227
+ if (target === null) return
228
+ el.scrollTop = Math.max(0, target)
229
+ scrollTop.value = el.scrollTop
230
+ }
231
+
232
+ defineExpose({ scrollToIndex, scrollerRef })
233
+
234
+ // ── Measure the viewport ──
235
+ let observer = null
236
+ onMounted(() => {
237
+ const el = scrollerRef.value
238
+ if (!el) return
239
+ const measure = () => {
240
+ viewport.value = el.clientHeight || props.initialHeight
241
+ checkEnd()
242
+ }
243
+ measure()
244
+ if (typeof ResizeObserver !== 'undefined') {
245
+ observer = new ResizeObserver(measure)
246
+ observer.observe(el)
247
+ }
248
+ })
249
+ onBeforeUnmount(() => observer?.disconnect())
250
+ </script>