@stacksjs/components 0.2.465 → 0.2.467

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 (66) hide show
  1. package/dist/{ChatLauncher-pc9nqh43.stx → ChatLauncher-3zph4trh.stx} +10 -2
  2. package/dist/{Combobox-9xh1jgjr.stx → Combobox-9n04ec5w.stx} +16 -4
  3. package/dist/{CommandPalette-vvaqtqat.stx → CommandPalette-qqjdbcyg.stx} +33 -8
  4. package/dist/{DateRangePicker-z64qqw84.stx → DateRangePicker-4zmf34e4.stx} +13 -9
  5. package/dist/Dialog-pdq8rxvy.stx +162 -0
  6. package/dist/{Drawer-hc0jjpsw.stx → Drawer-nmq100vy.stx} +24 -128
  7. package/dist/{Dropdown-25160tc6.stx → Dropdown-hb8ks69x.stx} +16 -4
  8. package/dist/{EmailInput-ffzwvp9r.stx → EmailInput-57626391.stx} +3 -0
  9. package/dist/{Listbox-32gzxace.stx → Listbox-83j2q4x6.stx} +16 -4
  10. package/dist/{NumberInput-r2wsh8w9.stx → NumberInput-pq4k2k4k.stx} +31 -10
  11. package/dist/{PasswordInput-36bws0kf.stx → PasswordInput-dg41ktgd.stx} +8 -1
  12. package/dist/{Popover-9m42pym0.stx → Popover-khtvagcs.stx} +16 -4
  13. package/dist/{Select-pv44ngbe.stx → Select-61zv5tr7.stx} +98 -17
  14. package/dist/Tab-46mxm7f8.stx +35 -0
  15. package/dist/TabList-76bpx5fp.stx +154 -0
  16. package/dist/Table-0a4y46vd.stx +43 -0
  17. package/dist/TableCell-jhvsxw8b.stx +17 -0
  18. package/dist/TableHeader-krzcx4sv.stx +62 -0
  19. package/dist/{Tabs-3j2h0jjn.stx → Tabs-1swcxmfb.stx} +4 -1
  20. package/dist/{TextInput-h9trj9f9.stx → TextInput-nknmz9md.stx} +57 -4
  21. package/dist/{Textarea-k8cjs4zs.stx → Textarea-9xmhjqp8.stx} +30 -7
  22. package/dist/{Tooltip-z13bn0pv.stx → Tooltip-rg1c2zrc.stx} +13 -3
  23. package/dist/index.d.ts +2 -0
  24. package/dist/index.js +7 -7
  25. package/dist/ui/input/index.d.ts +18 -5
  26. package/dist/ui/select/index.d.ts +1 -1
  27. package/dist/ui/table/index.d.ts +4 -0
  28. package/dist/ui/tabs/index.d.ts +35 -3
  29. package/dist/utils/overlays.d.ts +39 -0
  30. package/package.json +3 -3
  31. package/src/index.ts +2 -0
  32. package/src/ui/chat/ChatLauncher.stx +10 -2
  33. package/src/ui/combobox/Combobox.stx +16 -4
  34. package/src/ui/command-palette/CommandPalette.stx +33 -8
  35. package/src/ui/date-range-picker/DateRangePicker.stx +13 -9
  36. package/src/ui/dialog/Dialog.stx +29 -169
  37. package/src/ui/dialog/README.md +5 -0
  38. package/src/ui/drawer/Drawer.stx +24 -128
  39. package/src/ui/drawer/README.md +4 -0
  40. package/src/ui/dropdown/Dropdown.stx +16 -4
  41. package/src/ui/input/EmailInput.stx +3 -0
  42. package/src/ui/input/NumberInput.stx +31 -10
  43. package/src/ui/input/PasswordInput.stx +8 -1
  44. package/src/ui/input/TextInput.stx +57 -4
  45. package/src/ui/input/index.ts +31 -5
  46. package/src/ui/listbox/Listbox.stx +16 -4
  47. package/src/ui/popover/Popover.stx +16 -4
  48. package/src/ui/select/README.md +59 -21
  49. package/src/ui/select/Select.stx +98 -17
  50. package/src/ui/select/index.ts +8 -1
  51. package/src/ui/table/README.md +69 -1
  52. package/src/ui/table/Table.stx +31 -2
  53. package/src/ui/table/TableCell.stx +8 -1
  54. package/src/ui/table/TableHeader.stx +47 -5
  55. package/src/ui/table/index.ts +9 -0
  56. package/src/ui/tabs/Tab.stx +35 -0
  57. package/src/ui/tabs/TabList.stx +154 -0
  58. package/src/ui/tabs/Tabs.stx +4 -1
  59. package/src/ui/tabs/index.ts +37 -3
  60. package/src/ui/textarea/Textarea.stx +30 -7
  61. package/src/ui/tooltip/Tooltip.stx +13 -3
  62. package/src/utils/overlays.ts +69 -0
  63. package/dist/Dialog-e9a4r6d0.stx +0 -302
  64. package/dist/Table-3ek3w95h.stx +0 -14
  65. package/dist/TableCell-8swtcq8s.stx +0 -10
  66. package/dist/TableHeader-qsc3e208.stx +0 -20
