@cognitap/radiant 0.0.66 → 0.0.68

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.
Files changed (45) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/library/AppShell.d.ts +71 -0
  3. package/dist/library/AppShell.d.ts.map +1 -0
  4. package/dist/library/AppShell.js +189 -0
  5. package/dist/library/AppShell.js.map +1 -0
  6. package/dist/library/Form.d.ts +41 -0
  7. package/dist/library/Form.d.ts.map +1 -0
  8. package/dist/library/Form.js +171 -0
  9. package/dist/library/Form.js.map +1 -0
  10. package/dist/library/cn.d.ts.map +1 -1
  11. package/dist/library/cn.js +11 -0
  12. package/dist/library/cn.js.map +1 -1
  13. package/dist/library/index.d.ts +3 -0
  14. package/dist/library/index.d.ts.map +1 -1
  15. package/dist/library/index.js +2 -0
  16. package/dist/library/index.js.map +1 -1
  17. package/dist/library/motion/host.d.ts +2 -1
  18. package/dist/library/motion/host.d.ts.map +1 -1
  19. package/dist/library/motion/host.js.map +1 -1
  20. package/dist/library/motion/layoutHost.d.ts +5 -0
  21. package/dist/library/motion/layoutHost.d.ts.map +1 -0
  22. package/dist/library/motion/layoutHost.js +24 -0
  23. package/dist/library/motion/layoutHost.js.map +1 -0
  24. package/dist/library/motion/press.d.ts.map +1 -1
  25. package/dist/library/motion/press.js +6 -5
  26. package/dist/library/motion/press.js.map +1 -1
  27. package/dist/library/motion/react.d.ts.map +1 -1
  28. package/dist/library/motion/react.js +3 -1
  29. package/dist/library/motion/react.js.map +1 -1
  30. package/dist/library/styles.css +1 -1
  31. package/dist/library/themes/components.d.ts +25 -0
  32. package/dist/library/themes/components.d.ts.map +1 -1
  33. package/dist/library/themes/components.js +23 -0
  34. package/dist/library/themes/components.js.map +1 -1
  35. package/docs/architecture.md +15 -11
  36. package/package.json +1 -1
  37. package/src/AppShell.tsx +517 -0
  38. package/src/Form.tsx +260 -0
  39. package/src/cn.ts +11 -0
  40. package/src/index.ts +39 -0
  41. package/src/motion/host.ts +2 -1
  42. package/src/motion/layoutHost.ts +25 -0
  43. package/src/motion/press.ts +6 -5
  44. package/src/motion/react.ts +3 -1
  45. package/src/themes/components.ts +23 -0
