@stacksjs/components 0.2.369 → 0.2.371
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.
- package/dist/{Checkbox-e7gn3w06.stx → Checkbox-tt8bze29.stx} +19 -3
- package/dist/Combobox-9xh1jgjr.stx +266 -0
- package/dist/{ComboboxButton-g76stp1e.stx → ComboboxButton-4r6a45s1.stx} +8 -1
- package/dist/{ComboboxInput-a5g0vn3e.stx → ComboboxInput-1gyfpnca.stx} +3 -0
- package/dist/{ComboboxOption-vnb67p8d.stx → ComboboxOption-wkw70js7.stx} +27 -1
- package/dist/{CommandPalette-y6t67rjj.stx → CommandPalette-vvaqtqat.stx} +2 -2
- package/dist/CommandPaletteItem-7290ce1z.stx +40 -0
- package/dist/Dropdown-25160tc6.stx +204 -0
- package/dist/{DropdownButton-6by2ybde.stx → DropdownButton-wxc25hsy.stx} +2 -0
- package/dist/{DropdownItem-wht1vcm5.stx → DropdownItem-af0vzc8a.stx} +15 -1
- package/dist/Listbox-32gzxace.stx +273 -0
- package/dist/{ListboxButton-3yc7tm8w.stx → ListboxButton-gan8e3nw.stx} +2 -0
- package/dist/{ListboxOption-9c9jpz1n.stx → ListboxOption-gpaqvk3s.stx} +14 -1
- package/dist/{Login-3dsk93vm.stx → Login-4666t7j4.stx} +18 -7
- package/dist/{NumberInput-awn66hfs.stx → NumberInput-g3jfy120.stx} +21 -4
- package/dist/{PasswordInput-hradj0xd.stx → PasswordInput-p2rt5926.stx} +21 -4
- package/dist/Popover-9m42pym0.stx +101 -0
- package/dist/{PopoverButton-htfypg00.stx → PopoverButton-3w6550hv.stx} +1 -0
- package/dist/{Radio-nr8rbb6g.stx → Radio-625byys2.stx} +19 -3
- package/dist/{RadioGroup-neej7522.stx → RadioGroup-smqcqgds.stx} +14 -1
- package/dist/{RadioGroupOption-c6xd0at9.stx → RadioGroupOption-twcw1b2a.stx} +13 -1
- package/dist/{Select-danh680d.stx → Select-1p9mcbsb.stx} +19 -3
- package/dist/{Signup-dh96kskd.stx → Signup-pm2r0f2m.stx} +21 -10
- package/dist/{SubscriptionCheckout-75x9mfv2.stx → SubscriptionCheckout-f1ev0ry8.stx} +27 -11
- package/dist/{TextInput-48ghmccm.stx → TextInput-t0a4ch49.stx} +19 -3
- package/dist/{Textarea-dndmk6ah.stx → Textarea-k1gf21kw.stx} +21 -4
- package/dist/{TwoFactorChallenge-ete7yk9y.stx → TwoFactorChallenge-st48qkzw.stx} +25 -5
- package/dist/index.js +7 -7
- package/package.json +3 -3
- package/src/components/Footer.stx +2 -2
- package/src/ui/auth/Login.stx +18 -7
- package/src/ui/auth/Signup.stx +21 -10
- package/src/ui/auth/TwoFactorChallenge.stx +25 -5
- package/src/ui/checkbox/Checkbox.stx +19 -3
- package/src/ui/combobox/Combobox.stx +174 -4
- package/src/ui/combobox/ComboboxButton.stx +8 -1
- package/src/ui/combobox/ComboboxInput.stx +3 -0
- package/src/ui/combobox/ComboboxOption.stx +27 -1
- package/src/ui/command-palette/CommandPalette.stx +2 -2
- package/src/ui/command-palette/CommandPaletteItem.stx +14 -1
- package/src/ui/dropdown/Dropdown.stx +122 -0
- package/src/ui/dropdown/DropdownButton.stx +2 -0
- package/src/ui/dropdown/DropdownItem.stx +15 -1
- package/src/ui/input/NumberInput.stx +21 -4
- package/src/ui/input/PasswordInput.stx +21 -4
- package/src/ui/input/TextInput.stx +19 -3
- package/src/ui/listbox/Listbox.stx +175 -3
- package/src/ui/listbox/ListboxButton.stx +2 -0
- package/src/ui/listbox/ListboxOption.stx +14 -1
- package/src/ui/payment/SubscriptionCheckout.stx +27 -11
- package/src/ui/popover/Popover.stx +43 -2
- package/src/ui/popover/PopoverButton.stx +1 -0
- package/src/ui/radio/Radio.stx +19 -3
- package/src/ui/radio-group/RadioGroup.stx +14 -1
- package/src/ui/radio-group/RadioGroupOption.stx +13 -1
- package/src/ui/select/Select.stx +19 -3
- package/src/ui/textarea/Textarea.stx +21 -4
- package/dist/Combobox-06x0nywb.stx +0 -96
- package/dist/CommandPaletteItem-6rp133ta.stx +0 -27
- package/dist/Dropdown-cmwge7vx.stx +0 -82
- package/dist/Listbox-e5jreb0a.stx +0 -101
- package/dist/Popover-3g8rgxt1.stx +0 -60
package/src/ui/auth/Signup.stx
CHANGED
|
@@ -3,6 +3,17 @@ export const headingText = $props.headingText || 'Sign up for Stacks'
|
|
|
3
3
|
export const showSocialSignup = $bool($props.showSocialSignup, true)
|
|
4
4
|
export const className = $props.className || ''
|
|
5
5
|
|
|
6
|
+
/*
|
|
7
|
+
* Field ids unique to this instance.
|
|
8
|
+
*
|
|
9
|
+
* They were `email`, `password` and the like -- names generic enough to
|
|
10
|
+
* collide with the host app's own ids, which breaks getElementById and
|
|
11
|
+
* `<label for>` for both. `name` deliberately stays generic: it is the key the
|
|
12
|
+
* form submits under, and it is what `autocomplete` and password managers read
|
|
13
|
+
* alongside `type`, so the autofill behaviour is unchanged.
|
|
14
|
+
*/
|
|
15
|
+
export const fieldPrefix = $props.id || `signup-${$uid}`
|
|
16
|
+
|
|
6
17
|
export const containerClasses = `min-h-full flex flex-col justify-center py-12 lg:px-8 sm:px-6 bg-page ${className}`.trim()
|
|
7
18
|
</script>
|
|
8
19
|
|
|
@@ -33,8 +44,8 @@ function onSubmit(event) {
|
|
|
33
44
|
emit('submit', { name, email, password })
|
|
34
45
|
|
|
35
46
|
setTimeout(() => {
|
|
36
|
-
event.target.querySelector('
|
|
37
|
-
event.target.querySelector('
|
|
47
|
+
event.target.querySelector('input[name="password"]')?.setAttribute('value', '')
|
|
48
|
+
event.target.querySelector('input[name="confirm_email"]')?.setAttribute('value', '')
|
|
38
49
|
}, 1000)
|
|
39
50
|
}
|
|
40
51
|
</script>
|
|
@@ -50,12 +61,12 @@ function onSubmit(event) {
|
|
|
50
61
|
<div class="bg-panel px-6 py-12 shadow sm:rounded-panel sm:px-12">
|
|
51
62
|
<form class="space-y-6" @submit="onSubmit($event)">
|
|
52
63
|
<div>
|
|
53
|
-
<label for="name" class="block text-sm text-fg font-medium leading-6">
|
|
64
|
+
<label for="{{ fieldPrefix }}-name" class="block text-sm text-fg font-medium leading-6">
|
|
54
65
|
Full Name
|
|
55
66
|
</label>
|
|
56
67
|
<div class="mt-2">
|
|
57
68
|
<input
|
|
58
|
-
id="name"
|
|
69
|
+
id="{{ fieldPrefix }}-name"
|
|
59
70
|
name="name"
|
|
60
71
|
type="text"
|
|
61
72
|
autocomplete="name"
|
|
@@ -66,12 +77,12 @@ function onSubmit(event) {
|
|
|
66
77
|
</div>
|
|
67
78
|
|
|
68
79
|
<div>
|
|
69
|
-
<label for="email" class="block text-sm text-fg font-medium leading-6">
|
|
80
|
+
<label for="{{ fieldPrefix }}-email" class="block text-sm text-fg font-medium leading-6">
|
|
70
81
|
Email address
|
|
71
82
|
</label>
|
|
72
83
|
<div class="mt-2">
|
|
73
84
|
<input
|
|
74
|
-
id="email"
|
|
85
|
+
id="{{ fieldPrefix }}-email"
|
|
75
86
|
name="email"
|
|
76
87
|
type="email"
|
|
77
88
|
autocomplete="email"
|
|
@@ -82,12 +93,12 @@ function onSubmit(event) {
|
|
|
82
93
|
</div>
|
|
83
94
|
|
|
84
95
|
<div>
|
|
85
|
-
<label for="confirm_email" class="block text-sm text-fg font-medium leading-6">
|
|
96
|
+
<label for="{{ fieldPrefix }}-confirm_email" class="block text-sm text-fg font-medium leading-6">
|
|
86
97
|
Confirm Email
|
|
87
98
|
</label>
|
|
88
99
|
<div class="mt-2">
|
|
89
100
|
<input
|
|
90
|
-
id="confirm_email"
|
|
101
|
+
id="{{ fieldPrefix }}-confirm_email"
|
|
91
102
|
name="confirm_email"
|
|
92
103
|
type="email"
|
|
93
104
|
autocomplete="email"
|
|
@@ -98,12 +109,12 @@ function onSubmit(event) {
|
|
|
98
109
|
</div>
|
|
99
110
|
|
|
100
111
|
<div>
|
|
101
|
-
<label for="password" class="block text-sm text-fg font-medium leading-6">
|
|
112
|
+
<label for="{{ fieldPrefix }}-password" class="block text-sm text-fg font-medium leading-6">
|
|
102
113
|
Password
|
|
103
114
|
</label>
|
|
104
115
|
<div class="mt-2">
|
|
105
116
|
<input
|
|
106
|
-
id="password"
|
|
117
|
+
id="{{ fieldPrefix }}-password"
|
|
107
118
|
name="password"
|
|
108
119
|
type="password"
|
|
109
120
|
autocomplete="new-password"
|
|
@@ -6,12 +6,32 @@ export const codeLength = $props.codeLength || 6
|
|
|
6
6
|
export const showRecoveryCode = $bool($props.showRecoveryCode, true)
|
|
7
7
|
export const className = $props.className || ''
|
|
8
8
|
|
|
9
|
+
/*
|
|
10
|
+
* The digit inputs' ids, unique to this instance.
|
|
11
|
+
*
|
|
12
|
+
* They were `code-0` ... `code-5`, and the focus management looks them up with
|
|
13
|
+
* `document.getElementById` -- a DOCUMENT-wide lookup on an id as generic as
|
|
14
|
+
* `code-1`. Any element in the host app with that id would take the focus
|
|
15
|
+
* instead, and two of these on a page would type into each other.
|
|
16
|
+
*
|
|
17
|
+
* `name` deliberately stays `code-<i>`: that is the key the form submits
|
|
18
|
+
* under, so namespacing it would change the payload a server sees. Only the
|
|
19
|
+
* id, which exists to connect the label and the focus moves, is instance-local.
|
|
20
|
+
*/
|
|
21
|
+
export const codeIdPrefix = $props.id || `code-${$uid}`
|
|
22
|
+
|
|
9
23
|
export const containerClasses = `py-32 bg-page ${className}`.trim()
|
|
10
24
|
</script>
|
|
11
25
|
|
|
12
26
|
<script client>
|
|
13
27
|
const emit = defineEmits()
|
|
14
28
|
const codeLength = {{ codeLength }}
|
|
29
|
+
const codeIdPrefix = {{ codeIdPrefix }}
|
|
30
|
+
|
|
31
|
+
/** The digit input at `index`, found by this instance's own id. */
|
|
32
|
+
function codeInput(index) {
|
|
33
|
+
return document.getElementById(codeIdPrefix + '-' + index)
|
|
34
|
+
}
|
|
15
35
|
|
|
16
36
|
function onCodeInput(event, index) {
|
|
17
37
|
const input = event.target
|
|
@@ -21,13 +41,13 @@ function onCodeInput(event, index) {
|
|
|
21
41
|
return
|
|
22
42
|
}
|
|
23
43
|
if (value.length === 1 && index < codeLength - 1) {
|
|
24
|
-
|
|
44
|
+
codeInput(index + 1)?.focus()
|
|
25
45
|
}
|
|
26
46
|
}
|
|
27
47
|
|
|
28
48
|
function onKeyDown(event, index) {
|
|
29
49
|
if (event.key === 'Backspace' && !event.target.value && index > 0) {
|
|
30
|
-
|
|
50
|
+
codeInput(index - 1)?.focus()
|
|
31
51
|
}
|
|
32
52
|
}
|
|
33
53
|
|
|
@@ -35,7 +55,7 @@ function onSubmit(event) {
|
|
|
35
55
|
event.preventDefault()
|
|
36
56
|
const code = []
|
|
37
57
|
for (let i = 0; i < codeLength; i++) {
|
|
38
|
-
const input =
|
|
58
|
+
const input = codeInput(i)
|
|
39
59
|
if (input?.value) code.push(input.value)
|
|
40
60
|
}
|
|
41
61
|
if (code.length === codeLength) {
|
|
@@ -59,14 +79,14 @@ function onRecoveryCode(event) {
|
|
|
59
79
|
<h3 class="text-center text-sm text-fg font-semibold">
|
|
60
80
|
{{ headingText }}
|
|
61
81
|
</h3>
|
|
62
|
-
<label for="
|
|
82
|
+
<label for="{{ codeIdPrefix }}-0" class="mt-4 block text-sm text-fg font-medium leading-6">
|
|
63
83
|
{{ labelText }}
|
|
64
84
|
</label>
|
|
65
85
|
<form @submit="onSubmit($event)">
|
|
66
86
|
<div class="mt-2 flex rounded-control shadow-sm space-x-2">
|
|
67
87
|
@for(let i = 0; i < codeLength; i++)
|
|
68
88
|
<input
|
|
69
|
-
id="
|
|
89
|
+
id="{{ codeIdPrefix }}-{{ i }}"
|
|
70
90
|
type="text"
|
|
71
91
|
maxlength="1"
|
|
72
92
|
name="code-{{ i }}"
|
|
@@ -19,7 +19,23 @@ const disabledClasses = disabled ? 'opacity-50 cursor-not-allowed' : 'cursor-poi
|
|
|
19
19
|
export const checkboxClasses = `${baseClasses} ${sizeClasses[size] || sizeClasses.md} ${stateClasses} ${disabledClasses}`.trim()
|
|
20
20
|
export const labelClasses = `text-sm font-medium ${error ? 'text-danger' : 'text-fg'} ${disabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer'}`.trim()
|
|
21
21
|
export const descriptionClasses = `text-sm ${error ? 'text-danger' : 'text-fg-soft'}`.trim()
|
|
22
|
-
|
|
22
|
+
/*
|
|
23
|
+
* Ids unique to this instance.
|
|
24
|
+
*
|
|
25
|
+
* These were derived from the LABEL or hardcoded outright, so two of these on
|
|
26
|
+
* one page -- an ordinary form -- emitted the same id twice. `<label for>`
|
|
27
|
+
* then activated the FIRST field from every label, and `aria-describedby`
|
|
28
|
+
* pointed every field's description at the first one's helper text. A label
|
|
29
|
+
* containing a space also produced an invalid id, `input-First name`, which
|
|
30
|
+
* getElementById and querySelector cannot address at all.
|
|
31
|
+
*
|
|
32
|
+
* `$uid` is unique per instance and stable across renders, which is what an id
|
|
33
|
+
* has to be (utils.ts explains why a render that changes its own bytes is
|
|
34
|
+
* uncacheable). The `id` prop overrides it, for a caller that needs to point
|
|
35
|
+
* its own `for` or `aria-describedby` at this control.
|
|
36
|
+
*/
|
|
37
|
+
export const inputId = $props.id || `checkbox-${$uid}`
|
|
38
|
+
export const descriptionId = `${inputId}-description`
|
|
23
39
|
</script>
|
|
24
40
|
|
|
25
41
|
<script client>
|
|
@@ -62,7 +78,7 @@ defineExpose({ isChecked })
|
|
|
62
78
|
class="{{ checkboxClasses }}"
|
|
63
79
|
@change="onInputChange($event)"
|
|
64
80
|
:aria-invalid="{{ error ? 'true' : 'false' }}"
|
|
65
|
-
@if(description)aria-describedby="
|
|
81
|
+
@if(description)aria-describedby="{{ descriptionId }}"@endif
|
|
66
82
|
/>
|
|
67
83
|
</div>
|
|
68
84
|
|
|
@@ -77,7 +93,7 @@ defineExpose({ isChecked })
|
|
|
77
93
|
</label>
|
|
78
94
|
@endif
|
|
79
95
|
@if(description)
|
|
80
|
-
<p id="
|
|
96
|
+
<p id="{{ descriptionId }}" class="{{ descriptionClasses }}">
|
|
81
97
|
{{ description }}
|
|
82
98
|
</p>
|
|
83
99
|
@endif
|
|
@@ -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
|
-
|
|
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
|
-
|
|
48
|
-
|
|
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-
|
|
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
|
-
|
|
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-
|
|
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-
|
|
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
|
-
|
|
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)
|