@appica/ui-react 1.3.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +8 -2
- package/agent-rules.md +9 -2
- package/appica.css +2 -2
- package/dist/components/badge/badge.js +1 -1
- package/dist/components/badge/badge.js.map +1 -1
- package/dist/components/button/button-variants.js +1 -1
- package/dist/components/button/button-variants.js.map +1 -1
- package/dist/components/card/card.js +1 -1
- package/dist/components/card/card.js.map +1 -1
- package/dist/components/carousel/carousel.d.ts +24 -3
- package/dist/components/carousel/carousel.d.ts.map +1 -1
- package/dist/components/carousel/carousel.js +149 -31
- package/dist/components/carousel/carousel.js.map +1 -1
- package/dist/components/checkbox/checkbox.js +1 -1
- package/dist/components/checkbox/checkbox.js.map +1 -1
- package/dist/components/command-palette/command-palette.d.ts +91 -0
- package/dist/components/command-palette/command-palette.d.ts.map +1 -0
- package/dist/components/command-palette/command-palette.js +376 -0
- package/dist/components/command-palette/command-palette.js.map +1 -0
- package/dist/components/date-field/date-field.js +1 -1
- package/dist/components/date-field/date-field.js.map +1 -1
- package/dist/components/date-picker/date-picker.js +1 -1
- package/dist/components/date-picker/date-picker.js.map +1 -1
- package/dist/components/input/input-variants.js +2 -2
- package/dist/components/input/input-variants.js.map +1 -1
- package/dist/components/radio/radio.js +1 -1
- package/dist/components/radio/radio.js.map +1 -1
- package/dist/components/splitter/splitter.d.ts +57 -0
- package/dist/components/splitter/splitter.d.ts.map +1 -0
- package/dist/components/splitter/splitter.js +395 -0
- package/dist/components/splitter/splitter.js.map +1 -0
- package/dist/components/textarea/textarea.js +1 -1
- package/dist/components/textarea/textarea.js.map +1 -1
- package/dist/components/time-field/time-field.js +1 -1
- package/dist/components/time-field/time-field.js.map +1 -1
- package/dist/components/toc/toc.d.ts +6 -1
- package/dist/components/toc/toc.d.ts.map +1 -1
- package/dist/components/toc/toc.js +15 -7
- package/dist/components/toc/toc.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/package.json +24 -14
- package/src/components/badge/badge.tsx +1 -1
- package/src/components/button/button-variants.ts +1 -1
- package/src/components/card/card.tsx +1 -1
- package/src/components/carousel/carousel.test.tsx +559 -21
- package/src/components/carousel/carousel.tsx +195 -40
- package/src/components/checkbox/checkbox.tsx +1 -1
- package/src/components/command-palette/command-palette.test.tsx +280 -0
- package/src/components/command-palette/command-palette.tsx +544 -0
- package/src/components/compare-slider/compare-slider.test.tsx +8 -7
- package/src/components/date-field/date-field.tsx +1 -1
- package/src/components/date-picker/date-picker.tsx +1 -1
- package/src/components/input/input-variants.ts +2 -2
- package/src/components/radio/radio.tsx +1 -1
- package/src/components/rating/rating.test.tsx +10 -3
- package/src/components/splitter/splitter.test.tsx +415 -0
- package/src/components/splitter/splitter.tsx +555 -0
- package/src/components/textarea/textarea.tsx +1 -1
- package/src/components/time-field/time-field.tsx +1 -1
- package/src/components/toc/toc.test.tsx +15 -0
- package/src/components/toc/toc.tsx +22 -7
- package/src/index.ts +2 -0
- package/src/test/css.ts +9 -0
- package/src/test/setup.ts +13 -0
- package/styles.css +3 -0
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { useDirection } from '../../hooks/use-direction'
|
|
5
|
+
import { cn } from '../../internal/utils'
|
|
6
|
+
|
|
7
|
+
type SplitterOrientation = 'horizontal' | 'vertical'
|
|
8
|
+
type ResizeMode = 'drag' | 'step'
|
|
9
|
+
|
|
10
|
+
interface PanelConstraints {
|
|
11
|
+
min: number
|
|
12
|
+
max: number
|
|
13
|
+
collapsible: boolean
|
|
14
|
+
collapsedSize: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface SplitterApi {
|
|
18
|
+
register: (id: string, constraints: PanelConstraints) => () => void
|
|
19
|
+
resize: (
|
|
20
|
+
index: number,
|
|
21
|
+
target: number,
|
|
22
|
+
mode: ResizeMode,
|
|
23
|
+
from: readonly number[],
|
|
24
|
+
current: readonly number[],
|
|
25
|
+
) => number[] | null
|
|
26
|
+
restoredSize: (index: number) => number
|
|
27
|
+
commit: (sizes: readonly number[]) => void
|
|
28
|
+
setDragging: (dragging: boolean) => void
|
|
29
|
+
measure: () => number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface SplitterContextValue {
|
|
33
|
+
orientation: SplitterOrientation
|
|
34
|
+
keyboardStep: number
|
|
35
|
+
sizes: readonly number[]
|
|
36
|
+
ids: readonly (string | undefined)[]
|
|
37
|
+
constraints: readonly PanelConstraints[]
|
|
38
|
+
mounted: boolean
|
|
39
|
+
api: SplitterApi
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ScannedPanel {
|
|
43
|
+
id: string | undefined
|
|
44
|
+
constraints: PanelConstraints
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const DEFAULT_CONSTRAINTS: PanelConstraints = { min: 0, max: 100, collapsible: false, collapsedSize: 0 }
|
|
48
|
+
const EPSILON = 1e-6
|
|
49
|
+
|
|
50
|
+
const SplitterContext = React.createContext<SplitterContextValue | null>(null)
|
|
51
|
+
const SplitterIndexContext = React.createContext(-1)
|
|
52
|
+
|
|
53
|
+
function useSplitterContext(component: string): SplitterContextValue {
|
|
54
|
+
const ctx = React.useContext(SplitterContext)
|
|
55
|
+
if (!ctx) throw new Error(`Appica UI: <${component}> must be used within <Splitter>.`)
|
|
56
|
+
return ctx
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function clamp(value: number, min: number, max: number) {
|
|
60
|
+
return Math.min(max, Math.max(min, value))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function toConstraints({
|
|
64
|
+
minSize = 0,
|
|
65
|
+
maxSize = 100,
|
|
66
|
+
collapsible = false,
|
|
67
|
+
collapsedSize = 0,
|
|
68
|
+
}: Pick<SplitterPanelProps, 'minSize' | 'maxSize' | 'collapsible' | 'collapsedSize'>): PanelConstraints {
|
|
69
|
+
const min = clamp(minSize, 0, 100)
|
|
70
|
+
return { min, max: clamp(maxSize, min, 100), collapsible, collapsedSize: clamp(collapsedSize, 0, min) }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function sameConstraints(a: PanelConstraints | undefined, b: PanelConstraints) {
|
|
74
|
+
return (
|
|
75
|
+
a !== undefined &&
|
|
76
|
+
a.min === b.min &&
|
|
77
|
+
a.max === b.max &&
|
|
78
|
+
a.collapsible === b.collapsible &&
|
|
79
|
+
a.collapsedSize === b.collapsedSize
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function sameIds(a: readonly string[], b: readonly string[]) {
|
|
84
|
+
return a.length === b.length && a.every((id, index) => id === b[index])
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function isPanel(node: Element) {
|
|
88
|
+
return node.getAttribute('data-slot') === 'splitter-panel'
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function normalizeSizes(input: readonly (number | undefined)[] | undefined, count: number): number[] {
|
|
92
|
+
if (count <= 0) return []
|
|
93
|
+
const values = Array.from({ length: count }, (_, index) => {
|
|
94
|
+
const value = input?.[index]
|
|
95
|
+
return value !== undefined && Number.isFinite(value) && value >= 0 ? value : undefined
|
|
96
|
+
})
|
|
97
|
+
const known = values.reduce<number>((sum, value) => sum + (value ?? 0), 0)
|
|
98
|
+
const missing = values.filter((value) => value === undefined).length
|
|
99
|
+
const fill = missing === 0 ? 0 : known < 100 ? (100 - known) / missing : 100 / count
|
|
100
|
+
const filled = values.map((value) => value ?? fill)
|
|
101
|
+
const total = filled.reduce((sum, value) => sum + value, 0)
|
|
102
|
+
return filled.map((value) => (total > 0 ? (value * 100) / total : 100 / count))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isCollapsed(size: number | undefined, c: PanelConstraints) {
|
|
106
|
+
return c.collapsible && size !== undefined && Math.abs(size - c.collapsedSize) < EPSILON
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function allows(c: PanelConstraints, size: number) {
|
|
110
|
+
return isCollapsed(size, c) || (size > c.min - EPSILON && size < c.max + EPSILON)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function snapsShut(target: number, current: number, c: PanelConstraints, mode: ResizeMode) {
|
|
114
|
+
if (!isCollapsed(current, c)) return mode === 'drag' && target < (c.collapsedSize + c.min) / 2
|
|
115
|
+
return mode === 'drag' ? target < c.min : target <= current
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function fit(target: number, current: number, c: PanelConstraints, mode: ResizeMode) {
|
|
119
|
+
if (c.collapsible && c.collapsedSize < c.min) {
|
|
120
|
+
if (Math.abs(target - c.collapsedSize) < EPSILON || snapsShut(target, current, c, mode)) return c.collapsedSize
|
|
121
|
+
}
|
|
122
|
+
return clamp(target, c.min, c.max)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function resizePair(
|
|
126
|
+
from: readonly number[],
|
|
127
|
+
current: readonly number[],
|
|
128
|
+
index: number,
|
|
129
|
+
target: number,
|
|
130
|
+
constraints: readonly PanelConstraints[],
|
|
131
|
+
mode: ResizeMode,
|
|
132
|
+
): number[] | null {
|
|
133
|
+
const a = constraints[index]
|
|
134
|
+
const b = constraints[index + 1]
|
|
135
|
+
const fromA = from[index]
|
|
136
|
+
const fromB = from[index + 1]
|
|
137
|
+
const currentA = current[index]
|
|
138
|
+
const currentB = current[index + 1]
|
|
139
|
+
if (!a || !b || fromA === undefined || fromB === undefined || currentA === undefined || currentB === undefined) {
|
|
140
|
+
return null
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const pair = fromA + fromB
|
|
144
|
+
let first = fit(target, currentA, a, mode)
|
|
145
|
+
let second = fit(pair - first, currentB, b, mode)
|
|
146
|
+
first = pair - second
|
|
147
|
+
if (!allows(a, first)) {
|
|
148
|
+
first = fit(first, currentA, a, mode)
|
|
149
|
+
second = pair - first
|
|
150
|
+
if (!allows(b, second)) return null
|
|
151
|
+
}
|
|
152
|
+
if (Math.abs(first - currentA) < EPSILON && Math.abs(second - currentB) < EPSILON) return null
|
|
153
|
+
|
|
154
|
+
const next = [...from]
|
|
155
|
+
next[index] = first
|
|
156
|
+
next[index + 1] = second
|
|
157
|
+
return next
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function scanChildren(children: React.ReactNode) {
|
|
161
|
+
const panels: ScannedPanel[] = []
|
|
162
|
+
const content = React.Children.map(children, (child) => {
|
|
163
|
+
if (!React.isValidElement(child)) return child
|
|
164
|
+
if (child.type === SplitterPanel) {
|
|
165
|
+
const props = child.props as SplitterPanelProps
|
|
166
|
+
const index = panels.push({ id: props.id, constraints: toConstraints(props) }) - 1
|
|
167
|
+
return <SplitterIndexContext.Provider value={index}>{child}</SplitterIndexContext.Provider>
|
|
168
|
+
}
|
|
169
|
+
if (child.type === SplitterHandle) {
|
|
170
|
+
return <SplitterIndexContext.Provider value={panels.length - 1}>{child}</SplitterIndexContext.Provider>
|
|
171
|
+
}
|
|
172
|
+
return child
|
|
173
|
+
})
|
|
174
|
+
return { content, panels }
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const rootClasses =
|
|
178
|
+
'flex h-full w-full data-[orientation=vertical]:flex-col data-dragging:select-none data-dragging:data-[orientation=horizontal]:cursor-col-resize data-dragging:data-[orientation=vertical]:cursor-row-resize data-dragging:*:data-[slot=splitter-panel]:pointer-events-none'
|
|
179
|
+
|
|
180
|
+
interface SplitterProps extends React.ComponentPropsWithoutRef<'div'> {
|
|
181
|
+
/**
|
|
182
|
+
* Axis the panels are laid out along. Horizontal places them side by side, vertical stacks them.
|
|
183
|
+
* @default 'horizontal'
|
|
184
|
+
*/
|
|
185
|
+
orientation?: SplitterOrientation
|
|
186
|
+
/** Panel sizes as percentages, in DOM order. Pass it to control the component. */
|
|
187
|
+
sizes?: number[]
|
|
188
|
+
/** Panel sizes on first render, as percentages in DOM order, when the component is uncontrolled. Panels split evenly without it. */
|
|
189
|
+
defaultSizes?: number[]
|
|
190
|
+
/** Fires on every change, including each frame of a drag, with the sizes in DOM order. */
|
|
191
|
+
onSizesChange?: (sizes: number[]) => void
|
|
192
|
+
/** Fires once a drag ends or a key press settles, with the sizes landed on. */
|
|
193
|
+
onSizesCommitted?: (sizes: number[]) => void
|
|
194
|
+
/**
|
|
195
|
+
* Percentage an arrow key moves a handle by.
|
|
196
|
+
* @default 5
|
|
197
|
+
*/
|
|
198
|
+
keyboardStep?: number
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function Splitter({
|
|
202
|
+
orientation = 'horizontal',
|
|
203
|
+
sizes: sizesProp,
|
|
204
|
+
defaultSizes,
|
|
205
|
+
onSizesChange,
|
|
206
|
+
onSizesCommitted,
|
|
207
|
+
keyboardStep = 5,
|
|
208
|
+
className,
|
|
209
|
+
children,
|
|
210
|
+
...props
|
|
211
|
+
}: SplitterProps) {
|
|
212
|
+
const rootRef = React.useRef<HTMLDivElement>(null)
|
|
213
|
+
const restoreRef = React.useRef(new Map<string, number>())
|
|
214
|
+
const [panelIds, setPanelIds] = React.useState<readonly string[] | null>(null)
|
|
215
|
+
const [registry, setRegistry] = React.useState<Readonly<Record<string, PanelConstraints>>>({})
|
|
216
|
+
const [internal, setInternal] = React.useState<Readonly<Record<string, number>> | null>(null)
|
|
217
|
+
const [dragging, setDragging] = React.useState(false)
|
|
218
|
+
|
|
219
|
+
const { content, panels } = React.useMemo(() => scanChildren(children), [children])
|
|
220
|
+
|
|
221
|
+
const ids = React.useMemo(() => panelIds ?? panels.map((panel) => panel.id), [panelIds, panels])
|
|
222
|
+
const constraints = React.useMemo(
|
|
223
|
+
() =>
|
|
224
|
+
panelIds ? panelIds.map((id) => registry[id] ?? DEFAULT_CONSTRAINTS) : panels.map((panel) => panel.constraints),
|
|
225
|
+
[panelIds, registry, panels],
|
|
226
|
+
)
|
|
227
|
+
const sizes = React.useMemo(
|
|
228
|
+
() =>
|
|
229
|
+
normalizeSizes(
|
|
230
|
+
sizesProp ?? (internal && panelIds ? panelIds.map((id) => internal[id]) : defaultSizes),
|
|
231
|
+
ids.length,
|
|
232
|
+
),
|
|
233
|
+
[sizesProp, internal, panelIds, defaultSizes, ids.length],
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
const latest = { orientation, ids, constraints, keyboardStep, sizesProp, onSizesChange, onSizesCommitted }
|
|
237
|
+
const latestRef = React.useRef(latest)
|
|
238
|
+
latestRef.current = latest
|
|
239
|
+
|
|
240
|
+
React.useLayoutEffect(() => {
|
|
241
|
+
const root = rootRef.current
|
|
242
|
+
if (!root) return
|
|
243
|
+
const next = Array.from(root.children)
|
|
244
|
+
.filter(isPanel)
|
|
245
|
+
.map((node) => node.id)
|
|
246
|
+
setPanelIds((prev) => (prev && sameIds(prev, next) ? prev : next))
|
|
247
|
+
}, [content, registry])
|
|
248
|
+
|
|
249
|
+
const api = React.useMemo<SplitterApi>(
|
|
250
|
+
() => ({
|
|
251
|
+
register: (id, panelConstraints) => {
|
|
252
|
+
setRegistry((prev) =>
|
|
253
|
+
sameConstraints(prev[id], panelConstraints) ? prev : { ...prev, [id]: panelConstraints },
|
|
254
|
+
)
|
|
255
|
+
return () =>
|
|
256
|
+
setRegistry((prev) => {
|
|
257
|
+
const { [id]: _removed, ...rest } = prev
|
|
258
|
+
return rest
|
|
259
|
+
})
|
|
260
|
+
},
|
|
261
|
+
resize: (index, target, mode, from, current) => {
|
|
262
|
+
const latest = latestRef.current
|
|
263
|
+
const next = resizePair(from, current, index, target, latest.constraints, mode)
|
|
264
|
+
if (!next) return null
|
|
265
|
+
for (const k of [index, index + 1]) {
|
|
266
|
+
const c = latest.constraints[k]
|
|
267
|
+
const id = latest.ids[k]
|
|
268
|
+
const before = current[k]
|
|
269
|
+
const start = from[k]
|
|
270
|
+
if (!c || !id || before === undefined || start === undefined) continue
|
|
271
|
+
if (isCollapsed(next[k], c) && !isCollapsed(before, c)) {
|
|
272
|
+
restoreRef.current.set(id, isCollapsed(start, c) ? before : start)
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (latest.sizesProp === undefined) {
|
|
276
|
+
const entries = latest.ids.flatMap((id, k) => (id === undefined ? [] : [[id, next[k] ?? 0] as const]))
|
|
277
|
+
setInternal((prev) => ({ ...prev, ...Object.fromEntries(entries) }))
|
|
278
|
+
}
|
|
279
|
+
latest.onSizesChange?.(next)
|
|
280
|
+
return next
|
|
281
|
+
},
|
|
282
|
+
restoredSize: (index) => {
|
|
283
|
+
const latest = latestRef.current
|
|
284
|
+
const c = latest.constraints[index] ?? DEFAULT_CONSTRAINTS
|
|
285
|
+
const id = latest.ids[index]
|
|
286
|
+
const restored = id === undefined ? undefined : restoreRef.current.get(id)
|
|
287
|
+
return restored ?? Math.max(c.min, c.collapsedSize + latest.keyboardStep)
|
|
288
|
+
},
|
|
289
|
+
commit: (committed) => latestRef.current.onSizesCommitted?.([...committed]),
|
|
290
|
+
setDragging,
|
|
291
|
+
measure: () => {
|
|
292
|
+
const rect = rootRef.current?.getBoundingClientRect()
|
|
293
|
+
if (!rect) return 0
|
|
294
|
+
return latestRef.current.orientation === 'vertical' ? rect.height : rect.width
|
|
295
|
+
},
|
|
296
|
+
}),
|
|
297
|
+
[],
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
const ctx = React.useMemo<SplitterContextValue>(
|
|
301
|
+
() => ({ orientation, keyboardStep, sizes, ids, constraints, mounted: panelIds !== null, api }),
|
|
302
|
+
[orientation, keyboardStep, sizes, ids, constraints, panelIds, api],
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
return (
|
|
306
|
+
<SplitterContext.Provider value={ctx}>
|
|
307
|
+
<div
|
|
308
|
+
ref={rootRef}
|
|
309
|
+
{...props}
|
|
310
|
+
data-slot="splitter"
|
|
311
|
+
data-orientation={orientation}
|
|
312
|
+
data-dragging={dragging ? '' : undefined}
|
|
313
|
+
className={cn(rootClasses, className)}
|
|
314
|
+
>
|
|
315
|
+
{content}
|
|
316
|
+
</div>
|
|
317
|
+
</SplitterContext.Provider>
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
interface SplitterPanelProps extends React.ComponentPropsWithoutRef<'div'> {
|
|
322
|
+
/**
|
|
323
|
+
* Smallest size the panel resizes to, as a percentage.
|
|
324
|
+
* @default 0
|
|
325
|
+
*/
|
|
326
|
+
minSize?: number
|
|
327
|
+
/**
|
|
328
|
+
* Largest size the panel resizes to, as a percentage.
|
|
329
|
+
* @default 100
|
|
330
|
+
*/
|
|
331
|
+
maxSize?: number
|
|
332
|
+
/**
|
|
333
|
+
* Let the panel snap to `collapsedSize` when dragged halfway from `minSize` toward it, or with Home and Enter on the handle after it.
|
|
334
|
+
* @default false
|
|
335
|
+
*/
|
|
336
|
+
collapsible?: boolean
|
|
337
|
+
/**
|
|
338
|
+
* Size of the panel while collapsed, as a percentage.
|
|
339
|
+
* @default 0
|
|
340
|
+
*/
|
|
341
|
+
collapsedSize?: number
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function SplitterPanel({
|
|
345
|
+
minSize,
|
|
346
|
+
maxSize,
|
|
347
|
+
collapsible,
|
|
348
|
+
collapsedSize,
|
|
349
|
+
id: idProp,
|
|
350
|
+
className,
|
|
351
|
+
style,
|
|
352
|
+
...props
|
|
353
|
+
}: SplitterPanelProps) {
|
|
354
|
+
const { sizes, ids, mounted, api } = useSplitterContext('SplitterPanel')
|
|
355
|
+
const renderIndex = React.useContext(SplitterIndexContext)
|
|
356
|
+
const generatedId = React.useId()
|
|
357
|
+
const id = idProp ?? generatedId
|
|
358
|
+
const constraints = toConstraints({ minSize, maxSize, collapsible, collapsedSize })
|
|
359
|
+
const { min, max, collapsible: canCollapse, collapsedSize: collapsedAt } = constraints
|
|
360
|
+
const { register } = api
|
|
361
|
+
|
|
362
|
+
React.useLayoutEffect(
|
|
363
|
+
() => register(id, { min, max, collapsible: canCollapse, collapsedSize: collapsedAt }),
|
|
364
|
+
[register, id, min, max, canCollapse, collapsedAt],
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
const size = sizes[mounted ? ids.indexOf(id) : renderIndex]
|
|
368
|
+
|
|
369
|
+
return (
|
|
370
|
+
<div
|
|
371
|
+
{...props}
|
|
372
|
+
id={id}
|
|
373
|
+
data-slot="splitter-panel"
|
|
374
|
+
data-collapsed={isCollapsed(size, constraints) ? '' : undefined}
|
|
375
|
+
className={cn('min-h-0 min-w-0 overflow-hidden', className)}
|
|
376
|
+
style={{ ...style, flex: `${size ?? 1} 1 0px` }}
|
|
377
|
+
/>
|
|
378
|
+
)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// The line is a pseudo-element that scales to 2px, so the handle keeps its 1px box and the panels never shift.
|
|
382
|
+
const handleClasses = cn(
|
|
383
|
+
'group/splitter-handle outline-ring relative z-10 shrink-0 touch-none select-none focus-visible:outline-2',
|
|
384
|
+
'after:bg-border after:absolute after:inset-0 after:-z-1 after:transition-[scale] after:duration-250 motion-reduce:after:transition-none',
|
|
385
|
+
'data-[orientation=horizontal]:hover:after:scale-x-200 data-[orientation=horizontal]:data-dragging:after:scale-x-200',
|
|
386
|
+
'data-[orientation=vertical]:hover:after:scale-y-200 data-[orientation=vertical]:data-dragging:after:scale-y-200',
|
|
387
|
+
'before:absolute data-[orientation=horizontal]:before:inset-y-0 data-[orientation=vertical]:before:inset-x-0',
|
|
388
|
+
'data-[orientation=horizontal]:w-(--border-width) data-[orientation=horizontal]:cursor-col-resize data-[orientation=horizontal]:before:-inset-x-1.5 data-[orientation=horizontal]:pointer-coarse:before:-inset-x-3',
|
|
389
|
+
'data-[orientation=vertical]:h-(--border-width) data-[orientation=vertical]:cursor-row-resize data-[orientation=vertical]:before:-inset-y-1.5 data-[orientation=vertical]:pointer-coarse:before:-inset-y-3',
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
const gripClasses = cn(
|
|
393
|
+
'bg-background border-border text-foreground-muted absolute top-1/2 left-1/2 flex -translate-1/2 items-center justify-center gap-1 rounded-full border shadow-xs',
|
|
394
|
+
'dark:bg-background-subtle dark:backdrop-blur-xl',
|
|
395
|
+
'transition-[border-width] duration-250 group-hover/splitter-handle:border-2 group-data-dragging/splitter-handle:border-2 motion-reduce:transition-none',
|
|
396
|
+
'data-[orientation=horizontal]:h-7 data-[orientation=horizontal]:w-4 data-[orientation=horizontal]:flex-col data-[orientation=vertical]:h-4 data-[orientation=vertical]:w-7',
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
const gripDot = <span className="size-0.75 rounded-full bg-current" />
|
|
400
|
+
|
|
401
|
+
interface SplitterHandleProps extends React.ComponentPropsWithoutRef<'div'> {
|
|
402
|
+
/**
|
|
403
|
+
* Show a pill with dots on the line, to mark it as draggable.
|
|
404
|
+
* @default false
|
|
405
|
+
*/
|
|
406
|
+
grip?: boolean
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
interface DragState {
|
|
410
|
+
pointerId: number
|
|
411
|
+
origin: number
|
|
412
|
+
extent: number
|
|
413
|
+
from: readonly number[]
|
|
414
|
+
latest: readonly number[] | null
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function SplitterHandle({
|
|
418
|
+
grip = false,
|
|
419
|
+
className,
|
|
420
|
+
children,
|
|
421
|
+
onKeyDown,
|
|
422
|
+
onPointerDown,
|
|
423
|
+
onPointerMove,
|
|
424
|
+
onPointerUp,
|
|
425
|
+
onPointerCancel,
|
|
426
|
+
...props
|
|
427
|
+
}: SplitterHandleProps) {
|
|
428
|
+
const { orientation, keyboardStep, sizes, ids, constraints, mounted, api } = useSplitterContext('SplitterHandle')
|
|
429
|
+
const rtl = useDirection() === 'rtl'
|
|
430
|
+
const vertical = orientation === 'vertical'
|
|
431
|
+
const renderIndex = React.useContext(SplitterIndexContext)
|
|
432
|
+
const handleRef = React.useRef<HTMLDivElement>(null)
|
|
433
|
+
const dragRef = React.useRef<DragState | null>(null)
|
|
434
|
+
const [domIndex, setDomIndex] = React.useState<number | null>(null)
|
|
435
|
+
const [dragging, setDragging] = React.useState(false)
|
|
436
|
+
|
|
437
|
+
React.useLayoutEffect(() => {
|
|
438
|
+
if (!mounted) return
|
|
439
|
+
let node = handleRef.current?.previousElementSibling ?? null
|
|
440
|
+
while (node && !isPanel(node)) node = node.previousElementSibling
|
|
441
|
+
setDomIndex(node ? ids.indexOf(node.id) : -1)
|
|
442
|
+
}, [mounted, ids])
|
|
443
|
+
|
|
444
|
+
const index = domIndex ?? renderIndex
|
|
445
|
+
const size = sizes[index]
|
|
446
|
+
const c = constraints[index]
|
|
447
|
+
const resizable = index >= 0 && index + 1 < sizes.length && size !== undefined && c !== undefined
|
|
448
|
+
const neighbor = constraints[index + 1]
|
|
449
|
+
const pair = (size ?? 0) + (sizes[index + 1] ?? 0)
|
|
450
|
+
const valueMin = c && Math.max(c.collapsible ? c.collapsedSize : c.min, neighbor ? pair - neighbor.max : 0)
|
|
451
|
+
const valueMax =
|
|
452
|
+
c && Math.min(c.max, neighbor ? pair - (neighbor.collapsible ? neighbor.collapsedSize : neighbor.min) : 100)
|
|
453
|
+
|
|
454
|
+
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
|
455
|
+
onKeyDown?.(event)
|
|
456
|
+
if (event.defaultPrevented || !resizable) return
|
|
457
|
+
const forward = vertical ? 'ArrowDown' : rtl ? 'ArrowLeft' : 'ArrowRight'
|
|
458
|
+
const backward = vertical ? 'ArrowUp' : rtl ? 'ArrowRight' : 'ArrowLeft'
|
|
459
|
+
const targets: Record<string, number | undefined> = {
|
|
460
|
+
[forward]: size + keyboardStep,
|
|
461
|
+
[backward]: size - keyboardStep,
|
|
462
|
+
Home: c.collapsible ? c.collapsedSize : c.min,
|
|
463
|
+
End: c.max,
|
|
464
|
+
Enter: c.collapsible ? (isCollapsed(size, c) ? api.restoredSize(index) : c.collapsedSize) : undefined,
|
|
465
|
+
}
|
|
466
|
+
const target = targets[event.key]
|
|
467
|
+
if (target === undefined) return
|
|
468
|
+
event.preventDefault()
|
|
469
|
+
const next = api.resize(index, target, 'step', sizes, sizes)
|
|
470
|
+
if (next) api.commit(next)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const handlePointerDown = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
474
|
+
onPointerDown?.(event)
|
|
475
|
+
if (event.defaultPrevented || !resizable || (event.pointerType === 'mouse' && event.button !== 0)) return
|
|
476
|
+
const extent = api.measure()
|
|
477
|
+
if (!extent) return
|
|
478
|
+
dragRef.current = {
|
|
479
|
+
pointerId: event.pointerId,
|
|
480
|
+
origin: vertical ? event.clientY : event.clientX,
|
|
481
|
+
extent,
|
|
482
|
+
from: sizes,
|
|
483
|
+
latest: null,
|
|
484
|
+
}
|
|
485
|
+
setDragging(true)
|
|
486
|
+
api.setDragging(true)
|
|
487
|
+
try {
|
|
488
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
489
|
+
} catch {}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const handlePointerMove = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
493
|
+
onPointerMove?.(event)
|
|
494
|
+
const drag = dragRef.current
|
|
495
|
+
if (!drag || drag.pointerId !== event.pointerId) return
|
|
496
|
+
const delta = (((vertical ? event.clientY : event.clientX) - drag.origin) / drag.extent) * 100
|
|
497
|
+
const target = (drag.from[index] ?? 0) + (!vertical && rtl ? -delta : delta)
|
|
498
|
+
const next = api.resize(index, target, 'drag', drag.from, drag.latest ?? drag.from)
|
|
499
|
+
if (next) drag.latest = next
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const endDrag = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
503
|
+
const drag = dragRef.current
|
|
504
|
+
if (!drag || drag.pointerId !== event.pointerId) return
|
|
505
|
+
dragRef.current = null
|
|
506
|
+
if (event.currentTarget.hasPointerCapture?.(event.pointerId)) {
|
|
507
|
+
event.currentTarget.releasePointerCapture(event.pointerId)
|
|
508
|
+
}
|
|
509
|
+
setDragging(false)
|
|
510
|
+
api.setDragging(false)
|
|
511
|
+
if (drag.latest) api.commit(drag.latest)
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
return (
|
|
515
|
+
<div
|
|
516
|
+
ref={handleRef}
|
|
517
|
+
role="separator"
|
|
518
|
+
tabIndex={0}
|
|
519
|
+
aria-orientation={vertical ? 'horizontal' : 'vertical'}
|
|
520
|
+
data-base-ui-swipe-ignore=""
|
|
521
|
+
{...props}
|
|
522
|
+
aria-valuenow={size === undefined ? undefined : Math.round(size)}
|
|
523
|
+
aria-valuemin={valueMin === undefined ? undefined : Math.round(valueMin)}
|
|
524
|
+
aria-valuemax={valueMax === undefined ? undefined : Math.round(valueMax)}
|
|
525
|
+
aria-controls={ids[index]}
|
|
526
|
+
data-slot="splitter-handle"
|
|
527
|
+
data-orientation={orientation}
|
|
528
|
+
data-dragging={dragging ? '' : undefined}
|
|
529
|
+
className={cn(handleClasses, className)}
|
|
530
|
+
onKeyDown={handleKeyDown}
|
|
531
|
+
onPointerDown={handlePointerDown}
|
|
532
|
+
onPointerMove={handlePointerMove}
|
|
533
|
+
onPointerUp={(event) => {
|
|
534
|
+
onPointerUp?.(event)
|
|
535
|
+
endDrag(event)
|
|
536
|
+
}}
|
|
537
|
+
onPointerCancel={(event) => {
|
|
538
|
+
onPointerCancel?.(event)
|
|
539
|
+
endDrag(event)
|
|
540
|
+
}}
|
|
541
|
+
>
|
|
542
|
+
{grip ? (
|
|
543
|
+
<span data-slot="splitter-grip" data-orientation={orientation} aria-hidden="true" className={gripClasses}>
|
|
544
|
+
{gripDot}
|
|
545
|
+
{gripDot}
|
|
546
|
+
{gripDot}
|
|
547
|
+
</span>
|
|
548
|
+
) : null}
|
|
549
|
+
{children}
|
|
550
|
+
</div>
|
|
551
|
+
)
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
export { Splitter, SplitterPanel, SplitterHandle }
|
|
555
|
+
export type { SplitterProps, SplitterPanelProps, SplitterHandleProps }
|
|
@@ -115,7 +115,7 @@ function Textarea({
|
|
|
115
115
|
'h-(--textarea-h) resize-y items-start overflow-hidden',
|
|
116
116
|
sizeMinHeight[inputSize],
|
|
117
117
|
sizePaddingY[inputSize],
|
|
118
|
-
'has-[textarea:disabled]:border-border-strong! has-[textarea:disabled]:bg-background-subtle! has-[textarea:disabled]:opacity-disabled has-[textarea:disabled]:cursor-not-allowed has-[textarea:disabled]:border-dashed',
|
|
118
|
+
'has-[textarea:disabled]:border-border-strong! has-[textarea:disabled]:bg-background-subtle! has-[textarea:disabled]:opacity-disabled has-[textarea:disabled]:cursor-not-allowed has-[textarea:disabled]:border-dashed has-[textarea:disabled]:backdrop-blur-md',
|
|
119
119
|
className,
|
|
120
120
|
)}
|
|
121
121
|
>
|
|
@@ -577,7 +577,7 @@ function TimeField({
|
|
|
577
577
|
: cn(
|
|
578
578
|
inputVariants({ variant, size, state: 'within' }),
|
|
579
579
|
'select-none',
|
|
580
|
-
'data-disabled:border-border-strong! data-disabled:bg-background-subtle! data-disabled:opacity-disabled data-disabled:pointer-events-none data-disabled:cursor-not-allowed data-disabled:border-dashed',
|
|
580
|
+
'data-disabled:border-border-strong! data-disabled:bg-background-subtle! data-disabled:opacity-disabled data-disabled:pointer-events-none data-disabled:cursor-not-allowed data-disabled:border-dashed data-disabled:backdrop-blur-md',
|
|
581
581
|
),
|
|
582
582
|
className,
|
|
583
583
|
)}
|
|
@@ -170,6 +170,21 @@ describe('Toc', () => {
|
|
|
170
170
|
expect(list.style.getPropertyValue('--toc-indicator-height')).toBe('56px')
|
|
171
171
|
})
|
|
172
172
|
|
|
173
|
+
it('tracks the same links with highlight on, without passing the prop to the DOM', async () => {
|
|
174
|
+
const { container } = renderToc({ highlight: true })
|
|
175
|
+
const links = screen.getAllByRole('link')
|
|
176
|
+
Object.defineProperty(links[0], 'offsetTop', { value: 8 })
|
|
177
|
+
Object.defineProperty(links[0], 'offsetHeight', { value: 24 })
|
|
178
|
+
|
|
179
|
+
lastObserver().trigger([entry('intro', true)])
|
|
180
|
+
|
|
181
|
+
expect(screen.getByRole('navigation')).not.toHaveAttribute('highlight')
|
|
182
|
+
expect(links[0]).toHaveAttribute('data-active')
|
|
183
|
+
expect(links[0]).toHaveAttribute('aria-current', 'location')
|
|
184
|
+
expect(screen.getByRole('list').style.getPropertyValue('--toc-indicator-height')).toBe('24px')
|
|
185
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
186
|
+
})
|
|
187
|
+
|
|
173
188
|
it('supports the render prop on TocLink', () => {
|
|
174
189
|
render(
|
|
175
190
|
<>
|
|
@@ -8,6 +8,7 @@ import { cn } from '../../internal/utils'
|
|
|
8
8
|
type TocContextValue = {
|
|
9
9
|
activeIds: readonly string[]
|
|
10
10
|
currentId: string | null
|
|
11
|
+
highlight: boolean
|
|
11
12
|
register: (id: string, element: HTMLElement) => () => void
|
|
12
13
|
getLinkElement: (id: string) => HTMLElement | undefined
|
|
13
14
|
}
|
|
@@ -47,9 +48,14 @@ interface TocProps extends React.ComponentPropsWithoutRef<'nav'> {
|
|
|
47
48
|
* @default '0px'
|
|
48
49
|
*/
|
|
49
50
|
rootMargin?: string
|
|
51
|
+
/**
|
|
52
|
+
* Tints the area behind the active links, with the indicator line along its edge.
|
|
53
|
+
* @default false
|
|
54
|
+
*/
|
|
55
|
+
highlight?: boolean
|
|
50
56
|
}
|
|
51
57
|
|
|
52
|
-
function Toc({ className, rootMargin = '0px', ...props }: TocProps) {
|
|
58
|
+
function Toc({ className, rootMargin = '0px', highlight = false, ...props }: TocProps) {
|
|
53
59
|
const [ids, setIds] = React.useState<readonly string[]>([])
|
|
54
60
|
const [activeIds, setActiveIds] = React.useState<readonly string[]>([])
|
|
55
61
|
const linkElementsRef = React.useRef(new Map<string, HTMLElement>())
|
|
@@ -95,8 +101,8 @@ function Toc({ className, rootMargin = '0px', ...props }: TocProps) {
|
|
|
95
101
|
}, [ids, rootMargin])
|
|
96
102
|
|
|
97
103
|
const ctx = React.useMemo<TocContextValue>(
|
|
98
|
-
() => ({ activeIds, currentId: activeIds[0] ?? null, register, getLinkElement }),
|
|
99
|
-
[activeIds, register, getLinkElement],
|
|
104
|
+
() => ({ activeIds, currentId: activeIds[0] ?? null, highlight, register, getLinkElement }),
|
|
105
|
+
[activeIds, highlight, register, getLinkElement],
|
|
100
106
|
)
|
|
101
107
|
|
|
102
108
|
return (
|
|
@@ -108,8 +114,16 @@ function Toc({ className, rootMargin = '0px', ...props }: TocProps) {
|
|
|
108
114
|
|
|
109
115
|
type TocListProps = React.ComponentPropsWithoutRef<'ul'>
|
|
110
116
|
|
|
117
|
+
const INDICATOR_VARIANTS: Record<'line' | 'highlight', string> = {
|
|
118
|
+
line: 'before:w-0.5 before:bg-foreground-intense',
|
|
119
|
+
highlight: cn(
|
|
120
|
+
'isolate before:-z-1 before:inset-e-0 before:rounded-e-2xs',
|
|
121
|
+
'before:border-s-2 before:border-foreground-intense before:bg-background-soft before:backdrop-blur-md',
|
|
122
|
+
),
|
|
123
|
+
}
|
|
124
|
+
|
|
111
125
|
function TocList({ className, style, ...props }: TocListProps) {
|
|
112
|
-
const { activeIds, getLinkElement } = useTocContext('TocList')
|
|
126
|
+
const { activeIds, highlight, getLinkElement } = useTocContext('TocList')
|
|
113
127
|
const listRef = React.useRef<HTMLUListElement>(null)
|
|
114
128
|
const [indicator, setIndicator] = React.useState({ top: 0, height: 0, visible: false })
|
|
115
129
|
const [animate, setAnimate] = React.useState(false)
|
|
@@ -152,9 +166,9 @@ function TocList({ className, style, ...props }: TocListProps) {
|
|
|
152
166
|
role="list"
|
|
153
167
|
className={cn(
|
|
154
168
|
'border-border relative flex flex-col border-s',
|
|
155
|
-
'before:pointer-events-none before:absolute before:-inset-s-px
|
|
169
|
+
'before:pointer-events-none before:absolute before:-inset-s-px',
|
|
156
170
|
'before:top-(--toc-indicator-top) before:h-(--toc-indicator-height)',
|
|
157
|
-
'
|
|
171
|
+
INDICATOR_VARIANTS[highlight ? 'highlight' : 'line'],
|
|
158
172
|
'before:transition-opacity before:duration-300 before:ease-out',
|
|
159
173
|
animate && 'before:transition-[top,height,opacity]',
|
|
160
174
|
'motion-reduce:before:transition-none',
|
|
@@ -202,7 +216,7 @@ interface TocLinkProps extends useRender.ComponentProps<'a', TocLinkState> {
|
|
|
202
216
|
}
|
|
203
217
|
|
|
204
218
|
function TocLink({ className, depth = 2, href, render, ref, ...props }: TocLinkProps) {
|
|
205
|
-
const { activeIds, currentId, register } = useTocContext('TocLink')
|
|
219
|
+
const { activeIds, currentId, highlight, register } = useTocContext('TocLink')
|
|
206
220
|
const id = href.startsWith('#') ? href.slice(1) : null
|
|
207
221
|
const linkRef = React.useRef<HTMLAnchorElement>(null)
|
|
208
222
|
|
|
@@ -231,6 +245,7 @@ function TocLink({ className, depth = 2, href, render, ref, ...props }: TocLinkP
|
|
|
231
245
|
'active:scale-[0.98] active:translate-y-px active:duration-100 active:ease-in-out',
|
|
232
246
|
"[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='stroke-'])]:stroke-[1.85] [&_svg:not([class*='size-'])]:size-4.5",
|
|
233
247
|
DEPTH_INDENT[Math.min(Math.max(Math.trunc(depth), 2), 6)],
|
|
248
|
+
highlight && 'pe-3',
|
|
234
249
|
className,
|
|
235
250
|
),
|
|
236
251
|
} as unknown as React.AnchorHTMLAttributes<HTMLAnchorElement>,
|
package/src/index.ts
CHANGED
|
@@ -25,6 +25,7 @@ export * from './components/color-slider/color-slider'
|
|
|
25
25
|
export * from './components/color-swatch/color-swatch'
|
|
26
26
|
export * from './components/color-swatch-picker/color-swatch-picker'
|
|
27
27
|
export * from './components/combobox/combobox'
|
|
28
|
+
export * from './components/command-palette/command-palette'
|
|
28
29
|
export * from './components/compare-slider/compare-slider'
|
|
29
30
|
export * from './components/context-menu/context-menu'
|
|
30
31
|
export * from './components/copy-button/copy-button'
|
|
@@ -61,6 +62,7 @@ export * from './components/skeleton/skeleton'
|
|
|
61
62
|
export * from './components/slider/slider'
|
|
62
63
|
export * from './components/sparkline/sparkline'
|
|
63
64
|
export * from './components/spinner/spinner'
|
|
65
|
+
export * from './components/splitter/splitter'
|
|
64
66
|
export * from './components/switch/switch'
|
|
65
67
|
export * from './components/table/table'
|
|
66
68
|
export * from './components/tabs/tabs'
|