@stacksjs/components 0.2.369 → 0.2.370

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 (42) hide show
  1. package/dist/Combobox-9xh1jgjr.stx +266 -0
  2. package/dist/{ComboboxButton-g76stp1e.stx → ComboboxButton-4r6a45s1.stx} +8 -1
  3. package/dist/{ComboboxInput-a5g0vn3e.stx → ComboboxInput-1gyfpnca.stx} +3 -0
  4. package/dist/{ComboboxOption-vnb67p8d.stx → ComboboxOption-wkw70js7.stx} +27 -1
  5. package/dist/{CommandPalette-y6t67rjj.stx → CommandPalette-vvaqtqat.stx} +2 -2
  6. package/dist/CommandPaletteItem-7290ce1z.stx +40 -0
  7. package/dist/Dropdown-25160tc6.stx +204 -0
  8. package/dist/{DropdownButton-6by2ybde.stx → DropdownButton-wxc25hsy.stx} +2 -0
  9. package/dist/{DropdownItem-wht1vcm5.stx → DropdownItem-af0vzc8a.stx} +15 -1
  10. package/dist/Listbox-32gzxace.stx +273 -0
  11. package/dist/{ListboxButton-3yc7tm8w.stx → ListboxButton-gan8e3nw.stx} +2 -0
  12. package/dist/{ListboxOption-9c9jpz1n.stx → ListboxOption-gpaqvk3s.stx} +14 -1
  13. package/dist/Popover-9m42pym0.stx +101 -0
  14. package/dist/{PopoverButton-htfypg00.stx → PopoverButton-3w6550hv.stx} +1 -0
  15. package/dist/{RadioGroup-neej7522.stx → RadioGroup-smqcqgds.stx} +14 -1
  16. package/dist/{RadioGroupOption-c6xd0at9.stx → RadioGroupOption-twcw1b2a.stx} +13 -1
  17. package/dist/{SubscriptionCheckout-75x9mfv2.stx → SubscriptionCheckout-n3btqrzk.stx} +1 -1
  18. package/dist/index.js +7 -7
  19. package/package.json +3 -3
  20. package/src/components/Footer.stx +2 -2
  21. package/src/ui/combobox/Combobox.stx +174 -4
  22. package/src/ui/combobox/ComboboxButton.stx +8 -1
  23. package/src/ui/combobox/ComboboxInput.stx +3 -0
  24. package/src/ui/combobox/ComboboxOption.stx +27 -1
  25. package/src/ui/command-palette/CommandPalette.stx +2 -2
  26. package/src/ui/command-palette/CommandPaletteItem.stx +14 -1
  27. package/src/ui/dropdown/Dropdown.stx +122 -0
  28. package/src/ui/dropdown/DropdownButton.stx +2 -0
  29. package/src/ui/dropdown/DropdownItem.stx +15 -1
  30. package/src/ui/listbox/Listbox.stx +175 -3
  31. package/src/ui/listbox/ListboxButton.stx +2 -0
  32. package/src/ui/listbox/ListboxOption.stx +14 -1
  33. package/src/ui/payment/SubscriptionCheckout.stx +1 -1
  34. package/src/ui/popover/Popover.stx +43 -2
  35. package/src/ui/popover/PopoverButton.stx +1 -0
  36. package/src/ui/radio-group/RadioGroup.stx +14 -1
  37. package/src/ui/radio-group/RadioGroupOption.stx +13 -1
  38. package/dist/Combobox-06x0nywb.stx +0 -96
  39. package/dist/CommandPaletteItem-6rp133ta.stx +0 -27
  40. package/dist/Dropdown-cmwge7vx.stx +0 -82
  41. package/dist/Listbox-e5jreb0a.stx +0 -101
  42. package/dist/Popover-3g8rgxt1.stx +0 -60
@@ -1,5 +1,6 @@
1
1
  <script server>
2
2
  export const value = $props.value
3
+ export const valueJson = JSON.stringify(value === undefined ? null : value)
3
4
  export const className = $props.className || ''
4
5
  export const as = $props.as || 'div'
5
6
 
@@ -11,11 +12,33 @@ export const comboboxInstanceId = $uid
11
12
  <script client>
12
13
  const emit = defineEmits()
13
14
  const comboboxInstanceId = {{ comboboxInstanceId }}
