basalt-ui 2.13.3 → 2.14.0

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 (123) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/MIGRATING.md +36 -12
  3. package/dist/agent/chat/thread-feed-row.d.ts.map +1 -1
  4. package/dist/agent/chat/thread-feed-row.js +2 -2
  5. package/dist/agent/chat/thread-message.d.ts.map +1 -1
  6. package/dist/agent/chat/thread-message.js +2 -2
  7. package/dist/agent/chat/thread-outcome-card.js +2 -2
  8. package/dist/charts/DualPanel.d.ts.map +1 -1
  9. package/dist/charts/DualPanel.js +1 -0
  10. package/dist/charts/compile.d.ts.map +1 -1
  11. package/dist/charts/compile.js +3 -2
  12. package/dist/charts/cursor.d.ts +10 -6
  13. package/dist/charts/cursor.d.ts.map +1 -1
  14. package/dist/charts/cursor.js +20 -13
  15. package/dist/charts/legend.d.ts +5 -4
  16. package/dist/charts/legend.d.ts.map +1 -1
  17. package/dist/charts/legend.js +3 -3
  18. package/dist/charts/panel.d.ts +2 -0
  19. package/dist/charts/panel.d.ts.map +1 -1
  20. package/dist/charts/panel.js +1 -0
  21. package/dist/commands/notifications/center.d.ts.map +1 -1
  22. package/dist/commands/notifications/center.js +2 -11
  23. package/dist/controls/control-name.d.ts +27 -0
  24. package/dist/controls/control-name.d.ts.map +1 -0
  25. package/dist/controls/control-name.js +11 -0
  26. package/dist/controls/enum-filter.d.ts +3 -6
  27. package/dist/controls/enum-filter.d.ts.map +1 -1
  28. package/dist/controls/enum-filter.js +8 -4
  29. package/dist/controls/filter-base.d.ts +30 -0
  30. package/dist/controls/filter-base.d.ts.map +1 -0
  31. package/dist/controls/filter-base.js +0 -0
  32. package/dist/controls/filter-pill.d.ts +3 -1
  33. package/dist/controls/filter-pill.d.ts.map +1 -1
  34. package/dist/controls/filter-pill.js +2 -0
  35. package/dist/controls/filter-sheet.d.ts +0 -32
  36. package/dist/controls/filter-sheet.d.ts.map +1 -1
  37. package/dist/controls/filter-sheet.js +1 -28
  38. package/dist/controls/glyphs.d.ts +12 -0
  39. package/dist/controls/glyphs.d.ts.map +1 -1
  40. package/dist/controls/glyphs.js +23 -2
  41. package/dist/controls/index.d.ts +2 -1
  42. package/dist/controls/index.d.ts.map +1 -1
  43. package/dist/controls/multi-select-filter.d.ts +8 -3
  44. package/dist/controls/multi-select-filter.d.ts.map +1 -1
  45. package/dist/controls/multi-select-filter.js +27 -10
  46. package/dist/controls/number-filter.d.ts +3 -12
  47. package/dist/controls/number-filter.d.ts.map +1 -1
  48. package/dist/controls/number-filter.js +17 -6
  49. package/dist/controls/panel-row.d.ts +5 -8
  50. package/dist/controls/panel-row.d.ts.map +1 -1
  51. package/dist/controls/panel-row.js +3 -0
  52. package/dist/controls/range-filter.d.ts +14 -14
  53. package/dist/controls/range-filter.d.ts.map +1 -1
  54. package/dist/controls/range-filter.js +18 -41
  55. package/dist/controls/search-filter.d.ts +5 -2
  56. package/dist/controls/search-filter.d.ts.map +1 -1
  57. package/dist/controls/search-filter.js +74 -40
  58. package/dist/controls/select-filter.d.ts +2 -15
  59. package/dist/controls/select-filter.d.ts.map +1 -1
  60. package/dist/controls/sync-button.d.ts +0 -6
  61. package/dist/controls/sync-button.d.ts.map +1 -1
  62. package/dist/controls/sync-button.js +6 -36
  63. package/dist/controls/toggle-filter.d.ts +2 -2
  64. package/dist/controls/toggle-filter.d.ts.map +1 -1
  65. package/dist/controls/toggle-filter.js +3 -2
  66. package/dist/controls/view-tabs.d.ts +3 -4
  67. package/dist/controls/view-tabs.d.ts.map +1 -1
  68. package/dist/controls/view-tabs.js +24 -5
  69. package/dist/format/date.d.ts +43 -10
  70. package/dist/format/date.d.ts.map +1 -1
  71. package/dist/format/date.js +109 -18
  72. package/dist/format/index.d.ts +12 -11
  73. package/dist/format/index.d.ts.map +1 -1
  74. package/dist/format/index.js +11 -2
  75. package/dist/format/measure.d.ts +9 -9
  76. package/dist/format/measure.d.ts.map +1 -1
  77. package/dist/format/measure.js +19 -8
  78. package/dist/format/number.d.ts +35 -27
  79. package/dist/format/number.d.ts.map +1 -1
  80. package/dist/format/number.js +41 -20
  81. package/dist/format/shared.d.ts +38 -7
  82. package/dist/format/shared.d.ts.map +1 -1
  83. package/dist/format/shared.js +61 -13
  84. package/dist/provider/index.d.ts +13 -1
  85. package/dist/provider/index.d.ts.map +1 -1
  86. package/dist/provider/index.js +8 -0
  87. package/llms.txt +35 -14
  88. package/package.json +1 -1
  89. package/src/agent/chat/thread-feed-row.tsx +2 -2
  90. package/src/agent/chat/thread-message.tsx +2 -2
  91. package/src/agent/chat/thread-outcome-card.tsx +2 -2
  92. package/src/charts/DualPanel.tsx +1 -0
  93. package/src/charts/compile.ts +3 -2
  94. package/src/charts/cursor.ts +44 -27
  95. package/src/charts/legend.tsx +9 -6
  96. package/src/charts/panel.tsx +3 -0
  97. package/src/commands/notifications/center.tsx +2 -14
  98. package/src/controls/control-name.ts +33 -0
  99. package/src/controls/enum-filter.tsx +29 -27
  100. package/src/controls/filter-base.ts +31 -0
  101. package/src/controls/filter-pill.tsx +4 -0
  102. package/src/controls/filter-sheet.tsx +0 -56
  103. package/src/controls/glyphs.tsx +45 -2
  104. package/src/controls/index.ts +2 -1
  105. package/src/controls/multi-select-filter.tsx +67 -43
  106. package/src/controls/number-filter.tsx +47 -47
  107. package/src/controls/panel-row.tsx +7 -8
  108. package/src/controls/range-filter.tsx +54 -78
  109. package/src/controls/search-filter.tsx +107 -54
  110. package/src/controls/select-filter.tsx +2 -18
  111. package/src/controls/sync-button.tsx +7 -40
  112. package/src/controls/toggle-filter.tsx +9 -7
  113. package/src/controls/view-tabs.tsx +41 -21
  114. package/src/format/date.ts +189 -36
  115. package/src/format/index.ts +12 -11
  116. package/src/format/measure.ts +29 -18
  117. package/src/format/number.ts +76 -37
  118. package/src/format/shared.ts +116 -25
  119. package/src/provider/index.tsx +23 -0
  120. package/dist/agent/chat/relative-time.d.ts +0 -27
  121. package/dist/agent/chat/relative-time.d.ts.map +0 -1
  122. package/dist/agent/chat/relative-time.js +0 -27
  123. package/src/agent/chat/relative-time.ts +0 -55
