@stacksjs/components 0.2.465 → 0.2.466

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 (37) 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/{Listbox-32gzxace.stx → Listbox-83j2q4x6.stx} +16 -4
  9. package/dist/{Popover-9m42pym0.stx → Popover-khtvagcs.stx} +16 -4
  10. package/dist/Tab-46mxm7f8.stx +35 -0
  11. package/dist/TabList-76bpx5fp.stx +154 -0
  12. package/dist/{Tabs-3j2h0jjn.stx → Tabs-1swcxmfb.stx} +4 -1
  13. package/dist/{Tooltip-z13bn0pv.stx → Tooltip-rg1c2zrc.stx} +13 -3
  14. package/dist/index.d.ts +2 -0
  15. package/dist/index.js +7 -7
  16. package/dist/ui/tabs/index.d.ts +35 -3
  17. package/dist/utils/overlays.d.ts +39 -0
  18. package/package.json +3 -3
  19. package/src/index.ts +2 -0
  20. package/src/ui/chat/ChatLauncher.stx +10 -2
  21. package/src/ui/combobox/Combobox.stx +16 -4
  22. package/src/ui/command-palette/CommandPalette.stx +33 -8
  23. package/src/ui/date-range-picker/DateRangePicker.stx +13 -9
  24. package/src/ui/dialog/Dialog.stx +29 -169
  25. package/src/ui/dialog/README.md +5 -0
  26. package/src/ui/drawer/Drawer.stx +24 -128
  27. package/src/ui/drawer/README.md +4 -0
  28. package/src/ui/dropdown/Dropdown.stx +16 -4
  29. package/src/ui/listbox/Listbox.stx +16 -4
  30. package/src/ui/popover/Popover.stx +16 -4
  31. package/src/ui/tabs/Tab.stx +35 -0
  32. package/src/ui/tabs/TabList.stx +154 -0
  33. package/src/ui/tabs/Tabs.stx +4 -1
  34. package/src/ui/tabs/index.ts +37 -3
  35. package/src/ui/tooltip/Tooltip.stx +13 -3
  36. package/src/utils/overlays.ts +69 -0
  37. package/dist/Dialog-e9a4r6d0.stx +0 -302
@@ -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 |
@@ -2,6 +2,10 @@
2
2
  export const open = $bool($props.open)
3
3
  export const position = $props.position || 'right'
4
4
  export const title = $props.title || ''
5
+ /** Set `closeOnEscape="false"` for a drawer that must be answered, not dismissed. */
6
+ export const closeOnEscape = $bool($props.closeOnEscape, true)
7
+ /** The same, for a click on the backdrop. */
8
+ export const closeOnBackdrop = $bool($props.closeOnBackdrop, true)
5
9
  export const className = $props.className || ''
6
10
 
7
11
  /*
@@ -122,153 +126,45 @@ const drawerInstanceId = {{ drawerInstanceId }}
122
126
  // See stacksjs/stx#1704.
123
127
  const isOpen = useReactiveProp('open', {{ open }})
124
128
 
129
+ const closeOnEscape = {{ closeOnEscape }}
130
+ const closeOnBackdrop = {{ closeOnBackdrop }}
131
+
125
132
  function close() {
126
133
  isOpen.set(false)
127
134
  emit('close')
128
135
  }
129
136
 
130
137
  function onBackdropClick() {
131
- close()
132
- }
133
-
134
- function onPanelKey(event) {
135
- if (event.key === 'Escape') close()
136
- }
137
-
138
- /*
139
- * Escape closes it from anywhere, not just from inside the panel.
140
- *
141
- * The keydown handler on the panel only fires when focus is already in it, and
142
- * opening a drawer does not move focus - so the very first Escape, pressed
143
- * while focus is still on whatever opened it, did nothing. Bound on the
144
- * document instead, and only while open.
145
- */
146
- function onDocumentKey(event) {
147
- if (event.key === 'Escape' && isOpen()) close()
148
- }
149
-
150
- /*
151
- * Hold the page still behind the drawer.
152
- *
153
- * Without this the page scrolls under an open drawer, which on a phone means a
154
- * drag inside the cart scrolls the restaurant behind it and the cart stays
155
- * put. The scrollbar's width is replaced as padding so the page does not jump
156
- * sideways when it disappears.
157
- */
158
- let restoreScroll = null
159
-
160
- function lockScroll() {
161
- if (restoreScroll) return
162
-
163
- const gap = window.innerWidth - document.documentElement.clientWidth
164
- const previous = { overflow: document.body.style.overflow, paddingRight: document.body.style.paddingRight }
165
-
166
- document.body.style.overflow = 'hidden'
167
- if (gap > 0) document.body.style.paddingRight = `${gap}px`
168
-
169
- restoreScroll = () => {
170
- document.body.style.overflow = previous.overflow
171
- document.body.style.paddingRight = previous.paddingRight
172
- restoreScroll = null
173
- }
138
+ if (closeOnBackdrop && window.stx.overlays.isTop(drawerInstanceId)) close()
174
139
  }
175
140
 
