@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
package/src/ui/select/README.md
CHANGED
|
@@ -13,27 +13,54 @@ bun add @stacksjs/components
|
|
|
13
13
|
### Basic Select
|
|
14
14
|
|
|
15
15
|
```stx
|
|
16
|
-
<script
|
|
16
|
+
<script client>
|
|
17
17
|
const options = [
|
|
18
18
|
{ value: 'option1', label: 'Option 1' },
|
|
19
19
|
{ value: 'option2', label: 'Option 2' },
|
|
20
20
|
{ value: 'option3', label: 'Option 3' },
|
|
21
21
|
]
|
|
22
|
+
const selected = state('option1')
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<Select v-model:value="selected" :options="options" placeholder="Choose an option" />
|
|
26
|
+
```
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
`v-model:value` keeps the page's signal in step both ways, with `.number` when
|
|
29
|
+
the values are numbers. `@change` is still emitted with the chosen value, for
|
|
30
|
+
code that listens for it.
|
|
24
31
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
### Options from client data
|
|
33
|
+
|
|
34
|
+
`options` is live on the client, so a list that arrives with the page's data
|
|
35
|
+
renders, and re-renders when the list changes. A value bound before its option
|
|
36
|
+
exists is selected once the option arrives.
|
|
37
|
+
|
|
38
|
+
```stx
|
|
39
|
+
<script client>
|
|
40
|
+
const rooms = state([])
|
|
41
|
+
const room = state(null)
|
|
42
|
+
onMount(async () => rooms.set(await (await fetch('/api/rooms')).json()))
|
|
29
43
|
</script>
|
|
30
44
|
|
|
31
|
-
<Select
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
45
|
+
<Select label="Room" v-model:value="room" :options="rooms()" />
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
A static list (known when the page renders) is also served in the HTML, with
|
|
49
|
+
the chosen option `selected`, so first paint is right and the form posts
|
|
50
|
+
correctly with JavaScript off.
|
|
51
|
+
|
|
52
|
+
An option may be a `{ value, label, disabled }` object or a bare string or
|
|
53
|
+
number, which is its own value and label: `:options="['S', 'M', 'L']"`.
|
|
54
|
+
|
|
55
|
+
### Options as children
|
|
56
|
+
|
|
57
|
+
For a list the page draws itself, pass `<option>` children. `placeholder=""`
|
|
58
|
+
drops the placeholder option.
|
|
59
|
+
|
|
60
|
+
```stx
|
|
61
|
+
<Select label="Athlete" v-model:value="athlete" placeholder="">
|
|
62
|
+
<option :for="a in athletes()" :value="a.id">{{ a.name }}</option>
|
|
63
|
+
</Select>
|
|
37
64
|
```
|
|
38
65
|
|
|
39
66
|
### With Label and Helper Text
|
|
@@ -42,8 +69,7 @@ const handleChange = (value) => {
|
|
|
42
69
|
<Select
|
|
43
70
|
label="Country"
|
|
44
71
|
helperText="Select your country of residence"
|
|
45
|
-
:value="country"
|
|
46
|
-
:onChange="(val) => country = val"
|
|
72
|
+
v-model:value="country"
|
|
47
73
|
:options="countries"
|
|
48
74
|
required
|
|
49
75
|
/>
|
|
@@ -54,8 +80,7 @@ const handleChange = (value) => {
|
|
|
54
80
|
```stx
|
|
55
81
|
<Select
|
|
56
82
|
label="Category"
|
|
57
|
-
:value="category"
|
|
58
|
-
:onChange="(val) => category = val"
|
|
83
|
+
v-model:value="category"
|
|
59
84
|
:options="categories"
|
|
60
85
|
error
|
|
61
86
|
helperText="Please select a valid category"
|
|
@@ -100,18 +125,31 @@ const options = [
|
|
|
100
125
|
|
|
101
126
|
| Prop | Type | Default | Description |
|
|
102
127
|
|------|------|---------|-------------|
|
|
103
|
-
| `value` | `string \| number` | `''` | Selected value |
|
|
104
|
-
| `
|
|
105
|
-
| `
|
|
106
|
-
| `placeholder` | `string` | `'Select an option'` | Placeholder text |
|
|
128
|
+
| `value` | `string \| number` | `''` | Selected value; two-way with `v-model:value` |
|
|
129
|
+
| `options` | `Array<SelectOption \| string \| number>` | `[]` | Options; static or bound from client data |
|
|
130
|
+
| `placeholder` | `string` | `'Select an option'` | Placeholder option; `''` renders none |
|
|
107
131
|
| `disabled` | `boolean` | `false` | Disable the select |
|
|
108
|
-
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Select size |
|
|
132
|
+
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Select size; `md` reads the `--stx-control-*` density tokens |
|
|
109
133
|
| `error` | `boolean` | `false` | Show error state |
|
|
110
134
|
| `label` | `string` | `''` | Label text |
|
|
111
135
|
| `helperText` | `string` | `''` | Helper/error text below select |
|
|
112
136
|
| `required` | `boolean` | `false` | Mark as required |
|
|
137
|
+
| `name` | `string` | `''` | Form field name |
|
|
113
138
|
| `className` | `string` | `''` | Additional CSS classes |
|
|
114
139
|
|
|
140
|
+
## Events
|
|
141
|
+
|
|
142
|
+
| Event | Payload | When |
|
|
143
|
+
|-------|---------|------|
|
|
144
|
+
| `update:value` | the new value | A choice is made (drives `v-model:value`) |
|
|
145
|
+
| `change` | the new value | A choice is made |
|
|
146
|
+
|
|
147
|
+
## Slots
|
|
148
|
+
|
|
149
|
+
| Slot | Description |
|
|
150
|
+
|------|-------------|
|
|
151
|
+
| default | `<option>` children, rendered after any `options` |
|
|
152
|
+
|
|
115
153
|
## SelectOption Interface
|
|
116
154
|
|
|
117
155
|
```typescript
|
package/src/ui/select/Select.stx
CHANGED
|
@@ -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
|
|
package/src/ui/select/index.ts
CHANGED
|
@@ -7,9 +7,16 @@ export interface SelectOption {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export interface SelectProps {
|
|
10
|
+
/** Two-way with `v-model:value`; `change` is still emitted. */
|
|
10
11
|
value?: string | number
|
|
11
12
|
onChange?: (value: string | number) => void
|
|
12
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Static or bound from client data; re-renders when the list changes. A
|
|
15
|
+
* bare string or number is its own value and label. Options may also be
|
|
16
|
+
* passed as `<option>` children.
|
|
17
|
+
*/
|
|
18
|
+
options?: Array<SelectOption | string | number>
|
|
19
|
+
/** `''` renders no placeholder option. */
|
|
13
20
|
placeholder?: string
|
|
14
21
|
disabled?: boolean
|
|
15
22
|
size?: 'sm' | 'md' | 'lg'
|
package/src/ui/table/README.md
CHANGED
|
@@ -30,10 +30,78 @@ bun add @stacksjs/components
|
|
|
30
30
|
</Table>
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
+
### A caption
|
|
34
|
+
|
|
35
|
+
A table's accessible name comes from its `<caption>`. `caption` renders one as
|
|
36
|
+
the table's first child, visually hidden by default; `captionVisible` shows it.
|
|
37
|
+
|
|
38
|
+
```stx
|
|
39
|
+
<Table caption="Recent workouts">…</Table>
|
|
40
|
+
<Table caption="Recent workouts" captionVisible>…</Table>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Sortable columns
|
|
44
|
+
|
|
45
|
+
A sortable header renders its label inside a `<button type="button">`, so it is
|
|
46
|
+
reachable by Tab and operated by Enter or Space, and the button emits `click`.
|
|
47
|
+
`sort` (`'asc'`, `'desc'` or `''`) sets `aria-sort` on the `<th>`
|
|
48
|
+
(`ascending`, `descending`, `none`) and follows the page when bound.
|
|
49
|
+
|
|
50
|
+
```stx
|
|
51
|
+
<script client>
|
|
52
|
+
const direction = state('asc')
|
|
53
|
+
function flip() { direction.set(direction() === 'asc' ? 'desc' : 'asc') }
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<TableHeader sortable :sort="direction()" @click="flip()">Date</TableHeader>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Row headers
|
|
60
|
+
|
|
61
|
+
Every `TableHeader` carries `scope`, `col` by default. Use `scope="row"` for a
|
|
62
|
+
header at the start of a body row.
|
|
63
|
+
|
|
64
|
+
```stx
|
|
65
|
+
<TableRow>
|
|
66
|
+
<TableHeader scope="row">Monday</TableHeader>
|
|
67
|
+
<TableCell>Easy run</TableCell>
|
|
68
|
+
</TableRow>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Density
|
|
72
|
+
|
|
73
|
+
Cells read the density tokens, so an app sets its table spacing without a
|
|
74
|
+
`className`: `--stx-cell-px` (both rows), `--stx-cell-py`, `--stx-cell-text`,
|
|
75
|
+
`--stx-cell-leading`, and `--stx-cell-head-py`, `--stx-cell-head-text`,
|
|
76
|
+
`--stx-cell-head-leading` for the header row. See
|
|
77
|
+
[theming](../../../../../docs/features/theming.md).
|
|
78
|
+
|
|
79
|
+
## Props
|
|
80
|
+
|
|
81
|
+
### Table
|
|
82
|
+
|
|
83
|
+
| Prop | Type | Default | Description |
|
|
84
|
+
|------|------|---------|-------------|
|
|
85
|
+
| `caption` | `string` | `''` | The table's accessible name, rendered as `<caption>` |
|
|
86
|
+
| `captionVisible` | `boolean` | `false` | Show the caption instead of hiding it visually |
|
|
87
|
+
| `striped` | `boolean` | `false` | Passed to the slot |
|
|
88
|
+
| `hoverable` | `boolean` | `true` | Passed to the slot |
|
|
89
|
+
| `className` | `string` | `''` | Extra classes on the `<table>` |
|
|
90
|
+
|
|
91
|
+
### TableHeader
|
|
92
|
+
|
|
93
|
+
| Prop | Type | Default | Description |
|
|
94
|
+
|------|------|---------|-------------|
|
|
95
|
+
| `scope` | `'col' \| 'row' \| 'colgroup' \| 'rowgroup'` | `'col'` | Which cells the header heads |
|
|
96
|
+
| `sortable` | `boolean` | `false` | Render the label as a sorting button that emits `click` |
|
|
97
|
+
| `sort` | `'asc' \| 'desc' \| ''` | `''` | Current direction, as `aria-sort` |
|
|
98
|
+
| `className` | `string` | `''` | Extra classes on the `<th>` |
|
|
99
|
+
|
|
33
100
|
## Features
|
|
34
101
|
|
|
35
102
|
- Responsive design
|
|
36
|
-
- Sortable columns
|
|
103
|
+
- Sortable columns, operable from the keyboard, with `aria-sort`
|
|
104
|
+
- Captions and header `scope` for screen readers
|
|
37
105
|
- Striped rows
|
|
38
106
|
- Hover effects
|
|
39
107
|
- Dark mode support
|
package/src/ui/table/Table.stx
CHANGED
|
@@ -3,12 +3,41 @@ export const className = $props.className || ''
|
|
|
3
3
|
export const striped = $bool($props.striped)
|
|
4
4
|
export const hoverable = $bool($props.hoverable, true)
|
|
5
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
|
+
|
|
6
21
|
const baseClasses = 'min-w-full divide-y divide-line'
|
|
7
|
-
const tableClasses = `${baseClasses} ${className}`.trim()
|
|
22
|
+
export const tableClasses = `${baseClasses} ${className}`.trim()
|
|
8
23
|
</script>
|
|
9
24
|
|
|
10
25
|
<div class="overflow-x-auto">
|
|
11
|
-
<table class="{{ tableClasses }}">
|
|
26
|
+
<table class="{{ tableClasses }}" @if(caption)data-stx-caption@endif>
|
|
27
|
+
@if(caption)<caption class="{{ captionClasses }}">{{ caption }}</caption>@endif
|
|
12
28
|
<slot :striped="striped" :hoverable="hoverable" />
|
|
13
29
|
</table>
|
|
14
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>
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
<script server>
|
|
2
2
|
export const className = $props.className || ''
|
|
3
3
|
|
|
4
|
-
|
|
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'
|
|
5
12
|
const cellClasses = `${baseClasses} ${className}`.trim()
|
|
6
13
|
</script>
|
|
7
14
|
|
|
@@ -2,19 +2,61 @@
|
|
|
2
2
|
export const className = $props.className || ''
|
|
3
3
|
export const sortable = $bool($props.sortable)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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'
|
|
8
40
|
</script>
|
|
9
41
|
|
|
10
42
|
<script client>
|
|
11
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')
|
|
12
46
|
|
|
13
47
|
function onClick() {
|
|
14
48
|
emit('click')
|
|
15
49
|
}
|
|
16
50
|
</script>
|
|
17
51
|
|
|
18
|
-
<th
|
|
19
|
-
|
|
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
|
|
20
62
|
</th>
|
package/src/ui/table/index.ts
CHANGED
|
@@ -6,6 +6,10 @@ export { default as TableHeader } from './TableHeader.stx'
|
|
|
6
6
|
export { default as TableRow } from './TableRow.stx'
|
|
7
7
|
|
|
8
8
|
export interface TableProps {
|
|
9
|
+
/** The table's accessible name, rendered as its first child `<caption>`. */
|
|
10
|
+
caption?: string
|
|
11
|
+
/** Show the caption; it is visually hidden (`sr-only`) by default. */
|
|
12
|
+
captionVisible?: boolean
|
|
9
13
|
className?: string
|
|
10
14
|
striped?: boolean
|
|
11
15
|
hoverable?: boolean
|
|
@@ -26,7 +30,12 @@ export interface TableRowProps {
|
|
|
26
30
|
|
|
27
31
|
export interface TableHeaderProps {
|
|
28
32
|
className?: string
|
|
33
|
+
/** Which cells this header heads; `col` by default. */
|
|
34
|
+
scope?: 'col' | 'row' | 'colgroup' | 'rowgroup'
|
|
35
|
+
/** Render the label as a `<button>` that emits `click`. */
|
|
29
36
|
sortable?: boolean
|
|
37
|
+
/** The current direction, set as `aria-sort` on the `<th>`. */
|
|
38
|
+
sort?: 'asc' | 'desc' | ''
|
|
30
39
|
onClick?: () => void
|
|
31
40
|
}
|
|
32
41
|
|
|
@@ -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>
|