@@ -6,33 +6,42 @@
6
6
  * Keystrokes are DEBOUNCED (200ms) before they reach the field. `field.string()` already writes with
7
7
  * `history: 'replace'`, so an un-debounced input would still not stack history entries — what the
8
8
  * debounce buys is one navigation per phrase instead of one per character, which is the difference
9
- * between a typed query and a re-rendered route tree eight times over.
9
+ * between a typed query and a re-rendered route tree eight times over. The pending phrase is
10
+ * committed at once on Enter and blur, and on unmount while the store still holds the value the
11
+ * draft was based on, so a query submitted, tabbed away from or navigated away from inside the
12
+ * window is never lost.
10
13
  *
11
14
  * @example
12
15
  * // q: field.string({ max: 120 })
13
16
  * <SearchFilter field={table.field.q} placeholder="Search pages" />
14
17
  */
15
- import { TextInput } from '@mantine/core'
18
+ import { CloseButton, TextInput } from '@mantine/core'
16
19
  import { useDebouncedCallback } from '@mantine/hooks'
17
20
  import { useEffect, useRef, useState } from 'react'
18
21
  import type { ReactNode } from 'react'
22
+ import { useIsomorphicLayoutEffect } from '../common/isomorphic-layout-effect'
19
23
  import { assertRequiredProps } from '../common/validate'
