@cognitap/radiant 0.0.32 → 0.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -8
- package/dist/library/Button.d.ts +17 -2
- package/dist/library/Button.d.ts.map +1 -1
- package/dist/library/Button.js +15 -3
- package/dist/library/Button.js.map +1 -1
- package/dist/library/Combobox.d.ts +2 -1
- package/dist/library/Combobox.d.ts.map +1 -1
- package/dist/library/Combobox.js.map +1 -1
- package/dist/library/Menu.d.ts +3 -2
- package/dist/library/Menu.d.ts.map +1 -1
- package/dist/library/Menu.js.map +1 -1
- package/dist/library/Pagination.d.ts +2 -1
- package/dist/library/Pagination.d.ts.map +1 -1
- package/dist/library/Pagination.js.map +1 -1
- package/dist/library/Select.d.ts +2 -1
- package/dist/library/Select.d.ts.map +1 -1
- package/dist/library/Select.js.map +1 -1
- package/dist/library/Tabs.d.ts +1 -1
- package/dist/library/Tabs.d.ts.map +1 -1
- package/dist/library/Tabs.js +1 -1
- package/dist/library/Tabs.js.map +1 -1
- package/dist/library/ToggleGroup.d.ts +67 -0
- package/dist/library/ToggleGroup.d.ts.map +1 -0
- package/dist/library/ToggleGroup.js +165 -0
- package/dist/library/ToggleGroup.js.map +1 -0
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +5 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +2 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +1 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/internal/types.d.ts +3 -0
- package/dist/library/internal/types.d.ts.map +1 -0
- package/dist/library/internal/types.js +2 -0
- package/dist/library/internal/types.js.map +1 -0
- package/dist/library/motion/host.d.ts +2 -1
- package/dist/library/motion/host.d.ts.map +1 -1
- package/dist/library/motion/host.js.map +1 -1
- package/dist/library/motion/indicator.d.ts +5 -0
- package/dist/library/motion/indicator.d.ts.map +1 -0
- package/dist/library/motion/indicator.js +73 -0
- package/dist/library/motion/indicator.js.map +1 -0
- package/dist/library/motion/react.d.ts.map +1 -1
- package/dist/library/motion/react.js +3 -1
- package/dist/library/motion/react.js.map +1 -1
- package/dist/library/react/useRovingFocus.d.ts +20 -0
- package/dist/library/react/useRovingFocus.d.ts.map +1 -0
- package/dist/library/react/useRovingFocus.js +109 -0
- package/dist/library/react/useRovingFocus.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +9 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +9 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +22 -18
- package/package.json +1 -1
- package/src/Button.tsx +53 -21
- package/src/Combobox.tsx +2 -1
- package/src/Menu.tsx +3 -2
- package/src/Pagination.tsx +2 -1
- package/src/Select.tsx +2 -1
- package/src/Tabs.tsx +6 -0
- package/src/ToggleGroup.tsx +363 -0
- package/src/cn.ts +5 -0
- package/src/index.ts +10 -0
- package/src/internal/types.ts +4 -0
- package/src/motion/host.ts +2 -1
- package/src/motion/indicator.ts +77 -0
- package/src/motion/react.ts +3 -1
- package/src/react/useRovingFocus.ts +147 -0
- package/src/themes/components.ts +9 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/* oxlint-disable jsx-a11y/prefer-tag-over-role -- Pressed action groups are not form fieldsets. */
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
createContext,
|
|
6
|
+
useContext,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
type ComponentProps
|
|
11
|
+
} from 'react'
|
|
12
|
+
import { Button, type ButtonProps } from './Button.js'
|
|
13
|
+
import { cn } from './cn.js'
|
|
14
|
+
import { joinIds } from './internal/ids.js'
|
|
15
|
+
import { isItemOpen, nextSelection, sameSelection, type Selection } from './internal/selection.js'
|
|
16
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
17
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
18
|
+
import { CompositeFocusContext, useRovingFocus } from './react/useRovingFocus.js'
|
|
19
|
+
import { useTransitionHost } from './motion/host.js'
|
|
20
|
+
import type { ControlSize } from './presentation.js'
|
|
21
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
22
|
+
|
|
23
|
+
type Name =
|
|
24
|
+
| { 'aria-label': string; 'aria-labelledby'?: string }
|
|
25
|
+
| { 'aria-label'?: string; 'aria-labelledby': string }
|
|
26
|
+
type Owner<Value> =
|
|
27
|
+
| { value: Value; defaultValue?: never }
|
|
28
|
+
| { value?: undefined; defaultValue?: Value }
|
|
29
|
+
type PartStyle = { className?: string; style?: ThemeStyle }
|
|
30
|
+
export type ToggleGroupOrientation = 'horizontal' | 'vertical'
|
|
31
|
+
export type ToggleGroupAppearance = 'buttons' | 'segmented'
|
|
32
|
+
export type ToggleGroupValue = Selection
|
|
33
|
+
type Base = Omit<
|
|
34
|
+
ComponentProps<'div'>,
|
|
35
|
+
'style' | 'role' | 'defaultValue' | 'contentEditable' | 'dangerouslySetInnerHTML' | 'tabIndex'
|
|
36
|
+
> &
|
|
37
|
+
Name & {
|
|
38
|
+
style?: ThemeStyle
|
|
39
|
+
orientation?: ToggleGroupOrientation
|
|
40
|
+
loop?: boolean
|
|
41
|
+
disabled?: boolean
|
|
42
|
+
controlSize?: ControlSize
|
|
43
|
+
motion?: 'inherit' | 'off'
|
|
44
|
+
parts?: { indicator?: PartStyle }
|
|
45
|
+
'data-slot'?: string
|
|
46
|
+
}
|
|
47
|
+
export type ToggleGroupProps = Base &
|
|
48
|
+
(
|
|
49
|
+
| (Owner<string | null> & {
|
|
50
|
+
type?: 'single'
|
|
51
|
+
appearance?: ToggleGroupAppearance
|
|
52
|
+
onValueChange?: (value: string | null) => void
|
|
53
|
+
})
|
|
54
|
+
| (Owner<string[]> & {
|
|
55
|
+
type: 'multiple'
|
|
56
|
+
appearance?: 'buttons'
|
|
57
|
+
onValueChange?: (value: string[]) => void
|
|
58
|
+
})
|
|
59
|
+
)
|
|
60
|
+
export type ToggleGroupItemProps = Omit<
|
|
61
|
+
ButtonProps,
|
|
62
|
+
| 'pressed'
|
|
63
|
+
| 'defaultPressed'
|
|
64
|
+
| 'onPressedChange'
|
|
65
|
+
| 'aria-pressed'
|
|
66
|
+
| 'value'
|
|
67
|
+
| 'type'
|
|
68
|
+
| 'role'
|
|
69
|
+
| 'tabIndex'
|
|
70
|
+
> & { value: string }
|
|
71
|
+
export type ToggleGroupState = {
|
|
72
|
+
type: 'single' | 'multiple'
|
|
73
|
+
value: ToggleGroupValue
|
|
74
|
+
controlled: boolean
|
|
75
|
+
disabled: boolean
|
|
76
|
+
orientation: ToggleGroupOrientation
|
|
77
|
+
loop: boolean
|
|
78
|
+
controlSize: ControlSize
|
|
79
|
+
appearance: ToggleGroupAppearance
|
|
80
|
+
motion: 'inherit' | 'off'
|
|
81
|
+
setItemPressed: (value: string, pressed: boolean) => void
|
|
82
|
+
}
|
|
83
|
+
const Context = createContext<ToggleGroupState | null>(null)
|
|
84
|
+
export function useToggleGroup(): ToggleGroupState | null {
|
|
85
|
+
return useContext(Context)
|
|
86
|
+
}
|
|
87
|
+
function useOwner() {
|
|
88
|
+
const state = useToggleGroup()
|
|
89
|
+
if (!state) throw new Error('ToggleGroupItem must render inside ToggleGroup.')
|
|
90
|
+
return state
|
|
91
|
+
}
|
|
92
|
+
const rootClass =
|
|
93
|
+
'relative isolate inline-flex w-fit max-w-full flex-wrap gap-[var(--rad-toggle-group-gap,0.25rem)]'
|
|
94
|
+
const segmentedClass =
|
|
95
|
+
'rounded-[var(--rad-toggle-group-radius,var(--rad-control-radius,0.5rem))] bg-[var(--rad-toggle-group-background,var(--rad-color-neutral,oklch(0.955_0.004_286)))] p-[var(--rad-toggle-group-padding,0.25rem)]'
|
|
96
|
+
|
|
97
|
+
export function ToggleGroup(allProps: ToggleGroupProps) {
|
|
98
|
+
const {
|
|
99
|
+
ref,
|
|
100
|
+
type = 'single',
|
|
101
|
+
value,
|
|
102
|
+
defaultValue,
|
|
103
|
+
onValueChange: _onValueChange,
|
|
104
|
+
appearance = 'buttons',
|
|
105
|
+
orientation = 'horizontal',
|
|
106
|
+
loop = true,
|
|
107
|
+
disabled = false,
|
|
108
|
+
controlSize = 'medium',
|
|
109
|
+
motion = 'inherit',
|
|
110
|
+
parts,
|
|
111
|
+
className,
|
|
112
|
+
children,
|
|
113
|
+
onFocus,
|
|
114
|
+
onBlur,
|
|
115
|
+
onKeyDown,
|
|
116
|
+
...props
|
|
117
|
+
} = allProps
|
|
118
|
+
const [initialType] = useState(type)
|
|
119
|
+
if (type !== initialType)
|
|
120
|
+
throw new TypeError('ToggleGroup cannot change type during a mount. Remount with a new key.')
|
|
121
|
+
if (!props['aria-label']?.trim() && !props['aria-labelledby']?.trim())
|
|
122
|
+
throw new TypeError('ToggleGroup requires aria-label or aria-labelledby.')
|
|
123
|
+
if (type === 'multiple' && appearance === 'segmented')
|
|
124
|
+
throw new TypeError('The segmented indicator requires single selection.')
|
|
125
|
+
const {
|
|
126
|
+
value: current,
|
|
127
|
+
request,
|
|
128
|
+
controlled
|
|
129
|
+
} = useControllableState<Selection>({
|
|
130
|
+
name: 'ToggleGroup',
|
|
131
|
+
value,
|
|
132
|
+
defaultValue,
|
|
133
|
+
initialValue: type === 'multiple' ? [] : null,
|
|
134
|
+
equal: sameSelection,
|
|
135
|
+
onChange: next => {
|
|
136
|
+
if (allProps.type === 'multiple' && Array.isArray(next)) allProps.onValueChange?.(next)
|
|
137
|
+
else if (allProps.type !== 'multiple' && !Array.isArray(next)) allProps.onValueChange?.(next)
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
if (
|
|
141
|
+
type === 'multiple'
|
|
142
|
+
? !Array.isArray(current) ||
|
|
143
|
+
current.some(item => typeof item !== 'string') ||
|
|
144
|
+
new Set(current).size !== current.length
|
|
145
|
+
: current !== null && typeof current !== 'string'
|
|
146
|
+
)
|
|
147
|
+
throw new TypeError(
|
|
148
|
+
'ToggleGroup values must match its single or multiple type and contain distinct strings.'
|
|
149
|
+
)
|
|
150
|
+
const host = useRef<HTMLDivElement>(null),
|
|
151
|
+
hostRef = useComposedRefs(host, ref),
|
|
152
|
+
parentOwnsFocus = useContext(CompositeFocusContext)
|
|
153
|
+
const roving = useRovingFocus({
|
|
154
|
+
host,
|
|
155
|
+
selector: 'button[data-slot~="toggle-group-item"]',
|
|
156
|
+
scope: '[data-slot~="toggle-group"]',
|
|
157
|
+
preferred: Array.isArray(current) ? current[0] : current,
|
|
158
|
+
orientation,
|
|
159
|
+
loop,
|
|
160
|
+
active: !parentOwnsFocus
|
|
161
|
+
})
|
|
162
|
+
const state: ToggleGroupState = {
|
|
163
|
+
type,
|
|
164
|
+
value: current,
|
|
165
|
+
controlled,
|
|
166
|
+
disabled,
|
|
167
|
+
orientation,
|
|
168
|
+
loop,
|
|
169
|
+
controlSize,
|
|
170
|
+
appearance,
|
|
171
|
+
motion,
|
|
172
|
+
setItemPressed(item, pressed) {
|
|
173
|
+
if (!disabled) request(current => nextSelection(current, item, pressed))
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return (
|
|
177
|
+
<Context.Provider value={state}>
|
|
178
|
+
{/* oxlint-disable-next-line jsx-a11y/no-noninteractive-element-interactions -- The group delegates roving keyboard navigation from its native button children. */}
|
|
179
|
+
<div
|
|
180
|
+
{...props}
|
|
181
|
+
ref={hostRef}
|
|
182
|
+
role="group"
|
|
183
|
+
tabIndex={undefined}
|
|
184
|
+
contentEditable={undefined}
|
|
185
|
+
dangerouslySetInnerHTML={undefined}
|
|
186
|
+
className={cn(
|
|
187
|
+
rootClass,
|
|
188
|
+
orientation === 'vertical' ? 'flex-col items-stretch' : 'items-center',
|
|
189
|
+
appearance === 'segmented' && segmentedClass,
|
|
190
|
+
className
|
|
191
|
+
)}
|
|
192
|
+
data-slot={joinIds('toggle-group', props['data-slot'])}
|
|
193
|
+
data-type={type}
|
|
194
|
+
data-appearance={appearance}
|
|
195
|
+
data-orientation={orientation}
|
|
196
|
+
data-control-size={controlSize}
|
|
197
|
+
data-disabled={disabled ? '' : undefined}
|
|
198
|
+
onFocus={event => {
|
|
199
|
+
onFocus?.(event)
|
|
200
|
+
roving.onFocus(event)
|
|
201
|
+
}}
|
|
202
|
+
onBlur={event => {
|
|
203
|
+
onBlur?.(event)
|
|
204
|
+
roving.onBlur(event)
|
|
205
|
+
}}
|
|
206
|
+
onKeyDown={event => {
|
|
207
|
+
onKeyDown?.(event)
|
|
208
|
+
roving.onKeyDown(event)
|
|
209
|
+
}}
|
|
210
|
+
>
|
|
211
|
+
{appearance === 'segmented' ? (
|
|
212
|
+
<Indicator
|
|
213
|
+
value={current}
|
|
214
|
+
motion={motion}
|
|
215
|
+
part={parts?.indicator}
|
|
216
|
+
/>
|
|
217
|
+
) : null}
|
|
218
|
+
{children}
|
|
219
|
+
</div>
|
|
220
|
+
</Context.Provider>
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
export function ToggleGroupItem({
|
|
224
|
+
value,
|
|
225
|
+
disabled = false,
|
|
226
|
+
controlSize,
|
|
227
|
+
variant = 'outline',
|
|
228
|
+
tone = 'neutral',
|
|
229
|
+
className,
|
|
230
|
+
...props
|
|
231
|
+
}: ToggleGroupItemProps) {
|
|
232
|
+
const group = useOwner()
|
|
233
|
+
if (typeof value !== 'string') throw new TypeError('ToggleGroupItem requires a string value.')
|
|
234
|
+
const pressed = isItemOpen(group.value, value),
|
|
235
|
+
unavailable = disabled || group.disabled
|
|
236
|
+
return (
|
|
237
|
+
<Button
|
|
238
|
+
{...props}
|
|
239
|
+
type="button"
|
|
240
|
+
role="button"
|
|
241
|
+
value={value}
|
|
242
|
+
tabIndex={0}
|
|
243
|
+
disabled={unavailable}
|
|
244
|
+
pressed={pressed}
|
|
245
|
+
defaultPressed={undefined}
|
|
246
|
+
aria-pressed={undefined}
|
|
247
|
+
onPressedChange={next => group.setItemPressed(value, next)}
|
|
248
|
+
controlSize={controlSize ?? group.controlSize}
|
|
249
|
+
variant={group.appearance === 'segmented' ? 'ghost' : variant}
|
|
250
|
+
tone={tone}
|
|
251
|
+
motion={group.motion === 'off' ? 'off' : props.motion}
|
|
252
|
+
data-slot={joinIds('toggle-group-item', props['data-slot'])}
|
|
253
|
+
data-value={value}
|
|
254
|
+
data-state={pressed ? 'on' : 'off'}
|
|
255
|
+
data-disabled={unavailable ? '' : undefined}
|
|
256
|
+
className={cn(
|
|
257
|
+
'relative z-10',
|
|
258
|
+
group.appearance === 'segmented' &&
|
|
259
|
+
'bg-transparent hover:bg-transparent active:bg-transparent disabled:bg-transparent aria-busy:bg-transparent [background-image:none] shadow-none inset-shadow-none [border-color:transparent]',
|
|
260
|
+
className
|
|
261
|
+
)}
|
|
262
|
+
/>
|
|
263
|
+
)
|
|
264
|
+
}
|
|
265
|
+
function Indicator({
|
|
266
|
+
value,
|
|
267
|
+
motion,
|
|
268
|
+
part
|
|
269
|
+
}: {
|
|
270
|
+
value: Selection
|
|
271
|
+
motion: 'inherit' | 'off'
|
|
272
|
+
part?: PartStyle
|
|
273
|
+
}) {
|
|
274
|
+
const node = useRef<HTMLSpanElement>(null),
|
|
275
|
+
{ ref: indicatorRef } = useTransitionHost('indicator', node, motion === 'off')
|
|
276
|
+
useLayoutEffect(() => {
|
|
277
|
+
const mark = node.current,
|
|
278
|
+
root = mark?.closest<HTMLDivElement>('[data-slot~="toggle-group"]')
|
|
279
|
+
if (!root || !mark) return
|
|
280
|
+
const view = root.ownerDocument.defaultView!
|
|
281
|
+
const items = () =>
|
|
282
|
+
Array.from(
|
|
283
|
+
root.querySelectorAll<HTMLButtonElement>('button[data-slot~="toggle-group-item"]')
|
|
284
|
+
).filter(item => item.closest('[data-slot~="toggle-group"]') === root)
|
|
285
|
+
const measure = () => {
|
|
286
|
+
const selected = items().find(
|
|
287
|
+
item => item.getAttribute('aria-pressed') === 'true' && item.getClientRects().length > 0
|
|
288
|
+
)
|
|
289
|
+
mark.hidden = !selected
|
|
290
|
+
if (!selected) {
|
|
291
|
+
mark.removeAttribute('data-rad-indicator-target')
|
|
292
|
+
return
|
|
293
|
+
}
|
|
294
|
+
const target = [
|
|
295
|
+
selected.offsetLeft,
|
|
296
|
+
selected.offsetTop,
|
|
297
|
+
selected.offsetWidth,
|
|
298
|
+
selected.offsetHeight
|
|
299
|
+
]
|
|
300
|
+
for (const [index, axis] of ['x', 'y', 'width', 'height'].entries())
|
|
301
|
+
mark.style.setProperty(`--_rad-indicator-target-${axis}`, `${target[index]}px`)
|
|
302
|
+
const serialized = JSON.stringify(target)
|
|
303
|
+
if (mark.getAttribute('data-rad-indicator-target') !== serialized)
|
|
304
|
+
mark.setAttribute('data-rad-indicator-target', serialized)
|
|
305
|
+
}
|
|
306
|
+
let frame = 0
|
|
307
|
+
const schedule = () => {
|
|
308
|
+
if (!frame)
|
|
309
|
+
frame = view.requestAnimationFrame(() => {
|
|
310
|
+
frame = 0
|
|
311
|
+
measure()
|
|
312
|
+
})
|
|
313
|
+
}
|
|
314
|
+
const resize = new view.ResizeObserver(schedule)
|
|
315
|
+
const observe = () => {
|
|
316
|
+
resize.disconnect()
|
|
317
|
+
resize.observe(root)
|
|
318
|
+
items().forEach(item => resize.observe(item))
|
|
319
|
+
}
|
|
320
|
+
const changes = new view.MutationObserver(records => {
|
|
321
|
+
if (records.some(record => record.target !== mark && !mark.contains(record.target))) {
|
|
322
|
+
observe()
|
|
323
|
+
schedule()
|
|
324
|
+
}
|
|
325
|
+
})
|
|
326
|
+
changes.observe(root, {
|
|
327
|
+
subtree: true,
|
|
328
|
+
childList: true,
|
|
329
|
+
characterData: true,
|
|
330
|
+
attributes: true,
|
|
331
|
+
attributeFilter: ['aria-pressed', 'class', 'style', 'hidden', 'dir']
|
|
332
|
+
})
|
|
333
|
+
observe()
|
|
334
|
+
measure()
|
|
335
|
+
view.addEventListener('resize', schedule)
|
|
336
|
+
root.ownerDocument.fonts?.addEventListener('loadingdone', schedule)
|
|
337
|
+
return () => {
|
|
338
|
+
changes.disconnect()
|
|
339
|
+
resize.disconnect()
|
|
340
|
+
view.cancelAnimationFrame(frame)
|
|
341
|
+
view.removeEventListener('resize', schedule)
|
|
342
|
+
root.ownerDocument.fonts?.removeEventListener('loadingdone', schedule)
|
|
343
|
+
}
|
|
344
|
+
}, [value])
|
|
345
|
+
return (
|
|
346
|
+
<span
|
|
347
|
+
ref={indicatorRef}
|
|
348
|
+
aria-hidden="true"
|
|
349
|
+
data-rad-indicator=""
|
|
350
|
+
hidden={value === null}
|
|
351
|
+
className="pointer-events-none absolute top-0 left-0 z-0 [translate:var(--_rad-indicator-x,var(--_rad-indicator-target-x,0px))_var(--_rad-indicator-y,var(--_rad-indicator-target-y,0px))] [width:var(--_rad-indicator-width,var(--_rad-indicator-target-width,0px))] [height:var(--_rad-indicator-height,var(--_rad-indicator-target-height,0px))]"
|
|
352
|
+
>
|
|
353
|
+
<span
|
|
354
|
+
data-slot="toggle-group-indicator"
|
|
355
|
+
className={cn(
|
|
356
|
+
'block size-full rounded-[var(--rad-toggle-group-indicator-radius,var(--rad-control-radius,0.5rem))] bg-[var(--rad-toggle-group-indicator-color,var(--rad-color-surface,white))] [box-shadow:var(--rad-toggle-group-indicator-shadow,var(--rad-surface-shadow,0_1px_3px_#0002))] forced-colors:bg-[Canvas] forced-colors:[outline:1px_solid_Highlight]',
|
|
357
|
+
part?.className
|
|
358
|
+
)}
|
|
359
|
+
style={part?.style}
|
|
360
|
+
/>
|
|
361
|
+
</span>
|
|
362
|
+
)
|
|
363
|
+
}
|
package/src/cn.ts
CHANGED
|
@@ -86,6 +86,7 @@ export const cn: CnFunction = createCn({
|
|
|
86
86
|
],
|
|
87
87
|
mb: ['mb-(--rad-fieldset-gap)'],
|
|
88
88
|
gap: [
|
|
89
|
+
'gap-(--rad-toggle-group-gap)',
|
|
89
90
|
'gap-(--rad-checkbox-group-gap)',
|
|
90
91
|
'gap-(--rad-fieldset-gap)',
|
|
91
92
|
'gap-(--rad-pagination-gap)',
|
|
@@ -112,6 +113,7 @@ export const cn: CnFunction = createCn({
|
|
|
112
113
|
'px-(--rad-tabs-trigger-padding)'
|
|
113
114
|
],
|
|
114
115
|
p: [
|
|
116
|
+
'p-(--rad-toggle-group-padding)',
|
|
115
117
|
'p-(--rad-toast-padding)',
|
|
116
118
|
'p-(--rad-alert-padding)',
|
|
117
119
|
'p-(--rad-menu-padding)',
|
|
@@ -124,6 +126,8 @@ export const cn: CnFunction = createCn({
|
|
|
124
126
|
'p-(--rad-tabs-content-padding)'
|
|
125
127
|
],
|
|
126
128
|
rounded: [
|
|
129
|
+
'rounded-(--rad-toggle-group-radius)',
|
|
130
|
+
'rounded-(--rad-toggle-group-indicator-radius)',
|
|
127
131
|
'rounded-(--rad-scroll-area-radius)',
|
|
128
132
|
'rounded-(--rad-table-radius)',
|
|
129
133
|
'rounded-(--rad-pagination-radius)',
|
|
@@ -184,6 +188,7 @@ export const cn: CnFunction = createCn({
|
|
|
184
188
|
'border-(--rad-switch-border-style)'
|
|
185
189
|
],
|
|
186
190
|
shadow: [
|
|
191
|
+
'shadow-(--rad-toggle-group-indicator-shadow)',
|
|
187
192
|
'shadow-(--rad-toast-shadow)',
|
|
188
193
|
'shadow-(--rad-alert-shadow)',
|
|
189
194
|
'shadow-(--rad-menu-shadow)',
|
package/src/index.ts
CHANGED
|
@@ -384,3 +384,13 @@ export type { AspectRatioProps } from './AspectRatio.js'
|
|
|
384
384
|
|
|
385
385
|
export { ScrollArea } from './ScrollArea.js'
|
|
386
386
|
export type { ScrollAreaProps, ScrollAreaOrientation, ScrollAreaVisibility } from './ScrollArea.js'
|
|
387
|
+
|
|
388
|
+
export { ToggleGroup, ToggleGroupItem, useToggleGroup } from './ToggleGroup.js'
|
|
389
|
+
export type {
|
|
390
|
+
ToggleGroupProps,
|
|
391
|
+
ToggleGroupItemProps,
|
|
392
|
+
ToggleGroupState,
|
|
393
|
+
ToggleGroupValue,
|
|
394
|
+
ToggleGroupOrientation,
|
|
395
|
+
ToggleGroupAppearance
|
|
396
|
+
} from './ToggleGroup.js'
|
package/src/motion/host.ts
CHANGED
|
@@ -15,6 +15,7 @@ export interface MotionHostRuntime {
|
|
|
15
15
|
disclosure?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
|
|
16
16
|
dialog?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
|
|
17
17
|
popup?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
|
|
18
|
+
indicator?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
|
|
18
19
|
}
|
|
19
20
|
export const MotionHostContext = createContext<MotionHostRuntime | null>(null)
|
|
20
21
|
/** Engine-free bridge: only the opt-in provider supplies implementation. */
|
|
@@ -59,7 +60,7 @@ export function usePressHost<Node extends HTMLElement>(
|
|
|
59
60
|
|
|
60
61
|
/** Shared lifecycle for native presentation adapters; no engine enters the root path. */
|
|
61
62
|
export function useTransitionHost<Node extends HTMLElement>(
|
|
62
|
-
kind: 'disclosure' | 'dialog' | 'popup',
|
|
63
|
+
kind: 'disclosure' | 'dialog' | 'popup' | 'indicator',
|
|
63
64
|
ref: Ref<Node> | undefined,
|
|
64
65
|
disabled: boolean
|
|
65
66
|
) {
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { createSpring } from './runtime.js'
|
|
2
|
+
import { resolveMotion, type MotionProfile } from './config.js'
|
|
3
|
+
import type { MotionHostBinding } from './host.js'
|
|
4
|
+
|
|
5
|
+
const axes = ['x', 'y', 'width', 'height'] as const
|
|
6
|
+
/** Follow an engine-free host's local rectangle; geometry and selection belong to that host. */
|
|
7
|
+
export function bindIndicatorMotion(node: HTMLElement, initial: MotionProfile): MotionHostBinding {
|
|
8
|
+
const view = node.ownerDocument.defaultView!,
|
|
9
|
+
query = view.matchMedia('(prefers-reduced-motion: reduce)')
|
|
10
|
+
let profile = initial,
|
|
11
|
+
visible = false
|
|
12
|
+
const read = (): number[] | null => {
|
|
13
|
+
const raw = node.getAttribute('data-rad-indicator-target')
|
|
14
|
+
if (!raw || node.hidden) return null
|
|
15
|
+
const value: unknown = JSON.parse(raw)
|
|
16
|
+
return Array.isArray(value) &&
|
|
17
|
+
value.length === 4 &&
|
|
18
|
+
value.every(item => typeof item === 'number' && Number.isFinite(item))
|
|
19
|
+
? value
|
|
20
|
+
: null
|
|
21
|
+
}
|
|
22
|
+
const start = read() ?? [0, 0, 0, 0],
|
|
23
|
+
previous = axes.map(axis => node.style.getPropertyValue(`--_rad-indicator-${axis}`)),
|
|
24
|
+
owned = [...previous]
|
|
25
|
+
const springs = start.map(value =>
|
|
26
|
+
createSpring(value, { ...resolveMotion(profile, 'follow', query.matches), reducedValue: value })
|
|
27
|
+
)
|
|
28
|
+
const offs = springs.map((spring, index) =>
|
|
29
|
+
spring.subscribe(value => {
|
|
30
|
+
owned[index] = `${index > 1 ? Math.max(0, value) : value}px`
|
|
31
|
+
node.style.setProperty(`--_rad-indicator-${axes[index]}`, owned[index])
|
|
32
|
+
})
|
|
33
|
+
)
|
|
34
|
+
const sync = () => {
|
|
35
|
+
const target = read()
|
|
36
|
+
if (!target) {
|
|
37
|
+
springs.forEach(spring => spring.stop())
|
|
38
|
+
visible = false
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
const resolved = resolveMotion(profile, 'follow', query.matches)
|
|
42
|
+
springs.forEach((spring, index) => {
|
|
43
|
+
spring.configure({ ...resolved, reducedValue: target[index] })
|
|
44
|
+
if (!visible || !resolved.enabled) spring.jump(target[index])
|
|
45
|
+
else spring.to(target[index])
|
|
46
|
+
owned[index] = `${index > 1 ? Math.max(0, spring.get()) : spring.get()}px`
|
|
47
|
+
node.style.setProperty(`--_rad-indicator-${axes[index]}`, owned[index])
|
|
48
|
+
})
|
|
49
|
+
visible = true
|
|
50
|
+
}
|
|
51
|
+
const observer = new view.MutationObserver(sync)
|
|
52
|
+
observer.observe(node, {
|
|
53
|
+
attributes: true,
|
|
54
|
+
attributeFilter: ['data-rad-indicator-target', 'hidden']
|
|
55
|
+
})
|
|
56
|
+
query.addEventListener('change', sync)
|
|
57
|
+
sync()
|
|
58
|
+
return {
|
|
59
|
+
update(next) {
|
|
60
|
+
profile = next
|
|
61
|
+
sync()
|
|
62
|
+
},
|
|
63
|
+
destroy() {
|
|
64
|
+
observer.disconnect()
|
|
65
|
+
query.removeEventListener('change', sync)
|
|
66
|
+
offs.forEach(off => off())
|
|
67
|
+
springs.forEach(spring => spring.destroy())
|
|
68
|
+
axes.forEach((axis, index) => {
|
|
69
|
+
const name = `--_rad-indicator-${axis}`
|
|
70
|
+
if (node.style.getPropertyValue(name) === owned[index]) {
|
|
71
|
+
if (previous[index]) node.style.setProperty(name, previous[index])
|
|
72
|
+
else node.style.removeProperty(name)
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
package/src/motion/react.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { MotionHostContext } from './host.js'
|
|
|
13
13
|
import { bindDisclosureMotion, bindDialogMotion } from './transitions.js'
|
|
14
14
|
import { bindPressMotion } from './press.js'
|
|
15
15
|
import { bindPopupMotion } from './popup.js'
|
|
16
|
+
import { bindIndicatorMotion } from './indicator.js'
|
|
16
17
|
import { MotionProfileContext } from './context.js'
|
|
17
18
|
import { resolveMotion, type MotionInteraction } from './config.js'
|
|
18
19
|
import { createSpringDrag, type SpringDragController, type SpringDragOptions } from './drag.js'
|
|
@@ -136,7 +137,8 @@ const hostRuntime = {
|
|
|
136
137
|
press: bindPressMotion,
|
|
137
138
|
disclosure: bindDisclosureMotion,
|
|
138
139
|
dialog: bindDialogMotion,
|
|
139
|
-
popup: bindPopupMotion
|
|
140
|
+
popup: bindPopupMotion,
|
|
141
|
+
indicator: bindIndicatorMotion
|
|
140
142
|
}
|
|
141
143
|
/** Opt into native Radiant spring interactions without adding a DOM wrapper. */
|
|
142
144
|
export function MotionProvider({ children }: { children?: ReactNode }) {
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useLayoutEffect,
|
|
6
|
+
type KeyboardEvent,
|
|
7
|
+
type FocusEvent,
|
|
8
|
+
type RefObject
|
|
9
|
+
} from 'react'
|
|
10
|
+
|
|
11
|
+
/** A surrounding composite may own all of its nested groups' tab stops. */
|
|
12
|
+
export const CompositeFocusContext = createContext(false)
|
|
13
|
+
|
|
14
|
+
type Options<Node extends HTMLElement> = {
|
|
15
|
+
host: RefObject<Node | null>
|
|
16
|
+
selector: string
|
|
17
|
+
scope: string
|
|
18
|
+
preferred?: string | null
|
|
19
|
+
orientation: 'horizontal' | 'vertical'
|
|
20
|
+
loop: boolean
|
|
21
|
+
active?: boolean
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Native hosts and DOM order stay authoritative, including dynamic disabled items. */
|
|
25
|
+
export function useRovingFocus<Node extends HTMLElement>({
|
|
26
|
+
host,
|
|
27
|
+
selector,
|
|
28
|
+
scope,
|
|
29
|
+
preferred,
|
|
30
|
+
orientation,
|
|
31
|
+
loop,
|
|
32
|
+
active = true
|
|
33
|
+
}: Options<Node>) {
|
|
34
|
+
const items = (node: Node) =>
|
|
35
|
+
Array.from(node.querySelectorAll<HTMLButtonElement>(selector)).filter(
|
|
36
|
+
item => item.closest(scope) === node
|
|
37
|
+
)
|
|
38
|
+
const enabled = (node: Node) => {
|
|
39
|
+
const view = node.ownerDocument.defaultView!
|
|
40
|
+
return items(node).filter(
|
|
41
|
+
item =>
|
|
42
|
+
!item.matches(':disabled') &&
|
|
43
|
+
item.getAttribute('aria-disabled') !== 'true' &&
|
|
44
|
+
!item.closest('[inert], [hidden]') &&
|
|
45
|
+
item.getClientRects().length > 0 &&
|
|
46
|
+
!['hidden', 'collapse'].includes(view.getComputedStyle(item).visibility)
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
const sync = (node: Node, preferFocus = true) => {
|
|
50
|
+
const all = items(node),
|
|
51
|
+
available = enabled(node)
|
|
52
|
+
const entry =
|
|
53
|
+
(preferFocus && available.find(item => item === node.ownerDocument.activeElement)) ||
|
|
54
|
+
available.find(item => item.dataset.value === preferred) ||
|
|
55
|
+
available[0]
|
|
56
|
+
for (const item of all) {
|
|
57
|
+
const index = item === entry ? 0 : -1
|
|
58
|
+
if (item.tabIndex !== index) item.tabIndex = index
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
const node = host.current
|
|
63
|
+
if (!node || !active) return
|
|
64
|
+
const view = node.ownerDocument.defaultView!
|
|
65
|
+
const reconcile = () => sync(node)
|
|
66
|
+
reconcile()
|
|
67
|
+
const observer = new view.MutationObserver(records => {
|
|
68
|
+
if (
|
|
69
|
+
records.some(
|
|
70
|
+
record =>
|
|
71
|
+
record.type === 'childList' ||
|
|
72
|
+
record.target === node ||
|
|
73
|
+
(record.target instanceof view.Element &&
|
|
74
|
+
(record.target.matches(selector) || record.target.querySelector(selector)))
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
reconcile()
|
|
78
|
+
})
|
|
79
|
+
observer.observe(node, {
|
|
80
|
+
subtree: true,
|
|
81
|
+
childList: true,
|
|
82
|
+
attributes: true,
|
|
83
|
+
attributeFilter: [
|
|
84
|
+
'disabled',
|
|
85
|
+
'aria-disabled',
|
|
86
|
+
'hidden',
|
|
87
|
+
'inert',
|
|
88
|
+
'class',
|
|
89
|
+
'style',
|
|
90
|
+
'tabindex',
|
|
91
|
+
'data-value'
|
|
92
|
+
]
|
|
93
|
+
})
|
|
94
|
+
// Native fieldsets can disable descendants without changing a button attribute.
|
|
95
|
+
for (let ancestor = node.parentElement; ancestor; ancestor = ancestor.parentElement)
|
|
96
|
+
observer.observe(ancestor, {
|
|
97
|
+
attributes: true,
|
|
98
|
+
attributeFilter: ['disabled', 'hidden', 'inert', 'class', 'style']
|
|
99
|
+
})
|
|
100
|
+
const resize = new view.ResizeObserver(reconcile)
|
|
101
|
+
resize.observe(node)
|
|
102
|
+
return () => {
|
|
103
|
+
observer.disconnect()
|
|
104
|
+
resize.disconnect()
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
return {
|
|
108
|
+
onFocus(event: FocusEvent<Node>) {
|
|
109
|
+
if (active) sync(event.currentTarget)
|
|
110
|
+
},
|
|
111
|
+
onBlur(event: FocusEvent<Node>) {
|
|
112
|
+
if (active && !event.currentTarget.contains(event.relatedTarget))
|
|
113
|
+
sync(event.currentTarget, false)
|
|
114
|
+
},
|
|
115
|
+
onKeyDown(event: KeyboardEvent<Node>) {
|
|
116
|
+
if (
|
|
117
|
+
!active ||
|
|
118
|
+
event.defaultPrevented ||
|
|
119
|
+
event.altKey ||
|
|
120
|
+
event.ctrlKey ||
|
|
121
|
+
event.metaKey ||
|
|
122
|
+
event.nativeEvent.isComposing
|
|
123
|
+
)
|
|
124
|
+
return
|
|
125
|
+
const node = event.currentTarget,
|
|
126
|
+
available = enabled(node),
|
|
127
|
+
current = (event.target as Element).closest(selector),
|
|
128
|
+
index = available.findIndex(item => item === current)
|
|
129
|
+
if (index < 0) return
|
|
130
|
+
const rtl = node.ownerDocument.defaultView!.getComputedStyle(node).direction === 'rtl'
|
|
131
|
+
let next: number
|
|
132
|
+
if (event.key === 'Home') next = 0
|
|
133
|
+
else if (event.key === 'End') next = available.length - 1
|
|
134
|
+
else {
|
|
135
|
+
const forward = orientation === 'vertical' ? 'ArrowDown' : rtl ? 'ArrowLeft' : 'ArrowRight',
|
|
136
|
+
backward = orientation === 'vertical' ? 'ArrowUp' : rtl ? 'ArrowRight' : 'ArrowLeft'
|
|
137
|
+
if (event.key !== forward && event.key !== backward) return
|
|
138
|
+
next = index + (event.key === forward ? 1 : -1)
|
|
139
|
+
next = loop
|
|
140
|
+
? (next + available.length) % available.length
|
|
141
|
+
: Math.max(0, Math.min(available.length - 1, next))
|
|
142
|
+
}
|
|
143
|
+
event.preventDefault()
|
|
144
|
+
available[next]?.focus()
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
package/src/themes/components.ts
CHANGED
|
@@ -500,6 +500,15 @@ export const componentRegistry = {
|
|
|
500
500
|
},
|
|
501
501
|
fieldset: { gap: 'css' },
|
|
502
502
|
checkboxGroup: { gap: 'css' },
|
|
503
|
+
toggleGroup: {
|
|
504
|
+
gap: 'css',
|
|
505
|
+
padding: 'css',
|
|
506
|
+
radius: 'css',
|
|
507
|
+
background: 'css',
|
|
508
|
+
indicatorColor: 'css',
|
|
509
|
+
indicatorRadius: 'css',
|
|
510
|
+
indicatorShadow: 'css'
|
|
511
|
+
},
|
|
503
512
|
scrollArea: { size: 'css', radius: 'css', thumbColor: 'css', trackColor: 'css' },
|
|
504
513
|
radioGroup: { gap: 'css' }
|
|
505
514
|
} as const
|