@stacksjs/components 0.2.466 → 0.2.468

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/{EmailInput-ffzwvp9r.stx → EmailInput-57626391.stx} +3 -0
  2. package/dist/{NumberInput-r2wsh8w9.stx → NumberInput-pq4k2k4k.stx} +31 -10
  3. package/dist/{PasswordInput-36bws0kf.stx → PasswordInput-dg41ktgd.stx} +8 -1
  4. package/dist/{Select-pv44ngbe.stx → Select-61zv5tr7.stx} +98 -17
  5. package/dist/Table-0a4y46vd.stx +43 -0
  6. package/dist/TableCell-jhvsxw8b.stx +17 -0
  7. package/dist/TableHeader-krzcx4sv.stx +62 -0
  8. package/dist/{TextInput-h9trj9f9.stx → TextInput-nknmz9md.stx} +57 -4
  9. package/dist/{Textarea-k8cjs4zs.stx → Textarea-9xmhjqp8.stx} +30 -7
  10. package/dist/index.js +5 -5
  11. package/dist/overlays/overlays.js +22 -0
  12. package/dist/ui/input/index.d.ts +18 -5
  13. package/dist/ui/select/index.d.ts +1 -1
  14. package/dist/ui/table/index.d.ts +4 -0
  15. package/dist/utils/overlays.d.ts +5 -1
  16. package/package.json +8 -3
  17. package/src/ui/input/EmailInput.stx +3 -0
  18. package/src/ui/input/NumberInput.stx +31 -10
  19. package/src/ui/input/PasswordInput.stx +8 -1
  20. package/src/ui/input/TextInput.stx +57 -4
  21. package/src/ui/input/index.ts +31 -5
  22. package/src/ui/select/README.md +59 -21
  23. package/src/ui/select/Select.stx +98 -17
  24. package/src/ui/select/index.ts +8 -1
  25. package/src/ui/table/README.md +69 -1
  26. package/src/ui/table/Table.stx +31 -2
  27. package/src/ui/table/TableCell.stx +8 -1
  28. package/src/ui/table/TableHeader.stx +47 -5
  29. package/src/ui/table/index.ts +9 -0
  30. package/src/ui/textarea/Textarea.stx +30 -7
  31. package/src/utils/overlays.ts +5 -1
  32. package/dist/Table-3ek3w95h.stx +0 -14
  33. package/dist/TableCell-8swtcq8s.stx +0 -10
  34. 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
- export const min = $props.min
29
- export const max = $props.max
30
- export const step = $props.step || 1
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
- const sizeClasses = { sm: 'px-3 py-1.5 text-sm', md: 'px-4 py-2 text-base', lg: 'px-5 py-2.5 text-lg' }
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
- // Reactive prop binding — parent can drive value via signals. See #1704.
89
- const inputValue = useReactiveProp('value', {{ value }}, { parse: (v) => v == null || v === '' ? '' : String(v) })
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.set(event.target.value)
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', String(next))
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', String(next))
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
- const sizeClasses = { sm: 'px-3 py-1.5 text-sm', md: 'px-4 py-2 text-base', lg: 'px-5 py-2.5 text-lg' }
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'
@@ -11,7 +11,7 @@ export const required = $bool($props.required)
11
11
  export const leftIcon = $props.leftIcon || ''
12
12
  export const rightIcon = $props.rightIcon || ''
13
13
  export const clearable = $bool($props.clearable)
14
- export const maxLength = $props.maxLength
14
+ export const maxLength = $props.maxLength ?? $props.maxlength
15
15
  export const showCount = $bool($props.showCount)
16
16
  export const className = $props.className || ''
17
17
  export const inputClassName = $props.inputClassName || ''
@@ -26,7 +26,14 @@ export const inputClassName = $props.inputClassName || ''
26
26
  * is 3.25rem of content inside a declared 3rem box - so py-2.5 lands on the
27
27
  * 3rem it always claimed. sm and md were already exact.
28
28
  */
29
- const sizeClasses = { sm: 'px-3 py-1.5 text-sm', md: 'px-4 py-2 text-base', lg: 'px-5 py-2.5 text-lg' }
29
+ /*
30
+ * md -- the size nearly every form uses -- reads the density roles, so an
31
+ * app sets its own field size with --stx-control-px, --stx-control-py,
32
+ * --stx-control-text and --stx-control-leading instead of a className
33
+ * that ties with py-2 on specificity. The fallbacks are px-4 / py-2 /
34
+ * text-base, so nothing moves by default (stacksjs/stx#1993).
35
+ */
36
+ 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' }
30
37
  const iconSizeClasses = { sm: 'h-4 w-4', md: 'h-5 w-5', lg: 'h-6 w-6' }
31
38
 
32
39
  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'
@@ -98,6 +105,44 @@ export const autocomplete = $props.autocomplete || $props.autoComplete || ''
98
105
  export const form = $props.form || ''
99
106
  export const inputMode = $props.inputMode || $props.inputmode || ''