20
24
  import type { FieldHandle } from '../state'
21
25
  import type { StringField } from '../state/fields'
26
+ import { useControlName } from './control-name'
27
+ import type { FilterBase } from './filter-base'
22
28
  import { useFilterRegistration, useFilterSurface } from './filter-context'
23
- import { useControlName } from './filter-sheet'
29
+ import { SearchGlyph } from './glyphs'
24
30
  import { PanelRow } from './panel-row'
25
31
  import type { UncontrolledProps } from './uncontrolled'
26
32
 
27
33
  /** One navigation per typed phrase, not per keystroke. */
28
34
  const DEBOUNCE_MS = 200
29
35
 
30
- export type SearchFilterProps = UncontrolledProps & {
31
- readonly field: FieldHandle<StringField>
32
- readonly placeholder?: string
33
- /** Sheet-form heading. @default 'Search' */
34
- readonly label?: string
35
- }
36
+ export type SearchFilterProps = UncontrolledProps &
37
+ Omit<FilterBase, 'icon' | 'label' | 'clearable'> & {
38
+ readonly field: FieldHandle<StringField>
39
+ readonly placeholder?: string
40
+ /** Sheet-form heading and the accessible name. @default 'Search' */
41
+ readonly label?: string
42
+ /** The clear button shown while the box holds text. @default true */
43
+ readonly clearable?: boolean
44
+ }
36
45
 
