@cognitap/radiant 0.0.60 → 0.0.62
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 +9 -0
- package/dist/library/Breadcrumb.d.ts.map +1 -1
- package/dist/library/Breadcrumb.js +8 -5
- package/dist/library/Breadcrumb.js.map +1 -1
- package/dist/library/Pagination.d.ts +2 -2
- package/dist/library/Pagination.d.ts.map +1 -1
- package/dist/library/Pagination.js +9 -8
- package/dist/library/Pagination.js.map +1 -1
- package/dist/library/Resizable.d.ts +56 -0
- package/dist/library/Resizable.d.ts.map +1 -0
- package/dist/library/Resizable.js +345 -0
- package/dist/library/Resizable.js.map +1 -0
- package/dist/library/Separator.d.ts +6 -1
- package/dist/library/Separator.d.ts.map +1 -1
- package/dist/library/Separator.js +11 -3
- package/dist/library/Separator.js.map +1 -1
- package/dist/library/Table.d.ts +2 -7
- package/dist/library/Table.d.ts.map +1 -1
- package/dist/library/Table.js +3 -4
- package/dist/library/Table.js.map +1 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +4 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +1 -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/decimal.d.ts +3 -0
- package/dist/library/internal/decimal.d.ts.map +1 -0
- package/dist/library/internal/decimal.js +15 -0
- package/dist/library/internal/decimal.js.map +1 -0
- package/dist/library/internal/numberField.d.ts +1 -1
- package/dist/library/internal/numberField.d.ts.map +1 -1
- package/dist/library/internal/numberField.js +3 -13
- package/dist/library/internal/numberField.js.map +1 -1
- package/dist/library/internal/resizable.d.ts +15 -0
- package/dist/library/internal/resizable.d.ts.map +1 -0
- package/dist/library/internal/resizable.js +86 -0
- package/dist/library/internal/resizable.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +10 -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 +4 -5
- package/package.json +1 -1
- package/src/Breadcrumb.tsx +8 -6
- package/src/Pagination.tsx +6 -10
- package/src/Resizable.tsx +581 -0
- package/src/Separator.tsx +22 -5
- package/src/Table.tsx +3 -10
- package/src/cn.ts +4 -0
- package/src/index.ts +13 -0
- package/src/internal/decimal.ts +17 -0
- package/src/internal/numberField.ts +4 -16
- package/src/internal/resizable.ts +115 -0
- package/src/themes/components.ts +10 -0
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useContext,
|
|
6
|
+
useId,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
useLayoutEffect,
|
|
10
|
+
useEffect,
|
|
11
|
+
useCallback,
|
|
12
|
+
type ComponentProps
|
|
13
|
+
} from 'react'
|
|
14
|
+
import { SeparatorRoot, type SeparatorProps } from './Separator.js'
|
|
15
|
+
import { cn } from './cn.js'
|
|
16
|
+
import { joinIds } from './internal/ids.js'
|
|
17
|
+
import { requireAccessibleName, type AccessibleName } from './internal/accessibleName.js'
|
|
18
|
+
import {
|
|
19
|
+
checkResizableItems,
|
|
20
|
+
checkResizableValue,
|
|
21
|
+
resizableIndex,
|
|
22
|
+
resizableNeighbor,
|
|
23
|
+
resizableRanges,
|
|
24
|
+
resizePanel,
|
|
25
|
+
sameResizableValue,
|
|
26
|
+
type ResizableItem
|
|
27
|
+
} from './internal/resizable.js'
|
|
28
|
+
import { addDecimal } from './internal/decimal.js'
|
|
29
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
30
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
31
|
+
import type { ValueOwner } from './react/useNativeOwner.js'
|
|
32
|
+
import type { Orientation } from './presentation.js'
|
|
33
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
34
|
+
export type { ResizableItem } from './internal/resizable.js'
|
|
35
|
+
|
|
36
|
+
type LayoutOwner = ValueOwner<number[]> & ({ value: number[] } | { defaultValue: number[] })
|
|
37
|
+
export type ResizableProps = Omit<
|
|
38
|
+
ComponentProps<'div'>,
|
|
39
|
+
'value' | 'defaultValue' | 'onChange' | 'style'
|
|
40
|
+
> &
|
|
41
|
+
LayoutOwner & {
|
|
42
|
+
items: readonly ResizableItem[]
|
|
43
|
+
onValueChange?: (value: number[]) => void
|
|
44
|
+
orientation?: Orientation
|
|
45
|
+
disabled?: boolean
|
|
46
|
+
style?: ThemeStyle
|
|
47
|
+
'data-slot'?: string
|
|
48
|
+
}
|
|
49
|
+
export type ResizablePanelState = {
|
|
50
|
+
id: string
|
|
51
|
+
size: number
|
|
52
|
+
min: number
|
|
53
|
+
max: number
|
|
54
|
+
collapsed: boolean
|
|
55
|
+
canCollapse: boolean
|
|
56
|
+
canExpand: boolean
|
|
57
|
+
}
|
|
58
|
+
export type ResizableState = {
|
|
59
|
+
value: number[]
|
|
60
|
+
controlled: boolean
|
|
61
|
+
orientation: Orientation
|
|
62
|
+
disabled: boolean
|
|
63
|
+
setValue: (value: number[]) => void
|
|
64
|
+
resize: (panel: string, size: number) => void
|
|
65
|
+
resizeBy: (panel: string, amount: number) => void
|
|
66
|
+
toggle: (panel: string) => void
|
|
67
|
+
getPanel: (panel: string) => ResizablePanelState
|
|
68
|
+
}
|
|
69
|
+
type Binding = {
|
|
70
|
+
state: ResizableState
|
|
71
|
+
items: readonly ResizableItem[]
|
|
72
|
+
id: string
|
|
73
|
+
dragging: string | null
|
|
74
|
+
registerPanel: (value: string, node: HTMLDivElement | null) => void
|
|
75
|
+
registerHandle: (value: string, node: HTMLHRElement | null) => void
|
|
76
|
+
focusHandle: (value: string) => void
|
|
77
|
+
begin: (value: string) => symbol | null
|
|
78
|
+
finish: (token: symbol) => void
|
|
79
|
+
room: () => number
|
|
80
|
+
direction: () => string
|
|
81
|
+
pointerResize: (value: string, size: number, sum: number) => void
|
|
82
|
+
}
|
|
83
|
+
const Context = createContext<Binding | null>(null)
|
|
84
|
+
export function useResizable(): ResizableState | null {
|
|
85
|
+
return useContext(Context)?.state ?? null
|
|
86
|
+
}
|
|
87
|
+
function useBinding() {
|
|
88
|
+
const binding = useContext(Context)
|
|
89
|
+
if (!binding) throw new Error('Resizable parts must render inside Resizable.')
|
|
90
|
+
return binding
|
|
91
|
+
}
|
|
92
|
+
export function Resizable({
|
|
93
|
+
items,
|
|
94
|
+
value,
|
|
95
|
+
defaultValue,
|
|
96
|
+
onValueChange,
|
|
97
|
+
orientation = 'horizontal',
|
|
98
|
+
disabled = false,
|
|
99
|
+
className,
|
|
100
|
+
style,
|
|
101
|
+
ref,
|
|
102
|
+
...props
|
|
103
|
+
}: ResizableProps) {
|
|
104
|
+
checkResizableItems(items)
|
|
105
|
+
if (value === undefined && defaultValue === undefined)
|
|
106
|
+
throw new TypeError('Resizable requires value or defaultValue.')
|
|
107
|
+
const selection = useControllableState({
|
|
108
|
+
name: 'Resizable',
|
|
109
|
+
value,
|
|
110
|
+
defaultValue,
|
|
111
|
+
initialValue: [],
|
|
112
|
+
onChange: (value: number[]) => onValueChange?.([...value]),
|
|
113
|
+
equal: sameResizableValue
|
|
114
|
+
})
|
|
115
|
+
checkResizableValue(items, selection.value)
|
|
116
|
+
const id = useId(),
|
|
117
|
+
root = useRef<HTMLDivElement>(null),
|
|
118
|
+
panels = useRef(new Map<string, HTMLDivElement>()),
|
|
119
|
+
handles = useRef(new Map<string, HTMLHRElement>()),
|
|
120
|
+
history = useRef(new Map<string, number>()),
|
|
121
|
+
gesture = useRef<symbol | null>(null)
|
|
122
|
+
const [dragging, setDragging] = useState<string | null>(null)
|
|
123
|
+
const composed = useComposedRefs(root, ref)
|
|
124
|
+
const registerPanel = useCallback((value: string, node: HTMLDivElement | null) => {
|
|
125
|
+
if (node) panels.current.set(value, node)
|
|
126
|
+
else panels.current.delete(value)
|
|
127
|
+
}, [])
|
|
128
|
+
const registerHandle = useCallback((value: string, node: HTMLHRElement | null) => {
|
|
129
|
+
if (node) handles.current.set(value, node)
|
|
130
|
+
else handles.current.delete(value)
|
|
131
|
+
}, [])
|
|
132
|
+
useLayoutEffect(() => {
|
|
133
|
+
items.forEach((item, index) => {
|
|
134
|
+
if (selection.value[index]! > 0) history.current.set(item.value, selection.value[index]!)
|
|
135
|
+
})
|
|
136
|
+
const node = root.current
|
|
137
|
+
if (!node) return
|
|
138
|
+
const sequence = Array.from(node.children).filter(child =>
|
|
139
|
+
child.matches('[data-slot~="resizable-panel"],[data-slot~="resizable-handle"]')
|
|
140
|
+
)
|
|
141
|
+
if (!sequence.length) return
|
|
142
|
+
if (
|
|
143
|
+
sequence.length !== items.length * 2 - 1 ||
|
|
144
|
+
sequence.some(
|
|
145
|
+
(child, index) =>
|
|
146
|
+
child.getAttribute('data-panel') !== items[Math.floor(index / 2)]?.value ||
|
|
147
|
+
!child.matches(
|
|
148
|
+
index % 2 ? '[data-slot~="resizable-handle"]' : '[data-slot~="resizable-panel"]'
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
throw new Error('Render Resizable panels and handles directly in items order.')
|
|
153
|
+
})
|
|
154
|
+
const request = (update: (value: number[]) => number[]) => {
|
|
155
|
+
if (disabled) return
|
|
156
|
+
selection.request(previous => {
|
|
157
|
+
const next = update(previous)
|
|
158
|
+
checkResizableValue(items, next)
|
|
159
|
+
return next
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
const resize = (panel: string, size: number) =>
|
|
163
|
+
request(previous => resizePanel(items, previous, resizableIndex(items, panel), size))
|
|
164
|
+
const resizeBy = (panel: string, amount: number) => {
|
|
165
|
+
if (!Number.isFinite(amount)) throw new TypeError('Resizable increments must be finite.')
|
|
166
|
+
request(previous => {
|
|
167
|
+
const index = resizableIndex(items, panel)
|
|
168
|
+
return resizePanel(items, previous, index, addDecimal(previous[index]!, amount))
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
const toggle = (panel: string) =>
|
|
172
|
+
request(previous => {
|
|
173
|
+
const index = resizableIndex(items, panel)
|
|
174
|
+
if (!items[index]!.collapsible) return previous
|
|
175
|
+
const size = previous[index]!
|
|
176
|
+
if (size > 0) {
|
|
177
|
+
if (!resizableRanges(items, previous, index).some(range => range[0] === 0)) return previous
|
|
178
|
+
history.current.set(panel, size)
|
|
179
|
+
return resizePanel(items, previous, index, 0)
|
|
180
|
+
}
|
|
181
|
+
const neighbor = resizableNeighbor(items, index)
|
|
182
|
+
return resizePanel(
|
|
183
|
+
items,
|
|
184
|
+
previous,
|
|
185
|
+
index,
|
|
186
|
+
history.current.get(panel) ?? addDecimal(previous[index]!, previous[neighbor]!) / 2
|
|
187
|
+
)
|
|
188
|
+
})
|
|
189
|
+
const getPanel = (panel: string): ResizablePanelState => {
|
|
190
|
+
const index = resizableIndex(items, panel),
|
|
191
|
+
ranges = resizableRanges(items, selection.value, index),
|
|
192
|
+
size = selection.value[index]!,
|
|
193
|
+
min = ranges[0]?.[0] ?? size,
|
|
194
|
+
max = ranges.at(-1)?.[1] ?? size
|
|
195
|
+
return {
|
|
196
|
+
id: id + '-panel-' + encodeURIComponent(panel),
|
|
197
|
+
size,
|
|
198
|
+
min,
|
|
199
|
+
max,
|
|
200
|
+
collapsed: size === 0,
|
|
201
|
+
canCollapse: !!items[index]!.collapsible && min === 0 && size > 0,
|
|
202
|
+
canExpand: !!items[index]!.collapsible && max > 0 && size === 0
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const state: ResizableState = {
|
|
206
|
+
value: selection.value,
|
|
207
|
+
controlled: selection.controlled,
|
|
208
|
+
orientation,
|
|
209
|
+
disabled,
|
|
210
|
+
setValue: value => request(() => [...value]),
|
|
211
|
+
resize,
|
|
212
|
+
resizeBy,
|
|
213
|
+
toggle,
|
|
214
|
+
getPanel
|
|
215
|
+
}
|
|
216
|
+
const beginGesture = useCallback(
|
|
217
|
+
(panel: string) => {
|
|
218
|
+
if (disabled || gesture.current) return null
|
|
219
|
+
const token = Symbol()
|
|
220
|
+
gesture.current = token
|
|
221
|
+
setDragging(panel)
|
|
222
|
+
return token
|
|
223
|
+
},
|
|
224
|
+
[disabled]
|
|
225
|
+
)
|
|
226
|
+
const finishGesture = useCallback((token: symbol) => {
|
|
227
|
+
if (gesture.current !== token) return
|
|
228
|
+
gesture.current = null
|
|
229
|
+
setDragging(null)
|
|
230
|
+
}, [])
|
|
231
|
+
const binding: Binding = {
|
|
232
|
+
state,
|
|
233
|
+
items,
|
|
234
|
+
id,
|
|
235
|
+
dragging,
|
|
236
|
+
registerPanel,
|
|
237
|
+
registerHandle,
|
|
238
|
+
focusHandle(panel) {
|
|
239
|
+
const index = resizableIndex(items, panel)
|
|
240
|
+
handles.current
|
|
241
|
+
.get(items[Math.min(index, items.length - 2)]!.value)
|
|
242
|
+
?.focus({ preventScroll: true })
|
|
243
|
+
},
|
|
244
|
+
begin: beginGesture,
|
|
245
|
+
finish: finishGesture,
|
|
246
|
+
direction() {
|
|
247
|
+
const node = root.current
|
|
248
|
+
return node?.ownerDocument.defaultView?.getComputedStyle(node).direction ?? 'ltr'
|
|
249
|
+
},
|
|
250
|
+
room() {
|
|
251
|
+
let room = 0
|
|
252
|
+
for (const panel of panels.current.values()) {
|
|
253
|
+
const rect = panel.getBoundingClientRect(),
|
|
254
|
+
view = panel.ownerDocument.defaultView
|
|
255
|
+
if (!view) continue
|
|
256
|
+
const css = view.getComputedStyle(panel),
|
|
257
|
+
horizontal = orientation === 'horizontal',
|
|
258
|
+
length = horizontal ? rect.width : rect.height,
|
|
259
|
+
outer = horizontal ? panel.offsetWidth : panel.offsetHeight
|
|
260
|
+
const scale = outer ? length / outer : 1
|
|
261
|
+
const edges =
|
|
262
|
+
(horizontal
|
|
263
|
+
? [css.paddingLeft, css.paddingRight, css.borderLeftWidth, css.borderRightWidth]
|
|
264
|
+
: [css.paddingTop, css.paddingBottom, css.borderTopWidth, css.borderBottomWidth]
|
|
265
|
+
).reduce((sum, edge) => sum + (parseFloat(edge) || 0), 0) * scale
|
|
266
|
+
room += Math.max(0, length - edges)
|
|
267
|
+
}
|
|
268
|
+
return room
|
|
269
|
+
},
|
|
270
|
+
pointerResize(panel, size, sum) {
|
|
271
|
+
request(previous => {
|
|
272
|
+
const index = resizableIndex(items, panel),
|
|
273
|
+
neighbor = resizableNeighbor(items, index)
|
|
274
|
+
return Math.abs(addDecimal(previous[index]!, previous[neighbor]!) - sum) > 1e-7
|
|
275
|
+
? previous
|
|
276
|
+
: resizePanel(items, previous, index, size, false)
|
|
277
|
+
})
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return (
|
|
281
|
+
<Context.Provider value={binding}>
|
|
282
|
+
<div
|
|
283
|
+
{...props}
|
|
284
|
+
ref={composed}
|
|
285
|
+
data-slot={joinIds('resizable', props['data-slot'])}
|
|
286
|
+
data-orientation={orientation}
|
|
287
|
+
data-disabled={disabled ? '' : undefined}
|
|
288
|
+
data-dragging={dragging ? '' : undefined}
|
|
289
|
+
className={cn(
|
|
290
|
+
'flex min-h-0 min-w-0 data-[orientation=horizontal]:flex-row data-[orientation=vertical]:flex-col data-dragging:select-none',
|
|
291
|
+
className
|
|
292
|
+
)}
|
|
293
|
+
style={style}
|
|
294
|
+
/>
|
|
295
|
+
</Context.Provider>
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
export type ResizablePanelProps = Omit<
|
|
299
|
+
ComponentProps<'div'>,
|
|
300
|
+
'id' | 'hidden' | 'inert' | 'style'
|
|
301
|
+
> & { value: string; style?: ThemeStyle; 'data-slot'?: string }
|
|
302
|
+
export function ResizablePanel({
|
|
303
|
+
value,
|
|
304
|
+
ref,
|
|
305
|
+
className,
|
|
306
|
+
style,
|
|
307
|
+
onFocusCapture,
|
|
308
|
+
onBlurCapture,
|
|
309
|
+
...props
|
|
310
|
+
}: ResizablePanelProps) {
|
|
311
|
+
const context = useBinding(),
|
|
312
|
+
index = resizableIndex(context.items, value),
|
|
313
|
+
size = context.state.value[index]!,
|
|
314
|
+
collapsed = size === 0
|
|
315
|
+
const node = useRef<HTMLDivElement>(null),
|
|
316
|
+
hadFocus = useRef(false)
|
|
317
|
+
const registerPanel = context.registerPanel
|
|
318
|
+
const register = useCallback(
|
|
319
|
+
(element: HTMLDivElement | null) => {
|
|
320
|
+
node.current = element
|
|
321
|
+
registerPanel(value, element)
|
|
322
|
+
},
|
|
323
|
+
[registerPanel, value]
|
|
324
|
+
)
|
|
325
|
+
const composed = useComposedRefs(register, ref)
|
|
326
|
+
useLayoutEffect(() => {
|
|
327
|
+
const panel = node.current
|
|
328
|
+
if (!collapsed || !panel) return
|
|
329
|
+
const active = panel.ownerDocument.activeElement
|
|
330
|
+
if (hadFocus.current && (panel.contains(active) || active === panel.ownerDocument.body))
|
|
331
|
+
context.focusHandle(value)
|
|
332
|
+
hadFocus.current = false
|
|
333
|
+
})
|
|
334
|
+
const panelStyle: ThemeStyle = { '--_rad-resizable-size': size, ...style }
|
|
335
|
+
return (
|
|
336
|
+
<div
|
|
337
|
+
{...props}
|
|
338
|
+
ref={composed}
|
|
339
|
+
id={context.id + '-panel-' + encodeURIComponent(value)}
|
|
340
|
+
hidden={collapsed}
|
|
341
|
+
inert={collapsed || undefined}
|
|
342
|
+
data-slot={joinIds('resizable-panel', props['data-slot'])}
|
|
343
|
+
data-panel={value}
|
|
344
|
+
data-size={size}
|
|
345
|
+
data-collapsed={collapsed ? '' : undefined}
|
|
346
|
+
className={cn(
|
|
347
|
+
'box-border min-h-0 min-w-0 basis-0 shrink grow-(--_rad-resizable-size) overflow-hidden',
|
|
348
|
+
className
|
|
349
|
+
)}
|
|
350
|
+
style={panelStyle}
|
|
351
|
+
onFocusCapture={event => {
|
|
352
|
+
onFocusCapture?.(event)
|
|
353
|
+
hadFocus.current = true
|
|
354
|
+
}}
|
|
355
|
+
onBlurCapture={event => {
|
|
356
|
+
onBlurCapture?.(event)
|
|
357
|
+
if (
|
|
358
|
+
!event.currentTarget.contains(event.relatedTarget) &&
|
|
359
|
+
(!collapsed || event.relatedTarget !== null)
|
|
360
|
+
)
|
|
361
|
+
hadFocus.current = false
|
|
362
|
+
}}
|
|
363
|
+
/>
|
|
364
|
+
)
|
|
365
|
+
}
|
|
366
|
+
export type ResizableHandleProps = Omit<
|
|
367
|
+
SeparatorProps,
|
|
368
|
+
| 'orientation'
|
|
369
|
+
| 'decorative'
|
|
370
|
+
| 'aria-controls'
|
|
371
|
+
| 'aria-disabled'
|
|
372
|
+
| 'aria-valuenow'
|
|
373
|
+
| 'aria-valuemin'
|
|
374
|
+
| 'aria-valuemax'
|
|
375
|
+
| 'aria-valuetext'
|
|
376
|
+
> &
|
|
377
|
+
AccessibleName & {
|
|
378
|
+
value: string
|
|
379
|
+
step?: number
|
|
380
|
+
largeStep?: number
|
|
381
|
+
}
|
|
382
|
+
type Drag = {
|
|
383
|
+
node: HTMLHRElement
|
|
384
|
+
pointer: number
|
|
385
|
+
token: symbol
|
|
386
|
+
start: number
|
|
387
|
+
size: number
|
|
388
|
+
room: number
|
|
389
|
+
sum: number
|
|
390
|
+
orientation: Orientation
|
|
391
|
+
direction: string
|
|
392
|
+
cleanup: () => void
|
|
393
|
+
}
|
|
394
|
+
export function ResizableHandle({
|
|
395
|
+
value,
|
|
396
|
+
step = 1,
|
|
397
|
+
largeStep = step * 10,
|
|
398
|
+
className,
|
|
399
|
+
ref,
|
|
400
|
+
onKeyDown,
|
|
401
|
+
onPointerDown,
|
|
402
|
+
onPointerMove,
|
|
403
|
+
onPointerUp,
|
|
404
|
+
onPointerCancel,
|
|
405
|
+
onLostPointerCapture,
|
|
406
|
+
onBlur,
|
|
407
|
+
...props
|
|
408
|
+
}: ResizableHandleProps) {
|
|
409
|
+
const context = useBinding(),
|
|
410
|
+
index = resizableIndex(context.items, value)
|
|
411
|
+
if (index === context.items.length - 1)
|
|
412
|
+
throw new TypeError('Resizable handles follow every panel except the last.')
|
|
413
|
+
requireAccessibleName('ResizableHandle', props)
|
|
414
|
+
if (!Number.isFinite(step) || step <= 0 || !Number.isFinite(largeStep) || largeStep <= 0)
|
|
415
|
+
throw new TypeError('Resizable handle steps must be finite and positive.')
|
|
416
|
+
const ranges = resizableRanges(context.items, context.state.value, index),
|
|
417
|
+
min = ranges[0]?.[0] ?? context.state.value[index]!,
|
|
418
|
+
max = ranges.at(-1)?.[1] ?? min
|
|
419
|
+
const disabled = context.state.disabled || min === max,
|
|
420
|
+
drag = useRef<Drag | null>(null)
|
|
421
|
+
const registerHandle = context.registerHandle,
|
|
422
|
+
finishGesture = context.finish
|
|
423
|
+
const register = useCallback(
|
|
424
|
+
(node: HTMLHRElement | null) => registerHandle(value, node),
|
|
425
|
+
[registerHandle, value]
|
|
426
|
+
)
|
|
427
|
+
const composed = useComposedRefs(register, ref)
|
|
428
|
+
const finish = useCallback(() => {
|
|
429
|
+
const current = drag.current
|
|
430
|
+
if (!current) return
|
|
431
|
+
drag.current = null
|
|
432
|
+
current.cleanup()
|
|
433
|
+
try {
|
|
434
|
+
if (current.node.hasPointerCapture(current.pointer))
|
|
435
|
+
current.node.releasePointerCapture(current.pointer)
|
|
436
|
+
} catch {}
|
|
437
|
+
finishGesture(current.token)
|
|
438
|
+
}, [finishGesture])
|
|
439
|
+
useEffect(() => () => finish(), [finish])
|
|
440
|
+
useLayoutEffect(() => {
|
|
441
|
+
if (disabled || (drag.current && drag.current.orientation !== context.state.orientation))
|
|
442
|
+
finish()
|
|
443
|
+
})
|
|
444
|
+
return (
|
|
445
|
+
<SeparatorRoot
|
|
446
|
+
{...props}
|
|
447
|
+
interactive
|
|
448
|
+
tabIndex={disabled ? -1 : 0}
|
|
449
|
+
decorative={false}
|
|
450
|
+
ref={composed}
|
|
451
|
+
orientation={context.state.orientation === 'horizontal' ? 'vertical' : 'horizontal'}
|
|
452
|
+
aria-controls={context.id + '-panel-' + encodeURIComponent(value)}
|
|
453
|
+
aria-disabled={disabled || undefined}
|
|
454
|
+
aria-valuenow={context.state.value[index]}
|
|
455
|
+
aria-valuemin={min}
|
|
456
|
+
aria-valuemax={max}
|
|
457
|
+
aria-valuetext={Number(context.state.value[index]!.toFixed(4)) + '%'}
|
|
458
|
+
data-slot={joinIds('resizable-handle', props['data-slot'])}
|
|
459
|
+
data-panel={value}
|
|
460
|
+
data-dragging={context.dragging === value ? '' : undefined}
|
|
461
|
+
className={cn(
|
|
462
|
+
'touch-none select-none m-0 bg-[var(--rad-resizable-handle-background,transparent)] [border-color:var(--rad-resizable-handle-color,var(--rad-color-border,oklch(0.885_0.005_286)))]',
|
|
463
|
+
'hover:bg-[var(--rad-resizable-handle-hover-background,var(--rad-color-neutral,oklch(0.955_0.004_286)))] data-dragging:bg-[var(--rad-resizable-handle-active-background,var(--rad-color-neutral,oklch(0.955_0.004_286)))]',
|
|
464
|
+
'focus-visible:outline-solid focus-visible:outline-[color:var(--rad-resizable-focus-color,var(--rad-color-focus,oklch(0.55_0.13_255)))] focus-visible:outline-[length:var(--rad-resizable-focus-width,var(--rad-focus-width,2px))] focus-visible:outline-offset-[var(--rad-resizable-focus-offset,-2px)]',
|
|
465
|
+
'aria-disabled:cursor-not-allowed aria-disabled:bg-[var(--rad-resizable-handle-background,transparent)] forced-colors:border-[CanvasText] forced-colors:focus-visible:outline-[Highlight]',
|
|
466
|
+
context.state.orientation === 'horizontal'
|
|
467
|
+
? 'h-auto w-[length:var(--rad-resizable-handle-size,0.5rem)] cursor-col-resize'
|
|
468
|
+
: 'w-auto h-[length:var(--rad-resizable-handle-size,0.5rem)] cursor-row-resize',
|
|
469
|
+
className
|
|
470
|
+
)}
|
|
471
|
+
onKeyDown={event => {
|
|
472
|
+
onKeyDown?.(event)
|
|
473
|
+
if (event.defaultPrevented || disabled || event.altKey || event.ctrlKey || event.metaKey)
|
|
474
|
+
return
|
|
475
|
+
if (event.key === 'Enter') {
|
|
476
|
+
event.preventDefault()
|
|
477
|
+
context.state.toggle(value)
|
|
478
|
+
return
|
|
479
|
+
}
|
|
480
|
+
if (event.key === 'Home' || event.key === 'End') {
|
|
481
|
+
event.preventDefault()
|
|
482
|
+
context.state.resize(value, event.key === 'Home' ? min : max)
|
|
483
|
+
return
|
|
484
|
+
}
|
|
485
|
+
const horizontal = context.state.orientation === 'horizontal',
|
|
486
|
+
direction = context.direction(),
|
|
487
|
+
amount = event.shiftKey ? largeStep : step
|
|
488
|
+
const sign = horizontal
|
|
489
|
+
? event.key === 'ArrowRight'
|
|
490
|
+
? 1
|
|
491
|
+
: event.key === 'ArrowLeft'
|
|
492
|
+
? -1
|
|
493
|
+
: 0
|
|
494
|
+
: event.key === 'ArrowDown'
|
|
495
|
+
? 1
|
|
496
|
+
: event.key === 'ArrowUp'
|
|
497
|
+
? -1
|
|
498
|
+
: 0
|
|
499
|
+
if (!sign) return
|
|
500
|
+
event.preventDefault()
|
|
501
|
+
context.state.resizeBy(value, sign * amount * (horizontal && direction === 'rtl' ? -1 : 1))
|
|
502
|
+
}}
|
|
503
|
+
onPointerDown={event => {
|
|
504
|
+
onPointerDown?.(event)
|
|
505
|
+
if (event.defaultPrevented || disabled || event.button !== 0 || !event.isPrimary) return
|
|
506
|
+
const room = context.room(),
|
|
507
|
+
token = room > 0 ? context.begin(value) : null
|
|
508
|
+
if (!token) return
|
|
509
|
+
event.preventDefault()
|
|
510
|
+
event.currentTarget.focus({ preventScroll: true })
|
|
511
|
+
try {
|
|
512
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
513
|
+
} catch {
|
|
514
|
+
context.finish(token)
|
|
515
|
+
return
|
|
516
|
+
}
|
|
517
|
+
const view = event.currentTarget.ownerDocument.defaultView
|
|
518
|
+
view?.addEventListener('blur', finish)
|
|
519
|
+
drag.current = {
|
|
520
|
+
node: event.currentTarget,
|
|
521
|
+
pointer: event.pointerId,
|
|
522
|
+
token,
|
|
523
|
+
start: context.state.orientation === 'horizontal' ? event.clientX : event.clientY,
|
|
524
|
+
size: context.state.value[index]!,
|
|
525
|
+
room,
|
|
526
|
+
sum: addDecimal(context.state.value[index]!, context.state.value[index + 1]!),
|
|
527
|
+
orientation: context.state.orientation,
|
|
528
|
+
direction: context.direction(),
|
|
529
|
+
cleanup: () => view?.removeEventListener('blur', finish)
|
|
530
|
+
}
|
|
531
|
+
}}
|
|
532
|
+
onPointerMove={event => {
|
|
533
|
+
onPointerMove?.(event)
|
|
534
|
+
const current = drag.current
|
|
535
|
+
if (!current || current.pointer !== event.pointerId) return
|
|
536
|
+
const room = context.room(),
|
|
537
|
+
coordinate = current.orientation === 'horizontal' ? event.clientX : event.clientY
|
|
538
|
+
if (
|
|
539
|
+
disabled ||
|
|
540
|
+
!event.currentTarget.isConnected ||
|
|
541
|
+
context.direction() !== current.direction ||
|
|
542
|
+
Math.abs(
|
|
543
|
+
addDecimal(context.state.value[index]!, context.state.value[index + 1]!) - current.sum
|
|
544
|
+
) > 1e-7
|
|
545
|
+
) {
|
|
546
|
+
finish()
|
|
547
|
+
return
|
|
548
|
+
}
|
|
549
|
+
if (event.defaultPrevented) return
|
|
550
|
+
if (Math.abs(room - current.room) > 0.5) {
|
|
551
|
+
current.start = coordinate
|
|
552
|
+
current.size = context.state.value[index]!
|
|
553
|
+
current.room = room
|
|
554
|
+
return
|
|
555
|
+
}
|
|
556
|
+
if (room <= 0) return
|
|
557
|
+
const delta =
|
|
558
|
+
((coordinate - current.start) / room) *
|
|
559
|
+
100 *
|
|
560
|
+
(current.orientation === 'horizontal' && current.direction === 'rtl' ? -1 : 1)
|
|
561
|
+
context.pointerResize(value, addDecimal(current.size, delta), current.sum)
|
|
562
|
+
}}
|
|
563
|
+
onPointerUp={event => {
|
|
564
|
+
onPointerUp?.(event)
|
|
565
|
+
if (drag.current?.pointer === event.pointerId) finish()
|
|
566
|
+
}}
|
|
567
|
+
onPointerCancel={event => {
|
|
568
|
+
onPointerCancel?.(event)
|
|
569
|
+
if (drag.current?.pointer === event.pointerId) finish()
|
|
570
|
+
}}
|
|
571
|
+
onLostPointerCapture={event => {
|
|
572
|
+
onLostPointerCapture?.(event)
|
|
573
|
+
if (drag.current?.pointer === event.pointerId) finish()
|
|
574
|
+
}}
|
|
575
|
+
onBlur={event => {
|
|
576
|
+
onBlur?.(event)
|
|
577
|
+
finish()
|
|
578
|
+
}}
|
|
579
|
+
/>
|
|
580
|
+
)
|
|
581
|
+
}
|
package/src/Separator.tsx
CHANGED
|
@@ -21,23 +21,40 @@ export type SeparatorProps = Omit<
|
|
|
21
21
|
'data-slot'?: string
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export function Separator({
|
|
24
|
+
export function Separator(props: SeparatorProps) {
|
|
25
|
+
return (
|
|
26
|
+
<SeparatorRoot
|
|
27
|
+
{...props}
|
|
28
|
+
interactive={undefined}
|
|
29
|
+
tabIndex={undefined}
|
|
30
|
+
/>
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Private native seam for an interactive separator; the public rule stays passive. */
|
|
35
|
+
export function SeparatorRoot({
|
|
36
|
+
interactive,
|
|
37
|
+
tabIndex,
|
|
25
38
|
decorative = false,
|
|
26
39
|
orientation = 'horizontal',
|
|
27
40
|
className,
|
|
28
41
|
...props
|
|
29
|
-
}: SeparatorProps) {
|
|
42
|
+
}: SeparatorProps & { interactive?: boolean; tabIndex?: number }) {
|
|
30
43
|
const {
|
|
31
44
|
children: _children,
|
|
32
45
|
dangerouslySetInnerHTML: _html,
|
|
33
46
|
...attributes
|
|
34
47
|
}: ComponentProps<'hr'> = props
|
|
35
|
-
const nativeProps = {
|
|
48
|
+
const nativeProps = {
|
|
49
|
+
...attributes,
|
|
50
|
+
role: interactive ? 'separator' : undefined,
|
|
51
|
+
tabIndex: interactive ? (tabIndex ?? 0) : undefined
|
|
52
|
+
}
|
|
36
53
|
return (
|
|
37
54
|
<hr
|
|
38
55
|
{...nativeProps}
|
|
39
|
-
aria-hidden={decorative ? true : undefined}
|
|
40
|
-
aria-orientation={decorative ? undefined : orientation}
|
|
56
|
+
aria-hidden={!interactive && decorative ? true : undefined}
|
|
57
|
+
aria-orientation={!interactive && decorative ? undefined : orientation}
|
|
41
58
|
data-slot={joinIds('separator', props['data-slot'])}
|
|
42
59
|
data-orientation={orientation}
|
|
43
60
|
data-decorative={decorative ? '' : undefined}
|
package/src/Table.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ComponentProps } from 'react'
|
|
2
2
|
import { cn } from './cn.js'
|
|
3
|
+
import { requireAccessibleName, type AccessibleName } from './internal/accessibleName.js'
|
|
3
4
|
import { joinIds } from './internal/ids.js'
|
|
4
5
|
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
5
6
|
type Part<
|
|
@@ -17,10 +18,7 @@ export type TableContainerProps = Omit<
|
|
|
17
18
|
Part<'section'>,
|
|
18
19
|
'aria-label' | 'aria-labelledby' | 'tabIndex'
|
|
19
20
|
> &
|
|
20
|
-
|
|
21
|
-
| { 'aria-label': string; 'aria-labelledby'?: never }
|
|
22
|
-
| { 'aria-label'?: never; 'aria-labelledby': string }
|
|
23
|
-
)
|
|
21
|
+
AccessibleName
|
|
24
22
|
const cellClass =
|
|
25
23
|
'px-[var(--rad-table-padding-inline,var(--rad-control-small-padding-inline,0.75rem))] py-[var(--rad-table-padding-block,0.625rem)] text-start align-top [overflow-wrap:anywhere]'
|
|
26
24
|
export function Table({ className, ...props }: TableProps) {
|
|
@@ -120,17 +118,12 @@ export function TableCell({ className, ...props }: TableCellProps) {
|
|
|
120
118
|
}
|
|
121
119
|
/** An optional named keyboard scroll surface; Table itself remains the native table. */
|
|
122
120
|
export function TableContainer({ className, ...props }: TableContainerProps) {
|
|
123
|
-
|
|
124
|
-
labelledBy = props['aria-labelledby']?.trim()
|
|
125
|
-
if ((!label && !labelledBy) || (label && labelledBy))
|
|
126
|
-
throw new TypeError('TableContainer needs exactly one nonempty accessible name.')
|
|
121
|
+
requireAccessibleName('TableContainer', props)
|
|
127
122
|
// Keyboard focus enables native scrolling of this named overflow surface.
|
|
128
123
|
const nativeProps = { ...props, role: undefined, tabIndex: 0 }
|
|
129
124
|
return (
|
|
130
125
|
<section
|
|
131
126
|
{...nativeProps}
|
|
132
|
-
aria-label={label || undefined}
|
|
133
|
-
aria-labelledby={labelledBy || undefined}
|
|
134
127
|
data-slot={joinIds('table-container', props['data-slot'])}
|
|
135
128
|
className={cn(
|
|
136
129
|
'min-w-0 max-w-full overflow-auto rounded-[var(--rad-table-radius,var(--rad-surface-radius,0.75rem))] border-[length:var(--rad-table-border-width,var(--rad-surface-border-width,1px))] border-solid border-[var(--rad-table-border-color,var(--rad-color-border))] focus-visible:outline-solid focus-visible:[outline-color:var(--rad-color-focus)] focus-visible:[outline-width:var(--rad-focus-width,2px)] focus-visible:[outline-offset:var(--rad-focus-offset,2px)] focus-visible:[outline-style:var(--rad-focus-style,solid)] forced-colors:border-[CanvasText]',
|
package/src/cn.ts
CHANGED
|
@@ -69,6 +69,7 @@ export const cn: CnFunction = createCn({
|
|
|
69
69
|
'text-(--rad-tabs-font-size)'
|
|
70
70
|
],
|
|
71
71
|
h: [
|
|
72
|
+
'h-(--rad-resizable-handle-size)',
|
|
72
73
|
'h-(--rad-meter-height)',
|
|
73
74
|
'h-(--rad-native-select-height)',
|
|
74
75
|
'h-(--rad-drawer-size)',
|
|
@@ -82,6 +83,7 @@ export const cn: CnFunction = createCn({
|
|
|
82
83
|
'h-(--rad-slider-length)'
|
|
83
84
|
],
|
|
84
85
|
w: [
|
|
86
|
+
'w-(--rad-resizable-handle-size)',
|
|
85
87
|
'w-(--rad-otp-field-slot-size)',
|
|
86
88
|
'w-(--rad-drawer-size)',
|
|
87
89
|
'w-(--rad-context-menu-width)',
|
|
@@ -292,6 +294,7 @@ export const cn: CnFunction = createCn({
|
|
|
292
294
|
'[corner-shape:var(--rad-tabs-corner-shape)]'
|
|
293
295
|
],
|
|
294
296
|
'outline-w': [
|
|
297
|
+
'outline-(--rad-resizable-focus-width)',
|
|
295
298
|
'outline-(--rad-nav-link-focus-width)',
|
|
296
299
|
'outline-(--rad-native-select-focus-width)',
|
|
297
300
|
'outline-(--rad-input-group-focus-width)',
|
|
@@ -302,6 +305,7 @@ export const cn: CnFunction = createCn({
|
|
|
302
305
|
'outline-(--rad-tabs-focus-width)'
|
|
303
306
|
],
|
|
304
307
|
'outline-offset': [
|
|
308
|
+
'outline-offset-(--rad-resizable-focus-offset)',
|
|
305
309
|
'outline-offset-(--rad-nav-link-focus-offset)',
|
|
306
310
|
'outline-offset-(--rad-native-select-focus-offset)',
|
|
307
311
|
'outline-offset-(--rad-input-group-focus-offset)',
|