100
107
  export const enterKeyHint = $props.enterKeyHint || $props.enterkeyhint || ''
108
+
109
+ /*
110
+ * The text-like input types (stacksjs/stx#2059).
111
+ *
112
+ * `type` was hard-coded to text, so a form with a date, a time, a URL or a
113
+ * phone number in it had to mix library and native controls -- the
114
+ * inconsistency the library exists to remove. email, number and password have
115
+ * components of their own but are accepted here too. Anything else (checkbox,
116
+ * file, range...) is not a text field, and is served as text rather than as a
117
+ * control this markup was not drawn for.
118
+ */
119
+ const TEXT_TYPES = ['text', 'date', 'time', 'datetime-local', 'month', 'week', 'url', 'tel', 'color', 'search', 'email', 'number', 'password']
120
+ export const type = TEXT_TYPES.includes(String($props.type)) ? String($props.type) : 'text'
121
+
122
+ /*
123
+ * The constraint attributes, each emitted only when given AND only on the
124
+ * types HTML applies it to, so a control renders exactly the markup it did
125
+ * before unless the caller asked for something it can use: min/max/step
126
+ * bound the date, time and number types; pattern, minlength, maxlength and
127
+ * size the free-text ones; list every type but password.
128
+ */
129
+ const RANGED_TYPES = ['date', 'time', 'datetime-local', 'month', 'week', 'number']
130
+ const FREE_TEXT_TYPES = ['text', 'search', 'url', 'tel', 'email', 'password']
131
+ const given = (v) => v !== undefined && v !== null && v !== '' && v !== false
132
+ const ranged = RANGED_TYPES.includes(type)
133
+ export const freeText = FREE_TEXT_TYPES.includes(type)
134
+ export const min = ranged && given($props.min) ? String($props.min) : ''
135
+ export const max = ranged && given($props.max) ? String($props.max) : ''
136
+ export const step = ranged && given($props.step) ? String($props.step) : ''
137
+ export const pattern = freeText && given($props.pattern) ? String($props.pattern) : ''
138
+ export const minLength = freeText && given($props.minLength ?? $props.minlength) ? String($props.minLength ?? $props.minlength) : ''
139
+ /*
140
+ * The HTML `size` attribute (a width in characters). Not `size`, which is
141
+ * already this component's sm / md / lg.
142
+ */
143
+ export const htmlSize = freeText && given($props.htmlSize) ? String($props.htmlSize) : ''
144
+ export const list = type !== 'password' && given($props.list) ? String($props.list) : ''
145
+ export const autofocus = $bool($props.autofocus)
101
146
  </script>
102
147
 
103
148
  <script client>
@@ -153,7 +198,15 @@ defineExpose({ value: inputValue, clear: onClear })
153
198
  @if(form)form="{{ form }}"@endif
154
199
  @if(inputMode)inputmode="{{ inputMode }}"@endif
155
200
  @if(enterKeyHint)enterkeyhint="{{ enterKeyHint }}"@endif
156
- type="text"
201
+ type="{{ type }}"
202
+ @if(min)min="{{ min }}"@endif
203
+ @if(max)max="{{ max }}"@endif
204
+ @if(step)step="{{ step }}"@endif
205
+ @if(pattern)pattern="{{ pattern }}"@endif
206
+ @if(minLength)minlength="{{ minLength }}"@endif
207
+ @if(htmlSize)size="{{ htmlSize }}"@endif
208
+ @if(list)list="{{ list }}"@endif
209
+ @if(autofocus)autofocus@endif
157
210
  id="{{ inputId }}"
158
211
  class="{{ inputClasses }}"
159
212
 
@@ -173,7 +226,7 @@ defineExpose({ value: inputValue, clear: onClear })
173
226
  @if(disabled)disabled@endif
174
227
  @if(readonly)readonly@endif
175
228
  @if(required)required@endif
176
- @if(maxLength)maxlength="{{ maxLength }}"@endif
229
+ @if(maxLength && freeText)maxlength="{{ maxLength }}"@endif
177
230
  @input="onInput($event)"
178
231
  @change="onChange($event)"
179
232
  aria-invalid="{{ error ? 'true' : 'false' }}"
@@ -22,12 +22,32 @@ export interface BaseInputProps {
22
22
  inputClassName?: string
23
23
  }
24
24
 
25
+ /** The text-like input types `TextInput` renders; anything else is served as `text`. */
26
+ export type TextInputType = 'text' | 'date' | 'time' | 'datetime-local' | 'month' | 'week' | 'url' | 'tel' | 'color' | 'search' | 'email' | 'number' | 'password'
27
+
25
28
  export interface TextInputProps extends BaseInputProps {
29
+ type?: TextInputType
26
30
  leftIcon?: string
27
31
  rightIcon?: string
28
32
  clearable?: boolean
29
33
  maxLength?: number
34
+ maxlength?: number
35
+ minLength?: number
36
+ minlength?: number
30
37
  showCount?: boolean
38
+ /** date, time, datetime-local, month, week and number only. */
39
+ min?: string | number
40
+ /** date, time, datetime-local, month, week and number only. */
41
+ max?: string | number
42
+ /** date, time, datetime-local, month, week and number only; `'any'` allowed. */
43
+ step?: string | number
44
+ /** text, search, url, tel, email and password only. */
45
+ pattern?: string
46
+ /** The id of a `<datalist>`; every type but password. */
47
+ list?: string
48
+ /** The HTML `size` attribute (width in characters); `size` is sm / md / lg. */
49
+ htmlSize?: number
50
+ autofocus?: boolean
31
51
  }