14
- const initialValue = {{ value === undefined ? 'null' : value }}
15
+ /*
16
+ * JSON built on the server and emitted raw, rather than a 'null' sentinel
17
+ * through an ordinary interpolation.
18
+ *
19
+ * A `value === undefined ? 'null' : value` ternary was meant to put the bare
20
+ * token null into the script. An interpolation QUOTES a string, so what it
21
+ * emitted was the four-character string "null" -- truthy, in the one place a
22
+ * value is meant to be absent. JSON.stringify produces the correct literal for
23
+ * every type, null included, and raw interpolation passes it through unquoted.
24
+ * Calendar and DateRangePicker already avoid this sentinel and say why
25
+ * (stacksjs/stx#1981).
26
+ */
27
+ const initialValue = {!! valueJson !!}
15
28
 
16
29
  const isOpen = state(false)
17
30
  const selected = state(initialValue)
18
31
  const query = state('')
32
+ /*
33
+ * The ACTIVE option, which is not the focused one.
34
+ *
35
+ * A combobox keeps focus in its input so the user can keep typing, so there is
36
+ * no focused option to move -- the active one is named by
37
+ * `aria-activedescendant` on the input instead. That is why <Listbox> and
38
+ * <Dropdown> move real focus with a roving tabindex and this does not, and why
39
+ * aria-activedescendant was absent until there was something to point it at.
40
+ */
41
+ const activeIndex = state(-1)
19
42
  let rootEl = null
20
43
  let outsideHandler = null
21
44
 
@@ -27,6 +50,7 @@ function selectValue(v) {
27
50
  selected.set(v)
28
51
  isOpen.set(false)
29
52
  syncOptionStates()
53
+ syncExpanded()
30
54
  emit('change', v)
31
55
  }
32
56
 
@@ -44,8 +68,124 @@ function syncOptionStates() {
44
68
  }
45
69
  }
46
70
 
