@stacksjs/components 0.2.583 → 0.2.585

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 (32) hide show
  1. package/dist/{Button-bpjmfkq6.stx → Button-55ahz0j6.stx} +5 -1
  2. package/dist/{Checkbox-kepzk3h5.stx → Checkbox-etjvbbxq.stx} +8 -13
  3. package/dist/{Dialog-pdq8rxvy.stx → Dialog-v79kqbbz.stx} +23 -6
  4. package/dist/{Drawer-nmq100vy.stx → Drawer-4rqfcg4f.stx} +10 -2
  5. package/dist/SegmentedControl-z2p70zps.stx +124 -0
  6. package/dist/{Switch-acwd7n07.stx → Switch-j1jf0mtn.stx} +9 -5
  7. package/dist/{TabList-76bpx5fp.stx → TabList-5xwk2zmz.stx} +26 -17
  8. package/dist/{Tabs-1swcxmfb.stx → Tabs-bp03efe6.stx} +69 -38
  9. package/dist/{Tooltip-rg1c2zrc.stx → Tooltip-4fv809dm.stx} +23 -11
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.js +7 -7
  12. package/dist/ui/button/index.d.ts +2 -0
  13. package/dist/ui/checkbox/Checkbox.d.ts +1 -1
  14. package/dist/ui/segmented-control/SegmentedControl.d.ts +70 -0
  15. package/dist/ui/segmented-control/index.d.ts +23 -0
  16. package/dist/ui/switch/Switch.d.ts +2 -2
  17. package/dist/ui/tabs/Tabs.d.ts +1 -1
  18. package/dist/utils/tooltip-position.d.ts +16 -0
  19. package/package.json +3 -3
  20. package/src/index.ts +1 -0
  21. package/src/ui/button/Button.stx +5 -1
  22. package/src/ui/button/index.ts +5 -0
  23. package/src/ui/checkbox/Checkbox.stx +8 -13
  24. package/src/ui/dialog/Dialog.stx +23 -6
  25. package/src/ui/drawer/Drawer.stx +10 -2
  26. package/src/ui/segmented-control/SegmentedControl.stx +124 -0
  27. package/src/ui/segmented-control/index.ts +29 -0
  28. package/src/ui/switch/Switch.stx +9 -5
  29. package/src/ui/tabs/TabList.stx +26 -17
  30. package/src/ui/tabs/Tabs.stx +69 -38
  31. package/src/ui/tooltip/Tooltip.stx +23 -11
  32. package/src/utils/tooltip-position.ts +40 -0
