@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 })
@@ -0,0 +1,162 @@
1
+ <script server>
2
+ export const open = $bool($props.open)
3
+ export const className = $props.className || ''
4
+
5
+ /*
6
+ * The dialog's accessible name.
7
+ *
8
+ * `role="dialog"` with no name is announced as just "dialog", which tells a
9
+ * screen reader user nothing about what has taken over the page -- WCAG 4.1.2.
10
+ * The slot idiom puts the name in a <DialogTitle>, which the client script
11
+ * below associates on mount; this prop is the server-rendered half, so a
12
+ * dialog is named before hydration and without JavaScript at all. Emitted only
13
+ * when set: an empty aria-label is worse than none, because it suppresses the
14
+ * name the title would otherwise supply. Same idiom as Drawer.
15
+ */
16
+ export const title = $props.title || ''
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
+
23
+ const baseClasses = 'relative z-50'
24
+ export const dialogClasses = `${baseClasses} ${className}`.trim()
25
+
26
+ /*
27
+ * This dialog's own id.
28
+ *
29
+ * Every lookup below is scoped to it, so two dialogs on one page - a list and
30
+ * the confirmation for deleting from it - do not take each other's panel or
31
+ * each other's Escape. Same idiom as Drawer, Dropdown and Popover.
32
+ */
33
+ export const dialogInstanceId = $uid
34
+ </script>
35
+
36
+ <script client>
37
+ const emit = defineEmits()
38
+ const dialogInstanceId = {{ dialogInstanceId }}
39
+ // useReactiveProp wires `:open="parentSignal()"` from the parent into our
40
+ // local `isOpen` signal — when the parent's signal changes, bindAttr
41
+ // updates the root's `open` attribute, our MutationObserver picks it up,
42
+ // and isOpen flips. See stacksjs/stx#1704.
43
+ const isOpen = useReactiveProp('open', {{ open }})
44
+ const rootRef = useRef('rootRef')
45
+
46
+ /*
47
+ * Whether hydration has run.
48
+ *
49
+ * Everything below reaches into the DOM through rootRef, and an effect's first
50
+ * run happens while this setup function is still executing - before x-ref is
51
+ * wired, when the ref can only be null. A dialog rendered open on first paint
52
+ * would otherwise get no scroll lock and no focus. See #1979 for the same
53
+ * mistake in Tabs.
54
+ */
55
+ const mounted = state(false)
56
+
57
+ onMount(() => {
58
+ mounted.set(true)
59
+ labelFromTitle()
60
+ })
61
+
62
+ /*
63
+ * Name the dialog after its <DialogTitle>, when the server could not.
64
+ *
65
+ * The documented idiom puts the name in the slot, which the server cannot see
66
+ * from here -- it renders the title as opaque slot content. So a dialog
67
+ * without the `title` prop reaches the browser announced as bare "dialog".
68
+ * This associates the two once the slot content exists. Skipped when a name is
69
+ * already present, so the prop always wins.
70
+ */
71
+ function labelFromTitle() {
72
+ const root = rootRef.value
73
+ if (!root) return
74
+ if (root.getAttribute('aria-label') || root.getAttribute('aria-labelledby')) return
75
+ const heading = root.querySelector('[data-stx-dialog-title]')
76
+ if (!heading) return
77
+ if (!heading.id) heading.id = `${dialogInstanceId}-title`
78
+ root.setAttribute('aria-labelledby', heading.id)
79
+ }
80
+
81
+ const closeOnEscape = {{ closeOnEscape }}
82
+ const closeOnBackdrop = {{ closeOnBackdrop }}
83
+
84
+ function close() {
85
+ isOpen.set(false)
86
+ emit('close')
87
+ }
88
+
89
+ /** The panel, or the dialog root if this dialog has no DialogPanel in it. */
90
+ function panelEl() {
91
+ const root = rootRef.value
92
+ if (!root) return null
93
+ return root.querySelector('[data-stx-dialog-panel]') || root
94
+ }
95
+
96
+ /*
97
+ * A click on the backdrop closes it.
98
+ *
99
+ * The backdrop is a child component, so this listens on the dialog root and
100
+ * asks whether the click landed on the marker DialogBackdrop renders. Checking
101
+ * the target rather than comparing against the panel means a click inside the
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.
105
+ */
106
+ function onRootClick(event) {
107
+ if (!closeOnBackdrop || !window.stx.overlays.isTop(dialogInstanceId)) return
108
+ const target = event.target
109
+ if (target && target.closest && target.closest('[data-stx-dialog-backdrop]')) close()
110
+ }
111
+
112
+ /*
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.
115
+ *
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).
122
+ */
123
+ effect(() => {
124
+ if (!mounted()) return
125
+
126
+ if (isOpen()) {
127
+ window.stx.overlays.open(dialogInstanceId, {
128
+ panel: panelEl,
129
+ root: rootRef.value,
130
+ onEscape: closeOnEscape ? close : null,
131
+ })
132
+ }
133
+ else {
134
+ window.stx.overlays.close(dialogInstanceId)
135
+ }
136
+ })
137
+
138
+ onDestroy(() => {
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)
142
+ })
143
+
144
+ defineExpose({
145
+ open: () => isOpen.set(true),
146
+ close,
147
+ isOpen,
148
+ })
149
+ </script>
150
+
151
+ <div
152
+ x-ref="rootRef"
153
+ :show="isOpen()"
154
+ class="{{ dialogClasses }}"
155
+ data-stx-dialog="{{ dialogInstanceId }}"
156
+ role="dialog"
157
+ aria-modal="true"
158
+ @if(title)aria-label="{{ title }}"@endif
159
+ @click="onRootClick($event)"
160
+ >
161
+ <slot />
162
+ </div>
@@ -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
@@ -72,8 +72,8 @@ function onClose(event) { event.stopPropagation(); close() }
72
72
  * Focus goes back to the trigger, but only if it had moved into the panel:
73
73
  * otherwise this would steal it from wherever the user actually is.
74
74
  */
75
- function onDocumentKey(event) {
76
- if (event.key !== 'Escape' || !isOpen()) return
75
+ function onEscape() {
76
+ if (!isOpen()) return
77
77
  close()
78
78
  if (!rootEl) return
79
79
  const active = rootEl.ownerDocument && rootEl.ownerDocument.activeElement
@@ -166,13 +166,25 @@ onMount(() => {
166
166
  if (rootEl && !rootEl.contains(event.target)) close()
167
167
  }
168
168
  document.addEventListener('click', outsideHandler, true)
169
- document.addEventListener('keydown', onDocumentKey)
170
169
  document.addEventListener('keydown', onMenuKey)
171
170
  })
172
171
 
172
+ /*
173
+ * Open means a layer on the page's overlay stack (window.stx.overlays), as a
174
+ * popup: while it is on top it takes Escape, so inside a dialog one Escape
175
+ * closes this and leaves the dialog open. It used to listen for Escape on the
176
+ * document itself, and whichever of its listener and the dialog's had been
177
+ * added first won (stacksjs/stx#2057). Not modal: it does not lock the page,
178
+ * trap Tab or move focus.
179
+ */
180
+ effect(() => {
181
+ if (isOpen()) window.stx.overlays.open(dropdownInstanceId, { modal: false, panel: () => rootEl, onEscape })
182
+ else window.stx.overlays.close(dropdownInstanceId)
183
+ })
184
+
173
185
  onDestroy(() => {
186
+ window.stx.overlays.close(dropdownInstanceId)
174
187
  if (outsideHandler) document.removeEventListener('click', outsideHandler, true)
175
- document.removeEventListener('keydown', onDocumentKey)
176
188
  document.removeEventListener('keydown', onMenuKey)
177
189
  })
178
190
 
@@ -107,8 +107,8 @@ function onSelect(event) {
107
107
  * Focus goes back to the trigger, but only if it had moved into the panel:
108
108
  * otherwise this would steal it from wherever the user actually is.
109
109
  */
110
- function onDocumentKey(event) {
111
- if (event.key !== 'Escape' || !isOpen()) return
110
+ function onEscape() {
111
+ if (!isOpen()) return
112
112
  close()
113
113
  if (!rootEl) return
114
114
  const active = rootEl.ownerDocument && rootEl.ownerDocument.activeElement
@@ -239,13 +239,25 @@ onMount(() => {
239
239
  if (rootEl && !rootEl.contains(event.target)) close()
240
240
  }
241
241
  document.addEventListener('click', outsideHandler, true)
242
- document.addEventListener('keydown', onDocumentKey)
243
242
  document.addEventListener('keydown', onListboxKey)
244
243
  })
245
244
 
245
+ /*
246
+ * Open means a layer on the page's overlay stack (window.stx.overlays), as a
247
+ * popup: while it is on top it takes Escape, so inside a dialog one Escape
248
+ * closes this and leaves the dialog open. It used to listen for Escape on the
249
+ * document itself, and whichever of its listener and the dialog's had been
250
+ * added first won (stacksjs/stx#2057). Not modal: it does not lock the page,
251
+ * trap Tab or move focus.
252
+ */
253
+ effect(() => {
254
+ if (isOpen()) window.stx.overlays.open(listboxInstanceId, { modal: false, panel: () => rootEl, onEscape })
255
+ else window.stx.overlays.close(listboxInstanceId)
256
+ })
257
+
246
258
  onDestroy(() => {
259
+ window.stx.overlays.close(listboxInstanceId)
247
260
  if (outsideHandler) document.removeEventListener('click', outsideHandler, true)
248
- document.removeEventListener('keydown', onDocumentKey)
249
261
  document.removeEventListener('keydown', onListboxKey)
250
262
  })
251
263
 
@@ -50,8 +50,8 @@ function onOpen(event) { event.stopPropagation(); open() }
50
50
  * Focus goes back to the trigger, but only if it had moved into the panel:
51
51
  * otherwise this would steal it from wherever the user actually is.
52
52
  */
53
- function onDocumentKey(event) {
54
- if (event.key !== 'Escape' || !isOpen()) return
53
+ function onEscape() {
54
+ if (!isOpen()) return
55
55
  close()
56
56
  if (!rootEl) return
57
57
  const active = rootEl.ownerDocument && rootEl.ownerDocument.activeElement
@@ -68,12 +68,24 @@ onMount(() => {
68
68
  if (rootEl && !rootEl.contains(event.target)) close()
69
69
  }
70
70
  document.addEventListener('click', outsideHandler, true)
71
- document.addEventListener('keydown', onDocumentKey)
71
+ })
72
+
73
+ /*
74
+ * Open means a layer on the page's overlay stack (window.stx.overlays), as a
75
+ * popup: while it is on top it takes Escape, so inside a dialog one Escape
76
+ * closes this and leaves the dialog open. It used to listen for Escape on the
77
+ * document itself, and whichever of its listener and the dialog's had been
78
+ * added first won (stacksjs/stx#2057). Not modal: it does not lock the page,
79
+ * trap Tab or move focus.
80
+ */
81
+ effect(() => {
82
+ if (isOpen()) window.stx.overlays.open(popoverInstanceId, { modal: false, panel: () => rootEl, onEscape })
83
+ else window.stx.overlays.close(popoverInstanceId)
72
84
  })
73
85
 
74
86
  onDestroy(() => {
87
+ window.stx.overlays.close(popoverInstanceId)
75
88
  if (outsideHandler) document.removeEventListener('click', outsideHandler, true)
76
- document.removeEventListener('keydown', onDocumentKey)
77
89
  })
78
90
 
79
91
  const dataState = derived(() => isOpen() ? 'open' : 'closed')