47
- function open() { if (!isOpen()) { isOpen.set(true) } }
48
- function close() { if (isOpen()) isOpen.set(false) }
71
+ /*
72
+ * role="combobox" makes aria-expanded REQUIRED, and the state lived only in
73
+ * `data-state` on the root. Nothing announced whether the list was showing.
74
+ *
75
+ * aria-activedescendant is deliberately absent: it names the option a user has
76
+ * moved to, and this combobox has no option keyboard navigation to move with,
77
+ * so pointing at one would describe a focus that does not exist.
78
+ */
79
+ function syncExpanded() {
80
+ if (!rootEl) return
81
+ const input = rootEl.querySelector('[data-stx-combobox-input]')
82
+ if (!input) return
83
+ input.setAttribute('aria-expanded', isOpen() ? 'true' : 'false')
84
+ const list = rootEl.querySelector('[data-stx-combobox-options]')
85
+ if (!list) return
86
+ if (!list.id) list.id = 'stx-combobox-list-' + comboboxInstanceId
87
+ input.setAttribute('aria-controls', list.id)
88
+ }
89
+
90
+ function optionElements() {
91
+ if (!rootEl) return []
92
+ const all = rootEl.querySelectorAll('[data-stx-combobox-option]')
93
+ return Array.prototype.filter.call(all, option => option.getAttribute('aria-disabled') !== 'true')
94
+ }
95
+
96
+ /*
97
+ * aria-activedescendant needs an id to point at, so the options are given one
98
+ * here rather than in the markup: the component does not know its own index
99
+ * and an id has to be unique on the page, not just within the list.
100
+ */
101
+ function syncActiveOption() {
102
+ if (!rootEl) return
103
+ const options = optionElements()
104
+ const index = activeIndex()
105
+ const doc = rootEl.ownerDocument
106
+ for (let i = 0; i < options.length; i++) {
107
+ /*
108
+ * An id is only usable here if it is UNIQUE, and an existing one may not
109
+ * be: event binding stamps every bound element `__stx_evt_0`, restarting
110
+ * per component render, so four options arrive sharing one id and
111
+ * getElementById returns an arbitrary one of them. An author's own id is
112
+ * kept when it really is unique; otherwise this assigns its own.
113
+ */
114
+ const existing = options[i].id
115
+ const unique = existing && doc.querySelectorAll('[id="' + existing + '"]').length === 1
116
+ if (!unique) options[i].id = 'stx-combobox-option-' + comboboxInstanceId + '-' + i
117
+ if (i === index) options[i].setAttribute('data-active', 'true')
118
+ else options[i].removeAttribute('data-active')
119
+ }
120
+ const input = rootEl.querySelector('[data-stx-combobox-input]')
121
+ if (!input) return
122
+ const active = index >= 0 ? options[index] : null
123
+ if (active) input.setAttribute('aria-activedescendant', active.id)
124
+ else input.removeAttribute('aria-activedescendant')
125
+ }
126
+
127
+ function moveActive(delta) {
128
+ const options = optionElements()
129
+ if (!options.length) return
130
+ const from = activeIndex()
131
+ const next = from < 0
132
+ ? (delta > 0 ? 0 : options.length - 1)
133
+ : (from + delta + options.length) % options.length
134
+ activeIndex.set(next)
135
+ syncActiveOption()
136
+ const landed = optionElements()[next]
137
+ // The list scrolls at max-h-60, so an option stepped past the fold has to be
138
+ // brought in: there is no focus move to do it for us.
139
+ if (landed && landed.scrollIntoView) landed.scrollIntoView({ block: 'nearest' })
140
+ }
141
+
142
+ /*
143
+ * Gated on the event coming from inside this combobox, like the Escape
144
+ * handler, so an arrow key elsewhere on the page still scrolls it.
145
+ *
146
+ * Space is deliberately absent. On <Listbox> and <Dropdown> it activates the
147
+ * focused item; here focus is in a text input and Space has to type a space.
148
+ */
149
+ function onComboboxKey(event) {
150
+ if (!rootEl || !rootEl.contains(event.target)) return
151
+ const key = event.key
152
+
153
+ if (!isOpen()) {
154
+ if (key !== 'ArrowDown' && key !== 'ArrowUp') return
155
+ event.preventDefault()
156
+ open()
157
+ activeIndex.set(-1)
158
+ moveActive(key === 'ArrowDown' ? 1 : -1)
159
+ return
160
+ }
161
+
162
+ if (key === 'ArrowDown' || key === 'ArrowUp') {
163
+ event.preventDefault()
164
+ moveActive(key === 'ArrowDown' ? 1 : -1)
165
+ return
166
+ }
167
+
168
+ if (key === 'Home' || key === 'End') {
169
+ event.preventDefault()
170
+ activeIndex.set(-1)
171
+ moveActive(key === 'Home' ? 1 : -1)
172
+ return
173
+ }
174
+
175
+ if (key === 'Enter') {
176
+ const options = optionElements()
177
+ const index = activeIndex()
178
+ if (index < 0 || !options[index]) return
179
+ event.preventDefault()
180
+ options[index].click()
181
+ return
182
+ }
183
+
184
+ if (key === 'Tab') close()
185
+ }
186
+
187
+ function open() { if (!isOpen()) { isOpen.set(true); syncExpanded() } }
188
+ function close() { if (isOpen()) { isOpen.set(false); activeIndex.set(-1); syncActiveOption(); syncExpanded() } }
49
189
  function toggle() { isOpen() ? close() : open() }
50
190
 
51
191
  function onToggle(event) { event.stopPropagation(); toggle() }
@@ -53,27 +193,57 @@ function onSelect(event) { event.stopPropagation(); selectValue(event.detail) }
53
193
  function onQuery(event) {
54
194
  event.stopPropagation()
55
195
  query.set(event.detail)
196
+ // The list the indexes referred to is about to change under them.
197
+ activeIndex.set(-1)
198
+ syncActiveOption()
56
199
  if (!isOpen()) open()
57
200
  emit('query', event.detail)
58
201
  }
59
202
 