@@ -0,0 +1,517 @@
1
+ 'use client'
2
+
3
+ import {
4
+ createContext,
5
+ useContext,
6
+ useEffect,
7
+ useId,
8
+ useLayoutEffect,
9
+ useRef,
10
+ useState,
11
+ type ComponentProps,
12
+ type ReactNode,
13
+ type RefObject
14
+ } from 'react'
15
+ import { createPortal } from 'react-dom'
16
+ import { Button, type ButtonProps, type ButtonRenderProps } from './Button.js'
17
+ import {
18
+ ComposedLink,
19
+ type ComposedLinkProps,
20
+ type ComposedLinkRenderProps
21
+ } from './composedLink.js'
22
+ import { Drawer, DrawerContent, DrawerTitle, DrawerClose, useDrawer } from './Drawer.js'
23
+ import { cn } from './cn.js'
24
+ import { joinIds } from './internal/ids.js'
25
+ import { composeAction } from './internal/events.js'
26
+ import type { WithoutToggle, DistributiveOmit } from './internal/types.js'
27
+ import { useControllableState } from './react/useControllableState.js'
28
+ import { useComposedRefs } from './react/useComposedRefs.js'
29
+ import type { StateOwner } from './react/useNativeOwner.js'
30
+ import { useTransitionHost } from './motion/host.js'
31
+ import type { PartStyle } from './presentation.js'
32
+ import type { ThemeStyle } from './themes/defineTheme.js'
33
+
34
+ export type AppShellProps = Omit<ComponentProps<'div'>, 'style'> &
35
+ StateOwner<'collapsed', boolean> &
36
+ StateOwner<'open', boolean> & {
37
+ style?: ThemeStyle
38
+ 'data-slot'?: string
39
+ onCollapsedChange?: (collapsed: boolean) => void
40
+ onOpenChange?: (open: boolean) => void
41
+ mainId?: string
42
+ sidebarId?: string
43
+ motion?: 'inherit' | 'off'
44
+ }
45
+ export type AppShellState = {
46
+ collapsed: boolean
47
+ open: boolean
48
+ mobile: boolean
49
+ collapsedControlled: boolean
50
+ openControlled: boolean
51
+ mainId: string
52
+ sidebarId: string
53
+ setCollapsed: (collapsed: boolean) => void
54
+ setOpen: (open: boolean) => void
55
+ }
56
+ type Binding = AppShellState & {
57
+ root: RefObject<HTMLDivElement | null>
58
+ drawerId: string
59
+ labelId: string
60
+ motion: 'inherit' | 'off'
61
+ }
62
+ const Context = createContext<Binding | null>(null)
63
+ export function useAppShell(): AppShellState | null {
64
+ return useContext(Context)
65
+ }
66
+ export function useSidebar(): AppShellState | null {
67
+ return useContext(Context)
68
+ }
69
+ function useBinding() {
70
+ const value = useContext(Context)
71
+ if (!value) throw new Error('AppShell and Sidebar parts must render inside AppShell.')
72
+ return value
73
+ }
74
+ function Provider({
75
+ binding,
76
+ children
77
+ }: {
78
+ binding: Omit<Binding, 'drawerId'>
79
+ children: ReactNode
80
+ }) {
81
+ const drawer = useDrawer()
82
+ if (!drawer) throw new Error('AppShell requires its Drawer.')
83
+ return (
84
+ <Context.Provider value={{ ...binding, drawerId: drawer.contentId }}>
85
+ {children}
86
+ </Context.Provider>
87
+ )
88
+ }
89
+ function toggleFocus(root: HTMLElement | null) {
90
+ ;(
91
+ root?.querySelector<HTMLElement>('[data-slot~="sidebar-trigger"]:not(:disabled)') ??
92
+ root?.querySelector<HTMLElement>('[data-slot~="app-shell-main"]')
93
+ )?.focus({ preventScroll: true })
94
+ }
95
+ export function AppShell({
96
+ collapsed,
97
+ defaultCollapsed,
98
+ onCollapsedChange,
99
+ open,
100
+ defaultOpen,
101
+ onOpenChange,
102
+ mainId,
103
+ sidebarId,
104
+ motion = 'inherit',
105
+ ref,
106
+ className,
107
+ onFocusCapture,
108
+ ...props
109
+ }: AppShellProps) {
110
+ const collapse = useControllableState({
111
+ name: 'AppShell collapsed',
112
+ value: collapsed,
113
+ defaultValue: defaultCollapsed,
114
+ initialValue: false,
115
+ onChange: onCollapsedChange
116
+ }),
117
+ drawer = useControllableState({
118
+ name: 'AppShell open',
119
+ value: open,
120
+ defaultValue: defaultOpen,
121
+ initialValue: false,
122
+ onChange: onOpenChange
123
+ })
124
+ if (typeof collapse.value !== 'boolean' || typeof drawer.value !== 'boolean')
125
+ throw new TypeError('AppShell states must be boolean.')
126
+ for (const id of [mainId, sidebarId])
127
+ if (id !== undefined && (!id || /\s/.test(id)))
128
+ throw new TypeError('AppShell relationship IDs must be nonempty and contain no whitespace.')
129
+ const root = useRef<HTMLDivElement>(null),
130
+ host = useComposedRefs(root, ref),
131
+ { ref: layoutRef } = useTransitionHost('layout', host, motion === 'off'),
132
+ [mobile, setMobile] = useState(false),
133
+ id = useId(),
134
+ latestOpen = useRef(drawer.request),
135
+ focused = useRef<HTMLElement | null>(null),
136
+ previous = useRef(collapse.value)
137
+ useLayoutEffect(() => {
138
+ latestOpen.current = drawer.request
139
+ })
140
+ useEffect(() => {
141
+ const node = root.current
142
+ if (!node) return
143
+ const query = node.ownerDocument.defaultView!.matchMedia('(width < 40rem)')
144
+ setMobile(query.matches)
145
+ const change = () => {
146
+ setMobile(query.matches)
147
+ if (!query.matches) latestOpen.current(false)
148
+ }
149
+ query.addEventListener('change', change)
150
+ return () => query.removeEventListener('change', change)
151
+ }, [])
152
+ useLayoutEffect(() => {
153
+ const node = root.current,
154
+ active = focused.current
155
+ if (
156
+ node &&
157
+ !mobile &&
158
+ collapse.value &&
159
+ !previous.current &&
160
+ active?.closest('[data-slot~="sidebar-content"][data-expanded-only]') &&
161
+ (node.ownerDocument.activeElement === node.ownerDocument.body ||
162
+ node.ownerDocument.activeElement === active)
163
+ )
164
+ toggleFocus(node)
165
+ previous.current = collapse.value
166
+ })
167
+ const setCollapsed = (next: boolean) => {
168
+ if (typeof next !== 'boolean')
169
+ throw new TypeError('AppShell collapsed requests must be boolean.')
170
+ collapse.request(next)
171
+ }
172
+ const setOpen = (next: boolean) => {
173
+ if (typeof next !== 'boolean') throw new TypeError('AppShell open requests must be boolean.')
174
+ drawer.request(next)
175
+ }
176
+ return (
177
+ <Drawer
178
+ open={mobile && drawer.value}
179
+ onOpenChange={setOpen}
180
+ snapPoints={[1]}
181
+ >
182
+ <Provider
183
+ binding={{
184
+ collapsed: collapse.value,
185
+ open: drawer.value,
186
+ mobile,
187
+ collapsedControlled: collapse.controlled,
188
+ openControlled: drawer.controlled,
189
+ setCollapsed,
190
+ setOpen,
191
+ mainId: mainId ?? `${id}-main`,
192
+ sidebarId: sidebarId ?? `${id}-sidebar`,
193
+ labelId: `${id}-sidebar-label`,
194
+ root,
195
+ motion
196
+ }}
197
+ >
198
+ <div
199
+ {...props}
200
+ ref={layoutRef}
201
+ data-slot={joinIds('app-shell', props['data-slot'])}
202
+ data-collapsed={collapse.value || undefined}
203
+ data-mobile={mobile || undefined}
204
+ data-state={
205
+ mobile ? (drawer.value ? 'open' : 'closed') : collapse.value ? 'closed' : 'open'
206
+ }
207
+ data-motion={motion}
208
+ className={cn(
209
+ 'relative grid h-dvh min-h-0 min-w-0 grid-cols-[var(--_rad-shell-sidebar)_minmax(0,1fr)] grid-rows-[auto_minmax(0,1fr)] [--_rad-shell-sidebar:var(--rad-sidebar-width,16rem)] data-collapsed:[--_rad-shell-sidebar:var(--rad-sidebar-collapsed-width,5rem)] max-[40rem]:grid-cols-[0_minmax(0,1fr)] bg-[var(--rad-app-shell-background,var(--rad-color-surface,white))] text-[var(--rad-app-shell-color,var(--rad-color-foreground,black))]',
210
+ className
211
+ )}
212
+ onFocusCapture={event => {
213
+ onFocusCapture?.(event)
214
+ focused.current = event.target
215
+ }}
216
+ >
217
+ {props.children}
218
+ </div>
219
+ </Provider>
220
+ </Drawer>
221
+ )
222
+ }
223
+ export type AppShellHeaderProps = Omit<ComponentProps<'header'>, 'style' | 'role'> & {
224
+ style?: ThemeStyle
225
+ 'data-slot'?: string
226
+ }
227
+ export function AppShellHeader({ className, ...props }: AppShellHeaderProps) {
228
+ useBinding()
229
+ return (
230
+ <div
231
+ data-rad-layout-slot=""
232
+ className="col-span-2 row-start-1 min-w-0"
233
+ >
234
+ <div
235
+ data-rad-layout-part=""
236
+ className="flow-root size-full"
237
+ >
238
+ <header
239
+ {...props}
240
+ role="banner"
241
+ data-slot={joinIds('app-shell-header', props['data-slot'])}
242
+ className={cn(
243
+ 'box-border flex min-w-0 items-center justify-between gap-4 min-h-[var(--rad-app-shell-header-min-height,3.5rem)] p-[var(--rad-app-shell-header-padding,1rem)] border-b-[length:var(--rad-app-shell-header-border-width,var(--rad-surface-border-width,1px))] [border-style:var(--rad-surface-border-style,solid)] border-[var(--rad-app-shell-header-border-color,var(--rad-color-border))] bg-[var(--rad-app-shell-header-background,var(--rad-color-surface))] text-[var(--rad-app-shell-header-color,var(--rad-color-foreground))]',
244
+ className
245
+ )}
246
+ />
247
+ </div>
248
+ </div>
249
+ )
250
+ }
251
+ export type AppShellMainProps = Omit<
252
+ ComponentProps<'main'>,
253
+ 'style' | 'role' | 'id' | 'tabIndex'
254
+ > & { style?: ThemeStyle; 'data-slot'?: string }
255
+ export function AppShellMain({ className, ...props }: AppShellMainProps) {
256
+ const shell = useBinding()
257
+ return (
258
+ <div
259
+ data-rad-layout-slot=""
260
+ className="col-start-2 row-start-2 min-h-0 min-w-0"
261
+ >
262
+ <div
263
+ data-rad-layout-part=""
264
+ className="flow-root size-full"
265
+ >
266
+ <main
267
+ {...props}
268
+ id={shell.mainId}
269
+ role="main"
270
+ tabIndex={-1}
271
+ data-slot={joinIds('app-shell-main', props['data-slot'])}
272
+ className={cn(
273
+ 'box-border size-full min-h-0 min-w-0 overflow-auto p-[var(--rad-app-shell-main-padding,var(--rad-surface-padding,1.5rem))] focus-visible:[outline-style:var(--rad-focus-style,solid)] focus-visible:outline-[color:var(--rad-color-focus)] focus-visible:outline-[length:var(--rad-focus-width,2px)] focus-visible:outline-offset-[-2px]',
274
+ className
275
+ )}
276
+ />
277
+ </div>
278
+ </div>
279
+ )
280
+ }
281
+ export type AppShellRenderProps = ComposedLinkRenderProps
282
+ export type AppShellSkipLinkProps = Omit<ComposedLinkProps, 'href'>
283
+ export function AppShellSkipLink({ className, ...props }: AppShellSkipLinkProps) {
284
+ const shell = useBinding()
285
+ return (
286
+ <ComposedLink
287
+ {...props}
288
+ identity="app-shell-skip-link"
289
+ href={`#${shell.mainId}`}
290
+ className={cn(
291
+ 'sr-only focus:not-sr-only focus:absolute focus:start-2 focus:top-2 focus:z-50 focus:rounded focus:bg-[var(--rad-color-surface)] focus:p-3',
292
+ className
293
+ )}
294
+ />
295
+ )
296
+ }
297
+ export type SidebarProps = Omit<
298
+ ComponentProps<'nav'>,
299
+ 'style' | 'role' | 'id' | 'aria-label' | 'aria-labelledby' | 'tabIndex'
300
+ > & {
301
+ style?: ThemeStyle
302
+ 'data-slot'?: string
303
+ label: ReactNode
304
+ closeLabel?: string
305
+ parts?: { label?: PartStyle; drawer?: PartStyle; viewport?: PartStyle; close?: PartStyle }
306
+ }
307
+ /* oxlint-disable jsx-a11y/no-noninteractive-tabindex -- The named native navigation viewport accepts focus for keyboard scrolling. */
308
+ export function Sidebar({
309
+ label,
310
+ closeLabel = 'Close navigation',
311
+ parts,
312
+ ref,
313
+ className,
314
+ children,
315
+ ...props
316
+ }: SidebarProps) {
317
+ const shell = useBinding(),
318
+ desktop = useRef<HTMLDivElement>(null),
319
+ dock = useRef<HTMLDivElement>(null),
320
+ [container, setContainer] = useState<HTMLDivElement | null>(null),
321
+ collapsed = shell.collapsed && !shell.mobile
322
+ if (
323
+ label === null ||
324
+ label === undefined ||
325
+ typeof label === 'boolean' ||
326
+ (typeof label === 'string' && !label.trim()) ||
327
+ !closeLabel.trim()
328
+ )
329
+ throw new TypeError('Sidebar requires meaningful navigation and close labels.')
330
+ useLayoutEffect(() => {
331
+ const slot = desktop.current
332
+ if (!slot) return
333
+ const node = slot.ownerDocument.createElement('div')
334
+ node.setAttribute('data-rad-layout-part', '')
335
+ node.className = 'flow-root size-full min-w-0'
336
+ setContainer(node)
337
+ return () => node.remove()
338
+ }, [])
339
+ useLayoutEffect(() => {
340
+ if (!container) return
341
+ const target = shell.mobile ? dock.current : desktop.current
342
+ if (!target) return
343
+ const doc = target.ownerDocument,
344
+ view = doc.defaultView!,
345
+ active = doc.activeElement,
346
+ held =
347
+ active instanceof doc.defaultView!.HTMLElement && container.contains(active) ? active : null
348
+ if (container.parentElement === target) return
349
+ target.append(container)
350
+ if (held && shell.mobile && !shell.open)
351
+ toggleFocus(desktop.current?.closest('[data-slot~="app-shell"]') ?? null)
352
+ let frame = 0
353
+ if (held && (!shell.mobile || shell.open))
354
+ frame = view.requestAnimationFrame(() => {
355
+ if (
356
+ doc.activeElement === doc.body ||
357
+ (doc.activeElement?.closest('[data-slot~="sidebar-trigger"]') &&
358
+ desktop.current?.closest('[data-slot~="app-shell"]')?.contains(doc.activeElement)) ||
359
+ dock.current?.closest('dialog')?.contains(doc.activeElement)
360
+ )
361
+ held.focus({ preventScroll: true })
362
+ })
363
+ return () => view.cancelAnimationFrame(frame)
364
+ }, [container, shell.mobile, shell.open])
365
+ const navigation = (
366
+ <nav
367
+ {...props}
368
+ ref={ref}
369
+ role="navigation"
370
+ id={shell.sidebarId}
371
+ aria-labelledby={shell.labelId}
372
+ tabIndex={0}
373
+ data-slot={joinIds('sidebar', props['data-slot'])}
374
+ data-collapsed={collapsed || undefined}
375
+ data-state={shell.mobile ? (shell.open ? 'open' : 'closed') : collapsed ? 'closed' : 'open'}
376
+ className={cn(
377
+ 'box-border flex size-full min-h-0 min-w-0 flex-col overflow-auto gap-[var(--rad-sidebar-gap,1rem)] p-[var(--rad-sidebar-padding,0.5rem)] border-e-[length:var(--rad-sidebar-border-width,var(--rad-surface-border-width,1px))] [border-style:var(--rad-surface-border-style,solid)] border-[var(--rad-sidebar-border-color,var(--rad-color-border))] bg-[var(--rad-sidebar-background,var(--rad-color-surface))] text-[var(--rad-sidebar-color,var(--rad-color-foreground))]',
378
+ className
379
+ )}
380
+ >
381
+ <h2
382
+ id={shell.labelId}
383
+ data-slot="sidebar-label"
384
+ className={cn(
385
+ 'm-0 text-[length:var(--rad-sidebar-label-size,1rem)] font-semibold',
386
+ collapsed || shell.mobile ? 'sr-only' : undefined,
387
+ parts?.label?.className
388
+ )}
389
+ style={parts?.label?.style}
390
+ >
391
+ {label}
392
+ </h2>
393
+ {children}
394
+ </nav>
395
+ )
396
+ return (
397
+ <>
398
+ <div
399
+ ref={desktop}
400
+ data-slot="sidebar-slot"
401
+ data-rad-layout-slot={shell.mobile ? undefined : ''}
402
+ className="col-start-1 row-start-2 min-h-0 min-w-0 max-[40rem]:hidden"
403
+ >
404
+ {!container && (
405
+ <div
406
+ data-rad-layout-part=""
407
+ className="flow-root size-full"
408
+ >
409
+ {navigation}
410
+ </div>
411
+ )}
412
+ </div>
413
+ <DrawerContent
414
+ side="start"
415
+ motion={shell.motion}
416
+ className={cn(
417
+ 'open:w-[min(var(--rad-sidebar-width,16rem),90dvw)]',
418
+ parts?.drawer?.className
419
+ )}
420
+ style={parts?.drawer?.style}
421
+ parts={{
422
+ viewport: {
423
+ className: cn('flex flex-col gap-0 p-0', parts?.viewport?.className),
424
+ style: parts?.viewport?.style
425
+ }
426
+ }}
427
+ >
428
+ <div className="flex items-center justify-between gap-3 p-3">
429
+ <DrawerTitle className="m-0 text-base">{label}</DrawerTitle>
430
+ <DrawerClose
431
+ variant="ghost"
432
+ tone="neutral"
433
+ controlSize="small"
434
+ className={parts?.close?.className}
435
+ style={parts?.close?.style}
436
+ >
437
+ {closeLabel}
438
+ </DrawerClose>
439
+ </div>
440
+ <div
441
+ ref={dock}
442
+ className="min-h-0 flex-1"
443
+ />
444
+ </DrawerContent>
445
+ {container && createPortal(navigation, container)}
446
+ </>
447
+ )
448
+ }
449
+ /* oxlint-enable jsx-a11y/no-noninteractive-tabindex */
450
+ export type SidebarRenderProps = ButtonRenderProps
451
+ export type SidebarTriggerProps = WithoutToggle<
452
+ DistributiveOmit<
453
+ ButtonProps,
454
+ 'aria-expanded' | 'aria-controls' | 'aria-haspopup' | 'popoverTarget' | 'popoverTargetAction'
455
+ >
456
+ >
457
+ export function SidebarTrigger({
458
+ onClick,
459
+ variant = 'ghost',
460
+ tone = 'neutral',
461
+ ...props
462
+ }: SidebarTriggerProps) {
463
+ const shell = useBinding()
464
+ return (
465
+ <Button
466
+ {...props}
467
+ variant={variant}
468
+ tone={tone}
469
+ pressed={undefined}
470
+ defaultPressed={undefined}
471
+ onPressedChange={undefined}
472
+ aria-pressed={undefined}
473
+ popoverTarget={undefined}
474
+ popoverTargetAction={undefined}
475
+ aria-expanded={shell.mobile ? shell.open : !shell.collapsed}
476
+ aria-controls={shell.mobile ? shell.drawerId : shell.sidebarId}
477
+ aria-haspopup={shell.mobile ? 'dialog' : undefined}
478
+ data-slot={joinIds('sidebar-trigger', props['data-slot'])}
479
+ onClick={composeAction(onClick, () =>
480
+ shell.mobile ? shell.setOpen(!shell.open) : shell.setCollapsed(!shell.collapsed)
481
+ )}
482
+ />
483
+ )
484
+ }
485
+ export type SidebarLabelProps = Omit<ComponentProps<'span'>, 'style'> & {
486
+ style?: ThemeStyle
487
+ 'data-slot'?: string
488
+ }
489
+ export function SidebarLabel({ className, ...props }: SidebarLabelProps) {
490
+ const shell = useBinding()
491
+ return (
492
+ <span
493
+ {...props}
494
+ data-slot={joinIds('sidebar-item-label', props['data-slot'])}
495
+ className={cn(shell.collapsed && !shell.mobile ? 'sr-only' : undefined, className)}
496
+ />
497
+ )
498
+ }
499
+ export type SidebarContentProps = Omit<ComponentProps<'div'>, 'style' | 'hidden' | 'inert'> & {
500
+ style?: ThemeStyle
501
+ 'data-slot'?: string
502
+ expandedOnly?: boolean
503
+ }
504
+ export function SidebarContent({ expandedOnly = false, className, ...props }: SidebarContentProps) {
505
+ const shell = useBinding(),
506
+ hidden = expandedOnly && shell.collapsed && !shell.mobile
507
+ return (
508
+ <div
509
+ {...props}
510
+ hidden={hidden}
511
+ inert={hidden}
512
+ data-slot={joinIds('sidebar-content', props['data-slot'])}
513
+ data-expanded-only={expandedOnly || undefined}
514
+ className={cn('min-w-0 [hidden]:hidden', className)}
515
+ />
516
+ )
517
+ }