37
46
  export function SearchFilter(props: SearchFilterProps): ReactNode {
38
47
  // F-ERR-1 — without this a missing `field` surfaces as `undefined is not an object
@@ -40,40 +49,72 @@ export function SearchFilter(props: SearchFilterProps): ReactNode {
40
49
  assertRequiredProps('SearchFilter', props, ['field'], {
41
50
  field: 'bind it to a store field (`store.field.<name>`), never a value/onChange pair.',
42
51
  })
43
- const { field, placeholder, label = 'Search', className, style } = props
52
+ const {
53
+ field,
54
+ placeholder,
55
+ label = 'Search',
56
+ clearable = true,
57
+ disabled,
58
+ className,
59
+ style,
60
+ } = props
44
61
  const [value, setValue] = field.use()
45
62
  const surface = useFilterSurface()
46
- const draft = useDebouncedField(value, setValue)
47
- useFilterRegistration(!field.isDefault(value), () => {
63
+ const draft = useDebouncedField(value, setValue, () => {
48
64
  field.clear()
49
65
  })
66
+ // One clear for the button and for `Reset all`: both cancel the pending phrase, so an unmount in
67
+ // the same commit as a reset cannot write the stale draft back over it.
68
+ useFilterRegistration(!field.isDefault(value), draft.clear)
50
69
 
51
70
  // `placeholder` is optional and, even when set, stops being the name the moment the user types —
52
71
  // so the name comes from `label`, or from the visible heading when the surface draws one.
53
- const named = surface === 'sheet' || surface === 'panel'
54
- const { labelId, nameProps } = useControlName(label, named)
55
- // The pill form has no wrapping home — the `TextInput` itself is the root, so it takes the class
56
- // directly. The panel/sheet forms wrap it in a home that becomes the root instead.
57
- const isPill = surface !== 'panel' && surface !== 'sheet'
72
+ // The sheet form is the panel form (`docs/CONTROLS-SPEC.md` §3: "sheet = panel rows inside a
73
+ // Drawer"). The pill form has no wrapping home — the `TextInput` itself is the root, so it takes
74
+ // the class directly; the panel/sheet forms wrap it in a home that becomes the root instead.
75
+ const inRow = surface === 'sheet' || surface === 'panel'
76
+ const { labelId, nameProps } = useControlName(label, inRow)
58
77
 
59
78
  const input = (
60
79
  <TextInput
61
80
  {...nameProps}
62
81
  size="ctl"
82
+ type="search"
83
+ enterKeyHint="search"
63
84
  value={draft.value}
64
- leftSection={<SearchIcon />}
85
+ leftSection={<SearchGlyph />}
86
+ rightSectionPointerEvents="all"
87
+ {...(clearable &&
88
+ draft.value !== '' && {
89
+ rightSection: (
90
+ <CloseButton
91
+ size="sm"
92
+ aria-label="Clear search"
93
+ disabled={disabled === true}
94
+ // Keeps focus in the box: a blur here would flush the phrase being cleared.
95
+ onMouseDown={(event) => {
96
+ event.preventDefault()
97
+ }}
98
+ onClick={draft.clear}
99
+ />
100
+ ),
101
+ })}
102
+ {...(disabled === true && { disabled: true })}
65
103
  {...(placeholder !== undefined && { placeholder })}
66
104
  onChange={(event) => {
67
105
  draft.set(event.currentTarget.value)
68
106
  }}
69
- {...(isPill && className !== undefined && { className })}
70
- {...(isPill && style !== undefined && { style })}
107
+ onBlur={draft.flush}
108
+ onKeyDown={(event) => {
109
+ // Enter that confirms an IME candidate is not a submit.
110
+ if (event.key === 'Enter' && !event.nativeEvent.isComposing) draft.flush()
111
+ }}
112
+ {...(!inRow && className !== undefined && { className })}
113
+ {...(!inRow && style !== undefined && { style })}
71
114
  />
72
115
  )
73
116
 
74
- // The sheet form is the panel form (`docs/CONTROLS-SPEC.md` §3: "sheet = panel rows inside a
75
- // Drawer") — the same `PanelRow` label-above-control row on both surfaces.
76
- if (surface === 'panel' || surface === 'sheet') {
117
+ if (inRow) {
77
118
  return (
78
119
  <PanelRow
79
120
  label={label}
@@ -89,28 +130,48 @@ export function SearchFilter(props: SearchFilterProps): ReactNode {
89
130
  }
90
131
 
91
132
  /**
92
- * A local draft that lands on the field `DEBOUNCE_MS` after the last keystroke, and that follows the
93
- * field when it changes from OUTSIDE the input (`Reset all`, a back navigation, a deep link).
94
- * `seen` is what separates the two directions: it holds the last value this hook itself committed,
95
- * so an incoming change it did not cause is the only thing that overwrites the draft.
133
+ * A local draft that lands on the field `DEBOUNCE_MS` after the last keystroke — or at once, via
134
+ * `flush` (Enter, blur) — and that follows the field when it changes from OUTSIDE the input
135
+ * (`Reset all`, a back navigation, a deep link).
136
+ *
137
+ * `seen` is what separates the two directions: it is the last value the store is known to hold —
138
+ * written synchronously by this hook's own commits, adopted from the store otherwise — so an
139
+ * incoming change it did not cause is the only thing that overwrites the draft.
96
140
  */
97
141
  function useDebouncedField(
98
142
  value: string,
99
143
  setValue: (next: string) => void,
100
- ): { value: string; set: (next: string) => void } {
144
+ clearField: () => void,
145
+ ): { value: string; set: (next: string) => void; flush: () => void; clear: () => void } {
101
146
  const [draft, setDraft] = useState(value)
102
147
  const seen = useRef(value)
103
- const held = useRef(value)
104
- useEffect(() => {
105
- held.current = value
106
- })
107
- const commit = useDebouncedCallback((next: string) => {
108
- // A phrase typed and erased inside the window ends where it began: nothing to write.
109
- if (next === held.current) return
148
+ const rendered = useRef(value)
149
+ const write = (next: string): void => {
110
150
  seen.current = next
111
151
  setValue(next)
152
+ }
153
+ const commit = useDebouncedCallback((next: string) => {
154
+ // A phrase typed and erased inside the window ends where it began: nothing to write.
155
+ if (next === seen.current) return
156
+ write(next)
112
157
  }, DEBOUNCE_MS)
113
158
 
159
+ // Unmount: a layout cleanup runs before Mantine's own passive one cancels the timer. Flush only
160
+ // while the store still holds what the draft was based on — otherwise an outside change (a
161
+ // `Reset all` that also closed this filter) would be overwritten by a stale phrase.
162
+ const commitRef = useRef(commit)
163
+ useEffect(() => {
164
+ rendered.current = value
165
+ commitRef.current = commit
166
+ })
167
+ useIsomorphicLayoutEffect(
168
+ () => () => {
169
+ if (rendered.current === seen.current) commitRef.current.flush()
170
+ else commitRef.current.cancel()
171
+ },
172
+ [],
173
+ )
174
+
114
175
  useEffect(() => {
115
176
  if (seen.current === value) return
116
177
  seen.current = value
@@ -124,24 +185,16 @@ function useDebouncedField(
124
185
  setDraft(next)
125
186
  commit(next)
126
187
  },
188
+ flush: () => {
189
+ commit.flush()
190
+ },
191
+ // Through the field's own `clear()` — it unsets rather than writing the fallback over it — and
192
+ // unconditional: the store may still hold the old phrase, or an unflushed one.
193
+ clear: () => {
194
+ commit.cancel()
195
+ setDraft('')
196
+ seen.current = rendered.current
197
+ clearField()
198
+ },
127
199
  }
128
200
  }
129
-
130
- function SearchIcon(): ReactNode {
131
- return (
132
- <svg
133
- width={16}
134
- height={16}
135
- viewBox="0 0 24 24"
136
- fill="none"
137
- stroke="currentColor"
138
- strokeWidth={2}
139
- strokeLinecap="round"
140
- strokeLinejoin="round"
141
- aria-hidden
142
- >
143
- <circle cx="11" cy="11" r="7" />
144
- <path d="M21 21l-4.35 -4.35" />
145
- </svg>
146
- )
147
- }
@@ -34,26 +34,10 @@ import { assertRequiredProps } from '../common/validate'
34
34
  import type { EnumField, FieldHandle } from '../state'
35
35
  import type { StringField } from '../state/fields'
36
36
  import { EnumFilter } from './enum-filter'
37
+ import type { FilterBase, FilterOption } from './filter-base'
37
38
  import type { UncontrolledProps } from './uncontrolled'
38
39
 
39
- /**
40
- * One row of a runtime catalogue. Wider than the store's own `FieldOption` by exactly one member:
41
- * `disabled` renders the row and refuses the selection, which a declared enum expresses by leaving
42
- * the value out and a live catalogue cannot (an archived project still labels the rows already
43
- * pointing at it).
44
- */
45
- export type FilterOption = {
46
- readonly value: string
47
- readonly label: string
48
- readonly disabled?: boolean
49
- }
50
-
51
- type SelectFilterCommon = UncontrolledProps & {
52
- readonly label: string
53
- readonly icon?: ReactNode
54
- /** Adds a `Clear` action that writes the field back to its fallback. */
55
- readonly clearable?: boolean
56
- }
40
+ type SelectFilterCommon = UncontrolledProps & FilterBase & { readonly label: string }
57
41
 
58
42
  /** A closed enum field: `options` relabels rows the field already declares, so it is optional. */
59
43
  export type SelectFilterEnumProps<T extends string> = SelectFilterCommon & {
@@ -25,7 +25,9 @@ import { useSyncExternalStore } from 'react'
25
25
  import type { ReactNode } from 'react'
26
26
  import { cx } from '../common/props'
27
27
  import type { BasaltProps } from '../common/props'
28
+ import { relativeTime } from '../format'
28
29
  import { IconSlot } from '../theme/icon-slot'
30
+ import { RefreshGlyph } from './glyphs'
29
31
  import classes from './sync-button.module.css'
30
32
 
31
33
  export type SyncButtonProps = BasaltProps & {
@@ -50,20 +52,6 @@ export type SyncButtonProps = BasaltProps & {
50
52
  /** How often the relative age re-renders. One interval, only while mounted. */
51
53
  export const AGE_REFRESH_MS = 30_000
52
54
 
53
- /**
54
- * Relative age, coarse on purpose: a sync indicator answers "is this stale?", not "how stale to
55
- * the second". Anything under a minute is `just now`, so the label stops flickering while a sync
56
- * has just landed.
57
- */
58
- export function formatAge(ms: number): string {
59
- if (ms < 60_000) return 'just now'
60
- const minutes = Math.floor(ms / 60_000)
61
- if (minutes < 60) return `${minutes}m ago`
62
- const hours = Math.floor(minutes / 60)
63
- if (hours < 24) return `${hours}h ago`
64
- return `${Math.floor(hours / 24)}d ago`
65
- }
66
-
67
55
  function toMillis(at: number | Date | null | undefined): number | undefined {
68
56
  if (at === null || at === undefined) return undefined
69
57
  return at instanceof Date ? at.getTime() : at
@@ -75,17 +63,17 @@ function toMillis(at: number | Date | null | undefined): number | undefined {
75
63
  *
76
64
  * The clock is read through `useSyncExternalStore`, whose server snapshot omits the age. A global
77
65
  * `SyncButton` in `globalActions` renders inline in the shell header (no portal), so under SSR the
78
- * server would emit `just now` and a client hydrating a minute later would emit `1m ago` — a React
66
+ * server would emit `now` and a client hydrating a minute later would emit `1 min. ago` — a React
79
67
  * text mismatch that discards the subtree. Server and hydrating paint both omit the age; React
80
68
  * re-renders with the client snapshot right after. The snapshot is the formatted label, a bucketed
81
- * string (`just now`, `1m ago`, …), so it is stable across calls within a bucket and a tick only
69
+ * string (`now`, `1 min. ago`, …), so it is stable across calls within a bucket and a tick only
82
70
  * re-renders when the label changes. With no timestamp there is no subscription and no timer.
83
71
  */
84
72
  function useRelativeAge(at: number | Date | null | undefined): string | undefined {
85
73
  const timestamp = toMillis(at)
86
74
  return useSyncExternalStore(
87
75
  timestamp === undefined ? subscribeNever : subscribeAgeTick,
88
- () => (timestamp === undefined ? undefined : formatAge(Date.now() - timestamp)),
76
+ () => (timestamp === undefined ? undefined : relativeTime(timestamp, { style: 'short' })),
89
77
  () => undefined,
90
78
  )
91
79
  }
@@ -97,27 +85,6 @@ function subscribeAgeTick(onTick: () => void): () => void {
97
85
  return () => clearInterval(id)
98
86
  }
99
87
 
100
- function RefreshGlyph({ spinning }: { spinning: boolean }) {
101
- return (
102
- <svg
103
- width={16}
104
- height={16}
105
- viewBox="0 0 24 24"
106
- fill="none"
107
- aria-hidden
108
- className={spinning ? classes.spin : undefined}
109
- >
110
- <path
111
- d="M20 11a8 8 0 1 0-2.34 5.66M20 5v6h-6"
112
- stroke="currentColor"
113
- strokeWidth={2}
114
- strokeLinecap="round"
115
- strokeLinejoin="round"
116
- />
117
- </svg>
118
- )
119
- }
120
-
121
88
  /**
122
89
  * @example
123
90
  * // page scope — the page's own data
@@ -181,7 +148,7 @@ export function SyncButton({
181
148
  {...(style !== undefined && { style })}
182
149
  >
183
150
  <IconSlot>
184
- <RefreshGlyph spinning={syncing} />
151
+ <RefreshGlyph className={syncing ? classes.spin : undefined} />
185
152
  </IconSlot>
186
153
  </ActionIcon>
187
154
  ) : (
@@ -191,7 +158,7 @@ export function SyncButton({
191
158
  {...(style !== undefined && { style })}
192
159
  leftSection={
193
160
  <IconSlot>
194
- <RefreshGlyph spinning={syncing} />
161
+ <RefreshGlyph className={syncing ? classes.spin : undefined} />
195
162
  </IconSlot>
196
163
  }
197
164
  {...control}
@@ -17,15 +17,16 @@ import { assertRequiredProps } from '../common/validate'
17
17
  import type { FieldHandle } from '../state'
18
18
  import type { BooleanField } from '../state/fields'
19
19
  import { useFilterRegistration, useFilterSurface } from './filter-context'
20
+ import type { FilterBase } from './filter-base'
20
21
  import { FilterPill } from './filter-pill'
21
22
  import { PanelRow } from './panel-row'
22
23
  import type { UncontrolledProps } from './uncontrolled'
23
24
 
24
- export type ToggleFilterProps = UncontrolledProps & {
25
- readonly field: FieldHandle<BooleanField>
26
- readonly label: string
27
- readonly icon?: ReactNode
28
- }
25
+ export type ToggleFilterProps = UncontrolledProps &
26
+ Omit<FilterBase, 'clearable'> & {
27
+ readonly field: FieldHandle<BooleanField>
28
+ readonly label: string
29
+ }
29
30
 
30
31
  export function ToggleFilter(props: ToggleFilterProps): ReactNode {
31
32
  // F-ERR-1 — without this a missing `field` surfaces as `undefined is not an object
@@ -33,7 +34,7 @@ export function ToggleFilter(props: ToggleFilterProps): ReactNode {
33
34
  assertRequiredProps('ToggleFilter', props, ['field'], {
34
35
  field: 'bind it to a store field (`store.field.<name>`), never a value/onChange pair.',
35
36
  })
36
- const { field, label, icon, className, style } = props
37
+ const { field, label, icon, disabled, className, style } = props
37
38
  const [value, setValue] = field.use()
38
39
  const surface = useFilterSurface()
39
40
  const id = useId()
@@ -51,7 +52,7 @@ export function ToggleFilter(props: ToggleFilterProps): ReactNode {
51
52
  end={
52
53
  <Switch
53
54
  id={id}
54
- aria-label={label}
55
+ {...(disabled === true && { disabled: true })}
55
56
  checked={value}
56
57
  onChange={(event) => {
57
58
  setValue(event.currentTarget.checked)
@@ -70,6 +71,7 @@ export function ToggleFilter(props: ToggleFilterProps): ReactNode {
70
71
  active={value}
71
72
  pressed={value}
72
73
  hideGlyph
74
+ {...(disabled === true && { disabled: true })}
73
75
  {...(icon !== undefined && { icon })}
74
76
  onClick={() => {
75
77
  setValue(!value)
@@ -34,33 +34,44 @@ import { assertRequiredProps } from '../common/validate'
34
34
  import type { EnumField, FieldHandle } from '../state'
35
35
  import classes from './controls.module.css'
36
36
  import { useFilterSurface } from './filter-context'
37
- import { useControlName } from './filter-sheet'
37
+ import { useControlName } from './control-name'
38
+ import type { FilterBase, FilterOption } from './filter-base'
38
39
  import { PanelChoice, PanelRow, useTrackFits } from './panel-row'
39
40
  import type { UncontrolledProps } from './uncontrolled'
40
41
 
42
+ /** The `data` both Mantine controls take — `disabled` always a boolean, never absent. */
43
+ function toSegmentData(
44
+ options: readonly FilterOption[],
45
+ ): { value: string; label: string; disabled: boolean }[] {
46
+ return options.map((option) => ({
47
+ value: option.value,
48
+ label: option.label,
49
+ disabled: option.disabled === true,
50
+ }))
51
+ }
52
+
41
53
  /** Past three options the phone form is a `Select`, not a track. */
42
54
  const PHONE_TRACK_MAX = 3
43
55
 
44
- export type ViewTabsOption<T extends string> = {
45
- readonly value: T
46
- readonly label: string
56
+ export type ViewTabsOption<T extends string> = FilterOption<T> & {
47
57
  /** Render this option on ONE viewport only. Omitted → both. */
48
58
  readonly only?: 'sm-up' | 'sm-down'
49
59
  }
50
60
 
51
- export type ViewTabsProps<T extends string> = UncontrolledProps & {
52
- readonly field: FieldHandle<EnumField<T>>
53
- /** Defaults to `field.options` (every declared value, labelled by `store.labels()`). */
54
- readonly options?: readonly ViewTabsOption<T>[]
55
- /**
56
- * The switch's accessible name. Both forms render without a visible Mantine `label` (a home has
57
- * no room for one), so without this the desktop track is an unnamed `radiogroup` and the phone
58
- * form an unnamed `combobox`.
59
- *
60
- * @default 'View'
61
- */
62
- readonly label?: string
63
- }
61
+ export type ViewTabsProps<T extends string> = UncontrolledProps &
62
+ Pick<FilterBase, 'disabled'> & {
63
+ readonly field: FieldHandle<EnumField<T>>
64
+ /** Defaults to `field.options` (every declared value, labelled by `store.labels()`). */
65
+ readonly options?: readonly ViewTabsOption<T>[]
66
+ /**
67
+ * The switch's accessible name. Both forms render without a visible Mantine `label` (a home has
68
+ * no room for one), so without this the desktop track is an unnamed `radiogroup` and the phone
69
+ * form an unnamed `combobox`.
70
+ *
71
+ * @default 'View'
72
+ */
73
+ readonly label?: string
74
+ }
64
75
 
65
76
  export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
66
77
  // F-ERR-1 — without this a missing `field` surfaces as `undefined is not an object
@@ -68,7 +79,7 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
68
79
  assertRequiredProps('ViewTabs', props, ['field'], {
69
80
  field: 'bind it to a store field (`store.field.<name>`), never a value/onChange pair.',
70
81
  })
71
- const { field, options, label = 'View', className, style } = props
82
+ const { field, options, label = 'View', disabled, className, style } = props
72
83
  const [value, setValue] = field.use()
73
84
  const surface = useFilterSurface()
74
85
  const inPanel = surface === 'panel'
@@ -84,7 +95,7 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
84
95
  setValue(next as T)
85
96
  }
86
97
 
87
- const phoneData = phone.map((option) => ({ value: option.value, label: option.label }))
98
+ const phoneData = toSegmentData(phone)
88
99
  const phoneCountFits = phone.length <= PHONE_TRACK_MAX
89
100
  // Called UNCONDITIONALLY, even on the panel branch that never renders the phone form — a hook
90
101
  // may not sit behind the `if (inPanel) return …` below (`react-hooks/rules-of-hooks`), and
@@ -106,7 +117,13 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
106
117
  {...(className !== undefined && { className })}
107
118
  {...(style !== undefined && { style })}
108
119
  >
109
- <PanelChoice nameProps={nameProps} value={value} options={all} onChange={commit} />
120
+ <PanelChoice
121
+ nameProps={nameProps}
122
+ value={value}
123
+ options={all}
124
+ onChange={commit}
125
+ {...(disabled === true && { disabled: true })}
126
+ />
110
127
  </PanelRow>
111
128
  )
112
129
  }
@@ -120,8 +137,9 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
120
137
  aria-label={label}
121
138
  visibleFrom="sm"
122
139
  size="ctl"
140
+ {...(disabled === true && { disabled: true })}
123
141
  value={value}
124
- data={desktop.map((option) => ({ value: option.value, label: option.label }))}
142
+ data={toSegmentData(desktop)}
125
143
  onChange={commit}
126
144
  {...(className !== undefined && { className })}
127
145
  {...(style !== undefined && { style })}
@@ -133,6 +151,7 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
133
151
  hiddenFrom="sm"
134
152
  size="ctl"
135
153
  fullWidth
154
+ {...(disabled === true && { disabled: true })}
136
155
  value={value}
137
156
  data={phoneData}
138
157
  onChange={commit}
@@ -151,6 +170,7 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
151
170
  {...(style !== undefined && { style })}
152
171
  value={value}
153
172
  allowDeselect={false}
173
+ {...(disabled === true && { disabled: true })}
154
174
  data={phoneData}
155
175
  onChange={(next) => {
156
176
  if (next !== null) commit(next)