@cognitap/radiant 0.0.32 → 0.0.34
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 +18 -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/ScrollArea.d.ts +2 -9
- package/dist/library/ScrollArea.d.ts.map +1 -1
- package/dist/library/ScrollArea.js +2 -2
- package/dist/library/ScrollArea.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 +61 -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/Toolbar.d.ts +13 -0
- package/dist/library/Toolbar.d.ts.map +1 -0
- package/dist/library/Toolbar.js +32 -0
- package/dist/library/Toolbar.js.map +1 -0
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +8 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +4 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +2 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/internal/accessibleName.d.ts +13 -0
- package/dist/library/internal/accessibleName.d.ts.map +1 -0
- package/dist/library/internal/accessibleName.js +6 -0
- package/dist/library/internal/accessibleName.js.map +1 -0
- 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 +21 -0
- package/dist/library/react/useRovingFocus.d.ts.map +1 -0
- package/dist/library/react/useRovingFocus.js +128 -0
- package/dist/library/react/useRovingFocus.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +15 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +10 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +23 -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/ScrollArea.tsx +4 -6
- package/src/Select.tsx +2 -1
- package/src/Tabs.tsx +6 -0
- package/src/ToggleGroup.tsx +360 -0
- package/src/Toolbar.tsx +79 -0
- package/src/cn.ts +8 -0
- package/src/index.ts +13 -0
- package/src/internal/accessibleName.ts +12 -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 +171 -0
- package/src/themes/components.ts +10 -0
|
@@ -0,0 +1,360 @@
|
|
|
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 { requireAccessibleName, type AccessibleName } from './internal/accessibleName.js'
|
|
14
|
+
import { cn } from './cn.js'
|
|
15
|
+
import { joinIds } from './internal/ids.js'
|
|
16
|
+
import { isItemOpen, nextSelection, sameSelection, type Selection } from './internal/selection.js'
|
|
17
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
18
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
19
|
+
import { CompositeFocusContext, useRovingFocus } from './react/useRovingFocus.js'
|
|
20
|
+
import { useTransitionHost } from './motion/host.js'
|
|
21
|
+
import type { ControlSize } from './presentation.js'
|
|
22
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
23
|
+
|
|
24
|
+
type Owner<Value> =
|
|
25
|
+
| { value: Value; defaultValue?: never }
|
|
26
|
+
| { value?: undefined; defaultValue?: Value }
|
|
27
|
+
type PartStyle = { className?: string; style?: ThemeStyle }
|
|
28
|
+
export type ToggleGroupOrientation = 'horizontal' | 'vertical'
|
|
29
|
+
export type ToggleGroupAppearance = 'buttons' | 'segmented'
|
|
30
|
+
export type ToggleGroupValue = Selection
|
|
31
|
+
type Base = Omit<
|
|
32
|
+
ComponentProps<'div'>,
|
|
33
|
+
'style' | 'role' | 'defaultValue' | 'contentEditable' | 'dangerouslySetInnerHTML' | 'tabIndex'
|
|
34
|
+
> &
|
|
35
|
+
AccessibleName & {
|
|
36
|
+
style?: ThemeStyle
|
|
37
|
+
orientation?: ToggleGroupOrientation
|
|
38
|
+
loop?: boolean
|
|
39
|
+
disabled?: boolean
|
|
40
|
+
controlSize?: ControlSize
|
|
41
|
+
motion?: 'inherit' | 'off'
|
|
42
|
+
parts?: { indicator?: PartStyle }
|
|
43
|
+
'data-slot'?: string
|
|
44
|
+
}
|
|
45
|
+
export type ToggleGroupProps = Base &
|
|
46
|
+
(
|
|
47
|
+
| (Owner<string | null> & {
|
|
48
|
+
type?: 'single'
|
|
49
|
+
appearance?: ToggleGroupAppearance
|
|
50
|
+
onValueChange?: (value: string | null) => void
|
|
51
|
+
})
|
|
52
|
+
| (Owner<string[]> & {
|
|
53
|
+
type: 'multiple'
|
|
54
|
+
appearance?: 'buttons'
|
|
55
|
+
onValueChange?: (value: string[]) => void
|
|
56
|
+
})
|
|
57
|
+
)
|
|
58
|
+
export type ToggleGroupItemProps = Omit<
|
|
59
|
+
ButtonProps,
|
|
60
|
+
| 'pressed'
|
|
61
|
+
| 'defaultPressed'
|
|
62
|
+
| 'onPressedChange'
|
|
63
|
+
| 'aria-pressed'
|
|
64
|
+
| 'value'
|
|
65
|
+
| 'type'
|
|
66
|
+
| 'role'
|
|
67
|
+
| 'tabIndex'
|
|
68
|
+
> & { value: string }
|
|
69
|
+
export type ToggleGroupState = {
|
|
70
|
+
type: 'single' | 'multiple'
|
|
71
|
+
value: ToggleGroupValue
|
|
72
|
+
controlled: boolean
|
|
73
|
+
disabled: boolean
|
|
74
|
+
orientation: ToggleGroupOrientation
|
|
75
|
+
loop: boolean
|
|
76
|
+
controlSize: ControlSize
|
|
77
|
+
appearance: ToggleGroupAppearance
|
|
78
|
+
motion: 'inherit' | 'off'
|
|
79
|
+
setItemPressed: (value: string, pressed: boolean) => void
|
|
80
|
+
}
|
|
81
|
+
const Context = createContext<ToggleGroupState | null>(null)
|
|
82
|
+
export function useToggleGroup(): ToggleGroupState | null {
|
|
83
|
+
return useContext(Context)
|
|
84
|
+
}
|
|
85
|
+
function useOwner() {
|
|
86
|
+
const state = useToggleGroup()
|
|
87
|
+
if (!state) throw new Error('ToggleGroupItem must render inside ToggleGroup.')
|
|
88
|
+
return state
|
|
89
|
+
}
|
|
90
|
+
const rootClass =
|
|
91
|
+
'relative isolate inline-flex w-fit max-w-full flex-wrap gap-[var(--rad-toggle-group-gap,0.25rem)]'
|
|
92
|
+
const segmentedClass =
|
|
93
|
+
'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)]'
|
|
94
|
+
|
|
95
|
+
export function ToggleGroup(allProps: ToggleGroupProps) {
|
|
96
|
+
const {
|
|
97
|
+
ref,
|
|
98
|
+
type = 'single',
|
|
99
|
+
value,
|
|
100
|
+
defaultValue,
|
|
101
|
+
onValueChange: _onValueChange,
|
|
102
|
+
appearance = 'buttons',
|
|
103
|
+
orientation = 'horizontal',
|
|
104
|
+
loop = true,
|
|
105
|
+
disabled = false,
|
|
106
|
+
controlSize = 'medium',
|
|
107
|
+
motion = 'inherit',
|
|
108
|
+
parts,
|
|
109
|
+
className,
|
|
110
|
+
children,
|
|
111
|
+
onFocus,
|
|
112
|
+
onBlur,
|
|
113
|
+
onKeyDown,
|
|
114
|
+
...props
|
|
115
|
+
} = allProps
|
|
116
|
+
const [initialType] = useState(type)
|
|
117
|
+
if (type !== initialType)
|
|
118
|
+
throw new TypeError('ToggleGroup cannot change type during a mount. Remount with a new key.')
|
|
119
|
+
requireAccessibleName('ToggleGroup', props)
|
|
120
|
+
if (type === 'multiple' && appearance === 'segmented')
|
|
121
|
+
throw new TypeError('The segmented indicator requires single selection.')
|
|
122
|
+
const {
|
|
123
|
+
value: current,
|
|
124
|
+
request,
|
|
125
|
+
controlled
|
|
126
|
+
} = useControllableState<Selection>({
|
|
127
|
+
name: 'ToggleGroup',
|
|
128
|
+
value,
|
|
129
|
+
defaultValue,
|
|
130
|
+
initialValue: type === 'multiple' ? [] : null,
|
|
131
|
+
equal: sameSelection,
|
|
132
|
+
onChange: next => {
|
|
133
|
+
if (allProps.type === 'multiple' && Array.isArray(next)) allProps.onValueChange?.(next)
|
|
134
|
+
else if (allProps.type !== 'multiple' && !Array.isArray(next)) allProps.onValueChange?.(next)
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
if (
|
|
138
|
+
type === 'multiple'
|
|
139
|
+
? !Array.isArray(current) ||
|
|
140
|
+
current.some(item => typeof item !== 'string') ||
|
|
141
|
+
new Set(current).size !== current.length
|
|
142
|
+
: current !== null && typeof current !== 'string'
|
|
143
|
+
)
|
|
144
|
+
throw new TypeError(
|
|
145
|
+
'ToggleGroup values must match its single or multiple type and contain distinct strings.'
|
|
146
|
+
)
|
|
147
|
+
const host = useRef<HTMLDivElement>(null),
|
|
148
|
+
hostRef = useComposedRefs(host, ref),
|
|
149
|
+
parentScope = useContext(CompositeFocusContext)
|
|
150
|
+
const roving = useRovingFocus({
|
|
151
|
+
host,
|
|
152
|
+
selector: 'button[data-slot~="toggle-group-item"]',
|
|
153
|
+
scope: '[data-slot~="toggle-group"]',
|
|
154
|
+
preferred: Array.isArray(current) ? current[0] : current,
|
|
155
|
+
orientation,
|
|
156
|
+
loop,
|
|
157
|
+
deferTo: parentScope ?? undefined
|
|
158
|
+
})
|
|
159
|
+
const state: ToggleGroupState = {
|
|
160
|
+
type,
|
|
161
|
+
value: current,
|
|
162
|
+
controlled,
|
|
163
|
+
disabled,
|
|
164
|
+
orientation,
|
|
165
|
+
loop,
|
|
166
|
+
controlSize,
|
|
167
|
+
appearance,
|
|
168
|
+
motion,
|
|
169
|
+
setItemPressed(item, pressed) {
|
|
170
|
+
if (!disabled) request(current => nextSelection(current, item, pressed))
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return (
|
|
174
|
+
<Context.Provider value={state}>
|
|
175
|
+
{/* oxlint-disable-next-line jsx-a11y/no-noninteractive-element-interactions -- The group delegates roving keyboard navigation from its native button children. */}
|
|
176
|
+
<div
|
|
177
|
+
{...props}
|
|
178
|
+
ref={hostRef}
|
|
179
|
+
role="group"
|
|
180
|
+
tabIndex={undefined}
|
|
181
|
+
contentEditable={undefined}
|
|
182
|
+
dangerouslySetInnerHTML={undefined}
|
|
183
|
+
className={cn(
|
|
184
|
+
rootClass,
|
|
185
|
+
orientation === 'vertical' ? 'flex-col items-stretch' : 'items-center',
|
|
186
|
+
appearance === 'segmented' && segmentedClass,
|
|
187
|
+
className
|
|
188
|
+
)}
|
|
189
|
+
data-slot={joinIds('toggle-group', props['data-slot'])}
|
|
190
|
+
data-type={type}
|
|
191
|
+
data-appearance={appearance}
|
|
192
|
+
data-orientation={orientation}
|
|
193
|
+
data-control-size={controlSize}
|
|
194
|
+
data-disabled={disabled ? '' : undefined}
|
|
195
|
+
onFocus={event => {
|
|
196
|
+
onFocus?.(event)
|
|
197
|
+
roving.onFocus(event)
|
|
198
|
+
}}
|
|
199
|
+
onBlur={event => {
|
|
200
|
+
onBlur?.(event)
|
|
201
|
+
roving.onBlur(event)
|
|
202
|
+
}}
|
|
203
|
+
onKeyDown={event => {
|
|
204
|
+
onKeyDown?.(event)
|
|
205
|
+
roving.onKeyDown(event)
|
|
206
|
+
}}
|
|
207
|
+
>
|
|
208
|
+
{appearance === 'segmented' ? (
|
|
209
|
+
<Indicator
|
|
210
|
+
value={current}
|
|
211
|
+
motion={motion}
|
|
212
|
+
part={parts?.indicator}
|
|
213
|
+
/>
|
|
214
|
+
) : null}
|
|
215
|
+
{children}
|
|
216
|
+
</div>
|
|
217
|
+
</Context.Provider>
|
|
218
|
+
)
|
|
219
|
+
}
|
|
220
|
+
export function ToggleGroupItem({
|
|
221
|
+
value,
|
|
222
|
+
disabled = false,
|
|
223
|
+
controlSize,
|
|
224
|
+
variant = 'outline',
|
|
225
|
+
tone = 'neutral',
|
|
226
|
+
className,
|
|
227
|
+
...props
|
|
228
|
+
}: ToggleGroupItemProps) {
|
|
229
|
+
const group = useOwner()
|
|
230
|
+
if (typeof value !== 'string') throw new TypeError('ToggleGroupItem requires a string value.')
|
|
231
|
+
const pressed = isItemOpen(group.value, value),
|
|
232
|
+
unavailable = disabled || group.disabled
|
|
233
|
+
return (
|
|
234
|
+
<Button
|
|
235
|
+
{...props}
|
|
236
|
+
type="button"
|
|
237
|
+
role="button"
|
|
238
|
+
value={value}
|
|
239
|
+
tabIndex={0}
|
|
240
|
+
disabled={unavailable}
|
|
241
|
+
pressed={pressed}
|
|
242
|
+
defaultPressed={undefined}
|
|
243
|
+
aria-pressed={undefined}
|
|
244
|
+
onPressedChange={next => group.setItemPressed(value, next)}
|
|
245
|
+
controlSize={controlSize ?? group.controlSize}
|
|
246
|
+
variant={group.appearance === 'segmented' ? 'ghost' : variant}
|
|
247
|
+
tone={tone}
|
|
248
|
+
motion={group.motion === 'off' ? 'off' : props.motion}
|
|
249
|
+
data-slot={joinIds('toggle-group-item', props['data-slot'])}
|
|
250
|
+
data-value={value}
|
|
251
|
+
data-state={pressed ? 'on' : 'off'}
|
|
252
|
+
data-disabled={unavailable ? '' : undefined}
|
|
253
|
+
className={cn(
|
|
254
|
+
'relative z-10',
|
|
255
|
+
group.appearance === 'segmented' &&
|
|
256
|
+
'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]',
|
|
257
|
+
className
|
|
258
|
+
)}
|
|
259
|
+
/>
|
|
260
|
+
)
|
|
261
|
+
}
|
|
262
|
+
function Indicator({
|
|
263
|
+
value,
|
|
264
|
+
motion,
|
|
265
|
+
part
|
|
266
|
+
}: {
|
|
267
|
+
value: Selection
|
|
268
|
+
motion: 'inherit' | 'off'
|
|
269
|
+
part?: PartStyle
|
|
270
|
+
}) {
|
|
271
|
+
const node = useRef<HTMLSpanElement>(null),
|
|
272
|
+
{ ref: indicatorRef } = useTransitionHost('indicator', node, motion === 'off')
|
|
273
|
+
useLayoutEffect(() => {
|
|
274
|
+
const mark = node.current,
|
|
275
|
+
root = mark?.closest<HTMLDivElement>('[data-slot~="toggle-group"]')
|
|
276
|
+
if (!root || !mark) return
|
|
277
|
+
const view = root.ownerDocument.defaultView!
|
|
278
|
+
const items = () =>
|
|
279
|
+
Array.from(
|
|
280
|
+
root.querySelectorAll<HTMLButtonElement>('button[data-slot~="toggle-group-item"]')
|
|
281
|
+
).filter(item => item.closest('[data-slot~="toggle-group"]') === root)
|
|
282
|
+
const measure = () => {
|
|
283
|
+
const selected = items().find(
|
|
284
|
+
item => item.getAttribute('aria-pressed') === 'true' && item.getClientRects().length > 0
|
|
285
|
+
)
|
|
286
|
+
mark.hidden = !selected
|
|
287
|
+
if (!selected) {
|
|
288
|
+
mark.removeAttribute('data-rad-indicator-target')
|
|
289
|
+
return
|
|
290
|
+
}
|
|
291
|
+
const target = [
|
|
292
|
+
selected.offsetLeft,
|
|
293
|
+
selected.offsetTop,
|
|
294
|
+
selected.offsetWidth,
|
|
295
|
+
selected.offsetHeight
|
|
296
|
+
]
|
|
297
|
+
for (const [index, axis] of ['x', 'y', 'width', 'height'].entries())
|
|
298
|
+
mark.style.setProperty(`--_rad-indicator-target-${axis}`, `${target[index]}px`)
|
|
299
|
+
const serialized = JSON.stringify(target)
|
|
300
|
+
if (mark.getAttribute('data-rad-indicator-target') !== serialized)
|
|
301
|
+
mark.setAttribute('data-rad-indicator-target', serialized)
|
|
302
|
+
}
|
|
303
|
+
let frame = 0
|
|
304
|
+
const schedule = () => {
|
|
305
|
+
if (!frame)
|
|
306
|
+
frame = view.requestAnimationFrame(() => {
|
|
307
|
+
frame = 0
|
|
308
|
+
measure()
|
|
309
|
+
})
|
|
310
|
+
}
|
|
311
|
+
const resize = new view.ResizeObserver(schedule)
|
|
312
|
+
const observe = () => {
|
|
313
|
+
resize.disconnect()
|
|
314
|
+
resize.observe(root)
|
|
315
|
+
items().forEach(item => resize.observe(item))
|
|
316
|
+
}
|
|
317
|
+
const changes = new view.MutationObserver(records => {
|
|
318
|
+
if (records.some(record => record.target !== mark && !mark.contains(record.target))) {
|
|
319
|
+
observe()
|
|
320
|
+
schedule()
|
|
321
|
+
}
|
|
322
|
+
})
|
|
323
|
+
changes.observe(root, {
|
|
324
|
+
subtree: true,
|
|
325
|
+
childList: true,
|
|
326
|
+
characterData: true,
|
|
327
|
+
attributes: true,
|
|
328
|
+
attributeFilter: ['aria-pressed', 'class', 'style', 'hidden', 'dir']
|
|
329
|
+
})
|
|
330
|
+
observe()
|
|
331
|
+
measure()
|
|
332
|
+
view.addEventListener('resize', schedule)
|
|
333
|
+
root.ownerDocument.fonts?.addEventListener('loadingdone', schedule)
|
|
334
|
+
return () => {
|
|
335
|
+
changes.disconnect()
|
|
336
|
+
resize.disconnect()
|
|
337
|
+
view.cancelAnimationFrame(frame)
|
|
338
|
+
view.removeEventListener('resize', schedule)
|
|
339
|
+
root.ownerDocument.fonts?.removeEventListener('loadingdone', schedule)
|
|
340
|
+
}
|
|
341
|
+
}, [value])
|
|
342
|
+
return (
|
|
343
|
+
<span
|
|
344
|
+
ref={indicatorRef}
|
|
345
|
+
aria-hidden="true"
|
|
346
|
+
data-rad-indicator=""
|
|
347
|
+
hidden={value === null}
|
|
348
|
+
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))]"
|
|
349
|
+
>
|
|
350
|
+
<span
|
|
351
|
+
data-slot="toggle-group-indicator"
|
|
352
|
+
className={cn(
|
|
353
|
+
'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]',
|
|
354
|
+
part?.className
|
|
355
|
+
)}
|
|
356
|
+
style={part?.style}
|
|
357
|
+
/>
|
|
358
|
+
</span>
|
|
359
|
+
)
|
|
360
|
+
}
|
package/src/Toolbar.tsx
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { useRef, type ComponentProps } from 'react'
|
|
3
|
+
import { cn } from './cn.js'
|
|
4
|
+
import { joinIds } from './internal/ids.js'
|
|
5
|
+
import { requireAccessibleName, type AccessibleName } from './internal/accessibleName.js'
|
|
6
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
7
|
+
import { CompositeFocusContext, useRovingFocus } from './react/useRovingFocus.js'
|
|
8
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
9
|
+
|
|
10
|
+
export type ToolbarOrientation = 'horizontal' | 'vertical'
|
|
11
|
+
export type ToolbarProps = Omit<
|
|
12
|
+
ComponentProps<'div'>,
|
|
13
|
+
'role' | 'aria-orientation' | 'tabIndex' | 'style' | 'contentEditable' | 'dangerouslySetInnerHTML'
|
|
14
|
+
> &
|
|
15
|
+
AccessibleName & {
|
|
16
|
+
orientation?: ToolbarOrientation
|
|
17
|
+
loop?: boolean
|
|
18
|
+
style?: ThemeStyle
|
|
19
|
+
'data-slot'?: string
|
|
20
|
+
}
|
|
21
|
+
const toolbarClass =
|
|
22
|
+
'inline-flex w-fit max-w-full flex-wrap gap-[var(--rad-toolbar-gap,0.25rem)] p-[var(--rad-toolbar-padding,0px)] rounded-[var(--rad-toolbar-radius,var(--rad-control-radius,0.5rem))] bg-[var(--rad-toolbar-background,transparent)]'
|
|
23
|
+
/** One native composite; child actions retain their own activation and state. */
|
|
24
|
+
export function Toolbar({
|
|
25
|
+
ref,
|
|
26
|
+
orientation = 'horizontal',
|
|
27
|
+
loop = true,
|
|
28
|
+
className,
|
|
29
|
+
onFocus,
|
|
30
|
+
onBlur,
|
|
31
|
+
onKeyDown,
|
|
32
|
+
children,
|
|
33
|
+
...props
|
|
34
|
+
}: ToolbarProps) {
|
|
35
|
+
requireAccessibleName('Toolbar', props)
|
|
36
|
+
const host = useRef<HTMLDivElement>(null),
|
|
37
|
+
hostRef = useComposedRefs(host, ref)
|
|
38
|
+
const roving = useRovingFocus({
|
|
39
|
+
host,
|
|
40
|
+
selector: 'button, a[href]',
|
|
41
|
+
scope: '[data-slot~="toolbar"]',
|
|
42
|
+
orientation,
|
|
43
|
+
loop
|
|
44
|
+
})
|
|
45
|
+
return (
|
|
46
|
+
<CompositeFocusContext.Provider value='[data-slot~="toolbar"]'>
|
|
47
|
+
<div
|
|
48
|
+
{...props}
|
|
49
|
+
ref={hostRef}
|
|
50
|
+
role="toolbar"
|
|
51
|
+
aria-orientation={orientation}
|
|
52
|
+
tabIndex={-1}
|
|
53
|
+
contentEditable={undefined}
|
|
54
|
+
dangerouslySetInnerHTML={undefined}
|
|
55
|
+
data-slot={joinIds('toolbar', props['data-slot'])}
|
|
56
|
+
data-orientation={orientation}
|
|
57
|
+
className={cn(
|
|
58
|
+
toolbarClass,
|
|
59
|
+
orientation === 'vertical' ? 'flex-col items-stretch' : 'items-center',
|
|
60
|
+
className
|
|
61
|
+
)}
|
|
62
|
+
onFocus={event => {
|
|
63
|
+
onFocus?.(event)
|
|
64
|
+
roving.onFocus(event)
|
|
65
|
+
}}
|
|
66
|
+
onBlur={event => {
|
|
67
|
+
onBlur?.(event)
|
|
68
|
+
roving.onBlur(event)
|
|
69
|
+
}}
|
|
70
|
+
onKeyDown={event => {
|
|
71
|
+
onKeyDown?.(event)
|
|
72
|
+
roving.onKeyDown(event)
|
|
73
|
+
}}
|
|
74
|
+
>
|
|
75
|
+
{children}
|
|
76
|
+
</div>
|
|
77
|
+
</CompositeFocusContext.Provider>
|
|
78
|
+
)
|
|
79
|
+
}
|
package/src/cn.ts
CHANGED
|
@@ -86,6 +86,8 @@ export const cn: CnFunction = createCn({
|
|
|
86
86
|
],
|
|
87
87
|
mb: ['mb-(--rad-fieldset-gap)'],
|
|
88
88
|
gap: [
|
|
89
|
+
'gap-(--rad-toolbar-gap)',
|
|
90
|
+
'gap-(--rad-toggle-group-gap)',
|
|
89
91
|
'gap-(--rad-checkbox-group-gap)',
|
|
90
92
|
'gap-(--rad-fieldset-gap)',
|
|
91
93
|
'gap-(--rad-pagination-gap)',
|
|
@@ -112,6 +114,8 @@ export const cn: CnFunction = createCn({
|
|
|
112
114
|
'px-(--rad-tabs-trigger-padding)'
|
|
113
115
|
],
|
|
114
116
|
p: [
|
|
117
|
+
'p-(--rad-toolbar-padding)',
|
|
118
|
+
'p-(--rad-toggle-group-padding)',
|
|
115
119
|
'p-(--rad-toast-padding)',
|
|
116
120
|
'p-(--rad-alert-padding)',
|
|
117
121
|
'p-(--rad-menu-padding)',
|
|
@@ -124,6 +128,9 @@ export const cn: CnFunction = createCn({
|
|
|
124
128
|
'p-(--rad-tabs-content-padding)'
|
|
125
129
|
],
|
|
126
130
|
rounded: [
|
|
131
|
+
'rounded-(--rad-toolbar-radius)',
|
|
132
|
+
'rounded-(--rad-toggle-group-radius)',
|
|
133
|
+
'rounded-(--rad-toggle-group-indicator-radius)',
|
|
127
134
|
'rounded-(--rad-scroll-area-radius)',
|
|
128
135
|
'rounded-(--rad-table-radius)',
|
|
129
136
|
'rounded-(--rad-pagination-radius)',
|
|
@@ -184,6 +191,7 @@ export const cn: CnFunction = createCn({
|
|
|
184
191
|
'border-(--rad-switch-border-style)'
|
|
185
192
|
],
|
|
186
193
|
shadow: [
|
|
194
|
+
'shadow-(--rad-toggle-group-indicator-shadow)',
|
|
187
195
|
'shadow-(--rad-toast-shadow)',
|
|
188
196
|
'shadow-(--rad-alert-shadow)',
|
|
189
197
|
'shadow-(--rad-menu-shadow)',
|
package/src/index.ts
CHANGED
|
@@ -384,3 +384,16 @@ 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'
|
|
397
|
+
|
|
398
|
+
export { Toolbar } from './Toolbar.js'
|
|
399
|
+
export type { ToolbarProps, ToolbarOrientation } from './Toolbar.js'
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type AccessibleName =
|
|
2
|
+
| { 'aria-label': string; 'aria-labelledby'?: string }
|
|
3
|
+
| { 'aria-label'?: string; 'aria-labelledby': string }
|
|
4
|
+
|
|
5
|
+
/** Named composite hosts have the same explicit ownership in every family. */
|
|
6
|
+
export function requireAccessibleName(
|
|
7
|
+
family: string,
|
|
8
|
+
attributes: { 'aria-label'?: string; 'aria-labelledby'?: string }
|
|
9
|
+
) {
|
|
10
|
+
if (!attributes['aria-label']?.trim() && !attributes['aria-labelledby']?.trim())
|
|
11
|
+
throw new TypeError(`${family} requires aria-label or aria-labelledby.`)
|
|
12
|
+
}
|
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 }) {
|