203
+ /*
204
+ * Escape dismisses it, from anywhere rather than only from inside the panel.
205
+ *
206
+ * Opening one of these does not move focus, so a keydown bound on the panel
207
+ * never sees the first Escape - focus is still on whatever opened it. Drawer
208
+ * reached the same conclusion and binds on the document; this follows it, and
209
+ * only acts while open so one open popup does not swallow Escape for the page.
210
+ *
211
+ * Focus goes back to the trigger, but only if it had moved into the panel:
212
+ * otherwise this would steal it from wherever the user actually is.
213
+ */
214
+ function onDocumentKey(event) {
215
+ if (event.key !== 'Escape' || !isOpen()) return
216
+ close()
217
+ if (!rootEl) return
218
+ const active = rootEl.ownerDocument && rootEl.ownerDocument.activeElement
219
+ if (!active || !rootEl.contains(active)) return
220
+ // The input is the tab stop here; the chevron is deliberately tabindex="-1".
221
+ const trigger = rootEl.querySelector('[data-stx-combobox-input]')
222
+ if (trigger && trigger.focus) trigger.focus()
223
+ }
224
+
60
225
  onMount(() => {
61
226
  rootEl = document.querySelector(`[data-combobox-id="${comboboxInstanceId}"]`)
62
227
  syncOptionStates()
228
+ syncExpanded()
63
229
  outsideHandler = (event) => {
64
230
  if (!isOpen()) return
65
231
  if (rootEl && !rootEl.contains(event.target)) close()
66
232
  }
67
233
  document.addEventListener('click', outsideHandler, true)
234
+ document.addEventListener('keydown', onDocumentKey)
235
+ document.addEventListener('keydown', onComboboxKey)
68
236
  })
69
237
 
70
238
  onDestroy(() => {
71
239
  if (outsideHandler) document.removeEventListener('click', outsideHandler, true)
240
+ document.removeEventListener('keydown', onDocumentKey)
241
+ document.removeEventListener('keydown', onComboboxKey)
72
242
  })
73
243
 
74
244
  const dataState = derived(() => isOpen() ? 'open' : 'closed')
75
245
 
76
- defineExpose({ isOpen, selected, query, toggle, open, close, selectValue })
246
+ defineExpose({ isOpen, selected, query, activeIndex, toggle, open, close, selectValue })
77
247
  </script>
78
248
 
79
249
  <component
@@ -1,6 +1,12 @@
1
1
  <script server>
2
2
  export const className = $props.className || ''
3
3
  export const as = $props.as || 'button'
4
+ /*
5
+ * An icon-only control needs a name. It is not in the tab order (APG puts the
6
+ * combobox itself there and gives this button tabindex="-1"), but a screen
7
+ * reader still reaches it in browse mode, where it announced "button".
8
+ */
9
+ export const label = $props.label || 'Show options'
4
10
 
5
11
  const baseClasses = 'absolute inset-y-0 right-0 flex items-center pr-2'
6
12
  export const buttonClasses = `${baseClasses} ${className}`.trim()