@@ -0,0 +1,39 @@
1
+ /** Put an overlay on top. Calling it again for an open id only updates its options. */
2
+ export declare function openOverlay(id: string, options: OverlayOptions): void;
3
+ /** Take an overlay off the stack, wherever it is in it. */
4
+ export declare function closeOverlay(id: string): void;
5
+ /** Whether this overlay is the one that has the keyboard. */
6
+ export declare function isTopOverlay(id: string): boolean;
7
+ /** Open overlay ids, outermost first. */
8
+ export declare function openOverlays(): string[];
9
+ /**
10
+ * The page's overlay stack, for an overlay the app draws itself.
11
+ *
12
+ * Dialog, Drawer and stxConfirm/stxAlert all go on one stack in the stx
13
+ * runtime (`window.stx.overlays`): the topmost overlay answers Escape and owns
14
+ * Tab, the page stays locked from the first open to the last close, and focus
15
+ * moves into an overlay and back to whatever opened it. An app's own slide-over
16
+ * or full-screen panel joins the same stack through these, so it nests with the
17
+ * library's dialogs instead of competing with them (stacksjs/stx#2057).
18
+ *
19
+ * ```ts
20
+ * import { closeOverlay, openOverlay } from '@stacksjs/components'
21
+ *
22
+ * effect(() => {
23
+ * if (panelOpen()) openOverlay('workout-panel', { panel: '#wp-panel', onEscape: close })
24
+ * else closeOverlay('workout-panel')
25
+ * })
26
+ * onDestroy(() => closeOverlay('workout-panel'))
27
+ * ```
28
+ *
29
+ * Every call is a no-op where there is no runtime (server rendering, tests
30
+ * without a DOM), so shared code can call them unguarded.
31
+ */
32
+ export declare interface OverlayOptions {
33
+ panel: Element | string | null | (() => Element | null)
34
+ root?: Element | null
35
+ onEscape?: (() => void) | null
36
+ initialFocus?: HTMLElement | null | (() => HTMLElement | null)
37
+ opener?: Element | null
38
+ restoreFocus?: boolean
39
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/components",
3
3
  "type": "module",
4
- "version": "0.2.465",
4
+ "version": "0.2.467",
5
5
  "description": "A modern component library built with STX, featuring headwind utility classes and TypeScript syntax highlighting.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",
@@ -69,8 +69,8 @@
69
69
  "dependencies": {
70
70
  "@iconify-json/f7": "^1.2.2",
71
71
  "@stacksjs/ts-css": "^0.3.2",
72
- "@stacksjs/stx": "0.2.465",
73
- "bun-plugin-stx": "0.2.465",
72
+ "@stacksjs/stx": "0.2.467",
73
+ "bun-plugin-stx": "0.2.467",
74
74
  "ts-syntax-highlighter": "^0.2.17"
75
75
  },
76
76
  "devDependencies": {
package/src/index.ts CHANGED
@@ -6,6 +6,8 @@ export { default as Hero } from './components/Hero.stx'
6
6
  export { default as Installation } from './components/Installation.stx'
7
7
  // Export composables
8
8
  export * from './composables'
9
+ // The page's overlay stack, for overlays an app draws itself (stacksjs/stx#2057).
10
+ export * from './utils/overlays'
9
11
  // Theme seam — see src/theme for how an app redirects the components' colours.
10
12
  export * from './theme'
11
13
  // Export UI components
@@ -19,7 +19,15 @@ function close() {
19
19
  open.set(false)
20
20
  nextTick(() => toggle()?.focus())
21
21
  }
22
- function keyboard(event) { if (event.key === 'Escape') close() }
22
+ // Open, the panel is a popup layer on the page's overlay stack, so Escape
23
+ // closes it while it is on top and nothing underneath (stacksjs/stx#2057).
24
+ const chatStoreName = {{ storeName }}
25
+ const chatLayerId = `stx-chat-${chatStoreName}`
26
+ effect(() => {
27
+ if (open()) window.stx.overlays.open(chatLayerId, { modal: false, panel: null, onEscape: close })
28
+ else window.stx.overlays.close(chatLayerId)
29
+ })
30
+ onDestroy(() => window.stx.overlays.close(chatLayerId))
23
31
  onMount(() => chat.load())
24
32
  onMount(() => chat.observe())
25
33
  </script>
@@ -30,7 +38,7 @@ onMount(() => chat.observe())
30
38
  <span :if="unread" class="absolute -top-1 -right-1 min-w-5 h-5 px-1 flex items-center justify-center text-[10px] font-semibold text-accent-ink bg-accent-solid border-2 border-panel rounded-full" :text="unread"></span>
31
39
  </StxLink>
32
40
  @else
33
- <section :if="open" role="dialog" aria-label="Messages" @keydown="keyboard($event)" class="fixed right-6 bottom-24 z-40 w-[420px] max-w-[calc(100vw-48px)] rounded-2xl border border-line bg-panel shadow-xl overflow-hidden">
41
+ <section :if="open" role="dialog" aria-label="Messages" class="fixed right-6 bottom-24 z-40 w-[420px] max-w-[calc(100vw-48px)] rounded-2xl border border-line bg-panel shadow-xl overflow-hidden">
34
42
  <header class="flex items-center justify-between px-4 py-3 border-b border-line">
35
43
  <StxLink to="{{ href }}" class="font-semibold text-sm text-fg-strong hover:text-accent">Messages <span aria-hidden="true" class="i-lucide-arrow-up-right inline-block"></span></StxLink>
36
44
  <button ref="chat-close" type="button" @click="close()" aria-label="Close chat" class="inline-flex items-center justify-center w-9 h-9 text-fg-muted rounded-full hover:bg-surface-hover focus-visible:ring-2 focus-visible:ring-accent"><span aria-hidden="true" class="i-lucide-x text-lg"></span></button>
@@ -211,8 +211,8 @@ function onQuery(event) {
211
211
  * Focus goes back to the trigger, but only if it had moved into the panel:
212
212
  * otherwise this would steal it from wherever the user actually is.
213
213
  */
214
- function onDocumentKey(event) {
215
- if (event.key !== 'Escape' || !isOpen()) return
214
+ function onEscape() {
215
+ if (!isOpen()) return
216
216
  close()
217
217
  if (!rootEl) return
218
218
  const active = rootEl.ownerDocument && rootEl.ownerDocument.activeElement
@@ -231,13 +231,25 @@ onMount(() => {
231
231
  if (rootEl && !rootEl.contains(event.target)) close()
232
232
  }
233
233
  document.addEventListener('click', outsideHandler, true)
234
- document.addEventListener('keydown', onDocumentKey)
235
234
  document.addEventListener('keydown', onComboboxKey)
236
235
  })
237
236
 
237
+ /*
238
+ * Open means a layer on the page's overlay stack (window.stx.overlays), as a
239
+ * popup: while it is on top it takes Escape, so inside a dialog one Escape
240
+ * closes this and leaves the dialog open. It used to listen for Escape on the
241
+ * document itself, and whichever of its listener and the dialog's had been
242
+ * added first won (stacksjs/stx#2057). Not modal: it does not lock the page,
243
+ * trap Tab or move focus.
244
+ */
245
+ effect(() => {
246
+ if (isOpen()) window.stx.overlays.open(comboboxInstanceId, { modal: false, panel: () => rootEl, onEscape })
247
+ else window.stx.overlays.close(comboboxInstanceId)
248
+ })
249
+
238
250
  onDestroy(() => {
251
+ window.stx.overlays.close(comboboxInstanceId)
239
252
  if (outsideHandler) document.removeEventListener('click', outsideHandler, true)
240
- document.removeEventListener('keydown', onDocumentKey)
241
253
  document.removeEventListener('keydown', onComboboxKey)
242
254
  })
243
255
 
@@ -13,7 +13,6 @@ const emit = defineEmits()
13
13
  const paletteInstanceId = {{ paletteInstanceId }}
14
14
  const isOpen = state({{ open }})
15
15
  const query = state({{ initialQuery }})
16
- let escHandler = null
17
16
 
18
17
  function close() {
19
18
  if (!isOpen()) return
@@ -37,15 +36,33 @@ function onItemSelect(event) {
37
36
  close()
38
37
  }
39
38
 
40
- onMount(() => {
41
- escHandler = (event) => {
42
- if (event.key === 'Escape' && isOpen()) close()
39
+ function panelEl() {
40
+ return document.querySelector(`[data-palette-id="${paletteInstanceId}"] [data-stx-command-palette-panel]`)
41
+ }
42
+
43
+ /*
44
+ * The palette takes over the page, so it is a modal layer on the page's
45
+ * overlay stack (window.stx.overlays): the page held still behind it, focus
46
+ * straight into the search field and back to whatever opened it, Tab kept
47
+ * inside, and Escape for whichever overlay is on top. It used to listen for
48
+ * Escape on the document and do none of the rest, so focus stayed behind it
49
+ * and a palette opened from a dialog closed the dialog too (stacksjs/stx#2057).
50
+ */
51
+ effect(() => {
52
+ if (isOpen()) {
53
+ window.stx.overlays.open(paletteInstanceId, {
54
+ panel: panelEl,
55
+ onEscape: close,
56
+ initialFocus: () => panelEl()?.querySelector('input'),
57
+ })
58
+ }
59
+ else {
60
+ window.stx.overlays.close(paletteInstanceId)
43
61
  }
44
- document.addEventListener('keydown', escHandler)
45
62
  })
46
63
 
47
64
  onDestroy(() => {
48
- if (escHandler) document.removeEventListener('keydown', escHandler)
65
+ window.stx.overlays.close(paletteInstanceId)
49
66
  })
50
67
 
51
68
  defineExpose({ isOpen, query, show, close })
@@ -58,9 +75,16 @@ defineExpose({ isOpen, query, show, close })
58
75
  data-palette-id="{{ paletteInstanceId }}"
59
76
  @stx:command-select="onItemSelect($event)"
60
77
  >
61
- <div class="fixed inset-0 bg-neutral-500/25 dark:bg-neutral-500/50 transition-opacity" @click="close()"></div>
78
+ <div class="fixed inset-0 bg-neutral-500/25 dark:bg-neutral-500/50 transition-opacity" aria-hidden="true" @click="close()"></div>
62
79
 
63
- <div class="mx-auto max-w-xl transform divide-y divide-line overflow-hidden rounded-xl bg-panel shadow-2xl ring-1 ring-line transition-all relative">
80
+ <div
81
+ data-stx-command-palette-panel
82
+ role="dialog"
83
+ aria-modal="true"
84
+ aria-label="Command palette"
85
+ tabindex="-1"
86
+ class="mx-auto max-w-xl transform divide-y divide-line overflow-hidden rounded-xl bg-panel shadow-2xl ring-1 ring-line transition-all relative"
87
+ >
64
88
  <div class="relative">
65
89
  <svg class="pointer-events-none absolute top-3.5 left-4 h-5 w-5 text-fg-soft" viewBox="0 0 20 20" fill="currentColor">
66
90
  <path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
@@ -69,6 +93,7 @@ defineExpose({ isOpen, query, show, close })
69
93
  type="text"
70
94
  class="h-12 w-full border-0 bg-transparent pl-11 pr-4 text-fg placeholder-fg-soft focus:ring-0 sm:text-sm"
71
95
  placeholder="Search..."
96
+ aria-label="Search commands"
72
97
  :value="query()"
73
98
  @input="onQueryInput($event)"
74
99
  />
@@ -57,7 +57,6 @@ const activePreset = state(null)
57
57
 
58
58
  let rootEl = null
59
59
  let outsideHandler = null
60
- let keyHandler = null
61
60
 
62
61
  function midnight(d) {
63
62
  return new Date(d.getFullYear(), d.getMonth(), d.getDate())
@@ -153,19 +152,24 @@ onMount(() => {
153
152
  if (!isOpen() || !rootEl) return
154
153
  if (!rootEl.contains(event.target)) close()
155
154
  }
156
- keyHandler = (event) => {
157
- if (event.key === 'Escape' && isOpen()) {
158
- event.preventDefault()
159
- close()
160
- }
161
- }
162
155
  document.addEventListener('click', outsideHandler)
163
- document.addEventListener('keydown', keyHandler)
156
+ })
157
+
158
+ /*
159
+ * Open means a popup layer on the page's overlay stack (window.stx.overlays):
160
+ * while it is on top it takes Escape, so a picker inside a dialog closes on
161
+ * Escape and leaves the dialog open. It listened on the document itself, and
162
+ * whether its listener or the dialog's ran first was down to which had been
163
+ * added first (stacksjs/stx#2057).
164
+ */
165
+ effect(() => {
166
+ if (isOpen()) window.stx.overlays.open(instanceId, { modal: false, panel: () => rootEl, onEscape: close })
167
+ else window.stx.overlays.close(instanceId)
164
168
  })
165
169
 
166
170
  onDestroy(() => {
171
+ window.stx.overlays.close(instanceId)
167
172
  if (outsideHandler) document.removeEventListener('click', outsideHandler)
168
- if (keyHandler) document.removeEventListener('keydown', keyHandler)
169
173
  })
170
174
 
171
175
  defineExpose({ isOpen, rangeStart, rangeEnd, activePreset, applyPreset, onCalendarChange, clear, label })
@@ -15,6 +15,11 @@ export const className = $props.className || ''
15
15
  */
16
16
  export const title = $props.title || ''
17
17
 
18
+ /** Set `closeOnEscape="false"` for a dialog that must be answered, not dismissed. */
19
+ export const closeOnEscape = $bool($props.closeOnEscape, true)
20
+ /** The same, for a click on the backdrop. */
21
+ export const closeOnBackdrop = $bool($props.closeOnBackdrop, true)
22
+
18
23
  const baseClasses = 'relative z-50'
19
24
  export const dialogClasses = `${baseClasses} ${className}`.trim()
20
25
 
@@ -73,6 +78,9 @@ function labelFromTitle() {
73
78
  root.setAttribute('aria-labelledby', heading.id)
74
79
  }
75
80
 
81
+ const closeOnEscape = {{ closeOnEscape }}
82
+ const closeOnBackdrop = {{ closeOnBackdrop }}
83
+
76
84
  function close() {
77
85
  isOpen.set(false)
78
86
  emit('close')
@@ -85,200 +93,52 @@ function panelEl() {
85
93
  return root.querySelector('[data-stx-dialog-panel]') || root
86
94
  }
87
95
 
88
- function focusable() {
89
- const panel = panelEl()
90
- if (!panel) return []
91
- return Array.from(panel.querySelectorAll('a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'))
92
- .filter(el => el.offsetParent !== null || el === document.activeElement)
93
- }
94
-
95
- /*
96
- * Escape closes it from anywhere, not just from inside the dialog.
97
- *
98
- * It used to be a @keydown on the root element, which only fires once focus is
99
- * already inside - and nothing moved focus there on open. So in the ordinary
100
- * case (click a trigger, dialog appears) Escape did nothing at all, and the
101
- * two omissions hid each other: no initial focus made the handler unreachable,
102
- * and the unreachable handler made the missing focus look harmless.
103
- * stacksjs/stx#1973, #1978
104
- */
105
- function onDocumentKey(event) {
106
- if (!isOpen()) return
107
-
108
- if (event.key === 'Escape') {
109
- close()
110
- return
111
- }
112
-
113
- if (event.key !== 'Tab') return
114
-
115
- /*
116
- * The focus trap.
117
- *
118
- * aria-modal="true" tells assistive technology that the rest of the page is
119
- * inert. Without containment that is not a missing feature but an active
120
- * misstatement to AT, so the attribute and this handler belong together: six
121
- * Tab presses used to walk focus out of a delete-confirmation dialog and
122
- * onto the page behind it.
123
- */
124
- const items = focusable()
125
- if (items.length === 0) {
126
- // Nothing to land on inside: keep focus on the panel rather than letting
127
- // Tab leave.
128
- event.preventDefault()
129
- panelEl()?.focus?.()
130
- return
131
- }
132
-
133
- const first = items[0]
134
- const last = items[items.length - 1]
135
- const active = document.activeElement
136
- const inside = panelEl()?.contains(active)
137
-
138
- if (!inside) {
139
- event.preventDefault()
140
- ;(event.shiftKey ? last : first).focus()
141
- return
142
- }
143
-
144
- if (event.shiftKey && active === first) {
145
- event.preventDefault()
146
- last.focus()
147
- }
148
- else if (!event.shiftKey && active === last) {
149
- event.preventDefault()
150
- first.focus()
151
- }
152
- }
153
-
154
96
  /*
155
97
  * A click on the backdrop closes it.
156
98
  *
157
99
  * The backdrop is a child component, so this listens on the dialog root and
158
100
  * asks whether the click landed on the marker DialogBackdrop renders. Checking
159
101
  * the target rather than comparing against the panel means a click inside the
160
- * panel is not a backdrop click even though the backdrop is behind it.
102
+ * panel is not a backdrop click even though the backdrop is behind it. Only the
103
+ * dialog on top closes: a click cannot reach one underneath, but a programmatic
104
+ * one could.
161
105
  */
162
106
  function onRootClick(event) {
107
+ if (!closeOnBackdrop || !window.stx.overlays.isTop(dialogInstanceId)) return
163
108
  const target = event.target
164
109
  if (target && target.closest && target.closest('[data-stx-dialog-backdrop]')) close()
165
110
  }
166
111
 
167
112
  /*
168
- * Hold the page still behind the dialog.
113
+ * Open means on the page's overlay stack (window.stx.overlays), which every
114
+ * overlay shares: Dialog, Drawer, stxConfirm and the app's own.
169
115
  *
170
- * Without this the page scrolls under the overlay, which on a phone means a
171
- * drag anywhere over the dialog scrolls what is behind it. The scrollbar's
172
- * width is replaced as padding so the page does not jump sideways when it
173
- * disappears. Same implementation as Drawer.
116
+ * It holds the page still from the first overlay to the last, moves focus in
117
+ * here and back to whatever opened it, traps Tab, and gives Escape to the
118
+ * topmost overlay only. This dialog used to do each of those for itself, so
119
+ * with one open over another a single Escape closed both, the two Tab traps
120
+ * fought, and the first to close unlocked the page under the other
121
+ * (stacksjs/stx#2057; the trap, lock and Escape themselves are #1973/#1978).
174
122
  */
175
- let restoreScroll = null
176
-
177
- function lockScroll() {
178
- if (restoreScroll) return
179
-
180
- const gap = window.innerWidth - document.documentElement.clientWidth
181
- const previous = { overflow: document.body.style.overflow, paddingRight: document.body.style.paddingRight }
182
-
183
- document.body.style.overflow = 'hidden'
184
- if (gap > 0) document.body.style.paddingRight = `${gap}px`
185
-
186
- restoreScroll = () => {
187
- document.body.style.overflow = previous.overflow
188
- document.body.style.paddingRight = previous.paddingRight
189
- restoreScroll = null
190
- }
191
- }
192
-
193
- function unlockScroll() {
194
- if (restoreScroll) restoreScroll()
195
- }
196
-
197
- /*
198
- * Focus moves in on open and back on close.
199
- *
200
- * A dialog that leaves focus behind is unreachable by keyboard: Tab walks the
201
- * page underneath it, and a screen reader never learns anything opened. Focus
202
- * returns to whatever opened it on the way out, so closing does not dump the
203
- * reader at the top of the document.
204
- */
205
- let opener = null
206
-
207
- /*
208
- * The next chance to try, whichever comes first.
209
- *
210
- * requestAnimationFrame is the right signal when the page is painting and is
211
- * suspended entirely when it is not - a background tab, a parked window - so a
212
- * timer races it and the first to arrive wins.
213
- */
214
- function soon(callback) {
215
- let done = false
216
-
217
- const once = () => {
218
- if (done) return
219
- done = true
220
- callback()
221
- }
222
-
223
- requestAnimationFrame(once)
224
- setTimeout(once, 32)
225
- }
226
-
227
- /*
228
- * Retried across frames, because focus is refused before the dialog is shown.
229
- *
230
- * `:show` toggles display, and that has not happened yet on the frame the
231
- * effect runs: focus() on a display:none element is a silent no-op, so a single
232
- * attempt left focus on the body every time. Bounded, so a dialog with nothing
233
- * focusable cannot spin - the panel carries tabindex=-1 and takes focus in that
234
- * case - and it stops if the dialog closed in the meantime.
235
- */
236
- function focusFirst(attempts) {
237
- if (!isOpen()) return
238
-
239
- const panel = panelEl()
240
- if (!panel) return
241
-
242
- const target = panel.querySelector('[autofocus]') || focusable()[0] || panel
243
-
244
- target.focus?.()
245
-
246
- if (document.activeElement === target) return
247
- if (attempts > 0) soon(() => focusFirst(attempts - 1))
248
- }
249
-
250
- function captureFocus() {
251
- opener = document.activeElement
252
-
253
- soon(() => focusFirst(10))
254
- }
255
-
256
- function releaseFocus() {
257
- const target = opener
258
- opener = null
259
- if (target && typeof target.focus === 'function') target.focus()
260
- }
261
-
262
123
  effect(() => {
263
124
  if (!mounted()) return
264
125
 
265
126
  if (isOpen()) {
266
- lockScroll()
267
- captureFocus()
268
- document.addEventListener('keydown', onDocumentKey)
127
+ window.stx.overlays.open(dialogInstanceId, {
128
+ panel: panelEl,
129
+ root: rootRef.value,
130
+ onEscape: closeOnEscape ? close : null,
131
+ })
269
132
  }
270
133
  else {
271
- unlockScroll()
272
- releaseFocus()
273
- document.removeEventListener('keydown', onDocumentKey)
134
+ window.stx.overlays.close(dialogInstanceId)
274
135
  }
275
136
  })
276
137
 
277
138
  onDestroy(() => {
278
- // A dialog removed while open would otherwise leave the page locked, with no
279
- // dialog left to unlock it.
280
- unlockScroll()
281
- document.removeEventListener('keydown', onDocumentKey)
139
+ // Removed while open: off the stack, so the page is not left locked with no
140
+ // dialog on it to unlock it.
141
+ window.stx.overlays.close(dialogInstanceId)
282
142
  })
283
143
 
284
144
  defineExpose({
@@ -49,6 +49,11 @@ function openModal() {
49
49
  | `onClose` | `(value: boolean) => void` | - | Close handler |
50
50
  | `className` | `string` | `''` | Additional CSS classes |
51
51
  | `as` | `string` | `'div'` | HTML element to render |
52
+ | `title` | `string` | `''` | Accessible name, when there is no `DialogTitle` |
53
+ | `closeOnEscape` | `boolean` | `true` | `false` for a dialog that must be answered rather than dismissed |
54
+ | `closeOnBackdrop` | `boolean` | `true` | `false` to keep it open on a backdrop click |
55
+
56
+ An open dialog goes on the page's overlay stack (`window.stx.overlays`), shared with `Drawer`, `CommandPalette`, `stxConfirm`/`stxAlert`, the library's menus and popovers, and the app's own overlays (`openOverlay` / `closeOverlay` from `@stacksjs/components`). The topmost layer answers Escape and owns Tab, so one Escape closes a menu inside a dialog, or the inner of two dialogs, and nothing else. The page stays locked from the first open to the last close, and focus returns to whatever opened each one.
52
57
 
53
58
  ### DialogBackdrop
54
59
  | Prop | Type | Default | Description |