32
52
 
33
53
  export interface EmailInputProps extends Omit<BaseInputProps, 'placeholder'> {
@@ -91,6 +111,12 @@ export const textInputPropSchema: PropSchema = {
91
111
  clearable: PropTypes.boolean,
92
112
  maxLength: PropTypes.number,
93
113
  showCount: PropTypes.boolean,
114
+ type: PropTypes.oneOf(['text', 'date', 'time', 'datetime-local', 'month', 'week', 'url', 'tel', 'color', 'search', 'email', 'number', 'password']),
115
+ pattern: PropTypes.string,
116
+ list: PropTypes.string,
117
+ minLength: PropTypes.number,
118
+ htmlSize: PropTypes.number,
119
+ autofocus: PropTypes.boolean,
94
120
  }
95
121
 
96
122
  /**
@@ -157,24 +183,24 @@ const searchInputPropSchema: PropSchema = {
157
183
  /**
158
184
  * Validate TextInput component props
159
185
  */
160
- export const validateTextInputProps: (props: Record<string, any>) => ValidationResult = createPropValidator('TextInput', textInputPropSchema)
186
+ export const validateTextInputProps: (props: Record<string, unknown>) => ValidationResult = createPropValidator('TextInput', textInputPropSchema)
161
187
 
162
188
  /**
163
189
  * Validate EmailInput component props
164
190
  */
165
- export const validateEmailInputProps: (props: Record<string, any>) => ValidationResult = createPropValidator('EmailInput', baseInputPropSchema)
191
+ export const validateEmailInputProps: (props: Record<string, unknown>) => ValidationResult = createPropValidator('EmailInput', baseInputPropSchema)
166
192
 
167
193
  /**
168
194
  * Validate PasswordInput component props
169
195
  */
170
- export const validatePasswordInputProps: (props: Record<string, any>) => ValidationResult = createPropValidator('PasswordInput', passwordInputPropSchema)
196
+ export const validatePasswordInputProps: (props: Record<string, unknown>) => ValidationResult = createPropValidator('PasswordInput', passwordInputPropSchema)
171
197
 
172
198
  /**
173
199
  * Validate NumberInput component props
174
200
  */
175
- export const validateNumberInputProps: (props: Record<string, any>) => ValidationResult = createPropValidator('NumberInput', numberInputPropSchema)
201
+ export const validateNumberInputProps: (props: Record<string, unknown>) => ValidationResult = createPropValidator('NumberInput', numberInputPropSchema)
176
202
 
177
203
  /**
178
204
  * Validate SearchInput component props
179
205
  */
180
- export const validateSearchInputProps: (props: Record<string, any>) => ValidationResult = createPropValidator('SearchInput', searchInputPropSchema)
206
+ export const validateSearchInputProps: (props: Record<string, unknown>) => ValidationResult = createPropValidator('SearchInput', searchInputPropSchema)
@@ -13,27 +13,54 @@ bun add @stacksjs/components
13
13
  ### Basic Select
14
14
 
15
15
  ```stx
16
- <script server>
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
- const selected = 'option1'
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
- const handleChange = (value) => {
26
- console.log('Selected:', value)
27
- selected = value
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
- :value="selected"
33
- :onChange="handleChange"
34
- :options="options"
35
- placeholder="Choose an option"
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
- | `onChange` | `function` | - | Callback when selection changes |
105
- | `options` | `SelectOption[]` | `[]` | Array of options |
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
@@ -1,7 +1,34 @@
1
1
  <script server>
2
- export const value = $props.value || ''
3
- export const options = $props.options || []
4
- export const placeholder = $props.placeholder || 'Select an option'
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
- const sizeClasses = { sm: 'px-3 py-1.5 text-sm', md: 'px-4 py-2 text-base', lg: 'px-5 py-2.5 text-lg' }
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
- // Reactive prop binding — parent can drive `value` via signals. See #1704.
85
- const selectedValue = useReactiveProp('value', {{ value }}, { parse: (v) => v == null ? '' : String(v) })
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
- :aria-invalid="isError() ? 'true' : 'false'"
118
- :class="{ '{{ stateInvalidClasses }}': isError(), '{{ stateValidClasses }}': !isError() }"
119
- aria-describedby="{{ helperText ? helperId : '' }}"
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
- {{-- Which option is served as the choice. `selected` is
130
- defaultSelected, so without it a select posted its FIRST option
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
 
@@ -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
- options?: SelectOption[]
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'
@@ -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