numori-ui 0.4.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.
Files changed (99) hide show
  1. package/CHANGELOG.md +120 -0
  2. package/README.md +27 -20
  3. package/dist/components/AppShell.js +234 -0
  4. package/dist/components/AppShell.js.map +1 -0
  5. package/dist/components/Avatar.js +71 -19
  6. package/dist/components/Avatar.js.map +1 -1
  7. package/dist/components/Checkbox.js +11 -2
  8. package/dist/components/Checkbox.js.map +1 -1
  9. package/dist/components/CopyField.js +195 -0
  10. package/dist/components/CopyField.js.map +1 -0
  11. package/dist/components/DescriptionList.js +116 -0
  12. package/dist/components/DescriptionList.js.map +1 -0
  13. package/dist/components/Dropdown.js +3 -1
  14. package/dist/components/Dropdown.js.map +1 -1
  15. package/dist/components/EmptyState.js +123 -0
  16. package/dist/components/EmptyState.js.map +1 -0
  17. package/dist/components/FormField.js +43 -13
  18. package/dist/components/FormField.js.map +1 -1
  19. package/dist/components/Input.js +15 -1
  20. package/dist/components/Input.js.map +1 -1
  21. package/dist/components/Modal.js +61 -4
  22. package/dist/components/Modal.js.map +1 -1
  23. package/dist/components/PageHeader.js +106 -0
  24. package/dist/components/PageHeader.js.map +1 -0
  25. package/dist/components/Pagination.js +236 -0
  26. package/dist/components/Pagination.js.map +1 -0
  27. package/dist/components/Prompt.js +11 -6
  28. package/dist/components/Prompt.js.map +1 -1
  29. package/dist/components/Segmented.js +10 -2
  30. package/dist/components/Segmented.js.map +1 -1
  31. package/dist/components/Select.js +11 -2
  32. package/dist/components/Select.js.map +1 -1
  33. package/dist/components/Sidebar.js +69 -0
  34. package/dist/components/Sidebar.js.map +1 -0
  35. package/dist/components/SidebarGroup.js +50 -0
  36. package/dist/components/SidebarGroup.js.map +1 -0
  37. package/dist/components/SidebarItem.js +167 -0
  38. package/dist/components/SidebarItem.js.map +1 -0
  39. package/dist/components/Skeleton.js +122 -0
  40. package/dist/components/Skeleton.js.map +1 -0
  41. package/dist/components/SplitPane.js +264 -0
  42. package/dist/components/SplitPane.js.map +1 -0
  43. package/dist/components/StatCard.js +185 -0
  44. package/dist/components/StatCard.js.map +1 -0
  45. package/dist/components/Table.js +182 -50
  46. package/dist/components/Table.js.map +1 -1
  47. package/dist/components/Tabs.js +216 -0
  48. package/dist/components/Tabs.js.map +1 -0
  49. package/dist/components/Toggle.js +11 -2
  50. package/dist/components/Toggle.js.map +1 -1
  51. package/dist/components/Tree.js +417 -0
  52. package/dist/components/Tree.js.map +1 -0
  53. package/dist/components/VirtualList.js +292 -0
  54. package/dist/components/VirtualList.js.map +1 -0
  55. package/dist/components/index.js +105 -81
  56. package/dist/components/index.js.map +1 -1
  57. package/dist/icons/mdi.js +12 -0
  58. package/dist/icons/mdi.js.map +1 -1
  59. package/dist/index.d.ts +16 -1
  60. package/dist/index.js +54 -39
  61. package/dist/index.js.map +1 -1
  62. package/dist/internal/focusTrap.js +163 -0
  63. package/dist/internal/focusTrap.js.map +1 -0
  64. package/dist/internal/formField.js +26 -0
  65. package/dist/internal/formField.js.map +1 -0
  66. package/dist/manifest.js +16 -1
  67. package/dist/manifest.js.map +1 -1
  68. package/package.json +1 -1
  69. package/src/components/AppShell.vue +231 -0
  70. package/src/components/Avatar.vue +69 -18
  71. package/src/components/Checkbox.vue +5 -0
  72. package/src/components/CopyField.vue +178 -0
  73. package/src/components/DescriptionList.vue +114 -0
  74. package/src/components/Dropdown.vue +4 -1
  75. package/src/components/EmptyState.vue +107 -0
  76. package/src/components/FormField.vue +37 -4
  77. package/src/components/Input.vue +9 -0
  78. package/src/components/Modal.vue +51 -3
  79. package/src/components/PageHeader.vue +102 -0
  80. package/src/components/Pagination.vue +199 -0
  81. package/src/components/Prompt.vue +14 -3
  82. package/src/components/Segmented.vue +5 -0
  83. package/src/components/Select.vue +6 -1
  84. package/src/components/Sidebar.vue +61 -0
  85. package/src/components/SidebarGroup.vue +43 -0
  86. package/src/components/SidebarItem.vue +159 -0
  87. package/src/components/Skeleton.vue +91 -0
  88. package/src/components/SplitPane.vue +264 -0
  89. package/src/components/StatCard.vue +165 -0
  90. package/src/components/Table.vue +148 -18
  91. package/src/components/Tabs.vue +217 -0
  92. package/src/components/Toggle.vue +5 -0
  93. package/src/components/Tree.vue +428 -0
  94. package/src/components/VirtualList.vue +250 -0
  95. package/src/components/index.js +45 -6
  96. package/src/icons/mdi.js +12 -0
  97. package/src/internal/focusTrap.js +185 -0
  98. package/src/internal/formField.js +23 -0
  99. package/src/manifest.js +15 -0