@@ -0,0 +1,70 @@
1
+ import type { DefineComponent } from '@stacksjs/stx';
2
+ declare const __dtsx_component__: __DtsxStxComponentType;
3
+ /*
4
+ * A segmented control: a few mutually exclusive choices in one track, the
5
+ * chosen one raised. The control iOS draws as UISegmentedControl and a phone
6
+ * app reaches for whenever a list has two to five ways to be shown.
7
+ *
8
+ * It is what applications kept drawing for themselves. One app copied the
9
+ * same track-and-segment markup into ten native screens and a stylesheet; the
10
+ * other drew seven by hand on the web, each with its own roles, and on native
11
+ * had none at all - a "Change the filter" button that cycled through the
12
+ * choices one tap at a time, so nobody could see what the others were.
13
+ *
14
+ * The same source renders on both targets. Each segment is a button with
15
+ * `aria-pressed`, inside a labelled group, which is what a toggle set is
16
+ * (a radio group would promise arrow-key roving the segments do not need,
17
+ * and tabs would promise panels). On a phone the host announces the chosen
18
+ * segment as selected, from the same attribute.
19
+ *
20
+ * <SegmentedControl v-model:value="period" label="Period" stretch
21
+ * :options="[{ value: 'week', label: 'This week' }, { value: 'all', label: 'All time' }]" />
22
+ *
23
+ * `options` takes strings or `{ value, label, disabled? }`; it is reactive,
24
+ * so a label carrying a count can change in place. The selection is a
25
+ * `v-model:value`, or `:value` with `@change`, which receives the new value.
26
+ * `testID` names the group, and each segment is `<testID>-<value>`.
27
+ *
28
+ * Colour and shape come from roles, so an app restyles it without touching
29
+ * it: the track is `surface-sunken`, the chosen segment `panel` (one step
30
+ * lighter than the track in dark mode, as iOS draws it), the words `fg` and
31
+ * `fg-muted`, and both corners `rounded-control` - a pill-shaped app gets a
32
+ * pill track by setting that one role.
33
+ */
34
+ export declare const value: unknown;
35
+ export declare const options: unknown;
36
+ export declare const label: unknown;
37
+ export declare const stretch: ReturnType<typeof $bool>;
38
+ export declare const disabled: ReturnType<typeof $bool>;
39
+ export declare const size: boolean;
40
+ export declare const testID: unknown;
41
+ export declare const className: unknown;
42
+ /**
43
+ * @defaultValue
44
+ * ```ts
45
+ * {
46
+ * sm: { segment: 'min-h-[28px] px-2.5 py-1', text: 'text-[13px]' },
47
+ * md: { segment: 'min-h-[32px] px-3 py-1.5', text: 'text-[13px]' }
48
+ * }
49
+ * ```
50
+ */
51
+ declare const sizeClasses: {
52
+ sm: {
53
+ /** @defaultValue 'min-h-[28px] px-2.5 py-1' */
54
+ segment: string;
55
+ /** @defaultValue 'text-[13px]' */
56
+ text: string
57
+ };
58
+ md: {
59
+ /** @defaultValue 'min-h-[32px] px-3 py-1.5' */
60
+ segment: string;
61
+ /** @defaultValue 'text-[13px]' */
62
+ text: string
63
+ }
64
+ };
65
+ export declare const sizing: unknown;
66
+ export declare const trackClasses: unknown;
67
+ export declare const segmentBase: unknown;
68
+ declare const selected: ReturnType<typeof useModel>;
69
+ declare type __DtsxStxComponentType = DefineComponent<{ value?: string, options?: unknown[], label?: string, stretch?: unknown, disabled?: unknown, size?: unknown, testID?: string, className?: string }> & { readonly __stxEmits?: ["change"], readonly __stxExposed?: { selected: typeof selected } }
70
+ export default __dtsx_component__;
@@ -0,0 +1,23 @@
1
+ /** One choice. A bare string is its own value and label. */
2
+ export declare interface SegmentedControlOption {
3
+ value: string
4
+ label?: string
5
+ disabled?: boolean
6
+ }
7
+ /**
8
+ * A few mutually exclusive choices in one track, the chosen one raised. The
9
+ * same source renders on the web and on a native host; on a phone the chosen
10
+ * segment is announced as selected.
11
+ */
12
+ export declare interface SegmentedControlProps {
13
+ value?: string
14
+ options: Array<string | SegmentedControlOption>
15
+ label?: string
16
+ stretch?: boolean
17
+ disabled?: boolean
18
+ size?: 'sm' | 'md'
19
+ testID?: string
20
+ className?: string
21
+ onChange?: (value: string) => void
22
+ }
23
+ export { default as SegmentedControl } from './SegmentedControl';
@@ -67,9 +67,9 @@ declare const sizeConfig: {
67
67
  // a SyntaxError that killed the whole component rather than mis-sizing it.
68
68
  export declare const config: unknown;
69
69
  export declare const trackBaseClasses: 'relative inline-flex shrink-0 cursor-pointer border-2 border-transparent rounded-full transition-colors duration-200 ease-in-out focus:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-panel';
70
- export declare const trackDisabledClasses: unknown;
70
+ export declare const trackDisabledClasses: 'opacity-50 cursor-not-allowed pointer-events-none';
71
71
  export declare const thumbBaseClasses: 'pointer-events-none inline-block transform rounded-full bg-white shadow-lg ring-0 transition duration-200 ease-in-out';
72
72
  // v-model:checked adds two-way binding; keep @change for existing callers.
73
73
  declare const isChecked: unknown;
74
- declare type __DtsxStxComponentType = DefineComponent<{ checked?: unknown, name?: string, form?: string, checkedValue?: string, uncheckedValue?: string, disabled?: unknown, size?: string, className?: string, label?: string }> & { readonly __stxExposed?: { isChecked: typeof isChecked, toggle: typeof toggle } }
74
+ declare type __DtsxStxComponentType = DefineComponent<{ checked?: unknown, name?: string, form?: string, checkedValue?: string, uncheckedValue?: string, disabled?: unknown, size?: string, className?: string, label?: string }> & { readonly __stxEmits?: ["change"], readonly __stxExposed?: { isChecked: typeof isChecked, toggle: typeof toggle } }
75
75
  export default __dtsx_component__;
@@ -23,7 +23,7 @@ export declare const containerClasses: unknown;
23
23
  export declare const tabListClasses: unknown;
24
24
  export declare const tabPanelClasses: unknown;
25
25
  // True when the parent passes a non-empty `tabs` array — switches to the
26
- // legacy render path so existing snapshots stay byte-identical.
26
+ // legacy render path, preserving the existing prop-array API.
27
27
  export declare const hasPropTabs: boolean;
28
28
  declare const activeTab: unknown;
29
29
  declare type __DtsxStxComponentType = DefineComponent<{ tabs?: unknown[], defaultTab?: unknown, orientation?: string, variant?: string, className?: string }> & { readonly __stxExposed?: { activeTab: typeof activeTab, selectTab: typeof selectTab } }
@@ -1,3 +1,5 @@
1
+ /** Read real host bounds, returning coordinates relative to the panel's positioning parent. */
2
+ export declare function measureTooltipPosition(trigger: HTMLElement | NativeTooltipView, panel: HTMLElement | NativeTooltipView, preferred?: TooltipSide): Promise<unknown>;
1
3
  /** Position in viewport coordinates; flip when possible, then keep every edge readable. */
2
4
  export declare function tooltipPosition(anchor: TooltipAnchor, panel: { width: number, height: number }, viewport: { width: number, height: number }, preferred?: TooltipSide): {
3
5
  left: unknown;
@@ -7,4 +9,18 @@ export declare function tooltipPosition(anchor: TooltipAnchor, panel: { width: n
7
9
  arrowY: unknown
8
10
  };
9
11
  export declare interface TooltipAnchor { left: number, top: number, right: number, bottom: number }
12
+ declare interface NativeTooltipBounds {
13
+ x: number
14
+ y: number
15
+ width: number
16
+ height: number
17
+ viewportWidth: number
18
+ viewportHeight: number
19
+ unit: 'points' | 'dp'
20
+ coordinateSpace: 'screenViewport'
21
+ }
22
+ declare interface NativeTooltipView {
23
+ measure: () => Promise<NativeTooltipBounds>
24
+ parentElement?: NativeTooltipView | null
25
+ }
10
26
  export type TooltipSide = 'top' | 'bottom' | 'left' | 'right';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/components",
3
3
  "type": "module",
4
- "version": "0.2.583",
4
+ "version": "0.2.585",
5
5
  "description": "A modern component library built with STX, featuring headwind utility classes and TypeScript syntax highlighting.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",
@@ -77,8 +77,8 @@
77
77
  "dependencies": {
78
78
  "@iconify-json/f7": "^1.2.2",
79
79
  "@stacksjs/ts-css": "^0.3.11",
80
- "@stacksjs/stx": "0.2.583",
81
- "bun-plugin-stx": "0.2.583",
80
+ "@stacksjs/stx": "0.2.585",
81
+ "bun-plugin-stx": "0.2.585",
82
82
  "ts-syntax-highlighter": "^0.2.17",
83
83
  "ts-video-player": "^0.1.9"
84
84
  },
package/src/index.ts CHANGED
@@ -47,6 +47,7 @@ export * from './ui/portal'
47
47
  export * from './ui/progress'
48
48
  export * from './ui/radio'
49
49
  export * from './ui/radio-group'
50
+ export * from './ui/segmented-control'
50
51
  export * from './ui/select'
51
52
  export * from './ui/sidebar'
52
53
  export * from './ui/skeleton'
@@ -133,7 +133,11 @@ const emit = defineEmits()
133
133
  *
134
134
  * See stacksjs/stx#1997.
135
135
  */
136
- const labelText = useReactiveProp('label', {{ label }}, { parse: value => value == null ? '' : String(value) })
136
+ const labelProp = useReactiveProp('label', {{ label }}, { parse: value => value == null ? '' : String(value) })
137
+ // Preserve native Button's text prop when the same shared file is selected.
138
+ // Read it as a prop, not a root text binding that would erase icons/spinners.
139
+ const textProp = useReactiveProp('text', '', { parse: value => value == null ? '' : String(value) })
140
+ const labelText = derived(() => labelProp() || textProp())
137
141
  const isLoading = useReactiveProp('loading', {{ loading }})
138
142
  const disabledProp = useReactiveProp('disabled', {{ disabled }})
139
143
 
@@ -11,6 +11,9 @@ export interface ButtonProps {
11
11
  loading?: boolean
12
12
  type?: 'button' | 'submit' | 'reset'
13
13
  fullWidth?: boolean
14
+ label?: string
15
+ /** Compatibility alias for native Button text; a nonempty label takes precedence. */
16
+ text?: string
14
17
  leftIcon?: string
15
18
  rightIcon?: string
16
19
  className?: string
@@ -27,6 +30,8 @@ export const buttonPropSchema: PropSchema = {
27
30
  loading: PropTypes.boolean,
28
31
  type: PropTypes.oneOf(['button', 'submit', 'reset']),
29
32
  fullWidth: PropTypes.boolean,
33
+ label: PropTypes.string,
34
+ text: PropTypes.string,
30
35
  leftIcon: PropTypes.string,
31
36
  rightIcon: PropTypes.string,
32
37
  className: PropTypes.string,
@@ -54,18 +54,18 @@ export const form = $props.form || ''
54
54
  </script>
55
55
 
56
56
  <script client>
57
- const emit = defineEmits()
57
+ const emit = defineEmits(['change'])
58
58
  // Validity is live, for the same reason `disabled` is: a form posted with
59
59
  // fetch learns it after the submit, and nothing re-renders (#2047).
60
60
  const isError = useReactiveProp('error', {{ error }})
61
- const isDisabled = {{ disabled }}
62
- const isIndeterminate = {{ indeterminate }}
61
+ const isDisabled = useReactiveProp('disabled', {{ disabled }})
62
+ const isIndeterminate = useReactiveProp('indeterminate', {{ indeterminate }})
63
63
  // Reactive prop binding so the parent can drive `checked` via signals.
64
64
  // See stacksjs/stx#1704.
65
65
  const isChecked = useReactiveProp('checked', {{ checked }})
66
66
 
67
67
  function onInputChange(event) {
68
- if (isDisabled) {
68
+ if (isDisabled()) {
69
69
  event.target.checked = isChecked()
70
70
  return
71
71
  }
@@ -73,13 +73,6 @@ function onInputChange(event) {
73
73
  emit('change', isChecked())
74
74
  }
75
75
 
76
- onMount(() => {
77
- if (isIndeterminate) {
78
- const el = document.getElementById({{ inputId }})
79
- if (el) el.indeterminate = true
80
- }
81
- })
82
-
83
76
  defineExpose({ isChecked })
84
77
  </script>
85
78
 
@@ -100,12 +93,14 @@ defineExpose({ isChecked })
100
93
  @if(checked)checked@endif
101
94
  :checked="isChecked()"
102
95
  @if(disabled)disabled@endif
96
+ :disabled="isDisabled()"
97
+ :indeterminate="isIndeterminate()"
103
98
  @if(required)required@endif
104
99
  class="{{ checkboxClasses }}"
105
100
  @change="onInputChange($event)"
106
101
  aria-invalid="{{ error ? 'true' : 'false' }}"
107
102
  :aria-invalid="isError() ? 'true' : 'false'"
108
- :class="{ '{{ stateInvalidClasses }}': isError() }"
103
+ :class="{ '{{ stateInvalidClasses }}': isError(), 'opacity-50 cursor-not-allowed': isDisabled(), 'cursor-pointer': !isDisabled() }"
109
104
  @if(description)aria-describedby="{{ descriptionId }}"@endif
110
105
  />
111
106
  </div>
@@ -113,7 +108,7 @@ defineExpose({ isChecked })
113
108
  @if(label || description)
114
109
  <div class="ml-3 text-sm leading-6">
115
110
  @if(label)
116
- <label for="{{ inputId }}" class="{{ labelClasses }}">
111
+ <label for="{{ inputId }}" class="{{ labelClasses }}" :class="{ 'opacity-50 cursor-not-allowed': isDisabled(), 'cursor-pointer': !isDisabled() }">
117
112
  {{ label }}
118
113
  @if(required)
119
114
  <span class="text-danger ml-1">*</span>
@@ -42,6 +42,20 @@ const dialogInstanceId = {{ dialogInstanceId }}
42
42
  // and isOpen flips. See stacksjs/stx#1704.
43
43
  const isOpen = useReactiveProp('open', {{ open }})
44
44
  const rootRef = useRef('rootRef')
45
+ const tree = window.stx._host
46
+
47
+ function closestMarker(node, marker) {
48
+ for (let current = node; current; current = tree.parent(current)) {
49
+ if (tree.getAttribute(current, marker) != null) return current
50
+ }
51
+ return null
52
+ }
53
+
54
+ function ownPart(marker) {
55
+ const root = rootRef.value
56
+ if (!root) return null
57
+ return tree.descendants(root).find(node => tree.getAttribute(node, marker) != null && closestMarker(node, 'data-stx-dialog') === root) || null
58
+ }
45
59
 
46
60
  /*
47
61
  * Whether hydration has run.
@@ -71,11 +85,12 @@ onMount(() => {
71
85
  function labelFromTitle() {
72
86
  const root = rootRef.value
73
87
  if (!root) return
74
- if (root.getAttribute('aria-label') || root.getAttribute('aria-labelledby')) return
75
- const heading = root.querySelector('[data-stx-dialog-title]')
88
+ if (tree.getAttribute(root, 'aria-label') || tree.getAttribute(root, 'aria-labelledby')) return
89
+ const heading = ownPart('data-stx-dialog-title')
76
90
  if (!heading) return
77
- if (!heading.id) heading.id = `${dialogInstanceId}-title`
78
- root.setAttribute('aria-labelledby', heading.id)
91
+ const headingId = tree.getAttribute(heading, 'id') || `${dialogInstanceId}-title`
92
+ tree.setAttribute(heading, 'id', headingId)
93
+ tree.setAttribute(root, 'aria-labelledby', headingId)
79
94
  }
80
95
 
81
96
  const closeOnEscape = {{ closeOnEscape }}
@@ -90,7 +105,7 @@ function close() {
90
105
  function panelEl() {
91
106
  const root = rootRef.value
92
107
  if (!root) return null
93
- return root.querySelector('[data-stx-dialog-panel]') || root
108
+ return ownPart('data-stx-dialog-panel') || root
94
109
  }
95
110
 
96
111
  /*
@@ -106,7 +121,7 @@ function panelEl() {
106
121
  function onRootClick(event) {
107
122
  if (!closeOnBackdrop || !window.stx.overlays.isTop(dialogInstanceId)) return
108
123
  const target = event.target
109
- if (target && target.closest && target.closest('[data-stx-dialog-backdrop]')) close()
124
+ if (closestMarker(target, 'data-stx-dialog-backdrop') && closestMarker(target, 'data-stx-dialog') === rootRef.value) close()
110
125
  }
111
126
 
112
127
  /*
@@ -128,6 +143,7 @@ effect(() => {
128
143
  panel: panelEl,
129
144
  root: rootRef.value,
130
145
  onEscape: closeOnEscape ? close : null,
146
+ onBackdrop: closeOnBackdrop ? close : null,
131
147
  })
132
148
  }
133
149
  else {
@@ -149,6 +165,7 @@ defineExpose({
149
165
  </script>
150
166
 
151
167
  <div
168
+ data-native="Modal"
152
169
  x-ref="rootRef"
153
170
  :show="isOpen()"
154
171
  class="{{ dialogClasses }}"
@@ -125,6 +125,10 @@ const drawerInstanceId = {{ drawerInstanceId }}
125
125
  // (`:open="drawerOpen()"`) instead of the prop being captured once.
126
126
  // See stacksjs/stx#1704.
127
127
  const isOpen = useReactiveProp('open', {{ open }})
128
+ const rootRef = useRef('rootRef')
129
+ const panelRef = useRef('panelRef')
130
+ const mounted = state(false)
131
+ onMount(() => mounted.set(true))
128
132
 
129
133
  const closeOnEscape = {{ closeOnEscape }}
130
134
  const closeOnBackdrop = {{ closeOnBackdrop }}
@@ -139,7 +143,7 @@ function onBackdropClick() {
139
143
  }
140
144
 
141
145
  function panelEl() {
142
- return document.querySelector(`[data-stx-drawer-panel="${drawerInstanceId}"]`)
146
+ return panelRef.value
143
147
  }
144
148
 
145
149
  /*
@@ -152,10 +156,13 @@ function panelEl() {
152
156
  * dialog over it together (stacksjs/stx#2057).
153
157
  */
154
158
  effect(() => {
159
+ if (!mounted()) return
155
160
  if (isOpen()) {
156
161
  window.stx.overlays.open(drawerInstanceId, {
157
162
  panel: panelEl,
163
+ root: rootRef.value,
158
164
  onEscape: closeOnEscape ? close : null,
165
+ onBackdrop: closeOnBackdrop ? close : null,
159
166
  })
160
167
  }
161
168
  else {
@@ -174,7 +181,7 @@ defineExpose({
174
181
  })
175
182
  </script>
176
183
 
177
- <div :show="isOpen()">
184
+ <div x-ref="rootRef" data-native="Modal" data-stx-drawer="{{ drawerInstanceId }}" :show="isOpen()">
178
185
  <div
179
186
  class="fixed inset-0 z-50 {{ backdropClass }} transition-opacity duration-500"
180
187
  style="animation: fadeIn 0.5s ease-in-out"
@@ -189,6 +196,7 @@ defineExpose({
189
196
  class="{{ panelClasses }}"
190
197
  style="animation: {{ slideAnimation }} 0.5s ease-in-out"
191
198
  data-stx-drawer-panel="{{ drawerInstanceId }}"
199
+ x-ref="panelRef"
192
200
  role="dialog"
193
201
  aria-modal="true"
194
202
  aria-label="{{ title }}"
@@ -0,0 +1,124 @@
1
+ <script server>
2
+ /*
3
+ * A segmented control: a few mutually exclusive choices in one track, the
4
+ * chosen one raised. The control iOS draws as UISegmentedControl and a phone
5
+ * app reaches for whenever a list has two to five ways to be shown.
6
+ *
7
+ * It is what applications kept drawing for themselves. One app copied the
8
+ * same track-and-segment markup into ten native screens and a stylesheet; the
9
+ * other drew seven by hand on the web, each with its own roles, and on native
10
+ * had none at all - a "Change the filter" button that cycled through the
11
+ * choices one tap at a time, so nobody could see what the others were.
12
+ *
13
+ * The same source renders on both targets. Each segment is a button with
14
+ * `aria-pressed`, inside a labelled group, which is what a toggle set is
15
+ * (a radio group would promise arrow-key roving the segments do not need,
16
+ * and tabs would promise panels). On a phone the host announces the chosen
17
+ * segment as selected, from the same attribute.
18
+ *
19
+ * <SegmentedControl v-model:value="period" label="Period" stretch
20
+ * :options="[{ value: 'week', label: 'This week' }, { value: 'all', label: 'All time' }]" />
21
+ *
22
+ * `options` takes strings or `{ value, label, disabled? }`; it is reactive,
23
+ * so a label carrying a count can change in place. The selection is a
24
+ * `v-model:value`, or `:value` with `@change`, which receives the new value.
25
+ * `testID` names the group, and each segment is `<testID>-<value>`.
26
+ *
27
+ * Colour and shape come from roles, so an app restyles it without touching
28
+ * it: the track is `surface-sunken`, the chosen segment `panel` (one step
29
+ * lighter than the track in dark mode, as iOS draws it), the words `fg` and
30
+ * `fg-muted`, and both corners `rounded-control` - a pill-shaped app gets a
31
+ * pill track by setting that one role.
32
+ */
33
+ export const value = $props.value ?? ''
34
+ export const options = $props.options || []
35
+ export const label = $props.label || ''
36
+ export const stretch = $bool($props.stretch)
37
+ export const disabled = $bool($props.disabled)
38
+ export const size = $props.size === 'sm' ? 'sm' : 'md'
39
+ export const testID = $props.testID || ''
40
+ export const className = $props.className || ''
41
+
42
+ const sizeClasses = {
43
+ sm: { segment: 'min-h-[28px] px-2.5 py-1', text: 'text-[13px]' },
44
+ md: { segment: 'min-h-[32px] px-3 py-1.5', text: 'text-[13px]' },
45
+ }
46
+ export const sizing = sizeClasses[size]
47
+
48
+ export const trackClasses = `${stretch ? 'flex w-full' : 'inline-flex'} flex-row gap-0.5 p-0.5 rounded-control bg-surface-sunken ${className}`.replace(/\s+/g, ' ').trim()
49
+ export const segmentBase = `${stretch ? 'flex-1' : ''} items-center justify-center rounded-control transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-accent ${sizing.segment}`.replace(/\s+/g, ' ').trim()
50
+ </script>
51
+
52
+ <script client>
53
+ const emit = defineEmits(['change'])
54
+ const selected = useModel('value', { default: {{ value }}, parse: v => v == null ? '' : String(v) })
55
+ const choices = useReactiveProp('options', {{ options }})
56
+ const isDisabled = useReactiveProp('disabled', {{ disabled }})
57
+ const segmentBase = {{ segmentBase }}
58
+ const textSize = {{ sizing.text }}
59
+ const groupID = {{ testID }}
60
+
61
+ /** A choice as `{ value, label, disabled }`, whichever way it was written. */
62
+ function normalise(choice) {
63
+ if (choice !== null && typeof choice === 'object') {
64
+ const value = choice.value == null ? '' : String(choice.value)
65
+ return { value, label: choice.label == null ? value : String(choice.label), disabled: choice.disabled === true }
66
+ }
67
+ const value = choice == null ? '' : String(choice)
68
+ return { value, label: value, disabled: false }
69
+ }
70
+
71
+ function segments() {
72
+ const list = choices()
73
+ return Array.isArray(list) ? list.map(normalise) : []
74
+ }
75
+
76
+ function isOn(segment) {
77
+ return segment.value === selected()
78
+ }
79
+
80
+ function isOff(segment) {
81
+ return isDisabled() || segment.disabled
82
+ }
83
+
84
+ function choose(segment) {
85
+ if (isOff(segment) || isOn(segment)) return
86
+ selected.set(segment.value)
87
+ emit('change', segment.value)
88
+ }
89
+
90
+ function segmentClasses(segment) {
91
+ const state = isOn(segment) ? 'bg-panel dark:bg-neutral-600 shadow-sm' : 'bg-transparent'
92
+ return `${segmentBase} ${state} ${isOff(segment) ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer'}`
93
+ }
94
+
95
+ function labelClasses(segment) {
96
+ return `${textSize} ${isOn(segment) ? 'font-semibold text-fg' : 'font-medium text-fg-muted'}`
97
+ }
98
+
99
+ function segmentID(segment) {
100
+ return groupID ? `${groupID}-${segment.value}` : ''
101
+ }
102
+
103
+ defineExpose({ selected })
104
+ </script>
105
+
106
+ <div
107
+ role="group"
108
+ @if(label)aria-label="{{ label }}"@endif
109
+ @if(testID)testID="{{ testID }}"@endif
110
+ class="{{ trackClasses }}"
111
+ data-stx-segmented-control
112
+ >
113
+ <button
114
+ :for="segment in segments()"
115
+ :key="segment.value"
116
+ type="button"
117
+ :testID="segmentID(segment)"
118
+ :aria-pressed="isOn(segment) ? 'true' : 'false'"
119
+ :aria-disabled="isOff(segment) ? 'true' : 'false'"
120
+ :disabled="isOff(segment)"
121
+ :class="segmentClasses(segment)"
122
+ @click="choose(segment)"
123
+ ><span :class="labelClasses(segment)" :text="segment.label"></span></button>
124
+ </div>
@@ -0,0 +1,29 @@
1
+ export { default as SegmentedControl } from './SegmentedControl.stx'
2
+
3
+ /** One choice. A bare string is its own value and label. */
4
+ export interface SegmentedControlOption {
5
+ value: string
6
+ label?: string
7
+ disabled?: boolean
8
+ }
9
+
10
+ /**
11
+ * A few mutually exclusive choices in one track, the chosen one raised. The
12
+ * same source renders on the web and on a native host; on a phone the chosen
13
+ * segment is announced as selected.
14
+ */
15
+ export interface SegmentedControlProps {
16
+ /** The chosen value. Use `v-model:value`, or `:value` with `@change`. */
17
+ value?: string
18
+ options: Array<string | SegmentedControlOption>
19
+ /** What the choices are of ("Period", "Show"), read before them. */
20
+ label?: string
21
+ /** Fill the width, segments sharing it equally. */
22
+ stretch?: boolean
23
+ disabled?: boolean
24
+ size?: 'sm' | 'md'
25
+ /** Names the group; each segment is `<testID>-<value>`. */
26
+ testID?: string
27
+ className?: string
28
+ onChange?: (value: string) => void
29
+ }
@@ -34,13 +34,13 @@ const sizeConfig = {
34
34
  export const config = sizeConfig[size] || sizeConfig.md
35
35
 
36
36
  export const trackBaseClasses = 'relative inline-flex shrink-0 cursor-pointer border-2 border-transparent rounded-full transition-colors duration-200 ease-in-out focus:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-panel'
37
- export const trackDisabledClasses = disabled ? 'opacity-50 cursor-not-allowed pointer-events-none' : ''
37
+ export const trackDisabledClasses = 'opacity-50 cursor-not-allowed pointer-events-none'
38
38
  export const thumbBaseClasses = 'pointer-events-none inline-block transform rounded-full bg-white shadow-lg ring-0 transition duration-200 ease-in-out'
39
39
  </script>
40
40
 
41
41
  <script client>
42
- const emit = defineEmits()
43
- const isDisabled = {{ disabled }}
42
+ const emit = defineEmits(['change'])
43
+ const isDisabled = useReactiveProp('disabled', {{ disabled }})
44
44
  const config = {{ config }}
45
45
  const trackBase = {{ trackBaseClasses }}
46
46
  const trackDisabled = {{ trackDisabledClasses }}
@@ -50,7 +50,7 @@ const extraClass = {{ className }}
50
50
  const isChecked = useModel('checked', { default: {{ checked }} })
51
51
 
52
52
  function toggle() {
53
- if (isDisabled) return
53
+ if (isDisabled()) return
54
54
  isChecked.set(!isChecked())
55
55
  emit('change', isChecked())
56
56
  }
@@ -59,7 +59,7 @@ function trackClasses() {
59
59
  const stateClass = isChecked()
60
60
  ? 'bg-accent-solid'
61
61
  : 'bg-neutral-400 dark:bg-neutral-600'
62
- return `${trackBase} ${stateClass} ${trackDisabled} ${config.track} ${extraClass}`.trim()
62
+ return `${trackBase} ${stateClass} ${isDisabled() ? trackDisabled : ''} ${config.track} ${extraClass}`.trim()
63
63
  }
64
64
 
65
65
  function thumbClasses() {
@@ -85,6 +85,8 @@ defineExpose({ isChecked, toggle })
85
85
  <input
86
86
  type="hidden"
87
87
  name="{{ name }}"
88
+ @if(disabled)disabled@endif
89
+ :disabled="isDisabled()"
88
90
  @if(form)form="{{ form }}"@endif
89
91
  value="{{ checked ? checkedValue : uncheckedValue }}"
90
92
  :value="isChecked() ? '{{ checkedValue }}' : '{{ uncheckedValue }}'"
@@ -97,6 +99,8 @@ defineExpose({ isChecked, toggle })
97
99
  :aria-checked="isChecked() ? 'true' : 'false'"
98
100
  :class="trackClasses()"
99
101
  @if(disabled)disabled aria-disabled="true"@endif
102
+ :disabled="isDisabled()"
103
+ :aria-disabled="isDisabled() ? 'true' : 'false'"
100
104
  @click="toggle()"
101
105
  >
102
106
  <span class="sr-only">@if(label){{ label }}@else Toggle switch@endif</span>