@cognitap/radiant 0.0.66 → 0.0.67
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 +5 -0
- package/dist/library/AppShell.d.ts +71 -0
- package/dist/library/AppShell.d.ts.map +1 -0
- package/dist/library/AppShell.js +189 -0
- package/dist/library/AppShell.js.map +1 -0
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +10 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +2 -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/motion/host.d.ts +2 -1
- package/dist/library/motion/host.d.ts.map +1 -1
- package/dist/library/motion/host.js.map +1 -1
- package/dist/library/motion/layoutHost.d.ts +5 -0
- package/dist/library/motion/layoutHost.d.ts.map +1 -0
- package/dist/library/motion/layoutHost.js +24 -0
- package/dist/library/motion/layoutHost.js.map +1 -0
- package/dist/library/motion/press.d.ts.map +1 -1
- package/dist/library/motion/press.js +6 -5
- package/dist/library/motion/press.js.map +1 -1
- package/dist/library/motion/react.d.ts.map +1 -1
- package/dist/library/motion/react.js +3 -1
- package/dist/library/motion/react.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +22 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +22 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +9 -6
- package/package.json +1 -1
- package/src/AppShell.tsx +517 -0
- package/src/cn.ts +10 -0
- package/src/index.ts +26 -0
- package/src/motion/host.ts +2 -1
- package/src/motion/layoutHost.ts +25 -0
- package/src/motion/press.ts +6 -5
- package/src/motion/react.ts +3 -1
- package/src/themes/components.ts +22 -0
package/src/AppShell.tsx
ADDED
|
@@ -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
|
+
}
|
package/src/cn.ts
CHANGED
|
@@ -9,7 +9,9 @@ const roleClasses = (prefix: string, leaf: string) =>
|
|
|
9
9
|
export const cn: CnFunction = createCn({
|
|
10
10
|
extend: {
|
|
11
11
|
classGroups: {
|
|
12
|
+
'border-w-e': ['border-e-(--rad-sidebar-border-width)'],
|
|
12
13
|
'border-w-b': [
|
|
14
|
+
'border-b-(--rad-app-shell-header-border-width)',
|
|
13
15
|
'border-b-(--rad-code-block-border-width)',
|
|
14
16
|
'border-b-(--rad-table-border-width)'
|
|
15
17
|
],
|
|
@@ -43,6 +45,7 @@ export const cn: CnFunction = createCn({
|
|
|
43
45
|
'font-(--rad-tabs-font-weight)'
|
|
44
46
|
],
|
|
45
47
|
'font-size': [
|
|
48
|
+
'text-(--rad-sidebar-label-size)',
|
|
46
49
|
'text-(--rad-inline-code-font-size)',
|
|
47
50
|
'text-(--rad-code-block-font-size)',
|
|
48
51
|
'text-(--rad-empty-state-title-size)',
|
|
@@ -83,6 +86,8 @@ export const cn: CnFunction = createCn({
|
|
|
83
86
|
'h-(--rad-slider-length)'
|
|
84
87
|
],
|
|
85
88
|
w: [
|
|
89
|
+
'w-(--rad-sidebar-width)',
|
|
90
|
+
'w-(--rad-sidebar-collapsed-width)',
|
|
86
91
|
'w-(--rad-resizable-handle-size)',
|
|
87
92
|
'w-(--rad-otp-field-slot-size)',
|
|
88
93
|
'w-(--rad-drawer-size)',
|
|
@@ -113,6 +118,7 @@ export const cn: CnFunction = createCn({
|
|
|
113
118
|
],
|
|
114
119
|
mb: ['mb-(--rad-multi-select-gap)', 'mb-(--rad-fieldset-gap)'],
|
|
115
120
|
gap: [
|
|
121
|
+
'gap-(--rad-sidebar-gap)',
|
|
116
122
|
'gap-(--rad-navigation-menu-gap)',
|
|
117
123
|
'gap-(--rad-code-block-header-gap)',
|
|
118
124
|
'gap-(--rad-empty-state-gap)',
|
|
@@ -141,6 +147,7 @@ export const cn: CnFunction = createCn({
|
|
|
141
147
|
'gap-(--rad-tabs-list-gap)'
|
|
142
148
|
],
|
|
143
149
|
'min-h': [
|
|
150
|
+
'min-h-(--rad-app-shell-header-min-height)',
|
|
144
151
|
'min-h-(--rad-native-select-height)',
|
|
145
152
|
'min-h-(--rad-textarea-min-height)',
|
|
146
153
|
'min-h-(--rad-tabs-trigger-height)'
|
|
@@ -169,6 +176,9 @@ export const cn: CnFunction = createCn({
|
|
|
169
176
|
'px-(--rad-tabs-trigger-padding)'
|
|
170
177
|
],
|
|
171
178
|
p: [
|
|
179
|
+
'p-(--rad-app-shell-header-padding)',
|
|
180
|
+
'p-(--rad-app-shell-main-padding)',
|
|
181
|
+
'p-(--rad-sidebar-padding)',
|
|
172
182
|
'p-(--rad-navigation-menu-padding)',
|
|
173
183
|
'p-(--rad-code-block-padding)',
|
|
174
184
|
'p-(--rad-code-block-header-padding)',
|
package/src/index.ts
CHANGED
|
@@ -712,3 +712,29 @@ export type {
|
|
|
712
712
|
NavigationMenuRenderProps,
|
|
713
713
|
NavigationMenuLinkRenderProps
|
|
714
714
|
} from './NavigationMenu.js'
|
|
715
|
+
|
|
716
|
+
export {
|
|
717
|
+
AppShell,
|
|
718
|
+
AppShellHeader,
|
|
719
|
+
AppShellMain,
|
|
720
|
+
AppShellSkipLink,
|
|
721
|
+
Sidebar,
|
|
722
|
+
SidebarTrigger,
|
|
723
|
+
SidebarLabel,
|
|
724
|
+
SidebarContent,
|
|
725
|
+
useAppShell,
|
|
726
|
+
useSidebar
|
|
727
|
+
} from './AppShell.js'
|
|
728
|
+
export type {
|
|
729
|
+
AppShellProps,
|
|
730
|
+
AppShellState,
|
|
731
|
+
AppShellHeaderProps,
|
|
732
|
+
AppShellMainProps,
|
|
733
|
+
AppShellSkipLinkProps,
|
|
734
|
+
AppShellRenderProps,
|
|
735
|
+
SidebarProps,
|
|
736
|
+
SidebarTriggerProps,
|
|
737
|
+
SidebarRenderProps,
|
|
738
|
+
SidebarLabelProps,
|
|
739
|
+
SidebarContentProps
|
|
740
|
+
} from './AppShell.js'
|
package/src/motion/host.ts
CHANGED
|
@@ -21,6 +21,7 @@ export interface MotionHostRuntime {
|
|
|
21
21
|
disclosure?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
|
|
22
22
|
dialog?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
|
|
23
23
|
popup?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
|
|
24
|
+
layout?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
|
|
24
25
|
indicator?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
|
|
25
26
|
}
|
|
26
27
|
export const MotionHostContext = createContext<MotionHostRuntime | null>(null)
|
|
@@ -104,7 +105,7 @@ export function usePressHost<Node extends HTMLElement>(
|
|
|
104
105
|
|
|
105
106
|
/** Shared lifecycle for native presentation adapters; no engine enters the root path. */
|
|
106
107
|
export function useTransitionHost<Node extends HTMLElement>(
|
|
107
|
-
kind: 'disclosure' | 'dialog' | 'popup' | 'indicator',
|
|
108
|
+
kind: 'disclosure' | 'dialog' | 'popup' | 'indicator' | 'layout',
|
|
108
109
|
ref: Ref<Node> | undefined,
|
|
109
110
|
disabled: boolean
|
|
110
111
|
) {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { bindSpringLayout } from './layoutDom.js'
|
|
2
|
+
import { resolveMotion, type MotionProfile } from './config.js'
|
|
3
|
+
import type { MotionHostBinding } from './host.js'
|
|
4
|
+
/** The optional host bridge shares the public layout adapter and its property leases. */
|
|
5
|
+
export function bindLayoutMotion(node: HTMLElement, initial: MotionProfile): MotionHostBinding {
|
|
6
|
+
const view = node.ownerDocument.defaultView!,
|
|
7
|
+
query = view.matchMedia('(prefers-reduced-motion: reduce)')
|
|
8
|
+
let profile = initial
|
|
9
|
+
const controller = bindSpringLayout(node, resolveMotion(profile, 'layout', query.matches))
|
|
10
|
+
const sync = () => {
|
|
11
|
+
controller.configure(resolveMotion(profile, 'layout', query.matches))
|
|
12
|
+
controller.measure()
|
|
13
|
+
}
|
|
14
|
+
query.addEventListener('change', sync)
|
|
15
|
+
return {
|
|
16
|
+
update(next) {
|
|
17
|
+
profile = next
|
|
18
|
+
sync()
|
|
19
|
+
},
|
|
20
|
+
destroy() {
|
|
21
|
+
query.removeEventListener('change', sync)
|
|
22
|
+
controller.destroy()
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/motion/press.ts
CHANGED
|
@@ -9,7 +9,8 @@ export function bindPressMotion(
|
|
|
9
9
|
initialProfile: MotionProfile
|
|
10
10
|
): MotionHostBinding {
|
|
11
11
|
let profile = initialProfile
|
|
12
|
-
const
|
|
12
|
+
const view = node.ownerDocument.defaultView!
|
|
13
|
+
const query = view.matchMedia('(prefers-reduced-motion: reduce)')
|
|
13
14
|
const spring = createSpring(1, {
|
|
14
15
|
...resolveMotion(profile, 'press', query.matches),
|
|
15
16
|
reducedValue: 1
|
|
@@ -43,7 +44,7 @@ export function bindPressMotion(
|
|
|
43
44
|
}
|
|
44
45
|
const changed = () => update(profile)
|
|
45
46
|
query.addEventListener('change', changed)
|
|
46
|
-
const observer = new MutationObserver(() => {
|
|
47
|
+
const observer = new view.MutationObserver(() => {
|
|
47
48
|
if (blocked()) reset()
|
|
48
49
|
})
|
|
49
50
|
observer.observe(node, {
|
|
@@ -59,7 +60,7 @@ export function bindPressMotion(
|
|
|
59
60
|
document.addEventListener('pointercancel', release)
|
|
60
61
|
document.addEventListener('keyup', release)
|
|
61
62
|
const blur = () => release()
|
|
62
|
-
|
|
63
|
+
view.addEventListener('blur', blur)
|
|
63
64
|
return {
|
|
64
65
|
update,
|
|
65
66
|
press(event) {
|
|
@@ -78,7 +79,7 @@ export function bindPressMotion(
|
|
|
78
79
|
pressed = { pointerId: event.pointerId }
|
|
79
80
|
}
|
|
80
81
|
const scale = Number(
|
|
81
|
-
getComputedStyle(node).getPropertyValue('--rad-button-press-scale').trim() || '.97'
|
|
82
|
+
view.getComputedStyle(node).getPropertyValue('--rad-button-press-scale').trim() || '.97'
|
|
82
83
|
)
|
|
83
84
|
spring.to(Number.isFinite(scale) && scale >= 0.5 && scale <= 1 ? scale : 0.97)
|
|
84
85
|
},
|
|
@@ -89,7 +90,7 @@ export function bindPressMotion(
|
|
|
89
90
|
document.removeEventListener('pointerup', release)
|
|
90
91
|
document.removeEventListener('pointercancel', release)
|
|
91
92
|
document.removeEventListener('keyup', release)
|
|
92
|
-
|
|
93
|
+
view.removeEventListener('blur', blur)
|
|
93
94
|
spring.destroy()
|
|
94
95
|
unbind()
|
|
95
96
|
}
|