@@ -0,0 +1,217 @@
1
+ <template>
2
+ <div>
3
+ <div
4
+ role="tablist"
5
+ :aria-label="ariaLabel || undefined"
6
+ :class="listClass"
7
+ @keydown="onKeydown"
8
+ >
9
+ <button
10
+ v-for="(tab, index) in tabs"
11
+ :id="tabId(index)"
12
+ :key="tab.value"
13
+ :ref="(el) => (tabEls[index] = el)"
14
+ type="button"
15
+ role="tab"
16
+ :aria-selected="index === activeIndex ? 'true' : 'false'"
17
+ :aria-controls="panel ? panelId : undefined"
18
+ :tabindex="index === focusIndex ? 0 : -1"
19
+ :disabled="tab.disabled"
20
+ :class="tabClass(index, tab)"
21
+ @click="select(index)"
22
+ >
23
+ <UiIcon v-if="tab.icon" :name="tab.icon" class="size-4 shrink-0" />
24
+ <span>{{ tab.label }}</span>
25
+ <span
26
+ v-if="tab.badge !== undefined && tab.badge !== null && tab.badge !== ''"
27
+ class="inline-flex min-w-5 items-center justify-center rounded-full px-1.5 text-xs font-semibold tabular-nums"
28
+ :class="
29
+ index === activeIndex
30
+ ? 'bg-primary-100 text-primary-700 dark:bg-primary-400/20 dark:text-primary-200'
31
+ : 'bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-300'
32
+ "
33
+ >{{ tab.badge }}</span
34
+ >
35
+ </button>
36
+ </div>
37
+
38
+ <div
39
+ v-if="panel"
40
+ :id="panelId"
41
+ role="tabpanel"
42
+ :aria-labelledby="activeIndex >= 0 ? tabId(activeIndex) : undefined"
43
+ tabindex="0"
44
+ class="focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-primary-500"
45
+ :class="panelClass"
46
+ >
47
+ <slot :active="modelValue" />
48
+ </div>
49
+ </div>
50
+ </template>
51
+
52
+ <script setup>
53
+ /**
54
+ * UiTabs — an accessible tab list, with an optional panel for the active tab.
55
+ *
56
+ * Implements the WAI-ARIA tabs pattern: `role="tablist"` / `"tab"` /
57
+ * `"tabpanel"`, a single tab stop (roving `tabindex`), and arrow keys, Home and
58
+ * End to move between tabs. Moving with the keyboard selects the tab
59
+ * (automatic activation), skipping disabled tabs.
60
+ *
61
+ * Render the panel through the default slot, which receives the active value.
62
+ * When the content lives elsewhere — a routed `<RouterView>` below the tabs,
63
+ * say — set `:panel="false"` and use the tabs as navigation only.
64
+ *
65
+ * Two looks: `line` (an underline under the active tab, for page sections) and
66
+ * `pill` (a segmented track, for compact switches inside cards).
67
+ *
68
+ * @example
69
+ * <UiTabs
70
+ * v-model="tab"
71
+ * aria-label="App sections"
72
+ * :tabs="[
73
+ * { value: 'overview', label: 'Overview', icon: 'mdi:view-grid' },
74
+ * { value: 'keys', label: 'Keys', badge: 2 },
75
+ * { value: 'danger', label: 'Danger zone' },
76
+ * ]"
77
+ * >
78
+ * <template #default="{ active }">
79
+ * <OverviewPanel v-if="active === 'overview'" />
80
+ * <KeysPanel v-else-if="active === 'keys'" />
81
+ * </template>
82
+ * </UiTabs>
83
+ *
84
+ * @emits {string} update:modelValue — The selected tab's value.
85
+ *
86
+ * @slot default — Panel content. Scope: `{ active }`.
87
+ */
88
+ import { computed, nextTick, useId } from 'vue'
89
+ import UiIcon from '../icons/Icon.vue'
90
+
91
+ const props = defineProps({
92
+ /**
93
+ * Value of the active tab.
94
+ * @type {string}
95
+ * @default ''
96
+ */
97
+ modelValue: { type: String, default: '' },
98
+
99
+ /**
100
+ * Tabs, left to right. Each is `{ value, label, icon?, badge?, disabled? }`.
101
+ * @type {Array<{ value: string, label: string, icon?: string, badge?: string | number, disabled?: boolean }>}
102
+ */
103
+ tabs: { type: Array, required: true },
104
+
105
+ /**
106
+ * Accessible name of the tab list.
107
+ * @type {string}
108
+ * @default ''
109
+ */
110
+ ariaLabel: { type: String, default: '' },
111
+
112
+ /**
113
+ * Visual style.
114
+ * @type {string}
115
+ * @default 'line'
116
+ * @values 'line' | 'pill'
117
+ */
118
+ variant: { type: String, default: 'line' },
119
+
120
+ /**
121
+ * Render a `tabpanel` holding the default slot. Turn off when the tabs only
122
+ * navigate and the content is rendered elsewhere.
123
+ * @type {boolean}
124
+ * @default true
125
+ */
126
+ panel: { type: Boolean, default: true },
127
+
128
+ /**
129
+ * Classes for the panel.
130
+ * @type {string}
131
+ * @default 'pt-6'
132
+ */
133
+ panelClass: { type: String, default: 'pt-6' },
134
+ })
135
+
136
+ const emit = defineEmits(['update:modelValue'])
137
+
138
+ const baseId = useId()
139
+ const panelId = `${baseId}-panel`
140
+ const tabId = (index) => `${baseId}-tab-${index}`
141
+
142
+ /** Template refs, written by the function ref on each button. */
143
+ const tabEls = []
144
+
145
+ const activeIndex = computed(() => props.tabs.findIndex((t) => t.value === props.modelValue))
146
+
147
+ /** The tab that holds the single tab stop: the active one, else the first enabled. */
148
+ const focusIndex = computed(() => {
149
+ if (activeIndex.value >= 0 && !props.tabs[activeIndex.value].disabled) return activeIndex.value
150
+ return props.tabs.findIndex((t) => !t.disabled)
151
+ })
152
+
153
+ function select(index) {
154
+ const tab = props.tabs[index]
155
+ if (!tab || tab.disabled) return
156
+ if (tab.value !== props.modelValue) emit('update:modelValue', tab.value)
157
+ }
158
+
159
+ function step(from, delta) {
160
+ const count = props.tabs.length
161
+ for (let i = 1; i <= count; i++) {
162
+ const next = (from + delta * i + count * i) % count
163
+ if (!props.tabs[next].disabled) return next
164
+ }
165
+ return from
166
+ }
167
+
168
+ function onKeydown(event) {
169
+ const current = tabEls.indexOf(event.target)
170
+ if (current < 0) return
171
+ let next = null
172
+ if (event.key === 'ArrowRight' || event.key === 'ArrowDown') next = step(current, 1)
173
+ else if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') next = step(current, -1)
174
+ else if (event.key === 'Home') next = props.tabs.findIndex((t) => !t.disabled)
175
+ else if (event.key === 'End') {
176
+ for (let i = props.tabs.length - 1; i >= 0; i--) {
177
+ if (!props.tabs[i].disabled) {
178
+ next = i
179
+ break
180
+ }
181
+ }
182
+ }
183
+ if (next === null || next < 0) return
184
+ event.preventDefault()
185
+ select(next)
186
+ nextTick(() => tabEls[next]?.focus())
187
+ }
188
+
189
+ const listClass = computed(() =>
190
+ props.variant === 'pill'
191
+ ? 'inline-flex max-w-full gap-1 overflow-x-auto rounded-xl bg-gray-100 p-1 dark:bg-gray-800'
192
+ : 'flex gap-6 overflow-x-auto border-b border-gray-200 dark:border-gray-800',
193
+ )
194
+
195
+ function tabClass(index, tab) {
196
+ const active = index === activeIndex.value
197
+ const base =
198
+ 'inline-flex shrink-0 items-center gap-2 text-sm font-medium whitespace-nowrap transition-colors focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-500 disabled:cursor-not-allowed disabled:opacity-40'
199
+ if (props.variant === 'pill') {
200
+ return [
201
+ base,
202
+ 'rounded-lg px-3 py-1.5',
203
+ active
204
+ ? 'bg-white text-gray-900 shadow-sm dark:bg-gray-900 dark:text-gray-100'
205
+ : 'text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-100',
206
+ ]
207
+ }
208
+ return [
209
+ base,
210
+ '-mb-px border-b-2 px-1 py-3',
211
+ active
212
+ ? 'border-primary-500 text-primary-600 dark:border-primary-400 dark:text-primary-300'
213
+ : 'border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-800 dark:text-gray-400 dark:hover:border-gray-600 dark:hover:text-gray-200',
214
+ tab.disabled ? 'hover:border-transparent' : '',
215
+ ]
216
+ }
217
+ </script>
@@ -1,8 +1,10 @@
1
1
  <template>
2
2
  <!-- Accessible toggle switch rendered as a button with role="switch" -->
3
3
  <button
4
+ :id="field?.controlId.value"
4
5
  type="button"
5
6
  :disabled="disabled"
7
+ :aria-describedby="field?.describedBy.value"
6
8
  class="relative inline-flex items-center shrink-0 rounded-full transition-colors"
7
9
  :class="[
8
10
  sizeClasses.track,
@@ -40,6 +42,7 @@
40
42
  * @emits {boolean} update:modelValue — Emitted when the toggle is switched (v-model)
41
43
  */
42
44
  import { computed } from 'vue'
45
+ import { useFormField } from '../internal/formField.js'
43
46
  const props = defineProps({
44
47
  /**
45
48
  * Current on/off state (v-model).
@@ -82,6 +85,8 @@ const props = defineProps({
82
85
 
83
86
  const emit = defineEmits(['update:modelValue'])
84
87
 
88
+ const field = useFormField()
89
+
85
90
  // Active color uses different shade intensities per size for visual consistency
86
91
  const activeColor = computed(() => {
87
92
  if (props.size === 'sm') {
@@ -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>