@@ -17,9 +23,10 @@ function onClick(event) {
17
23
  class="{{ buttonClasses }}"
18
24
  data-stx-combobox-button
19
25
  tabindex="-1"
26
+ aria-label="{{ label }}"
20
27
  @click="onClick($event)"
21
28
  >
22
- <svg class="h-5 w-5 text-neutral-400" viewBox="0 0 20 20" fill="none" stroke="currentColor">
29
+ <svg class="h-5 w-5 text-fg-soft" aria-hidden="true" viewBox="0 0 20 20" fill="none" stroke="currentColor">
23
30
  <path d="M7 7l3-3 3 3m0 6l-3 3-3-3" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
24
31
  </svg>
25
32
  </component>
@@ -24,6 +24,9 @@ function onInput(event) {
24
24
  type="text"
25
25
  class="{{ inputClasses }}"
26
26
  data-stx-combobox-input
27
+ role="combobox"
28
+ aria-expanded="false"
29
+ aria-autocomplete="list"
27
30
  placeholder="{{ placeholder }}"
28
31
  value="{{ displayValue }}"
29
32
  @if(disabled)disabled@endif
@@ -12,7 +12,19 @@ export const valueJson = JSON.stringify(value === undefined ? null : value)
12
12
 
13
13
  <script client>
14
14
  const isDisabled = {{ disabled }}
15
- const optionValue = {{ value === undefined ? 'null' : value }}
15
+ /*
16
+ * JSON built on the server and emitted raw, rather than a 'null' sentinel
17
+ * through an ordinary interpolation.
18
+ *
19
+ * A `value === undefined ? 'null' : value` ternary was meant to put the bare
20
+ * token null into the script. An interpolation QUOTES a string, so what it
21
+ * emitted was the four-character string "null" -- truthy, in the one place a
22
+ * value is meant to be absent. JSON.stringify produces the correct literal for
23
+ * every type, null included, and raw interpolation passes it through unquoted.
24
+ * Calendar and DateRangePicker already avoid this sentinel and say why
25
+ * (stacksjs/stx#1981).
26
+ */
27
+ const optionValue = {!! valueJson !!}
16
28
 
17
29
  function onClick(event) {
18
30
  if (isDisabled) return
@@ -42,6 +54,20 @@ function onClick(event) {
42
54
  </component>
43
55
 
44
56
  <style>
57
+ /*
58
+ * The ACTIVE option, highlighted to match the hover state.
59
+ *
60
+ * A combobox keeps focus in its input, so the option a user has arrowed to has
61
+ * no focus ring to show it -- `aria-activedescendant` tells a screen reader
62
+ * which one it is and nothing told a sighted keyboard user. Written out rather
63
+ * than as a utility class because the runtime sets a data attribute, and it
64
+ * resolves through the same role variable `hover:bg-accent-soft` compiles to,
65
+ * so re-pointing the accent moves both together.
66
+ */
67
+ [data-stx-combobox-option][data-active="true"] {
68
+ background-color: var(--stx-accent-soft, oklch(93.2% 0.032 255.585));
69
+ }
70
+
45
71
  [data-stx-combobox-option][data-selected="false"] [data-stx-combobox-check],
46
72
  [data-stx-combobox-option]:not([data-selected]) [data-stx-combobox-check] {
47
73
  display: none;
@@ -62,12 +62,12 @@ defineExpose({ isOpen, query, show, close })
62
62
 
63
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">
64
64
  <div class="relative">
65
- <svg class="pointer-events-none absolute top-3.5 left-4 h-5 w-5 text-neutral-400" viewBox="0 0 20 20" fill="currentColor">
65
+ <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
66
  <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" />
67
67
  </svg>
68
68
  <input
69
69
  type="text"
70
- class="h-12 w-full border-0 bg-transparent pl-11 pr-4 text-fg placeholder-neutral-400 focus:ring-0 sm:text-sm"
70
+ 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
71
  placeholder="Search..."
72
72
  :value="query()"
73
73
  @input="onQueryInput($event)"
@@ -1,6 +1,7 @@
1
1
  <script server>
2
2
  export const className = $props.className || ''
3
3
  export const value = $props.value
4
+ export const valueJson = JSON.stringify(value === undefined ? null : value)
4
5
 
5
6
  const baseClasses = 'flex cursor-pointer select-none items-center px-4 py-2 text-fg hover:bg-accent-solid hover:text-accent-ink'
6
7
  export const itemClasses = `${baseClasses} ${className}`.trim()
@@ -8,7 +9,19 @@ export const itemClasses = `${baseClasses} ${className}`.trim()
8
9
 
9
10
  <script client>
10
11
  const emit = defineEmits()
11
- const itemValue = {{ value === undefined ? 'null' : value }}
12
+ /*
13
+ * JSON built on the server and emitted raw, rather than a 'null' sentinel
14
+ * through an ordinary interpolation.
15
+ *
16
+ * A `value === undefined ? 'null' : value` ternary was meant to put the bare
17
+ * token null into the script. An interpolation QUOTES a string, so what it
18
+ * emitted was the four-character string "null" -- truthy, in the one place a
19
+ * value is meant to be absent. JSON.stringify produces the correct literal for
20
+ * every type, null included, and raw interpolation passes it through unquoted.
21
+ * Calendar and DateRangePicker already avoid this sentinel and say why
22
+ * (stacksjs/stx#1981).
23
+ */
24
+ const itemValue = {!! valueJson !!}
12
25
 
13
26
  function onClick(event) {
14
27
  emit('click', event)
@@ -14,21 +14,41 @@ const isOpen = state(false)
14
14
  let rootEl = null
15
15
  let outsideHandler = null
16
16
 
17
+ /*
18
+ * The trigger carries the open state for assistive tech. `isOpen` already
19
+ * drove `data-state` on the root, which styles the panel but says nothing to a
20
+ * screen reader: the button announced itself as a plain button, with no
21
+ * indication that it opens anything or whether it is open right now.
22
+ */
23
+ function syncExpanded() {
24
+ if (!rootEl) return
25
+ const trigger = rootEl.querySelector('[data-stx-dropdown-button]')
26
+ if (!trigger) return
27
+ trigger.setAttribute('aria-expanded', isOpen() ? 'true' : 'false')
28
+ const panel = rootEl.querySelector('[data-stx-dropdown-items]')
29
+ if (!panel) return
30
+ if (!panel.id) panel.id = 'stx-dropdown-panel-' + dropdownInstanceId
31
+ trigger.setAttribute('aria-controls', panel.id)
32
+ }
33
+
17
34
  function toggle() {
18
35
  isOpen.set(!isOpen())
19
36
  emit('change', isOpen())
37
+ syncExpanded()
20
38
  }
21
39
 
22
40
  function open() {
23
41
  if (isOpen()) return
24
42
  isOpen.set(true)
25
43
  emit('change', true)
44
+ syncExpanded()
26
45
  }
27
46
 
28
47
  function close() {
29
48
  if (!isOpen()) return
30
49
  isOpen.set(false)
31
50
  emit('change', false)
51
+ syncExpanded()
32
52
  }
33
53
 
34
54
  function onItemSelect(event) {
@@ -41,17 +61,119 @@ function onToggle(event) { event.stopPropagation(); toggle() }
41
61
  function onOpen(event) { event.stopPropagation(); open() }
42
62
  function onClose(event) { event.stopPropagation(); close() }
43
63
 
64
+ /*
65
+ * Escape dismisses it, from anywhere rather than only from inside the panel.
66
+ *
67
+ * Opening one of these does not move focus, so a keydown bound on the panel
68
+ * never sees the first Escape - focus is still on whatever opened it. Drawer
69
+ * reached the same conclusion and binds on the document; this follows it, and
70
+ * only acts while open so one open popup does not swallow Escape for the page.
71
+ *
72
+ * Focus goes back to the trigger, but only if it had moved into the panel:
73
+ * otherwise this would steal it from wherever the user actually is.
74
+ */
75
+ function onDocumentKey(event) {
76
+ if (event.key !== 'Escape' || !isOpen()) return
77
+ close()
78
+ if (!rootEl) return
79
+ const active = rootEl.ownerDocument && rootEl.ownerDocument.activeElement
80
+ if (!active || !rootEl.contains(active)) return
81
+ const trigger = rootEl.querySelector('[data-stx-dropdown-button]')
82
+ if (trigger && trigger.focus) trigger.focus()
83
+ }
84
+
85
+ /*
86
+ * Roving focus over the menu items.
87
+ *
88
+ * `role="menuitem"` on a div is not focusable on its own, so the items carry
89
+ * tabindex="-1" and focus is moved programmatically -- the APG menu-button
90
+ * arrangement. Only the items are in the ring; a disabled one is skipped
91
+ * rather than focused and ignored, because landing on something inert reads as
92
+ * the menu being broken.
93
+ */
94
+ function menuItems() {
95
+ if (!rootEl) return []
96
+ const all = rootEl.querySelectorAll('[data-stx-dropdown-item]')
97
+ return Array.prototype.filter.call(all, item => item.getAttribute('aria-disabled') !== 'true')
98
+ }
99
+
100
+ function focusItemAt(index) {
101
+ const items = menuItems()
102
+ if (!items.length) return
103
+ const target = items[(index + items.length) % items.length]
104
+ if (target && target.focus) target.focus()
105
+ }
106
+
107
+ function focusedItemIndex() {
108
+ const doc = rootEl && rootEl.ownerDocument
109
+ return doc ? menuItems().indexOf(doc.activeElement) : -1
110
+ }
111
+
112
+ /*
113
+ * Keys are handled only while the event comes from inside this dropdown.
114
+ * Bound on the document so Escape works before focus has moved into the panel,
115
+ * which means an unguarded ArrowDown here would hijack scrolling for the whole
116
+ * page.
117
+ */
118
+ function onMenuKey(event) {
119
+ if (!rootEl || !rootEl.contains(event.target)) return
120
+ const key = event.key
121
+
122
+ if (!isOpen()) {
123
+ // ArrowDown opens and lands on the first item; ArrowUp on the last, which
124
+ // is how a menu button reached from below behaves.
125
+ if (key !== 'ArrowDown' && key !== 'ArrowUp') return
126
+ event.preventDefault()
127
+ open()
128
+ focusItemAt(key === 'ArrowDown' ? 0 : menuItems().length - 1)
129
+ return
130
+ }
131
+
132
+ if (key === 'ArrowDown' || key === 'ArrowUp') {
133
+ event.preventDefault()
134
+ const current = focusedItemIndex()
135
+ if (current === -1) focusItemAt(key === 'ArrowDown' ? 0 : menuItems().length - 1)
136
+ else focusItemAt(current + (key === 'ArrowDown' ? 1 : -1))
137
+ return
138
+ }
139
+
140
+ if (key === 'Home' || key === 'End') {
141
+ event.preventDefault()
142
+ focusItemAt(key === 'Home' ? 0 : menuItems().length - 1)
143
+ return
144
+ }
145
+
146
+ if (key === 'Enter' || key === ' ') {
147
+ // The item is a div, so Enter does not activate it the way it would a
148
+ // button. Clicking it runs the handler the item already has.
149
+ const current = focusedItemIndex()
150
+ if (current === -1) return
151
+ event.preventDefault()
152
+ menuItems()[current].click()
153
+ return
154
+ }
155
+
156
+ // Tabbing out of an open menu closes it, rather than leaving it hanging
157
+ // over the content the user has moved to.
158
+ if (key === 'Tab') close()
159
+ }
160
+
44
161
  onMount(() => {
45
162
  rootEl = document.querySelector(`[data-dropdown-id="${dropdownInstanceId}"]`)
163
+ syncExpanded()
46
164
  outsideHandler = (event) => {
47
165
  if (!isOpen()) return
48
166
  if (rootEl && !rootEl.contains(event.target)) close()
49
167
  }
50
168
  document.addEventListener('click', outsideHandler, true)
169
+ document.addEventListener('keydown', onDocumentKey)
170
+ document.addEventListener('keydown', onMenuKey)
51
171
  })
52
172
 
53
173
  onDestroy(() => {
54
174
  if (outsideHandler) document.removeEventListener('click', outsideHandler, true)
175
+ document.removeEventListener('keydown', onDocumentKey)
176
+ document.removeEventListener('keydown', onMenuKey)
55
177
  })
56
178
 
57
179
  const dataState = derived(() => isOpen() ? 'open' : 'closed')
@@ -22,6 +22,8 @@ function onClick(event) {
22
22
  :is="{{ as }}"
23
23
  class="{{ buttonClasses }}"
24
24
  data-stx-dropdown-button
25
+ aria-haspopup="menu"
26
+ aria-expanded="false"
25
27
  @if(disabled)disabled aria-disabled="true"@endif
26
28
  @click="onClick($event)"
27
29
  >
@@ -3,6 +3,7 @@ export const className = $props.className || ''
3
3
  export const as = $props.as || 'div'
4
4
  export const disabled = $bool($props.disabled)
5
5
  export const value = $props.value
6
+ export const valueJson = JSON.stringify(value === undefined ? null : value)
6
7
 
7
8
  const baseClasses = 'group flex w-full items-center px-2 py-2 text-sm transition-colors text-fg hover:bg-accent-solid hover:text-accent-ink'
8
9
  const disabledClasses = disabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer'
@@ -12,7 +13,19 @@ export const itemClasses = `${baseClasses} ${disabledClasses} ${className}`.trim
12
13
  <script client>
13
14
  const emit = defineEmits()
14
15
  const isDisabled = {{ disabled }}
15
- const itemValue = {{ value === undefined ? 'null' : value }}
16
+ /*
17
+ * JSON built on the server and emitted raw, rather than a 'null' sentinel
18
+ * through an ordinary interpolation.
19
+ *
20
+ * A `value === undefined ? 'null' : value` ternary was meant to put the bare
21
+ * token null into the script. An interpolation QUOTES a string, so what it
22
+ * emitted was the four-character string "null" -- truthy, in the one place a
23
+ * value is meant to be absent. JSON.stringify produces the correct literal for
24
+ * every type, null included, and raw interpolation passes it through unquoted.
25
+ * Calendar and DateRangePicker already avoid this sentinel and say why
26
+ * (stacksjs/stx#1981).
27
+ */
28
+ const itemValue = {!! valueJson !!}
16
29
 
17
30
  function onClick(event) {
18
31
  if (isDisabled) return
@@ -28,6 +41,7 @@ function onClick(event) {
28
41
  class="{{ itemClasses }}"
29
42
  data-stx-dropdown-item
30
43
  role="menuitem"
44
+ tabindex="-1"
31
45
  @if(disabled)aria-disabled="true"@endif
32
46
  @click="onClick($event)"
33
47
  >