@stacksjs/components 0.2.465 → 0.2.467
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ChatLauncher-pc9nqh43.stx → ChatLauncher-3zph4trh.stx} +10 -2
- package/dist/{Combobox-9xh1jgjr.stx → Combobox-9n04ec5w.stx} +16 -4
- package/dist/{CommandPalette-vvaqtqat.stx → CommandPalette-qqjdbcyg.stx} +33 -8
- package/dist/{DateRangePicker-z64qqw84.stx → DateRangePicker-4zmf34e4.stx} +13 -9
- package/dist/Dialog-pdq8rxvy.stx +162 -0
- package/dist/{Drawer-hc0jjpsw.stx → Drawer-nmq100vy.stx} +24 -128
- package/dist/{Dropdown-25160tc6.stx → Dropdown-hb8ks69x.stx} +16 -4
- package/dist/{EmailInput-ffzwvp9r.stx → EmailInput-57626391.stx} +3 -0
- package/dist/{Listbox-32gzxace.stx → Listbox-83j2q4x6.stx} +16 -4
- package/dist/{NumberInput-r2wsh8w9.stx → NumberInput-pq4k2k4k.stx} +31 -10
- package/dist/{PasswordInput-36bws0kf.stx → PasswordInput-dg41ktgd.stx} +8 -1
- package/dist/{Popover-9m42pym0.stx → Popover-khtvagcs.stx} +16 -4
- package/dist/{Select-pv44ngbe.stx → Select-61zv5tr7.stx} +98 -17
- package/dist/Tab-46mxm7f8.stx +35 -0
- package/dist/TabList-76bpx5fp.stx +154 -0
- package/dist/Table-0a4y46vd.stx +43 -0
- package/dist/TableCell-jhvsxw8b.stx +17 -0
- package/dist/TableHeader-krzcx4sv.stx +62 -0
- package/dist/{Tabs-3j2h0jjn.stx → Tabs-1swcxmfb.stx} +4 -1
- package/dist/{TextInput-h9trj9f9.stx → TextInput-nknmz9md.stx} +57 -4
- package/dist/{Textarea-k8cjs4zs.stx → Textarea-9xmhjqp8.stx} +30 -7
- package/dist/{Tooltip-z13bn0pv.stx → Tooltip-rg1c2zrc.stx} +13 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -7
- package/dist/ui/input/index.d.ts +18 -5
- package/dist/ui/select/index.d.ts +1 -1
- package/dist/ui/table/index.d.ts +4 -0
- package/dist/ui/tabs/index.d.ts +35 -3
- package/dist/utils/overlays.d.ts +39 -0
- package/package.json +3 -3
- package/src/index.ts +2 -0
- package/src/ui/chat/ChatLauncher.stx +10 -2
- package/src/ui/combobox/Combobox.stx +16 -4
- package/src/ui/command-palette/CommandPalette.stx +33 -8
- package/src/ui/date-range-picker/DateRangePicker.stx +13 -9
- package/src/ui/dialog/Dialog.stx +29 -169
- package/src/ui/dialog/README.md +5 -0
- package/src/ui/drawer/Drawer.stx +24 -128
- package/src/ui/drawer/README.md +4 -0
- package/src/ui/dropdown/Dropdown.stx +16 -4
- package/src/ui/input/EmailInput.stx +3 -0
- package/src/ui/input/NumberInput.stx +31 -10
- package/src/ui/input/PasswordInput.stx +8 -1
- package/src/ui/input/TextInput.stx +57 -4
- package/src/ui/input/index.ts +31 -5
- package/src/ui/listbox/Listbox.stx +16 -4
- package/src/ui/popover/Popover.stx +16 -4
- package/src/ui/select/README.md +59 -21
- package/src/ui/select/Select.stx +98 -17
- package/src/ui/select/index.ts +8 -1
- package/src/ui/table/README.md +69 -1
- package/src/ui/table/Table.stx +31 -2
- package/src/ui/table/TableCell.stx +8 -1
- package/src/ui/table/TableHeader.stx +47 -5
- package/src/ui/table/index.ts +9 -0
- package/src/ui/tabs/Tab.stx +35 -0
- package/src/ui/tabs/TabList.stx +154 -0
- package/src/ui/tabs/Tabs.stx +4 -1
- package/src/ui/tabs/index.ts +37 -3
- package/src/ui/textarea/Textarea.stx +30 -7
- package/src/ui/tooltip/Tooltip.stx +13 -3
- package/src/utils/overlays.ts +69 -0
- package/dist/Dialog-e9a4r6d0.stx +0 -302
- package/dist/Table-3ek3w95h.stx +0 -14
- package/dist/TableCell-8swtcq8s.stx +0 -10
- package/dist/TableHeader-qsc3e208.stx +0 -20
|
@@ -25,9 +25,13 @@ export const inputId = $props.id || `number-input-${$uid}`
|
|
|
25
25
|
export const helperId = `${inputId}-helper`
|
|
26
26
|
export const helperText = $props.helperText || ''
|
|
27
27
|
export const required = $bool($props.required)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
/*
|
|
29
|
+
* Numbers, not the strings an attribute arrives as: `step="2"` reached the
|
|
30
|
+
* client script as "2", and the Increment button computed 10 + "2" = "102".
|
|
31
|
+
*/
|
|
32
|
+
export const min = $props.min == null || $props.min === '' ? undefined : $num($props.min)
|
|
33
|
+
export const max = $props.max == null || $props.max === '' ? undefined : $num($props.max)
|
|
34
|
+
export const step = $num($props.step, 1)
|
|
31
35
|
export const showControls = $bool($props.showControls, true)
|
|
32
36
|
export const className = $props.className || ''
|
|
33
37
|
export const inputClassName = $props.inputClassName || ''
|
|
@@ -42,7 +46,14 @@ export const inputClassName = $props.inputClassName || ''
|
|
|
42
46
|
* is 3.25rem of content inside a declared 3rem box - so py-2.5 lands on the
|
|
43
47
|
* 3rem it always claimed. sm and md were already exact.
|
|
44
48
|
*/
|
|
45
|
-
|
|
49
|
+
/*
|
|
50
|
+
* md -- the size nearly every form uses -- reads the density roles, so an
|
|
51
|
+
* app sets its own field size with --stx-control-px, --stx-control-py,
|
|
52
|
+
* --stx-control-text and --stx-control-leading instead of a className
|
|
53
|
+
* that ties with py-2 on specificity. The fallbacks are px-4 / py-2 /
|
|
54
|
+
* text-base, so nothing moves by default (stacksjs/stx#1993).
|
|
55
|
+
*/
|
|
56
|
+
const sizeClasses = { sm: 'px-3 py-1.5 text-sm', md: 'px-control-x py-control-y text-control', lg: 'px-5 py-2.5 text-lg' }
|
|
46
57
|
const baseClasses = 'block w-full border-0 rounded-control shadow-sm ring-1 ring-inset transition-colors duration-200 focus:ring-2 focus:ring-inset bg-field'
|
|
47
58
|
const stateClasses = error
|
|
48
59
|
? 'ring-danger-line text-danger-fg placeholder-danger focus:ring-danger-focus'
|
|
@@ -85,12 +96,21 @@ const emit = defineEmits()
|
|
|
85
96
|
const stepSize = {{ step }}
|
|
86
97
|
const minVal = {{ min === undefined ? 'undefined' : min }}
|
|
87
98
|
const maxVal = {{ max === undefined ? 'undefined' : max }}
|
|
88
|
-
|
|
89
|
-
|
|
99
|
+
/*
|
|
100
|
+
* Two-way, like TextInput (stacksjs/stx#2059): `v-model:value="qty"` keeps the
|
|
101
|
+
* page's signal in step -- typing and the steppers both write it -- and
|
|
102
|
+
* `.number` hands the page a number. `input` is still emitted.
|
|
103
|
+
*/
|
|
104
|
+
const inputValue = useModel('value', { default: {{ value }}, parse: (v) => v == null || v === '' ? '' : String(v) })
|
|
90
105
|
|
|
91
106
|
function onInput(event) {
|
|
92
|
-
inputValue.
|
|
93
|
-
emit('input', inputValue())
|
|
107
|
+
inputValue.input(event.target.value)
|
|
108
|
+
if (!inputValue.modifiers.lazy) emit('input', inputValue())
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function onChange(event) {
|
|
112
|
+
inputValue.change(event.target.value)
|
|
113
|
+
if (inputValue.modifiers.lazy) emit('input', inputValue())
|
|
94
114
|
}
|
|
95
115
|
|
|
96
116
|
function increment() {
|
|
@@ -98,7 +118,7 @@ function increment() {
|
|
|
98
118
|
const next = current + stepSize
|
|
99
119
|
if (maxVal !== undefined && next > maxVal) return
|
|
100
120
|
inputValue.set(String(next))
|
|
101
|
-
emit('input',
|
|
121
|
+
emit('input', inputValue())
|
|
102
122
|
}
|
|
103
123
|
|
|
104
124
|
function decrement() {
|
|
@@ -106,7 +126,7 @@ function decrement() {
|
|
|
106
126
|
const next = current - stepSize
|
|
107
127
|
if (minVal !== undefined && next < minVal) return
|
|
108
128
|
inputValue.set(String(next))
|
|
109
|
-
emit('input',
|
|
129
|
+
emit('input', inputValue())
|
|
110
130
|
}
|
|
111
131
|
|
|
112
132
|
defineExpose({ value: inputValue, increment, decrement })
|
|
@@ -153,6 +173,7 @@ defineExpose({ value: inputValue, increment, decrement })
|
|
|
153
173
|
@if(max !== undefined)max="{{ max }}"@endif
|
|
154
174
|
step="{{ step }}"
|
|
155
175
|
@input="onInput($event)"
|
|
176
|
+
@change="onChange($event)"
|
|
156
177
|
:aria-invalid="{{ error ? 'true' : 'false' }}"
|
|
157
178
|
@if(helperText)aria-describedby="{{ helperId }}"@endif
|
|
158
179
|
/>
|
|
@@ -39,7 +39,14 @@ export const inputClassName = $props.inputClassName || ''
|
|
|
39
39
|
* is 3.25rem of content inside a declared 3rem box - so py-2.5 lands on the
|
|
40
40
|
* 3rem it always claimed. sm and md were already exact.
|
|
41
41
|
*/
|
|
42
|
-
|
|
42
|
+
/*
|
|
43
|
+
* md -- the size nearly every form uses -- reads the density roles, so an
|
|
44
|
+
* app sets its own field size with --stx-control-px, --stx-control-py,
|
|
45
|
+
* --stx-control-text and --stx-control-leading instead of a className
|
|
46
|
+
* that ties with py-2 on specificity. The fallbacks are px-4 / py-2 /
|
|
47
|
+
* text-base, so nothing moves by default (stacksjs/stx#1993).
|
|
48
|
+
*/
|
|
49
|
+
const sizeClasses = { sm: 'px-3 py-1.5 text-sm', md: 'px-control-x py-control-y text-control', lg: 'px-5 py-2.5 text-lg' }
|
|
43
50
|
const iconSizeClasses = { sm: 'h-4 w-4', md: 'h-5 w-5', lg: 'h-6 w-6' }
|
|
44
51
|
|
|
45
52
|
const baseClasses = 'block w-full border-0 rounded-control shadow-sm ring-1 ring-inset transition-colors duration-200 focus:ring-2 focus:ring-inset bg-field pr-10'
|
|
@@ -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
|
|
54
|
-
if (
|
|
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
|
-
|
|
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')
|
|
@@ -1,7 +1,34 @@
|
|
|
1
1
|
<script server>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
/*
|
|
3
|
+
* `??`, not `||`: a select whose chosen option is the id 0 is ordinary, and
|
|
4
|
+
* `||` served it as nothing selected.
|
|
5
|
+
*/
|
|
6
|
+
export const value = $props.value ?? ''
|
|
7
|
+
/*
|
|
8
|
+
* One shape for every option, whichever way it arrived (stacksjs/stx#2059).
|
|
9
|
+
*
|
|
10
|
+
* `{ value, label, disabled }` objects as before, plus bare strings or numbers
|
|
11
|
+
* (`['S', 'M', 'L']`), which are their own label. A string is parsed as JSON,
|
|
12
|
+
* because that is how a list written as a plain attribute arrives. The client
|
|
13
|
+
* script below applies the same rules to a list that arrives later.
|
|
14
|
+
*/
|
|
15
|
+
function normaliseOptions(list) {
|
|
16
|
+
if (typeof list === 'string') {
|
|
17
|
+
try { list = JSON.parse(list) }
|
|
18
|
+
catch { list = [] }
|
|
19
|
+
}
|
|
20
|
+
if (!Array.isArray(list))
|
|
21
|
+
return []
|
|
22
|
+
return list.map(option => option !== null && typeof option === 'object'
|
|
23
|
+
? { value: option.value == null ? '' : String(option.value), label: String(option.label ?? option.value ?? ''), disabled: Boolean(option.disabled) }
|
|
24
|
+
: { value: String(option), label: String(option), disabled: false })
|
|
25
|
+
}
|
|
26
|
+
export const options = normaliseOptions($props.options)
|
|
27
|
+
/*
|
|
28
|
+
* `??`: `placeholder=""` means no placeholder option at all, which a select
|
|
29
|
+
* whose options are its children usually wants. Unset keeps the default.
|
|
30
|
+
*/
|
|
31
|
+
export const placeholder = $props.placeholder ?? 'Select an option'
|
|
5
32
|
export const disabled = $bool($props.disabled)
|
|
6
33
|
export const size = $props.size || 'md'
|
|
7
34
|
export const error = $bool($props.error)
|
|
@@ -20,7 +47,14 @@ export const className = $props.className || ''
|
|
|
20
47
|
* is 3.25rem of content inside a declared 3rem box - so py-2.5 lands on the
|
|
21
48
|
* 3rem it always claimed. sm and md were already exact.
|
|
22
49
|
*/
|
|
23
|
-
|
|
50
|
+
/*
|
|
51
|
+
* md -- the size nearly every form uses -- reads the density roles, so an
|
|
52
|
+
* app sets its own field size with --stx-control-px, --stx-control-py,
|
|
53
|
+
* --stx-control-text and --stx-control-leading instead of a className
|
|
54
|
+
* that ties with py-2 on specificity. The fallbacks are px-4 / py-2 /
|
|
55
|
+
* text-base, so nothing moves by default (stacksjs/stx#1993).
|
|
56
|
+
*/
|
|
57
|
+
const sizeClasses = { sm: 'px-3 py-1.5 text-sm', md: 'px-control-x py-control-y text-control', lg: 'px-5 py-2.5 text-lg' }
|
|
24
58
|
const baseClasses = 'block w-full border-0 rounded-control shadow-sm ring-1 ring-inset transition-colors duration-200 focus:ring-2 focus:ring-inset bg-field'
|
|
25
59
|
/*
|
|
26
60
|
* The two halves of the validity state, exported so the CLIENT can swap them
|
|
@@ -81,15 +115,43 @@ const emit = defineEmits()
|
|
|
81
115
|
// Validity is live, for the same reason `disabled` is: a form posted with
|
|
82
116
|
// fetch learns it after the submit, and nothing re-renders (#2047).
|
|
83
117
|
const isError = useReactiveProp('error', {{ error }})
|
|
84
|
-
|
|
85
|
-
|
|
118
|
+
/*
|
|
119
|
+
* Two-way, like TextInput (stacksjs/stx#2059): `v-model:value="group"` keeps
|
|
120
|
+
* the page's signal in step, so a call site no longer pairs `:value` with an
|
|
121
|
+
* `@change` that writes it back. `change` is still emitted for the callers
|
|
122
|
+
* that listen for it.
|
|
123
|
+
*/
|
|
124
|
+
const selectedValue = useModel('value', { default: {{ value }}, parse: v => v == null ? '' : String(v) })
|
|
125
|
+
|
|
126
|
+
function normaliseOptions(list) {
|
|
127
|
+
if (typeof list === 'string') {
|
|
128
|
+
try { list = JSON.parse(list) }
|
|
129
|
+
catch (e) { list = [] }
|
|
130
|
+
}
|
|
131
|
+
if (!Array.isArray(list)) return []
|
|
132
|
+
return list.map(option => option !== null && typeof option === 'object'
|
|
133
|
+
? { value: option.value == null ? '' : String(option.value), label: String(option.label ?? option.value ?? ''), disabled: Boolean(option.disabled) }
|
|
134
|
+
: { value: String(option), label: String(option), disabled: false })
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/*
|
|
138
|
+
* The options are live (stacksjs/stx#2059).
|
|
139
|
+
*
|
|
140
|
+
* They were read once, in the server script, so a list that arrives with the
|
|
141
|
+
* page's data -- an athlete's groups, a coach's plans -- was not there to read
|
|
142
|
+
* and the select rendered its placeholder alone. Now the prop is a signal and
|
|
143
|
+
* the list below is drawn from it on the client, so a bound `:options` renders
|
|
144
|
+
* and re-renders when the parent's list changes.
|
|
145
|
+
*/
|
|
146
|
+
const optionSource = useReactiveProp('options', {{ options }}, { parse: v => normaliseOptions(v) })
|
|
147
|
+
const optionList = derived(() => normaliseOptions(optionSource()))
|
|
86
148
|
|
|
87
149
|
function onChange(event) {
|
|
88
150
|
selectedValue.set(event.target.value)
|
|
89
151
|
emit('change', selectedValue())
|
|
90
152
|
}
|
|
91
153
|
|
|
92
|
-
defineExpose({ value: selectedValue })
|
|
154
|
+
defineExpose({ value: selectedValue, options: optionList })
|
|
93
155
|
</script>
|
|
94
156
|
|
|
95
157
|
<div class="w-full">
|
|
@@ -114,26 +176,45 @@ defineExpose({ value: selectedValue })
|
|
|
114
176
|
@if(required)required@endif
|
|
115
177
|
@change="onChange($event)"
|
|
116
178
|
aria-invalid="{{ error ? 'true' : 'false' }}"
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
aria-describedby="{{
|
|
179
|
+
:aria-invalid="isError() ? 'true' : 'false'"
|
|
180
|
+
:class="{ '{{ stateInvalidClasses }}': isError(), '{{ stateValidClasses }}': !isError() }"
|
|
181
|
+
@if(helperText)aria-describedby="{{ helperId }}"@endif
|
|
120
182
|
>
|
|
121
183
|
@if(placeholder)
|
|
122
184
|
<option value="" disabled>{{ placeholder }}</option>
|
|
123
185
|
@endif
|
|
124
186
|
|
|
187
|
+
{{-- First paint, and the whole list with JavaScript off.
|
|
188
|
+
|
|
189
|
+
Server-rendered from a list known at render time, with `selected` on
|
|
190
|
+
the served choice (defaultSelected: without it a select posted its
|
|
191
|
+
FIRST option no matter what `value` said). On hydration these step
|
|
192
|
+
aside for the live list below, which draws the same options from
|
|
193
|
+
the same prop -- `x-if="false"` removes them in the pass that renders
|
|
194
|
+
it, and `x-no-cloak` keeps them visible until then. `x-if` rather
|
|
195
|
+
than `:if` because @foreach pre-evaluates every `:name="…"` inside
|
|
196
|
+
it as a component prop, which turns `:if` into a dead attribute. --}}
|
|
125
197
|
@foreach(option in options)
|
|
126
198
|
<option
|
|
199
|
+
x-no-cloak
|
|
200
|
+
x-if="false"
|
|
127
201
|
value="{{ option.value }}"
|
|
128
202
|
@if(option.disabled)disabled@endif
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
no matter what `value` said. --}}
|
|
132
|
-
@if(String(option.value) === String(value))selected@endif
|
|
133
|
-
>
|
|
134
|
-
{{ option.label }}
|
|
135
|
-
</option>
|
|
203
|
+
@if(option.value === String(value))selected@endif
|
|
204
|
+
>{{ option.label }}</option>
|
|
136
205
|
@endforeach
|
|
206
|
+
|
|
207
|
+
{{-- The live list. A <template>, so nothing of it shows before the
|
|
208
|
+
runtime expands it -- an <option :for> would paint as an empty
|
|
209
|
+
entry first. The select's own `:value` re-applies itself when its
|
|
210
|
+
options change, so the chosen value is reflected once they arrive. --}}
|
|
211
|
+
<template :for="option in optionList()" :key="option.value">
|
|
212
|
+
<option :value="option.value" :disabled="option.disabled" x-text="option.label"></option>
|
|
213
|
+
</template>
|
|
214
|
+
|
|
215
|
+
{{-- Or the options as children, for a list the page draws itself:
|
|
216
|
+
<Select v-model:value="x"><option :for="o in list" :value="o.id">…</option></Select> --}}
|
|
217
|
+
<slot />
|
|
137
218
|
</select>
|
|
138
219
|
</div>
|
|
139
220
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
/*
|
|
3
|
+
* One tab in a <TabList>. A plain button with the role, its value and the
|
|
4
|
+
* panel it controls; the list keeps `aria-selected` and `tabindex` on it.
|
|
5
|
+
*
|
|
6
|
+
* No client script, so no scope of its own: it is a button in the strip,
|
|
7
|
+
* nothing around it, and `@click` or bound attributes written on it land on
|
|
8
|
+
* the button in the caller's scope. Style the selected state with
|
|
9
|
+
* `aria-selected:` utilities, which outrank the base classes.
|
|
10
|
+
*/
|
|
11
|
+
export const value = $props.value ?? ''
|
|
12
|
+
export const controls = $props.controls || ''
|
|
13
|
+
export const disabled = $bool($props.disabled)
|
|
14
|
+
export const variant = $props.variant || 'line'
|
|
15
|
+
export const className = $props.className || ''
|
|
16
|
+
|
|
17
|
+
const focusRing = 'focus:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-panel'
|
|
18
|
+
const variantClasses = {
|
|
19
|
+
line: `-mb-px px-4 py-2 font-medium text-sm border-b-2 border-transparent text-fg-muted hover:text-fg aria-selected:border-accent aria-selected:text-accent transition-colors ${focusRing}`,
|
|
20
|
+
pills: `px-3 py-1.5 font-medium text-sm rounded-control text-fg-muted hover:text-fg aria-selected:bg-field aria-selected:text-fg aria-selected:shadow-sm transition-colors ${focusRing}`,
|
|
21
|
+
none: '',
|
|
22
|
+
}
|
|
23
|
+
export const tabClasses = `${variantClasses[variant] ?? variantClasses.line} ${disabled ? 'opacity-50 cursor-not-allowed' : ''} ${className}`.replace(/\s+/g, ' ').trim()
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<button
|
|
27
|
+
type="button"
|
|
28
|
+
role="tab"
|
|
29
|
+
data-value="{{ value }}"
|
|
30
|
+
aria-selected="false"
|
|
31
|
+
tabindex="-1"
|
|
32
|
+
@if(controls)aria-controls="{{ controls }}"@endif
|
|
33
|
+
@if(disabled)disabled@endif
|
|
34
|
+
class="{{ tabClasses }}"
|
|
35
|
+
><slot /></button>
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
/*
|
|
3
|
+
* A tab strip whose selection and panels belong to the page (stacksjs/stx#2058).
|
|
4
|
+
*
|
|
5
|
+
* <Tabs> owns both its selection and its panels: it renders every panel inside
|
|
6
|
+
* itself and remembers the tab it was told to start on. That suits a settings
|
|
7
|
+
* card. It does not suit tabs that switch between whole sections of a page,
|
|
8
|
+
* where the selection is kept in the URL or a store and each section is behind
|
|
9
|
+
* its own `:if`, so a section nobody opened never fetches its data.
|
|
10
|
+
*
|
|
11
|
+
* TabList is the behaviour alone. Its children are `role="tab"` elements, a
|
|
12
|
+
* `<Tab>` or any element with `role="tab"` and `data-value` (a `:for` row
|
|
13
|
+
* works), and it gives them what the role promises: one tab stop on the
|
|
14
|
+
* selected tab, the arrow keys, Home and End moving between them and selecting
|
|
15
|
+
* as they go, and `aria-selected` kept true on exactly one. The selection is a
|
|
16
|
+
* `v-model:value`, or `:value` with `@change`.
|
|
17
|
+
*
|
|
18
|
+
* <TabList v-model:value="tab" label="Store sections" variant="line">
|
|
19
|
+
* <Tab value="products" controls="products-panel">Products</Tab>
|
|
20
|
+
* <Tab value="sales" controls="sales-panel">Sales</Tab>
|
|
21
|
+
* </TabList>
|
|
22
|
+
* <section :if="tab === 'products'" id="products-panel" role="tabpanel">…</section>
|
|
23
|
+
*/
|
|
24
|
+
export const value = $props.value ?? ''
|
|
25
|
+
export const label = $props.label || ''
|
|
26
|
+
export const labelledby = $props.labelledby || ''
|
|
27
|
+
export const orientation = $props.orientation === 'vertical' ? 'vertical' : 'horizontal'
|
|
28
|
+
export const variant = $props.variant || 'line'
|
|
29
|
+
export const className = $props.className || ''
|
|
30
|
+
|
|
31
|
+
const variantClasses = {
|
|
32
|
+
line: 'flex gap-1 border-b border-line',
|
|
33
|
+
pills: 'inline-flex gap-1 p-1 bg-surface-raised rounded-panel',
|
|
34
|
+
none: '',
|
|
35
|
+
}
|
|
36
|
+
const directionClasses = orientation === 'vertical' ? 'flex-col' : ''
|
|
37
|
+
export const listClasses = `${variantClasses[variant] ?? variantClasses.line} ${directionClasses} ${className}`.replace(/\s+/g, ' ').trim()
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
<script client>
|
|
41
|
+
const emit = defineEmits()
|
|
42
|
+
const orientation = {{ orientation }}
|
|
43
|
+
const selected = useModel('value', { default: {{ value }}, parse: v => v == null ? '' : String(v) })
|
|
44
|
+
const listRef = useRef('listRef')
|
|
45
|
+
const mounted = state(false)
|
|
46
|
+
let observer = null
|
|
47
|
+
|
|
48
|
+
/** This list's own tabs: not a nested list's, and not a disabled one. */
|
|
49
|
+
function ownTabs(includeDisabled) {
|
|
50
|
+
const root = listRef.value
|
|
51
|
+
if (!root) return []
|
|
52
|
+
return Array.from(root.querySelectorAll('[role="tab"]')).filter(tab =>
|
|
53
|
+
tab.closest('[role="tablist"]') === root
|
|
54
|
+
&& (includeDisabled || (!tab.hasAttribute('disabled') && tab.getAttribute('aria-disabled') !== 'true')))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function valueOf(tab, index) {
|
|
58
|
+
const value = tab.getAttribute('data-value')
|
|
59
|
+
return value == null ? String(index) : value
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/*
|
|
63
|
+
* One stop in the tab order, on the selected tab; the first enabled tab when
|
|
64
|
+
* nothing is selected yet, so the strip can still be reached. Run on every
|
|
65
|
+
* change of selection and of the tabs themselves, because a `:for` adds and
|
|
66
|
+
* removes them after mount.
|
|
67
|
+
*/
|
|
68
|
+
function sync() {
|
|
69
|
+
const tabs = ownTabs(true)
|
|
70
|
+
const current = selected()
|
|
71
|
+
let any = false
|
|
72
|
+
tabs.forEach((tab, index) => {
|
|
73
|
+
const on = valueOf(tab, index) === current
|
|
74
|
+
if (on) any = true
|
|
75
|
+
tab.setAttribute('aria-selected', on ? 'true' : 'false')
|
|
76
|
+
tab.setAttribute('tabindex', on ? '0' : '-1')
|
|
77
|
+
})
|
|
78
|
+
if (!any) {
|
|
79
|
+
const first = ownTabs(false)[0]
|
|
80
|
+
if (first) first.setAttribute('tabindex', '0')
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function select(tab) {
|
|
85
|
+
const index = ownTabs(true).indexOf(tab)
|
|
86
|
+
const value = valueOf(tab, index)
|
|
87
|
+
if (value === selected()) return
|
|
88
|
+
selected.set(value)
|
|
89
|
+
emit('change', value)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function onClick(event) {
|
|
93
|
+
const tab = event.target && event.target.closest ? event.target.closest('[role="tab"]') : null
|
|
94
|
+
if (!tab || !ownTabs(false).includes(tab)) return
|
|
95
|
+
select(tab)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/*
|
|
99
|
+
* The keys the role announces. Selection follows focus (automatic
|
|
100
|
+
* activation), which is what a tab that switches a section of the page wants.
|
|
101
|
+
*/
|
|
102
|
+
function onKey(event) {
|
|
103
|
+
const tabs = ownTabs(false)
|
|
104
|
+
const from = event.target && event.target.closest ? event.target.closest('[role="tab"]') : null
|
|
105
|
+
const index = tabs.indexOf(from)
|
|
106
|
+
if (index === -1) return
|
|
107
|
+
const forward = orientation === 'vertical' ? 'ArrowDown' : 'ArrowRight'
|
|
108
|
+
const back = orientation === 'vertical' ? 'ArrowUp' : 'ArrowLeft'
|
|
109
|
+
let next = -1
|
|
110
|
+
if (event.key === forward) next = (index + 1) % tabs.length
|
|
111
|
+
else if (event.key === back) next = (index - 1 + tabs.length) % tabs.length
|
|
112
|
+
else if (event.key === 'Home') next = 0
|
|
113
|
+
else if (event.key === 'End') next = tabs.length - 1
|
|
114
|
+
if (next === -1) return
|
|
115
|
+
event.preventDefault()
|
|
116
|
+
tabs[next].focus()
|
|
117
|
+
select(tabs[next])
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
effect(() => {
|
|
121
|
+
selected()
|
|
122
|
+
if (mounted()) sync()
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
onMount(() => {
|
|
126
|
+
mounted.set(true)
|
|
127
|
+
sync()
|
|
128
|
+
const root = listRef.value
|
|
129
|
+
if (root && typeof MutationObserver !== 'undefined') {
|
|
130
|
+
observer = new MutationObserver(() => sync())
|
|
131
|
+
observer.observe(root, { childList: true, subtree: true })
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
onDestroy(() => {
|
|
136
|
+
if (observer) observer.disconnect()
|
|
137
|
+
observer = null
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
defineExpose({ selected })
|
|
141
|
+
</script>
|
|
142
|
+
|
|
143
|
+
<div
|
|
144
|
+
x-ref="listRef"
|
|
145
|
+
role="tablist"
|
|
146
|
+
aria-orientation="{{ orientation }}"
|
|
147
|
+
@if(label)aria-label="{{ label }}"@endif
|
|
148
|
+
@if(labelledby)aria-labelledby="{{ labelledby }}"@endif
|
|
149
|
+
class="{{ listClasses }}"
|
|
150
|
+
@click="onClick($event)"
|
|
151
|
+
@keydown="onKey($event)"
|
|
152
|
+
>
|
|
153
|
+
<slot />
|
|
154
|
+
</div>
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
export const className = $props.className || ''
|
|
3
|
+
export const striped = $bool($props.striped)
|
|
4
|
+
export const hoverable = $bool($props.hoverable, true)
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* The table's accessible name (stacksjs/stx#2060).
|
|
8
|
+
*
|
|
9
|
+
* A table is named by its <caption>, and there was no way to give a library
|
|
10
|
+
* table one: no prop, and no slot ahead of <thead>. It is the table's first
|
|
11
|
+
* child, as HTML requires, and visually hidden unless `captionVisible` -- most
|
|
12
|
+
* tables sit under a heading that already says what they are, and a second
|
|
13
|
+
* visible title would repeat it.
|
|
14
|
+
*/
|
|
15
|
+
export const caption = $props.caption || ''
|
|
16
|
+
export const captionVisible = $bool($props.captionVisible)
|
|
17
|
+
export const captionClasses = captionVisible
|
|
18
|
+
? 'caption-top px-cell-x py-2 text-left text-sm font-semibold text-fg'
|
|
19
|
+
: 'sr-only'
|
|
20
|
+
|
|
21
|
+
const baseClasses = 'min-w-full divide-y divide-line'
|
|
22
|
+
export const tableClasses = `${baseClasses} ${className}`.trim()
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<div class="overflow-x-auto">
|
|
26
|
+
<table class="{{ tableClasses }}" @if(caption)data-stx-caption@endif>
|
|
27
|
+
@if(caption)<caption class="{{ captionClasses }}">{{ caption }}</caption>@endif
|
|
28
|
+
<slot :striped="striped" :hoverable="hoverable" />
|
|
29
|
+
</table>
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
<style>
|
|
33
|
+
/*
|
|
34
|
+
* `divide-y` borders every child after the first, and with a caption first
|
|
35
|
+
* that includes the header row: a captioned table grew a rule along its top
|
|
36
|
+
* edge that an uncaptioned one does not have. This takes it back off. The
|
|
37
|
+
* selector has to outrank divide-y's own (0,3,0) -- hence the table's
|
|
38
|
+
* :not([hidden]) -- or which one won would be stylesheet order again.
|
|
39
|
+
*/
|
|
40
|
+
table[data-stx-caption]:not([hidden]) > caption + :not([hidden]) {
|
|
41
|
+
border-top-width: 0;
|
|
42
|
+
}
|
|
43
|
+
</style>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
export const className = $props.className || ''
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Padding and type read the density roles, so an app's tables take its own
|
|
6
|
+
* cell size from --stx-cell-px, --stx-cell-py, --stx-cell-text and
|
|
7
|
+
* --stx-cell-leading -- a className of py-2 ties with py-4 on
|
|
8
|
+
* specificity and wins only by stylesheet order. The fallbacks are px-6 /
|
|
9
|
+
* py-4 / text-sm, so nothing moves by default (stacksjs/stx#1993).
|
|
10
|
+
*/
|
|
11
|
+
const baseClasses = 'px-cell-x py-cell-y whitespace-nowrap text-cell text-fg'
|
|
12
|
+
const cellClasses = `${baseClasses} ${className}`.trim()
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<td class="{{ cellClasses }}">
|
|
16
|
+
<slot />
|
|
17
|
+
</td>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
export const className = $props.className || ''
|
|
3
|
+
export const sortable = $bool($props.sortable)
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Which cells this header heads (stacksjs/stx#2060).
|
|
7
|
+
*
|
|
8
|
+
* A <th> without `scope` leaves a screen reader to guess, and it guesses wrong
|
|
9
|
+
* in any table with row headers. `col` by default, because that is what a
|
|
10
|
+
* header in a <thead> is; `row` for a header at the start of a body row. An
|
|
11
|
+
* attribute written on <TableHeader> never reached the <th>, so there was no
|
|
12
|
+
* way to say it at all.
|
|
13
|
+
*/
|
|
14
|
+
const SCOPES = ['col', 'row', 'colgroup', 'rowgroup']
|
|
15
|
+
export const scope = SCOPES.includes(String($props.scope)) ? String($props.scope) : 'col'
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
* Which way the column is sorted now, as `aria-sort` on the <th> -- the one
|
|
19
|
+
* place a screen reader looks for it. `asc` / `desc`; anything else is
|
|
20
|
+
* unsorted, `none`. Served on every sortable header (and on any header given
|
|
21
|
+
* a `sort`), and live, so a page re-sorting a table moves it without a
|
|
22
|
+
* re-render.
|
|
23
|
+
*/
|
|
24
|
+
const SORTS = { asc: 'ascending', desc: 'descending' }
|
|
25
|
+
export const sortDirection = SORTS[String($props.sort)] ? String($props.sort) : ''
|
|
26
|
+
export const announcesSort = sortable || ($props.sort !== undefined && $props.sort !== null)
|
|
27
|
+
export const servedAriaSort = SORTS[sortDirection] || 'none'
|
|
28
|
+
|
|
29
|
+
const baseClasses = 'px-cell-x py-cell-head-y text-left text-cell-head font-medium text-fg-soft uppercase tracking-wider'
|
|
30
|
+
export const headerClasses = `${baseClasses} ${className}`.trim()
|
|
31
|
+
/*
|
|
32
|
+
* The control that sorts. A <th> is not focusable, so a click handler on it
|
|
33
|
+
* made sorting mouse-only (stacksjs/stx#2060); a <button> inside it is
|
|
34
|
+
* reachable by Tab and operated by Enter and Space, and it is what emits
|
|
35
|
+
* `click`. It takes on the header's type -- the preflight resets a button's
|
|
36
|
+
* text-transform, so `uppercase` is restated -- and draws a ring on keyboard
|
|
37
|
+
* focus only.
|
|
38
|
+
*/
|
|
39
|
+
export const buttonClasses = 'inline-flex items-center gap-1 uppercase tracking-wider text-left rounded-control cursor-pointer hover:text-fg-strong focus:outline-none focus-visible:ring-2 focus-visible:ring-accent'
|
|
40
|
+
</script>
|
|
41
|
+
|
|
42
|
+
<script client>
|
|
43
|
+
const emit = defineEmits()
|
|
44
|
+
const sortState = useReactiveProp('sort', {{ sortDirection }}, { parse: v => v === 'asc' || v === 'desc' ? v : '' })
|
|
45
|
+
const ariaSort = derived(() => sortState() === 'asc' ? 'ascending' : sortState() === 'desc' ? 'descending' : 'none')
|
|
46
|
+
|
|
47
|
+
function onClick() {
|
|
48
|
+
emit('click')
|
|
49
|
+
}
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
<th
|
|
53
|
+
scope="{{ scope }}"
|
|
54
|
+
class="{{ headerClasses }}"
|
|
55
|
+
@if(announcesSort)aria-sort="{{ servedAriaSort }}" :aria-sort="ariaSort()"@endif
|
|
56
|
+
>
|
|
57
|
+
@if(sortable)
|
|
58
|
+
<button type="button" class="{{ buttonClasses }}" @click="onClick()"><slot /></button>
|
|
59
|
+
@else
|
|
60
|
+
<slot />
|
|
61
|
+
@endif
|
|
62
|
+
</th>
|
|
@@ -167,7 +167,10 @@ function onTabKey(event, index) {
|
|
|
167
167
|
else if (event.key === 'End') { event.preventDefault(); next = tabCount - 1 }
|
|
168
168
|
if (next !== index) {
|
|
169
169
|
selectTab(next)
|
|
170
|
-
|
|
170
|
+
// Slot mode numbers its tabs per instance; `tab-N` is the prop mode's id,
|
|
171
|
+
// so arrowing in slot mode moved the selection and left focus behind.
|
|
172
|
+
const id = hasPropTabs ? `tab-${next}` : discoveredTabs()[next]?.tabId
|
|
173
|
+
if (id) document.getElementById(id)?.focus()
|
|
171
174
|
}
|
|
172
175
|
}
|
|
173
176
|
|