176
- function unlockScroll() {
177
- if (restoreScroll) restoreScroll()
141
+ function panelEl() {
142
+ return document.querySelector(`[data-stx-drawer-panel="${drawerInstanceId}"]`)
178
143
  }
179
144
 
180
145
  /*
181
- * Focus moves in on open and back on close.
182
- *
183
- * A drawer that leaves focus behind is unreachable by keyboard: Tab walks the
184
- * page underneath it, and a screen reader never learns anything opened. Focus
185
- * returns to whatever opened it on the way out, so closing does not dump the
186
- * user at the top of the document.
146
+ * Open means on the page's overlay stack (window.stx.overlays), shared with
147
+ * Dialog, stxConfirm and the app's own overlays: the page held still from the
148
+ * first overlay to the last, focus moved in and back out, Escape for the
149
+ * topmost only, and Tab trapped in the panel - which this drawer did not do at
150
+ * all before, so Tab walked out of an aria-modal panel into the page behind
151
+ * it. It also bound its own Escape, so one Escape closed a drawer and the
152
+ * dialog over it together (stacksjs/stx#2057).
187
153
  */
188
- let opener = null
189
-
190
- /*
191
- * Retried across frames, because focus is refused before the panel is shown.
192
- *
193
- * `:show` toggles the drawer's display, and that has not happened yet on the
194
- * frame the effect runs: `focus()` on a `display: none` element is a silent
195
- * no-op, so a single attempt left focus on the body every time. Nothing
196
- * throws, and the drawer looks right - it is only unreachable by keyboard.
197
- *
198
- * So the attempt repeats until `document.activeElement` proves it landed,
199
- * rather than assuming any one frame is late enough. Bounded, so a panel with
200
- * nothing focusable in it cannot spin; the panel itself carries `tabindex=-1`
201
- * and takes focus in that case. It also stops if the drawer closed in the
202
- * meantime, so a drawer opened and shut inside those few frames does not
203
- * pull focus back afterwards.
204
- */
205
- /*
206
- * The next chance to try, whichever comes first.
207
- *
208
- * `requestAnimationFrame` is the right signal when the page is painting, and
209
- * it is suspended entirely when the page is not - a background tab, or a
210
- * window the compositor has parked. A drawer opened in that state would never
211
- * take focus, and would still not have it when the reader came back, because
212
- * the one scheduled callback fires long after everything else has moved on.
213
- * So a timer races the frame and the first to arrive wins.
214
- */
215
- function soon(callback) {
216
- let done = false
217
-
218
- const once = () => {
219
- if (done) return
220
- done = true
221
- callback()
222
- }
223
-
224
- requestAnimationFrame(once)
225
- setTimeout(once, 32)
226
- }
227
-
228
- function focusFirst(attempts) {
229
- if (!isOpen()) return
230
-
231
- const panel = document.querySelector(`[data-stx-drawer-panel="${drawerInstanceId}"]`)
232
- if (!panel) return
233
-
234
- const target = panel.querySelector('[autofocus]') || panel.querySelector('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])') || panel
235
-
236
- target.focus?.()
237
-
238
- if (document.activeElement === target) return
239
- if (attempts > 0) soon(() => focusFirst(attempts - 1))
240
- }
241
-
242
- function captureFocus() {
243
- opener = document.activeElement
244
-
245
- soon(() => focusFirst(10))
246
- }
247
-
248
- function releaseFocus() {
249
- const target = opener
250
- opener = null
251
- if (target && typeof target.focus === 'function') target.focus()
252
- }
253
-
254
154
  effect(() => {
255
155
  if (isOpen()) {
256
- lockScroll()
257
- captureFocus()
258
- document.addEventListener('keydown', onDocumentKey)
156
+ window.stx.overlays.open(drawerInstanceId, {
157
+ panel: panelEl,
158
+ onEscape: closeOnEscape ? close : null,
159
+ })
259
160
  }
260
161
  else {
261
- unlockScroll()
262
- releaseFocus()
263
- document.removeEventListener('keydown', onDocumentKey)
162
+ window.stx.overlays.close(drawerInstanceId)
264
163
  }
265
164
  })
266
165
 
267
166
  onDestroy(() => {
268
- // A drawer removed while open would otherwise leave the page locked, with no
269
- // drawer left to unlock it.
270
- unlockScroll()
271
- document.removeEventListener('keydown', onDocumentKey)
167
+ window.stx.overlays.close(drawerInstanceId)
272
168
  })
273
169
 
274
170
  defineExpose({
@@ -286,7 +182,7 @@ defineExpose({
286
182
  @click="onBackdropClick()"
287
183
  ></div>
288
184
 
289
- <div class="fixed inset-0 z-50 overflow-hidden" @keydown="onPanelKey($event)">
185
+ <div class="fixed inset-0 z-50 overflow-hidden">
290
186
  <div class="absolute inset-0 overflow-hidden">
291
187
  <div class="{{ containerClasses }}">
292
188
  <div
@@ -43,6 +43,10 @@ let drawerOpen = false
43
43
  | `position` | `'right' \| 'left' \| 'top' \| 'bottom'` | `'right'` | Position of the drawer |
44
44
  | `title` | `string` | `''` | Optional title for the drawer |
45
45
  | `className` | `string` | `''` | Additional CSS classes |
46
+ | `closeOnEscape` | `boolean` | `true` | `false` for a drawer that must be answered rather than dismissed |
47
+ | `closeOnBackdrop` | `boolean` | `true` | `false` to keep it open on a backdrop click |
48
+
49
+ An open drawer is a modal layer on the page's overlay stack (`window.stx.overlays`): the page held still, focus moved in and back, Tab trapped in the panel, and Escape for whichever overlay is on top. See the Dialog README.
46
50
 
47
51
  ## Positions
48
52