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,387 @@
1
+ <template>
2
+ <div ref="rootRef" class="relative">
3
+ <div
4
+ class="flex min-h-10 w-full flex-wrap items-center gap-1.5 rounded-lg border bg-white px-2 py-1.5 transition-colors dark:bg-gray-800"
5
+ :class="[
6
+ focused
7
+ ? 'border-primary-500 ring-2 ring-primary-500/30'
8
+ : 'border-gray-300 dark:border-gray-600',
9
+ disabled ? 'pointer-events-none opacity-60' : 'cursor-text',
10
+ ]"
11
+ @click="inputRef?.focus()"
12
+ >
13
+ <span
14
+ v-for="(chip, index) in modelValue"
15
+ :key="keyOf(chip, index)"
16
+ class="inline-flex max-w-full items-center gap-1 rounded-md py-0.5 pr-1 pl-2 text-sm"
17
+ :class="
18
+ isValid(chip)
19
+ ? 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-100'
20
+ : 'bg-error-50 text-error-700 ring-1 ring-error-300 dark:bg-error-500/15 dark:text-error-200 dark:ring-error-500/40'
21
+ "
22
+ :title="titleOf(chip)"
23
+ >
24
+ <span class="truncate">{{ labelOf(chip) }}</span>
25
+ <span v-if="!isValid(chip)" class="sr-only">{{ invalidLabel }}</span>
26
+ <button
27
+ v-if="!disabled"
28
+ type="button"
29
+ tabindex="-1"
30
+ class="inline-flex size-5 shrink-0 items-center justify-center rounded text-current opacity-60 hover:bg-gray-200 hover:opacity-100 dark:hover:bg-gray-600"
31
+ :aria-label="removeLabel.replace('{chip}', labelOf(chip))"
32
+ @click.stop="remove(index)"
33
+ >
34
+ <UiIcon name="mdi:close" class="size-3.5" />
35
+ </button>
36
+ </span>
37
+ <input
38
+ :id="inputId"
39
+ ref="inputRef"
40
+ v-model="text"
41
+ type="text"
42
+ role="combobox"
43
+ autocomplete="off"
44
+ autocapitalize="off"
45
+ spellcheck="false"
46
+ :inputmode="inputmode"
47
+ :placeholder="modelValue.length ? '' : placeholder"
48
+ :disabled="disabled"
49
+ :aria-label="label || undefined"
50
+ :aria-expanded="open ? 'true' : 'false'"
51
+ :aria-controls="listId"
52
+ :aria-activedescendant="open && active >= 0 ? `${listId}-${active}` : undefined"
53
+ aria-autocomplete="list"
54
+ class="min-w-24 flex-1 bg-transparent px-1 py-0.5 text-sm text-gray-900 outline-none placeholder:text-gray-400 dark:text-gray-100"
55
+ @focus="focused = true"
56
+ @blur="onBlur"
57
+ @keydown="onKeydown"
58
+ @paste="onPaste"
59
+ />
60
+ </div>
61
+
62
+ <ul
63
+ v-show="open"
64
+ :id="listId"
65
+ role="listbox"
66
+ :aria-label="label || undefined"
67
+ class="absolute inset-x-0 top-full z-50 mt-1 max-h-64 overflow-y-auto py-1"
68
+ :class="MENU_PANEL_CLASS"
69
+ >
70
+ <li
71
+ v-for="(item, index) in suggestions"
72
+ :id="`${listId}-${index}`"
73
+ :key="keyOf(item, index)"
74
+ role="option"
75
+ :aria-selected="index === active ? 'true' : 'false'"
76
+ class="flex cursor-pointer flex-col px-3 py-1.5 text-sm"
77
+ :class="
78
+ index === active
79
+ ? 'bg-primary-50 text-primary-800 dark:bg-primary-400/15 dark:text-primary-100'
80
+ : 'text-gray-700 dark:text-gray-200'
81
+ "
82
+ @mousedown.prevent="choose(item)"
83
+ @mouseenter="active = index"
84
+ >
85
+ <slot name="suggestion" :item="item" :active="index === active">
86
+ <span class="truncate font-medium">{{ labelOf(item) }}</span>
87
+ <span v-if="detailOf(item)" class="truncate text-xs text-gray-500 dark:text-gray-400">{{
88
+ detailOf(item)
89
+ }}</span>
90
+ </slot>
91
+ </li>
92
+ </ul>
93
+ </div>
94
+ </template>
95
+
96
+ <script setup>
97
+ /**
98
+ * UiChipsInput — a text field that turns what you type into chips: email
99
+ * recipients, tags, labels.
100
+ *
101
+ * Typing a separator (comma, semicolon or Enter by default), Tab, or leaving
102
+ * the field turns the text into a chip; pasting a list ("Ana <ana@x.com>,
103
+ * ben@y.com") makes one chip per entry. Backspace in an empty field removes
104
+ * the last chip. Chips that fail `validate` stay, marked as invalid, so a typo
105
+ * is visible rather than silently dropped.
106
+ *
107
+ * Suggestions follow the WAI-ARIA combobox pattern: give `suggest` a
108
+ * function that receives the current text and returns (or resolves to) a
109
+ * list; Up and Down move through it, Enter or Tab picks, Escape closes it.
110
+ * Results from an older query are ignored if a newer one has started.
111
+ *
112
+ * Chips can be strings or objects. For objects, supply `chipLabel` (and
113
+ * optionally `chipDetail` and `chipKey`) and `parse`, which turns typed text
114
+ * into a chip (or null to reject it).
115
+ *
116
+ * @example Recipients
117
+ * <UiChipsInput
118
+ * v-model="to"
119
+ * label="To"
120
+ * :parse="parseAddress"
121
+ * :validate="(c) => /@/.test(c.address)"
122
+ * :suggest="findContacts"
123
+ * :chip-label="(c) => c.name || c.address"
124
+ * :chip-detail="(c) => c.address"
125
+ * :chip-key="(c) => c.address"
126
+ * />
127
+ *
128
+ * @example Plain tags
129
+ * <UiChipsInput v-model="tags" label="Tags" placeholder="Add a tag" />
130
+ *
131
+ * @emits {Array} update:modelValue — The chips changed.
132
+ * @emits {object} add — A chip was added.
133
+ * @emits {object} remove — A chip was removed.
134
+ *
135
+ * @slot suggestion — One suggestion row. Scope: `{ item, active }`.
136
+ */
137
+ import { computed, onBeforeUnmount, ref, useId, watch } from 'vue'
138
+ import UiIcon from '../icons/Icon.vue'
139
+ import { MENU_PANEL_CLASS } from '../internal/menu.js'
140
+
141
+ const props = defineProps({
142
+ /**
143
+ * The chips.
144
+ * @type {Array<string | object>}
145
+ * @default []
146
+ */
147
+ modelValue: { type: Array, default: () => [] },
148
+ /**
149
+ * Turn typed text into a chip, or null to reject it.
150
+ * @type {(text: string) => string | object | null}
151
+ * @default trimmed text
152
+ */
153
+ parse: { type: Function, default: (text) => text.trim() || null },
154
+ /**
155
+ * Whether a chip is valid. Invalid chips are kept and marked.
156
+ * @type {(chip: string | object) => boolean}
157
+ * @default () => true
158
+ */
159
+ validate: { type: Function, default: () => true },
160
+ /**
161
+ * Suggestions for the current text (may return a promise).
162
+ * @type {(text: string) => Array | Promise<Array>}
163
+ * @default null
164
+ */
165
+ suggest: { type: Function, default: null },
166
+ /**
167
+ * Label for a chip or suggestion.
168
+ * @type {(chip: string | object) => string}
169
+ */
170
+ chipLabel: { type: Function, default: (chip) => String(chip) },
171
+ /**
172
+ * Secondary text for a suggestion (and a chip's tooltip).
173
+ * @type {(chip: string | object) => string}
174
+ */
175
+ chipDetail: { type: Function, default: () => '' },
176
+ /**
177
+ * Identity of a chip, for de-duplication and keys.
178
+ * @type {(chip: string | object) => string}
179
+ */
180
+ chipKey: { type: Function, default: (chip) => String(chip).toLowerCase() },
181
+ /**
182
+ * Characters that finish a chip as you type.
183
+ * @type {string[]}
184
+ * @default [',', ';']
185
+ */
186
+ separators: { type: Array, default: () => [',', ';'] },
187
+ /**
188
+ * Accessible name of the field.
189
+ * @type {string}
190
+ * @default ''
191
+ */
192
+ label: { type: String, default: '' },
193
+ /**
194
+ * Shown while there are no chips.
195
+ * @type {string}
196
+ * @default ''
197
+ */
198
+ placeholder: { type: String, default: '' },
199
+ /**
200
+ * Virtual keyboard hint.
201
+ * @type {string}
202
+ * @default 'text'
203
+ */
204
+ inputmode: { type: String, default: 'text' },
205
+ /**
206
+ * Accessible name of each chip's remove button; `{chip}` is replaced.
207
+ * @type {string}
208
+ * @default 'Remove {chip}'
209
+ */
210
+ removeLabel: { type: String, default: 'Remove {chip}' },
211
+ /**
212
+ * Read after an invalid chip.
213
+ * @type {string}
214
+ * @default 'invalid'
215
+ */
216
+ invalidLabel: { type: String, default: 'invalid' },
217
+ /**
218
+ * The id of the text field, so a `<label for>` (or UiFormField) can name it.
219
+ * @type {string}
220
+ * @default ''
221
+ */
222
+ id: { type: String, default: '' },
223
+ /**
224
+ * Disable the field.
225
+ * @type {boolean}
226
+ * @default false
227
+ */
228
+ disabled: { type: Boolean, default: false },
229
+ })
230
+
231
+ const emit = defineEmits(['update:modelValue', 'add', 'remove'])
232
+
233
+ const baseId = useId()
234
+ const inputId = computed(() => props.id || `${baseId}-input`)
235
+ const listId = `${baseId}-list`
236
+ const rootRef = ref(null)
237
+ const inputRef = ref(null)
238
+ const text = ref('')
239
+ const focused = ref(false)
240
+ const suggestions = ref([])
241
+ const active = ref(-1)
242
+ const open = computed(() => focused.value && suggestions.value.length > 0)
243
+
244
+ const labelOf = (chip) => props.chipLabel(chip)
245
+ const detailOf = (chip) => props.chipDetail(chip)
246
+ const keyOf = (chip, index) => props.chipKey(chip) ?? index
247
+ const isValid = (chip) => props.validate(chip)
248
+ const titleOf = (chip) => detailOf(chip) || labelOf(chip)
249
+
250
+ // ── Adding and removing ──
251
+ function add(entries) {
252
+ const existing = new Set(props.modelValue.map((c) => props.chipKey(c)))
253
+ const added = []
254
+ for (const entry of entries) {
255
+ const chip = typeof entry === 'string' ? props.parse(entry) : entry
256
+ if (chip === null || chip === undefined || chip === '') continue
257
+ const key = props.chipKey(chip)
258
+ if (existing.has(key)) continue
259
+ existing.add(key)
260
+ added.push(chip)
261
+ }
262
+ if (!added.length) return
263
+ emit('update:modelValue', [...props.modelValue, ...added])
264
+ for (const chip of added) emit('add', chip)
265
+ }
266
+
267
+ function remove(index) {
268
+ const chip = props.modelValue[index]
269
+ const next = props.modelValue.slice()
270
+ next.splice(index, 1)
271
+ emit('update:modelValue', next)
272
+ emit('remove', chip)
273
+ }
274
+
275
+ /**
276
+ * Split a pasted or typed list on separators, but not inside quotes or angle
277
+ * brackets, so `"Smith, Ana" <ana@x.com>` stays one entry.
278
+ */
279
+ function splitList(value) {
280
+ const parts = []
281
+ let current = ''
282
+ let quoted = false
283
+ let angled = false
284
+ for (const char of value) {
285
+ if (char === '"') quoted = !quoted
286
+ else if (char === '<') angled = true
287
+ else if (char === '>') angled = false
288
+ if (!quoted && !angled && (props.separators.includes(char) || char === '\n')) {
289
+ parts.push(current)
290
+ current = ''
291
+ } else {
292
+ current += char
293
+ }
294
+ }
295
+ parts.push(current)
296
+ return parts.map((p) => p.trim()).filter(Boolean)
297
+ }
298
+
299
+ function commitText() {
300
+ if (!text.value.trim()) return false
301
+ add(splitList(text.value))
302
+ text.value = ''
303
+ suggestions.value = []
304
+ return true
305
+ }
306
+
307
+ function choose(item) {
308
+ add([item])
309
+ text.value = ''
310
+ suggestions.value = []
311
+ active.value = -1
312
+ inputRef.value?.focus()
313
+ }
314
+
315
+ // ── Suggestions ──
316
+ let generation = 0
317
+ let debounce = null
318
+ watch(text, (value) => {
319
+ // A separator typed at the end finishes the chip.
320
+ if (value && props.separators.includes(value.at(-1))) {
321
+ const before = value.slice(0, -1)
322
+ if (splitList(before).length) {
323
+ text.value = before
324
+ commitText()
325
+ return
326
+ }
327
+ }
328
+ clearTimeout(debounce)
329
+ if (!props.suggest || !value.trim()) {
330
+ suggestions.value = []
331
+ active.value = -1
332
+ return
333
+ }
334
+ const mine = ++generation
335
+ debounce = setTimeout(async () => {
336
+ const result = await props.suggest(value.trim())
337
+ if (mine !== generation) return
338
+ const taken = new Set(props.modelValue.map((c) => props.chipKey(c)))
339
+ suggestions.value = (result ?? []).filter((item) => !taken.has(props.chipKey(item)))
340
+ active.value = suggestions.value.length ? 0 : -1
341
+ }, 120)
342
+ })
343
+ onBeforeUnmount(() => clearTimeout(debounce))
344
+
345
+ // ── Keyboard ──
346
+ function onKeydown(event) {
347
+ if (event.key === 'ArrowDown' && suggestions.value.length) {
348
+ event.preventDefault()
349
+ active.value = (active.value + 1) % suggestions.value.length
350
+ } else if (event.key === 'ArrowUp' && suggestions.value.length) {
351
+ event.preventDefault()
352
+ active.value = (active.value - 1 + suggestions.value.length) % suggestions.value.length
353
+ } else if (event.key === 'Enter' || (event.key === 'Tab' && text.value.trim())) {
354
+ if (open.value && active.value >= 0) {
355
+ event.preventDefault()
356
+ choose(suggestions.value[active.value])
357
+ } else if (commitText()) {
358
+ event.preventDefault()
359
+ }
360
+ } else if (event.key === 'Escape' && open.value) {
361
+ // Claim the key so an enclosing dialog stays open.
362
+ event.preventDefault()
363
+ suggestions.value = []
364
+ } else if (event.key === 'Backspace' && !text.value && props.modelValue.length) {
365
+ remove(props.modelValue.length - 1)
366
+ }
367
+ }
368
+
369
+ function onPaste(event) {
370
+ const pasted = event.clipboardData?.getData('text') ?? ''
371
+ const parts = splitList(pasted)
372
+ if (parts.length > 1) {
373
+ event.preventDefault()
374
+ add(parts)
375
+ }
376
+ }
377
+
378
+ function onBlur(event) {
379
+ // A click inside the suggestion list is not leaving the field.
380
+ if (rootRef.value?.contains(event.relatedTarget)) return
381
+ focused.value = false
382
+ commitText()
383
+ suggestions.value = []
384
+ }
385
+
386
+ defineExpose({ focus: () => inputRef.value?.focus() })
387
+ </script>
@@ -0,0 +1,213 @@
1
+ <template>
2
+ <Teleport to="body">
3
+ <Transition
4
+ enter-active-class="transition-opacity duration-200 motion-reduce:transition-none"
5
+ enter-from-class="opacity-0"
6
+ leave-active-class="transition-opacity duration-150 motion-reduce:transition-none"
7
+ leave-to-class="opacity-0"
8
+ >
9
+ <div
10
+ v-if="show"
11
+ class="fixed inset-0 bg-gray-950/50 backdrop-blur-sm"
12
+ :class="z"
13
+ aria-hidden="true"
14
+ @click="!persistent && $emit('close')"
15
+ />
16
+ </Transition>
17
+ <Transition
18
+ enter-active-class="transition-transform duration-250 ease-out motion-reduce:transition-none"
19
+ :enter-from-class="offscreenClass"
20
+ leave-active-class="transition-transform duration-200 ease-in motion-reduce:transition-none"
21
+ :leave-to-class="offscreenClass"
22
+ >
23
+ <div
24
+ v-if="show"
25
+ ref="panelRef"
26
+ role="dialog"
27
+ aria-modal="true"
28
+ :aria-label="labelledby ? undefined : label || undefined"
29
+ :aria-labelledby="labelledby || undefined"
30
+ tabindex="-1"
31
+ class="fixed flex flex-col bg-white text-gray-900 shadow-modal focus:outline-none dark:bg-gray-925 dark:text-gray-100"
32
+ :class="[z, placementClass, panelClass]"
33
+ :style="safeAreaStyle"
34
+ >
35
+ <div
36
+ v-if="side === 'bottom' && handle"
37
+ class="flex shrink-0 justify-center pt-2 pb-1"
38
+ aria-hidden="true"
39
+ >
40
+ <span class="h-1 w-10 rounded-full bg-gray-300 dark:bg-gray-600" />
41
+ </div>
42
+ <slot :close="() => $emit('close')" />
43
+ </div>
44
+ </Transition>
45
+ </Teleport>
46
+ </template>
47
+
48
+ <script setup>
49
+ /**
50
+ * UiDrawer — a panel that slides in from an edge of the screen: a bottom
51
+ * sheet on phones, a side panel on larger screens.
52
+ *
53
+ * It is a modal dialog in every respect but its position: `role="dialog"`
54
+ * with `aria-modal`, named by `label` or `labelledby`; focus moves inside when
55
+ * it opens, Tab stays inside, Escape and the backdrop close it (unless
56
+ * `persistent`), and focus returns to whatever opened it. On Android the
57
+ * system back button closes it too. Stacked with modals, only the top one
58
+ * responds.
59
+ *
60
+ * The panel respects the device's safe areas: a bottom sheet keeps clear of
61
+ * the home indicator, a side panel of notches and rounded corners.
62
+ *
63
+ * Content and scrolling are the caller's: put a header in the slot and give
64
+ * the body `overflow-y-auto` and `min-h-0`.
65
+ *
66
+ * @example A compose sheet on mobile
67
+ * <UiDrawer :show="composing" side="bottom" size="full" labelledby="compose-title" @close="composing = false">
68
+ * <h2 id="compose-title" class="p-4 font-semibold">New message</h2>
69
+ * <div class="min-h-0 flex-1 overflow-y-auto p-4">…</div>
70
+ * </UiDrawer>
71
+ *
72
+ * @example A side panel
73
+ * <UiDrawer :show="open" side="right" size="md" label="Filters" @close="open = false">
74
+ * <div class="p-6">…</div>
75
+ * </UiDrawer>
76
+ *
77
+ * @emits {void} close — The drawer should close (backdrop, Escape, back button).
78
+ *
79
+ * @slot default — Drawer content. Scope: `{ close }`.
80
+ */
81
+ import { computed, onBeforeUnmount, ref, watch } from 'vue'
82
+ import { useBackButton } from '../composables/useBackButton.js'
83
+ import { useFocusTrap } from '../internal/focusTrap.js'
84
+
85
+ const props = defineProps({
86
+ /**
87
+ * Whether the drawer is open.
88
+ * @type {boolean}
89
+ * @default false
90
+ */
91
+ show: { type: Boolean, default: false },
92
+ /**
93
+ * The edge it slides in from.
94
+ * @type {string}
95
+ * @default 'bottom'
96
+ * @values 'bottom' | 'left' | 'right'
97
+ */
98
+ side: { type: String, default: 'bottom' },
99
+ /**
100
+ * How far it reaches: the height of a bottom sheet, the width of a side panel.
101
+ * @type {string}
102
+ * @default 'md'
103
+ * @values 'sm' | 'md' | 'lg' | 'full'
104
+ */
105
+ size: { type: String, default: 'md' },
106
+ /**
107
+ * Ignore the backdrop, Escape and the back button.
108
+ * @type {boolean}
109
+ * @default false
110
+ */
111
+ persistent: { type: Boolean, default: false },
112
+ /**
113
+ * Show a grab handle at the top of a bottom sheet.
114
+ * @type {boolean}
115
+ * @default true
116
+ */
117
+ handle: { type: Boolean, default: true },
118
+ /**
119
+ * Accessible name. Prefer `labelledby` when there is a visible heading.
120
+ * @type {string}
121
+ * @default ''
122
+ */
123
+ label: { type: String, default: '' },
124
+ /**
125
+ * Id of the element that names the drawer.
126
+ * @type {string}
127
+ * @default ''
128
+ */
129
+ labelledby: { type: String, default: '' },
130
+ /**
131
+ * Extra classes for the panel.
132
+ * @type {string}
133
+ * @default ''
134
+ */
135
+ panelClass: { type: String, default: '' },
136
+ /**
137
+ * Z-index class for the backdrop and panel.
138
+ * @type {string}
139
+ * @default 'z-50'
140
+ */
141
+ z: { type: String, default: 'z-50' },
142
+ })
143
+
144
+ const emit = defineEmits(['close'])
145
+
146
+ const panelRef = ref(null)
147
+ useFocusTrap(() => panelRef.value, {
148
+ active: () => props.show,
149
+ escape: () => !props.persistent,
150
+ onEscape: () => emit('close'),
151
+ })
152
+
153
+ // ── Back button (Capacitor) ──
154
+ const { register, unregister } = useBackButton()
155
+ let backId = null
156
+ watch(
157
+ () => props.show,
158
+ (open) => {
159
+ if (open && backId === null) {
160
+ backId = register(() => {
161
+ if (props.persistent) return false
162
+ emit('close')
163
+ return true
164
+ })
165
+ } else if (!open && backId !== null) {
166
+ unregister(backId)
167
+ backId = null
168
+ }
169
+ },
170
+ { immediate: true },
171
+ )
172
+ onBeforeUnmount(() => {
173
+ if (backId !== null) unregister(backId)
174
+ })
175
+
176
+ const SIZES = {
177
+ bottom: {
178
+ sm: 'max-h-[40dvh]',
179
+ md: 'max-h-[65dvh]',
180
+ lg: 'max-h-[85dvh]',
181
+ full: 'h-[calc(100dvh-env(safe-area-inset-top)-0.5rem)]',
182
+ },
183
+ side: { sm: 'w-72', md: 'w-96', lg: 'w-[32rem]', full: 'w-full' },
184
+ }
185
+
186
+ const placementClass = computed(() => {
187
+ if (props.side === 'left')
188
+ return `inset-y-0 left-0 max-w-full ${SIZES.side[props.size] ?? SIZES.side.md}`
189
+ if (props.side === 'right')
190
+ return `inset-y-0 right-0 max-w-full ${SIZES.side[props.size] ?? SIZES.side.md}`
191
+ return `inset-x-0 bottom-0 rounded-t-2xl ${SIZES.bottom[props.size] ?? SIZES.bottom.md}`
192
+ })
193
+
194
+ const offscreenClass = computed(
195
+ () =>
196
+ ({ left: '-translate-x-full', right: 'translate-x-full' })[props.side] ?? 'translate-y-full',
197
+ )
198
+
199
+ const safeAreaStyle = computed(() => {
200
+ if (props.side === 'bottom') return { paddingBottom: 'env(safe-area-inset-bottom)' }
201
+ if (props.side === 'left')
202
+ return {
203
+ paddingLeft: 'env(safe-area-inset-left)',
204
+ paddingTop: 'env(safe-area-inset-top)',
205
+ paddingBottom: 'env(safe-area-inset-bottom)',
206
+ }
207
+ return {
208
+ paddingRight: 'env(safe-area-inset-right)',
209
+ paddingTop: 'env(safe-area-inset-top)',
210
+ paddingBottom: 'env(safe-area-inset-bottom)',
211
+ }
212
+ })
213
+ </script>
@@ -155,7 +155,10 @@ const close = () => {
155
155
  onClickOutside(containerRef, close)
156
156
 
157
157
  useEventListener(globalThis.document, 'keydown', (e) => {
158
- if (e.key === 'Escape') close()
158
+ if (e.key !== 'Escape' || !isOpen.value) return
159
+ // Claim the key so an enclosing UiModal does not close too.
160
+ e.preventDefault()
161
+ close()
159
162
  })
160
163
 
161
164
  defineExpose({ isOpen, toggle, close })
@@ -27,7 +27,13 @@
27
27
  >
28
28
  <div
29
29
  v-if="show"
30
- class="modal-panel bg-white dark:bg-gray-925 overflow-hidden flex flex-col"
30
+ ref="panelRef"
31
+ role="dialog"
32
+ aria-modal="true"
33
+ :aria-label="labelledby ? undefined : label || undefined"
34
+ :aria-labelledby="labelledby || undefined"
35
+ tabindex="-1"
36
+ class="modal-panel bg-white dark:bg-gray-925 overflow-hidden flex flex-col focus:outline-none"
31
37
  :class="[panelClass, maxWidthClass, roundedClass, mobileLayoutClass]"
32
38
  @click.stop
33
39
  >
@@ -47,6 +53,13 @@
47
53
  * (xs through 5xl and full-screen), persistent mode to prevent backdrop-click dismissal,
48
54
  * and customizable z-index and padding. The panel uses a scale+fade transition on enter/leave.
49
55
  *
56
+ * The panel is a real dialog: `role="dialog"` and `aria-modal="true"`, named by
57
+ * `label` or `labelledby`. While open, focus moves into the panel (unless
58
+ * something inside already has it), Tab and Shift+Tab wrap within it, Escape
59
+ * closes it (unless `persistent` or `close-on-escape` is false), and focus
60
+ * returns to whatever opened it. Stacked modals behave: only the top one
61
+ * responds to Tab and Escape.
62
+ *
50
63
  * @example Basic modal
51
64
  * <UiModal :show="isOpen" @close="isOpen = false">
52
65
  * <div class="p-6">Hello!</div>
@@ -68,12 +81,19 @@
68
81
  * <div class="p-6">Centered on all screens</div>
69
82
  * </UiModal>
70
83
  *
71
- * @emits {void} close — Emitted when the modal should close (backdrop click or programmatic)
84
+ * @example Named by its heading
85
+ * <UiModal :show="isOpen" labelledby="settings-title" @close="isOpen = false">
86
+ * <h2 id="settings-title">Settings</h2>
87
+ * </UiModal>
88
+ *
89
+ * @emits {void} close — Emitted when the modal should close (backdrop click, Escape,
90
+ * the Android back button, or programmatic)
72
91
  *
73
92
  * @slot default — Modal content
74
93
  */
75
- import { computed, watch, onBeforeUnmount } from 'vue'
94
+ import { computed, ref, watch, onBeforeUnmount } from 'vue'
76
95
  import { useBackButton } from '../composables/useBackButton.js'
96
+ import { useFocusTrap } from '../internal/focusTrap.js'
77
97
  const props = defineProps({
78
98
  /**
79
99
  * Controls modal visibility. Use with v-model or :show + @close.
@@ -125,10 +145,38 @@ const props = defineProps({
125
145
  * @default 'md:p-4'
126
146
  */
127
147
  padding: { type: String, default: 'md:p-4' },
148
+ /**
149
+ * Accessible name for the dialog. Prefer `labelledby` when the content has a
150
+ * visible heading.
151
+ * @type {string}
152
+ * @default ''
153
+ */
154
+ label: { type: String, default: '' },
155
+ /**
156
+ * Id of the element that names the dialog, usually its heading.
157
+ * @type {string}
158
+ * @default ''
159
+ */
160
+ labelledby: { type: String, default: '' },
161
+ /**
162
+ * Whether Escape closes the modal. Ignored (treated as false) when
163
+ * `persistent` is set.
164
+ * @type {boolean}
165
+ * @default true
166
+ */
167
+ closeOnEscape: { type: Boolean, default: true },
128
168
  })
129
169
 
130
170
  const emit = defineEmits(['close'])
131
171
 
172
+ // ── Focus: move in, keep in, put back ──
173
+ const panelRef = ref(null)
174
+ useFocusTrap(() => panelRef.value, {
175
+ active: () => props.show,
176
+ escape: () => props.closeOnEscape && !props.persistent,
177
+ onEscape: () => emit('close'),
178
+ })
179
+
132
180
  // ── Back-button interception (Capacitor) ──
133
181
  const { register: registerBack, unregister: unregisterBack } = useBackButton()
134
182
  let backHandlerId = null