@leitware/composables 1.6.0 → 1.7.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/README.md +86 -23
- package/dist/presets/kumo.css +174 -100
- package/dist/styles.css +1 -1
- package/package.json +1 -1
- package/rules/composables.md +37 -32
- package/skills/use-composables/references/public-api.md +2 -2
- package/src/_internal/button.tsx +5 -5
- package/src/_internal/clipboard-text.tsx +102 -0
- package/src/_internal/flow.tsx +390 -0
- package/src/_internal/layer-card.tsx +61 -0
- package/src/_internal/table-of-contents.tsx +124 -0
- package/src/hooks/use-table-of-contents-active-id.ts +63 -0
- package/src/index.ts +50 -0
- package/src/lib/clipboard.ts +57 -0
- package/src/lib/flow.ts +53 -0
- package/src/opinionated/clipboard-text.tsx +135 -0
- package/src/opinionated/flow.tsx +74 -0
- package/src/opinionated/layer-card.tsx +49 -0
- package/src/opinionated/table-of-contents.tsx +121 -0
- package/src/styles/presets/kumo.css +174 -100
- package/src/styles/presets-data/kumo.ts +32 -50
- package/src/styles/tokens/components.css +20 -0
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import { mergeProps } from '@base-ui/react/merge-props'
|
|
2
|
+
import { useRender } from '@base-ui/react/use-render'
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
forwardRef,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useId,
|
|
9
|
+
useImperativeHandle,
|
|
10
|
+
useRef,
|
|
11
|
+
useState,
|
|
12
|
+
type ComponentPropsWithoutRef,
|
|
13
|
+
type PointerEvent,
|
|
14
|
+
type CSSProperties,
|
|
15
|
+
} from 'react'
|
|
16
|
+
import { cn, FOCUS_RING } from '../lib/utils'
|
|
17
|
+
import {
|
|
18
|
+
getFlowEdges,
|
|
19
|
+
getFlowPath,
|
|
20
|
+
type FlowTree,
|
|
21
|
+
type FlowOrientation,
|
|
22
|
+
type FlowPoint,
|
|
23
|
+
} from '../lib/flow'
|
|
24
|
+
|
|
25
|
+
export type FlowAlign = 'start' | 'center'
|
|
26
|
+
const FlowContext = createContext<{ orientation: FlowOrientation; align: FlowAlign }>({
|
|
27
|
+
orientation: 'horizontal',
|
|
28
|
+
align: 'start',
|
|
29
|
+
})
|
|
30
|
+
const sequenceClasses = (orientation: FlowOrientation, align: FlowAlign) =>
|
|
31
|
+
cn(
|
|
32
|
+
'flex w-max list-none gap-(--flow-gap) p-0',
|
|
33
|
+
orientation === 'vertical' ? 'flex-col' : 'flex-row',
|
|
34
|
+
align === 'center' ? 'items-center' : 'items-start',
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
function readTree(element: HTMLElement): FlowTree | undefined {
|
|
38
|
+
const kind = element.dataset.flowKind
|
|
39
|
+
if (kind === 'node') {
|
|
40
|
+
const id = element.querySelector<HTMLElement>('[data-flow-node]')?.dataset.flowNode
|
|
41
|
+
return id ? { kind: 'node', id } : undefined
|
|
42
|
+
}
|
|
43
|
+
if (kind !== 'sequence' && kind !== 'parallel') return
|
|
44
|
+
const items = element.querySelector<HTMLElement>(':scope > [data-flow-items]') ?? element
|
|
45
|
+
const children = Array.from(items.children).flatMap((child) => {
|
|
46
|
+
const tree = child instanceof HTMLElement ? readTree(child) : undefined
|
|
47
|
+
return tree ? [tree] : []
|
|
48
|
+
})
|
|
49
|
+
return { kind, children }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface FlowRootProps extends ComponentPropsWithoutRef<'div'> {
|
|
53
|
+
orientation?: FlowOrientation
|
|
54
|
+
align?: FlowAlign
|
|
55
|
+
/** Scrollable canvas with mouse dragging and native touch/keyboard scrolling. @default true */
|
|
56
|
+
canvas?: boolean
|
|
57
|
+
onOverflowChange?: (overflow: { x: boolean; y: boolean }) => void
|
|
58
|
+
}
|
|
59
|
+
interface MeasuredFlow {
|
|
60
|
+
width: number
|
|
61
|
+
height: number
|
|
62
|
+
paths: { id: string; d: string; disabled: boolean }[]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const FlowRoot = forwardRef<HTMLDivElement, FlowRootProps>(function FlowRoot(
|
|
66
|
+
{
|
|
67
|
+
orientation = 'horizontal',
|
|
68
|
+
align = 'start',
|
|
69
|
+
canvas = true,
|
|
70
|
+
onOverflowChange,
|
|
71
|
+
className,
|
|
72
|
+
children,
|
|
73
|
+
dir,
|
|
74
|
+
onPointerDown,
|
|
75
|
+
onPointerMove,
|
|
76
|
+
onPointerUp,
|
|
77
|
+
onPointerCancel,
|
|
78
|
+
onLostPointerCapture,
|
|
79
|
+
...props
|
|
80
|
+
},
|
|
81
|
+
ref,
|
|
82
|
+
) {
|
|
83
|
+
const viewportRef = useRef<HTMLDivElement>(null)
|
|
84
|
+
const contentRef = useRef<HTMLDivElement>(null)
|
|
85
|
+
const overflowCallback = useRef(onOverflowChange)
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
overflowCallback.current = onOverflowChange
|
|
88
|
+
}, [onOverflowChange])
|
|
89
|
+
const [drawing, setDrawing] = useState<MeasuredFlow>({ width: 0, height: 0, paths: [] })
|
|
90
|
+
const [panning, setPanning] = useState(false)
|
|
91
|
+
const drag = useRef<{ x: number; y: number; left: number; top: number } | null>(null)
|
|
92
|
+
useImperativeHandle(ref, () => viewportRef.current!, [])
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
const viewport = viewportRef.current
|
|
95
|
+
const content = contentRef.current
|
|
96
|
+
if (!viewport || !content) return
|
|
97
|
+
let frame = 0
|
|
98
|
+
let overflowKey = ''
|
|
99
|
+
const measure = () => {
|
|
100
|
+
const list = content.querySelector<HTMLElement>(':scope > [data-flow-items]')
|
|
101
|
+
const tree = list && readTree(list)
|
|
102
|
+
const origin = content.getBoundingClientRect()
|
|
103
|
+
const rtl = getComputedStyle(content).direction === 'rtl'
|
|
104
|
+
const nodes = new Map<string, HTMLElement>()
|
|
105
|
+
for (const node of content.querySelectorAll<HTMLElement>('[data-flow-node]')) {
|
|
106
|
+
if (node.closest('[data-slot="flow"]') === viewport && node.dataset.flowNode)
|
|
107
|
+
nodes.set(node.dataset.flowNode, node)
|
|
108
|
+
}
|
|
109
|
+
const point = (node: HTMLElement, exit: boolean): FlowPoint => {
|
|
110
|
+
const rect = node.getBoundingClientRect()
|
|
111
|
+
const anchor = node.querySelector<HTMLElement>(
|
|
112
|
+
`[data-flow-anchor="${exit ? 'end' : 'start'}"], [data-flow-anchor="both"]`,
|
|
113
|
+
)
|
|
114
|
+
const cross = anchor?.getBoundingClientRect() ?? rect
|
|
115
|
+
return orientation === 'vertical'
|
|
116
|
+
? {
|
|
117
|
+
x: cross.left + cross.width / 2 - origin.left,
|
|
118
|
+
y: (exit ? rect.bottom : rect.top) - origin.top,
|
|
119
|
+
}
|
|
120
|
+
: {
|
|
121
|
+
x: (exit !== rtl ? rect.right : rect.left) - origin.left,
|
|
122
|
+
y: cross.top + cross.height / 2 - origin.top,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const paths = (tree ? getFlowEdges(tree) : []).flatMap((edge) => {
|
|
126
|
+
const from = nodes.get(edge.from),
|
|
127
|
+
to = nodes.get(edge.to)
|
|
128
|
+
if (!from || !to) return []
|
|
129
|
+
const disabled = [from, to].some(
|
|
130
|
+
(node) =>
|
|
131
|
+
node.closest('[data-flow-kind="node"]')?.getAttribute('data-disabled') === 'true',
|
|
132
|
+
)
|
|
133
|
+
return [
|
|
134
|
+
{
|
|
135
|
+
id: `${edge.from}-${edge.to}`,
|
|
136
|
+
d: getFlowPath(point(from, true), point(to, false), orientation),
|
|
137
|
+
disabled,
|
|
138
|
+
},
|
|
139
|
+
]
|
|
140
|
+
})
|
|
141
|
+
const next = { width: content.offsetWidth, height: content.offsetHeight, paths }
|
|
142
|
+
setDrawing((previous) =>
|
|
143
|
+
JSON.stringify(previous) === JSON.stringify(next) ? previous : next,
|
|
144
|
+
)
|
|
145
|
+
const overflow = {
|
|
146
|
+
x: viewport.scrollWidth > viewport.clientWidth,
|
|
147
|
+
y: viewport.scrollHeight > viewport.clientHeight,
|
|
148
|
+
}
|
|
149
|
+
const key = JSON.stringify(overflow)
|
|
150
|
+
if (key !== overflowKey) {
|
|
151
|
+
overflowKey = key
|
|
152
|
+
overflowCallback.current?.(overflow)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const schedule = () => {
|
|
156
|
+
cancelAnimationFrame(frame)
|
|
157
|
+
frame = requestAnimationFrame(measure)
|
|
158
|
+
}
|
|
159
|
+
const resize = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(schedule)
|
|
160
|
+
const observeSizes = () => {
|
|
161
|
+
resize?.disconnect()
|
|
162
|
+
resize?.observe(content)
|
|
163
|
+
resize?.observe(viewport)
|
|
164
|
+
for (const node of content.querySelectorAll<HTMLElement>(
|
|
165
|
+
'[data-flow-node], [data-flow-anchor]',
|
|
166
|
+
))
|
|
167
|
+
resize?.observe(node)
|
|
168
|
+
}
|
|
169
|
+
const mutation = new MutationObserver(() => {
|
|
170
|
+
observeSizes()
|
|
171
|
+
schedule()
|
|
172
|
+
})
|
|
173
|
+
mutation.observe(content, {
|
|
174
|
+
subtree: true,
|
|
175
|
+
childList: true,
|
|
176
|
+
characterData: true,
|
|
177
|
+
attributes: true,
|
|
178
|
+
attributeFilter: ['class', 'style', 'data-disabled', 'dir'],
|
|
179
|
+
})
|
|
180
|
+
mutation.observe(viewport, { attributes: true, attributeFilter: ['dir'] })
|
|
181
|
+
observeSizes()
|
|
182
|
+
measure()
|
|
183
|
+
window.addEventListener('resize', schedule)
|
|
184
|
+
return () => {
|
|
185
|
+
cancelAnimationFrame(frame)
|
|
186
|
+
resize?.disconnect()
|
|
187
|
+
mutation.disconnect()
|
|
188
|
+
window.removeEventListener('resize', schedule)
|
|
189
|
+
}
|
|
190
|
+
}, [orientation, align, canvas, dir])
|
|
191
|
+
const stopDrag = (event: PointerEvent<HTMLDivElement>) => {
|
|
192
|
+
drag.current = null
|
|
193
|
+
setPanning(false)
|
|
194
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId))
|
|
195
|
+
event.currentTarget.releasePointerCapture(event.pointerId)
|
|
196
|
+
}
|
|
197
|
+
return (
|
|
198
|
+
<FlowContext.Provider value={{ orientation, align }}>
|
|
199
|
+
<div
|
|
200
|
+
ref={viewportRef}
|
|
201
|
+
data-slot="flow"
|
|
202
|
+
data-orientation={orientation}
|
|
203
|
+
dir={dir}
|
|
204
|
+
role="group"
|
|
205
|
+
aria-label="Flow diagram"
|
|
206
|
+
tabIndex={canvas ? 0 : undefined}
|
|
207
|
+
className={cn(
|
|
208
|
+
'max-w-full outline-none',
|
|
209
|
+
canvas &&
|
|
210
|
+
'overflow-auto rounded-(--flow-canvas-radius) border border-stroke bg-muted bg-[image:var(--flow-canvas-background-image)] [background-size:var(--flow-canvas-background-size)_var(--flow-canvas-background-size)]',
|
|
211
|
+
canvas && FOCUS_RING,
|
|
212
|
+
panning && 'cursor-grabbing select-none',
|
|
213
|
+
className,
|
|
214
|
+
)}
|
|
215
|
+
{...props}
|
|
216
|
+
onPointerDown={(event) => {
|
|
217
|
+
onPointerDown?.(event)
|
|
218
|
+
if (
|
|
219
|
+
!canvas ||
|
|
220
|
+
event.defaultPrevented ||
|
|
221
|
+
event.button !== 0 ||
|
|
222
|
+
event.pointerType !== 'mouse' ||
|
|
223
|
+
!(event.target instanceof Element) ||
|
|
224
|
+
event.target.closest('[data-flow-kind="node"], a, button, input, textarea, select')
|
|
225
|
+
)
|
|
226
|
+
return
|
|
227
|
+
const el = event.currentTarget
|
|
228
|
+
if (el.scrollWidth <= el.clientWidth && el.scrollHeight <= el.clientHeight) return
|
|
229
|
+
drag.current = {
|
|
230
|
+
x: event.clientX,
|
|
231
|
+
y: event.clientY,
|
|
232
|
+
left: el.scrollLeft,
|
|
233
|
+
top: el.scrollTop,
|
|
234
|
+
}
|
|
235
|
+
el.setPointerCapture(event.pointerId)
|
|
236
|
+
setPanning(true)
|
|
237
|
+
event.preventDefault()
|
|
238
|
+
}}
|
|
239
|
+
onPointerMove={(event) => {
|
|
240
|
+
onPointerMove?.(event)
|
|
241
|
+
if (event.defaultPrevented || !drag.current) return
|
|
242
|
+
event.currentTarget.scrollLeft = drag.current.left - (event.clientX - drag.current.x)
|
|
243
|
+
event.currentTarget.scrollTop = drag.current.top - (event.clientY - drag.current.y)
|
|
244
|
+
}}
|
|
245
|
+
onPointerUp={(event) => {
|
|
246
|
+
onPointerUp?.(event)
|
|
247
|
+
stopDrag(event)
|
|
248
|
+
}}
|
|
249
|
+
onPointerCancel={(event) => {
|
|
250
|
+
onPointerCancel?.(event)
|
|
251
|
+
stopDrag(event)
|
|
252
|
+
}}
|
|
253
|
+
onLostPointerCapture={(event) => {
|
|
254
|
+
onLostPointerCapture?.(event)
|
|
255
|
+
drag.current = null
|
|
256
|
+
setPanning(false)
|
|
257
|
+
}}
|
|
258
|
+
>
|
|
259
|
+
<div
|
|
260
|
+
ref={contentRef}
|
|
261
|
+
data-slot="flow-content"
|
|
262
|
+
className={cn(
|
|
263
|
+
'relative w-max min-w-full',
|
|
264
|
+
canvas && 'px-(--flow-canvas-padding-x) py-(--flow-canvas-padding-y)',
|
|
265
|
+
)}
|
|
266
|
+
>
|
|
267
|
+
<svg
|
|
268
|
+
data-slot="flow-connectors"
|
|
269
|
+
aria-hidden="true"
|
|
270
|
+
className="pointer-events-none absolute inset-0 overflow-visible"
|
|
271
|
+
width={drawing.width}
|
|
272
|
+
height={drawing.height}
|
|
273
|
+
fill="none"
|
|
274
|
+
stroke="var(--flow-connector-color,var(--border-tertiary))"
|
|
275
|
+
strokeWidth="1.5"
|
|
276
|
+
strokeLinecap="round"
|
|
277
|
+
strokeLinejoin="round"
|
|
278
|
+
>
|
|
279
|
+
{drawing.paths.map((path) => (
|
|
280
|
+
<path
|
|
281
|
+
key={path.id}
|
|
282
|
+
data-slot="flow-connector"
|
|
283
|
+
data-disabled={path.disabled || undefined}
|
|
284
|
+
d={path.d}
|
|
285
|
+
opacity={path.disabled ? 'var(--opacity-disabled)' : undefined}
|
|
286
|
+
/>
|
|
287
|
+
))}
|
|
288
|
+
</svg>
|
|
289
|
+
<ol
|
|
290
|
+
data-flow-items=""
|
|
291
|
+
data-flow-kind="sequence"
|
|
292
|
+
className={sequenceClasses(orientation, align)}
|
|
293
|
+
>
|
|
294
|
+
{children}
|
|
295
|
+
</ol>
|
|
296
|
+
</div>
|
|
297
|
+
</div>
|
|
298
|
+
</FlowContext.Provider>
|
|
299
|
+
)
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
export type FlowNodeProps = useRender.ComponentProps<'div'> & { disabled?: boolean }
|
|
303
|
+
export const FlowNode = forwardRef<HTMLDivElement, FlowNodeProps>(function FlowNode(
|
|
304
|
+
{ disabled = false, render, className, ...props },
|
|
305
|
+
ref,
|
|
306
|
+
) {
|
|
307
|
+
const nodeId = useId()
|
|
308
|
+
const defaultProps = {
|
|
309
|
+
'data-slot': 'flow-node',
|
|
310
|
+
'data-flow-node': nodeId,
|
|
311
|
+
'aria-disabled': disabled || undefined,
|
|
312
|
+
className: cn(
|
|
313
|
+
'relative min-w-0 rounded-(--flow-node-radius) border border-stroke-secondary bg-surface-default px-3 py-2 text-sm text-default shadow-(--flow-node-shadow)',
|
|
314
|
+
disabled && 'opacity-disabled',
|
|
315
|
+
className,
|
|
316
|
+
),
|
|
317
|
+
}
|
|
318
|
+
const node = useRender({
|
|
319
|
+
defaultTagName: 'div',
|
|
320
|
+
render,
|
|
321
|
+
ref,
|
|
322
|
+
props: mergeProps<'div'>(defaultProps, props),
|
|
323
|
+
})
|
|
324
|
+
return (
|
|
325
|
+
<li data-flow-kind="node" data-disabled={disabled} className="relative shrink-0">
|
|
326
|
+
{node}
|
|
327
|
+
</li>
|
|
328
|
+
)
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
export const FlowParallel = forwardRef<HTMLLIElement, ComponentPropsWithoutRef<'li'>>(
|
|
332
|
+
function FlowParallel({ className, children, ...props }, ref) {
|
|
333
|
+
const { orientation } = useContext(FlowContext)
|
|
334
|
+
return (
|
|
335
|
+
<li
|
|
336
|
+
ref={ref}
|
|
337
|
+
data-slot="flow-parallel"
|
|
338
|
+
data-flow-kind="parallel"
|
|
339
|
+
className={cn('shrink-0', className)}
|
|
340
|
+
{...props}
|
|
341
|
+
>
|
|
342
|
+
<ul
|
|
343
|
+
data-flow-items=""
|
|
344
|
+
className={cn(
|
|
345
|
+
'flex w-max list-none gap-(--flow-branch-gap) p-0',
|
|
346
|
+
orientation === 'horizontal' ? 'flex-col' : 'flex-row',
|
|
347
|
+
)}
|
|
348
|
+
>
|
|
349
|
+
{children}
|
|
350
|
+
</ul>
|
|
351
|
+
</li>
|
|
352
|
+
)
|
|
353
|
+
},
|
|
354
|
+
)
|
|
355
|
+
export const FlowList = forwardRef<HTMLLIElement, ComponentPropsWithoutRef<'li'>>(function FlowList(
|
|
356
|
+
{ className, children, ...props },
|
|
357
|
+
ref,
|
|
358
|
+
) {
|
|
359
|
+
const { orientation, align } = useContext(FlowContext)
|
|
360
|
+
return (
|
|
361
|
+
<li
|
|
362
|
+
ref={ref}
|
|
363
|
+
data-slot="flow-list"
|
|
364
|
+
data-flow-kind="sequence"
|
|
365
|
+
className={cn('shrink-0', className)}
|
|
366
|
+
{...props}
|
|
367
|
+
>
|
|
368
|
+
<ol data-flow-items="" className={sequenceClasses(orientation, align)}>
|
|
369
|
+
{children}
|
|
370
|
+
</ol>
|
|
371
|
+
</li>
|
|
372
|
+
)
|
|
373
|
+
})
|
|
374
|
+
export interface FlowAnchorProps extends ComponentPropsWithoutRef<'span'> {
|
|
375
|
+
type?: 'start' | 'end' | 'both'
|
|
376
|
+
}
|
|
377
|
+
export const FlowAnchor = forwardRef<HTMLSpanElement, FlowAnchorProps>(function FlowAnchor(
|
|
378
|
+
{ type = 'both', style, ...props },
|
|
379
|
+
ref,
|
|
380
|
+
) {
|
|
381
|
+
return (
|
|
382
|
+
<span
|
|
383
|
+
ref={ref}
|
|
384
|
+
data-slot="flow-anchor"
|
|
385
|
+
data-flow-anchor={type}
|
|
386
|
+
style={{ display: 'block', ...style } as CSSProperties}
|
|
387
|
+
{...props}
|
|
388
|
+
/>
|
|
389
|
+
)
|
|
390
|
+
})
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { mergeProps } from '@base-ui/react/merge-props'
|
|
2
|
+
import { useRender } from '@base-ui/react/use-render'
|
|
3
|
+
import { forwardRef, type ComponentPropsWithoutRef } from 'react'
|
|
4
|
+
import { cn } from '../lib/utils'
|
|
5
|
+
|
|
6
|
+
export type LayerCardRootProps = useRender.ComponentProps<'div'> & { layered?: boolean }
|
|
7
|
+
|
|
8
|
+
export const LayerCardRoot = forwardRef<HTMLDivElement, LayerCardRootProps>(function LayerCardRoot(
|
|
9
|
+
{ layered = true, render, className, ...props },
|
|
10
|
+
ref,
|
|
11
|
+
) {
|
|
12
|
+
const defaultProps = {
|
|
13
|
+
'data-slot': 'layer-card',
|
|
14
|
+
'data-layered': layered,
|
|
15
|
+
className: cn(
|
|
16
|
+
'w-full overflow-hidden rounded-(--layer-card-radius) border border-stroke text-default',
|
|
17
|
+
layered ? 'bg-muted' : 'bg-surface-default shadow-(--layer-card-shadow)',
|
|
18
|
+
className,
|
|
19
|
+
),
|
|
20
|
+
}
|
|
21
|
+
return useRender({
|
|
22
|
+
defaultTagName: 'div',
|
|
23
|
+
render,
|
|
24
|
+
ref,
|
|
25
|
+
props: mergeProps<'div'>(defaultProps, props),
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
export type LayerCardSectionProps = ComponentPropsWithoutRef<'div'>
|
|
30
|
+
|
|
31
|
+
export const LayerCardPrimary = forwardRef<HTMLDivElement, LayerCardSectionProps>(
|
|
32
|
+
function LayerCardPrimary({ className, ...props }, ref) {
|
|
33
|
+
return (
|
|
34
|
+
<div
|
|
35
|
+
ref={ref}
|
|
36
|
+
data-slot="layer-card-primary"
|
|
37
|
+
className={cn(
|
|
38
|
+
'relative z-10 flex min-w-0 flex-col gap-2 rounded-(--layer-card-radius) border border-stroke-secondary bg-surface-default p-(--layer-card-padding) shadow-(--layer-card-shadow)',
|
|
39
|
+
className,
|
|
40
|
+
)}
|
|
41
|
+
{...props}
|
|
42
|
+
/>
|
|
43
|
+
)
|
|
44
|
+
},
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
export const LayerCardSecondary = forwardRef<HTMLDivElement, LayerCardSectionProps>(
|
|
48
|
+
function LayerCardSecondary({ className, ...props }, ref) {
|
|
49
|
+
return (
|
|
50
|
+
<div
|
|
51
|
+
ref={ref}
|
|
52
|
+
data-slot="layer-card-secondary"
|
|
53
|
+
className={cn(
|
|
54
|
+
'flex min-w-0 items-center gap-2 px-(--layer-card-padding) py-3 text-sm font-medium text-content-secondary',
|
|
55
|
+
className,
|
|
56
|
+
)}
|
|
57
|
+
{...props}
|
|
58
|
+
/>
|
|
59
|
+
)
|
|
60
|
+
},
|
|
61
|
+
)
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { mergeProps } from '@base-ui/react/merge-props'
|
|
2
|
+
import { useRender } from '@base-ui/react/use-render'
|
|
3
|
+
import {
|
|
4
|
+
forwardRef,
|
|
5
|
+
type ComponentPropsWithoutRef,
|
|
6
|
+
type ReactNode,
|
|
7
|
+
type MouseEventHandler,
|
|
8
|
+
} from 'react'
|
|
9
|
+
import { cn, FOCUS_RING } from '../lib/utils'
|
|
10
|
+
|
|
11
|
+
export const TableOfContentsRoot = forwardRef<HTMLElement, ComponentPropsWithoutRef<'nav'>>(
|
|
12
|
+
function TableOfContentsRoot(
|
|
13
|
+
{ className, 'aria-label': label = 'Table of contents', ...props },
|
|
14
|
+
ref,
|
|
15
|
+
) {
|
|
16
|
+
return (
|
|
17
|
+
<nav
|
|
18
|
+
ref={ref}
|
|
19
|
+
data-slot="table-of-contents"
|
|
20
|
+
aria-label={label}
|
|
21
|
+
className={cn('min-w-0', className)}
|
|
22
|
+
{...props}
|
|
23
|
+
/>
|
|
24
|
+
)
|
|
25
|
+
},
|
|
26
|
+
)
|
|
27
|
+
export const TableOfContentsTitle = forwardRef<HTMLParagraphElement, ComponentPropsWithoutRef<'p'>>(
|
|
28
|
+
function TableOfContentsTitle({ className, ...props }, ref) {
|
|
29
|
+
return (
|
|
30
|
+
<p
|
|
31
|
+
ref={ref}
|
|
32
|
+
data-slot="table-of-contents-title"
|
|
33
|
+
className={cn(
|
|
34
|
+
'mb-3 text-xs font-semibold tracking-wide text-content-secondary uppercase',
|
|
35
|
+
className,
|
|
36
|
+
)}
|
|
37
|
+
{...props}
|
|
38
|
+
/>
|
|
39
|
+
)
|
|
40
|
+
},
|
|
41
|
+
)
|
|
42
|
+
export const TableOfContentsList = forwardRef<HTMLUListElement, ComponentPropsWithoutRef<'ul'>>(
|
|
43
|
+
function TableOfContentsList({ className, ...props }, ref) {
|
|
44
|
+
return (
|
|
45
|
+
<ul
|
|
46
|
+
ref={ref}
|
|
47
|
+
data-slot="table-of-contents-list"
|
|
48
|
+
className={cn('flex list-none flex-col gap-2 border-s-2 border-stroke p-0', className)}
|
|
49
|
+
{...props}
|
|
50
|
+
/>
|
|
51
|
+
)
|
|
52
|
+
},
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
export type TableOfContentsItemProps = useRender.ComponentProps<'a'> & { active?: boolean }
|
|
56
|
+
const TableOfContentsLink = forwardRef<HTMLAnchorElement, TableOfContentsItemProps>(
|
|
57
|
+
function TableOfContentsLink({ active = false, render, className, ...props }, ref) {
|
|
58
|
+
const defaultProps = {
|
|
59
|
+
'data-slot': 'table-of-contents-link',
|
|
60
|
+
'data-active': active,
|
|
61
|
+
'aria-current': active ? ('location' as const) : undefined,
|
|
62
|
+
className: cn(
|
|
63
|
+
'block w-full truncate border-s-2 border-transparent py-0.5 ps-4 text-start text-sm leading-5 no-underline outline-none',
|
|
64
|
+
FOCUS_RING,
|
|
65
|
+
active
|
|
66
|
+
? 'border-primary font-medium text-default'
|
|
67
|
+
: 'text-content-secondary hover:border-stroke-secondary hover:text-default',
|
|
68
|
+
className,
|
|
69
|
+
),
|
|
70
|
+
}
|
|
71
|
+
return useRender({
|
|
72
|
+
defaultTagName: 'a',
|
|
73
|
+
render,
|
|
74
|
+
ref,
|
|
75
|
+
props: mergeProps<'a'>(defaultProps, props),
|
|
76
|
+
})
|
|
77
|
+
},
|
|
78
|
+
)
|
|
79
|
+
export const TableOfContentsItem = forwardRef<HTMLAnchorElement, TableOfContentsItemProps>(
|
|
80
|
+
function TableOfContentsItem(props, ref) {
|
|
81
|
+
return (
|
|
82
|
+
<li data-slot="table-of-contents-item" className="-ms-0.5 min-w-0">
|
|
83
|
+
<TableOfContentsLink ref={ref} {...props} />
|
|
84
|
+
</li>
|
|
85
|
+
)
|
|
86
|
+
},
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
export interface TableOfContentsGroupProps extends Omit<
|
|
90
|
+
ComponentPropsWithoutRef<'li'>,
|
|
91
|
+
'title' | 'onClick'
|
|
92
|
+
> {
|
|
93
|
+
label: ReactNode
|
|
94
|
+
href?: string
|
|
95
|
+
active?: boolean
|
|
96
|
+
/** Called only for the group label link, never for nested item clicks. */
|
|
97
|
+
onLabelClick?: MouseEventHandler<HTMLAnchorElement>
|
|
98
|
+
}
|
|
99
|
+
export const TableOfContentsGroup = forwardRef<HTMLLIElement, TableOfContentsGroupProps>(
|
|
100
|
+
function TableOfContentsGroup(
|
|
101
|
+
{ label, href, active, onLabelClick, className, children, ...props },
|
|
102
|
+
ref,
|
|
103
|
+
) {
|
|
104
|
+
return (
|
|
105
|
+
<li
|
|
106
|
+
ref={ref}
|
|
107
|
+
data-slot="table-of-contents-group"
|
|
108
|
+
className={cn('-ms-0.5 flex min-w-0 flex-col gap-2', className)}
|
|
109
|
+
{...props}
|
|
110
|
+
>
|
|
111
|
+
{href ? (
|
|
112
|
+
<TableOfContentsLink href={href} active={active} onClick={onLabelClick}>
|
|
113
|
+
{label}
|
|
114
|
+
</TableOfContentsLink>
|
|
115
|
+
) : (
|
|
116
|
+
<p className="py-0.5 ps-4 text-sm font-medium text-content-secondary">{label}</p>
|
|
117
|
+
)}
|
|
118
|
+
<ul className="flex list-none flex-col gap-2 border-s-2 border-stroke p-0 [&_a]:ps-7">
|
|
119
|
+
{children}
|
|
120
|
+
</ul>
|
|
121
|
+
</li>
|
|
122
|
+
)
|
|
123
|
+
},
|
|
124
|
+
)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
export interface TableOfContentsTrackingOptions {
|
|
4
|
+
/** Space reserved for a sticky header, in pixels. @default 96 */
|
|
5
|
+
offset?: number
|
|
6
|
+
/** A scroll container; omitted for document scrolling. */
|
|
7
|
+
root?: HTMLElement | null
|
|
8
|
+
enabled?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Tracks supplied section IDs without scanning or changing document headings. */
|
|
12
|
+
export function useTableOfContentsActiveId(
|
|
13
|
+
ids: readonly string[],
|
|
14
|
+
{ offset = 96, root, enabled = true }: TableOfContentsTrackingOptions = {},
|
|
15
|
+
) {
|
|
16
|
+
const [activeId, setActiveId] = useState<string>()
|
|
17
|
+
const idsKey = JSON.stringify(ids)
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
if (!enabled) return
|
|
20
|
+
const sectionIds = JSON.parse(idsKey) as string[]
|
|
21
|
+
let frame = 0
|
|
22
|
+
const measure = () => {
|
|
23
|
+
const sections = sectionIds
|
|
24
|
+
.map((id) => document.getElementById(id))
|
|
25
|
+
.filter((el): el is HTMLElement => el !== null && (!root || root.contains(el)))
|
|
26
|
+
const top = (root?.getBoundingClientRect().top ?? 0) + offset
|
|
27
|
+
const ordered = sections
|
|
28
|
+
.map((el) => ({ id: el.id, top: el.getBoundingClientRect().top }))
|
|
29
|
+
.sort((a, b) => a.top - b.top)
|
|
30
|
+
let next: string | undefined = ordered[0]?.id
|
|
31
|
+
for (const section of ordered) if (section.top <= top + 1) next = section.id
|
|
32
|
+
const scroller = root ?? document.scrollingElement
|
|
33
|
+
if (
|
|
34
|
+
scroller &&
|
|
35
|
+
scroller.scrollHeight > scroller.clientHeight &&
|
|
36
|
+
scroller.scrollTop + scroller.clientHeight >= scroller.scrollHeight - 1
|
|
37
|
+
)
|
|
38
|
+
next = ordered.at(-1)?.id
|
|
39
|
+
setActiveId(next)
|
|
40
|
+
}
|
|
41
|
+
const schedule = () => {
|
|
42
|
+
cancelAnimationFrame(frame)
|
|
43
|
+
frame = requestAnimationFrame(measure)
|
|
44
|
+
}
|
|
45
|
+
const target = root ?? window
|
|
46
|
+
target.addEventListener('scroll', schedule, { passive: true })
|
|
47
|
+
window.addEventListener('resize', schedule)
|
|
48
|
+
const observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(schedule)
|
|
49
|
+
for (const id of sectionIds) {
|
|
50
|
+
const el = document.getElementById(id)
|
|
51
|
+
if (el) observer?.observe(el)
|
|
52
|
+
}
|
|
53
|
+
if (root) observer?.observe(root)
|
|
54
|
+
measure()
|
|
55
|
+
return () => {
|
|
56
|
+
cancelAnimationFrame(frame)
|
|
57
|
+
observer?.disconnect()
|
|
58
|
+
target.removeEventListener('scroll', schedule)
|
|
59
|
+
window.removeEventListener('resize', schedule)
|
|
60
|
+
}
|
|
61
|
+
}, [idsKey, offset, root, enabled])
|
|
62
|
+
return enabled ? activeId : undefined
|
|
63
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -523,3 +523,53 @@ export { SelectScrollDownButton, SelectScrollUpButton, SelectSeparator } from '.
|
|
|
523
523
|
export { spacerVariants } from './opinionated/spacer'
|
|
524
524
|
export { stackVariants } from './opinionated/stack'
|
|
525
525
|
export { typographyVariants } from './opinionated/typography'
|
|
526
|
+
|
|
527
|
+
// Layered surfaces, document navigation, workflow diagrams, and copy fields.
|
|
528
|
+
export {
|
|
529
|
+
LayerCard,
|
|
530
|
+
LayerCardRoot,
|
|
531
|
+
LayerCardPrimary,
|
|
532
|
+
LayerCardSecondary,
|
|
533
|
+
type LayerCardProps,
|
|
534
|
+
type LayerCardRootProps,
|
|
535
|
+
type LayerCardSectionProps,
|
|
536
|
+
} from './opinionated/layer-card'
|
|
537
|
+
export {
|
|
538
|
+
TableOfContents,
|
|
539
|
+
TableOfContentsRoot,
|
|
540
|
+
TableOfContentsTitle,
|
|
541
|
+
TableOfContentsList,
|
|
542
|
+
TableOfContentsItem,
|
|
543
|
+
TableOfContentsGroup,
|
|
544
|
+
type TableOfContentsProps,
|
|
545
|
+
type TableOfContentsItemData,
|
|
546
|
+
type TableOfContentsItemProps,
|
|
547
|
+
type TableOfContentsGroupProps,
|
|
548
|
+
} from './opinionated/table-of-contents'
|
|
549
|
+
export {
|
|
550
|
+
useTableOfContentsActiveId,
|
|
551
|
+
type TableOfContentsTrackingOptions,
|
|
552
|
+
} from './hooks/use-table-of-contents-active-id'
|
|
553
|
+
export {
|
|
554
|
+
ClipboardText,
|
|
555
|
+
type ClipboardTextProps,
|
|
556
|
+
type ClipboardTextSize,
|
|
557
|
+
type ClipboardTextStatus,
|
|
558
|
+
} from './opinionated/clipboard-text'
|
|
559
|
+
export {
|
|
560
|
+
Flow,
|
|
561
|
+
FlowRoot,
|
|
562
|
+
FlowNode,
|
|
563
|
+
FlowParallel,
|
|
564
|
+
FlowList,
|
|
565
|
+
FlowAnchor,
|
|
566
|
+
type FlowProps,
|
|
567
|
+
type FlowItem,
|
|
568
|
+
type FlowStep,
|
|
569
|
+
type FlowBranch,
|
|
570
|
+
type FlowRootProps,
|
|
571
|
+
type FlowNodeProps,
|
|
572
|
+
type FlowAnchorProps,
|
|
573
|
+
type FlowAlign,
|
|
574
|
+
type FlowOrientation,
|
|
575
|
+
} from './opinionated/flow'
|