@cognitap/radiant 0.0.36 → 0.0.38

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 (62) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/dist/library/Dialog.d.ts +4 -4
  3. package/dist/library/Dialog.d.ts.map +1 -1
  4. package/dist/library/Dialog.js +7 -11
  5. package/dist/library/Dialog.js.map +1 -1
  6. package/dist/library/Drawer.d.ts +50 -0
  7. package/dist/library/Drawer.d.ts.map +1 -0
  8. package/dist/library/Drawer.js +146 -0
  9. package/dist/library/Drawer.js.map +1 -0
  10. package/dist/library/Menu.d.ts +5 -1
  11. package/dist/library/Menu.d.ts.map +1 -1
  12. package/dist/library/Menu.js +7 -3
  13. package/dist/library/Menu.js.map +1 -1
  14. package/dist/library/Menubar.d.ts +53 -0
  15. package/dist/library/Menubar.d.ts.map +1 -0
  16. package/dist/library/Menubar.js +246 -0
  17. package/dist/library/Menubar.js.map +1 -0
  18. package/dist/library/cn.d.ts.map +1 -1
  19. package/dist/library/cn.js +5 -0
  20. package/dist/library/cn.js.map +1 -1
  21. package/dist/library/index.d.ts +4 -0
  22. package/dist/library/index.d.ts.map +1 -1
  23. package/dist/library/index.js +2 -0
  24. package/dist/library/index.js.map +1 -1
  25. package/dist/library/internal/drawer.d.ts +39 -0
  26. package/dist/library/internal/drawer.d.ts.map +1 -0
  27. package/dist/library/internal/drawer.js +286 -0
  28. package/dist/library/internal/drawer.js.map +1 -0
  29. package/dist/library/motion/drawer.d.ts +8 -0
  30. package/dist/library/motion/drawer.d.ts.map +1 -0
  31. package/dist/library/motion/drawer.js +37 -0
  32. package/dist/library/motion/drawer.js.map +1 -0
  33. package/dist/library/motion/host.d.ts +6 -0
  34. package/dist/library/motion/host.d.ts.map +1 -1
  35. package/dist/library/motion/host.js +29 -0
  36. package/dist/library/motion/host.js.map +1 -1
  37. package/dist/library/motion/react.d.ts.map +1 -1
  38. package/dist/library/motion/react.js +2 -0
  39. package/dist/library/motion/react.js.map +1 -1
  40. package/dist/library/react/useRovingFocus.d.ts +3 -1
  41. package/dist/library/react/useRovingFocus.d.ts.map +1 -1
  42. package/dist/library/react/useRovingFocus.js +3 -2
  43. package/dist/library/react/useRovingFocus.js.map +1 -1
  44. package/dist/library/styles.css +1 -1
  45. package/dist/library/themes/components.d.ts +9 -0
  46. package/dist/library/themes/components.d.ts.map +1 -1
  47. package/dist/library/themes/components.js +2 -0
  48. package/dist/library/themes/components.js.map +1 -1
  49. package/docs/architecture.md +24 -19
  50. package/package.json +1 -1
  51. package/src/Dialog.tsx +14 -13
  52. package/src/Drawer.tsx +288 -0
  53. package/src/Menu.tsx +20 -3
  54. package/src/Menubar.tsx +496 -0
  55. package/src/cn.ts +5 -0
  56. package/src/index.ts +61 -0
  57. package/src/internal/drawer.ts +336 -0
  58. package/src/motion/drawer.ts +43 -0
  59. package/src/motion/host.ts +44 -0
  60. package/src/motion/react.ts +2 -0
  61. package/src/react/useRovingFocus.ts +5 -2
  62. package/src/themes/components.ts +2 -0
@@ -0,0 +1,336 @@
1
+ export type DrawerSide = 'top' | 'right' | 'bottom' | 'left' | 'start' | 'end'
2
+ export function drawerPoints(points: readonly number[]) {
3
+ if (!points.length || points.some(point => !Number.isFinite(point) || point <= 0 || point > 1))
4
+ throw new TypeError('Drawer snap points must be fractions greater than zero and at most one.')
5
+ return [...new Set(points)].sort((a, b) => a - b)
6
+ }
7
+ export interface DrawerDriver {
8
+ get(): number
9
+ jump(value: number): void
10
+ to(value: number): void
11
+ subscribe(listener: (value: number) => void): () => void
12
+ destroy(): void
13
+ }
14
+ export interface DrawerDragDriver extends DrawerDriver {
15
+ begin(): void
16
+ move(value: number, time: number): void
17
+ release(time: number): { target: number }
18
+ cancel(): void
19
+ configure(options: { min: number; max: number; snapPoints: readonly number[] }): void
20
+ }
21
+ export interface DrawerOptions {
22
+ side: DrawerSide
23
+ snapPoint: number
24
+ snapPoints: readonly number[]
25
+ motion: 'inherit' | 'off'
26
+ request(point: number): void
27
+ dismiss(): void
28
+ }
29
+ export interface DrawerBinding {
30
+ update(options: DrawerOptions): void
31
+ destroy(): void
32
+ }
33
+ /** Functional fallback without the optional spring engine. */
34
+ export function immediateDrawerDriver(initial: number): DrawerDragDriver {
35
+ let value = initial,
36
+ previous = initial,
37
+ points: readonly number[] = [0, 1]
38
+ const listeners = new Set<(value: number) => void>()
39
+ const jump = (next: number) => {
40
+ value = Math.max(0, Math.min(1, next))
41
+ listeners.forEach(listener => listener(value))
42
+ }
43
+ return {
44
+ get: () => value,
45
+ jump,
46
+ to: jump,
47
+ subscribe(listener) {
48
+ listeners.add(listener)
49
+ return () => {
50
+ listeners.delete(listener)
51
+ }
52
+ },
53
+ begin() {
54
+ previous = value
55
+ },
56
+ move: jump,
57
+ release() {
58
+ const target = points.reduce(
59
+ (best, point) => (Math.abs(point - value) < Math.abs(best - value) ? point : best),
60
+ points[0]
61
+ )
62
+ jump(target)
63
+ return { target }
64
+ },
65
+ cancel() {
66
+ jump(previous)
67
+ },
68
+ configure(options) {
69
+ points = options.snapPoints
70
+ },
71
+ destroy() {
72
+ listeners.clear()
73
+ }
74
+ }
75
+ }
76
+ /** Native handle events and geometry; injected drivers own presentation only. */
77
+ export function bindDrawer(
78
+ node: HTMLDialogElement,
79
+ initial: DrawerOptions,
80
+ drag: DrawerDragDriver = immediateDrawerDriver(initial.snapPoint),
81
+ entry: DrawerDriver = immediateDrawerDriver(1)
82
+ ): DrawerBinding {
83
+ const view = node.ownerDocument.defaultView!,
84
+ doc = node.ownerDocument
85
+ let options = initial,
86
+ open = false,
87
+ extent = 1,
88
+ destroyed = false
89
+ let physical: Exclude<DrawerSide, 'start' | 'end'> = 'right'
90
+ let gesture: {
91
+ id: number
92
+ x: number
93
+ y: number
94
+ point: number
95
+ handle: HTMLElement
96
+ active: boolean
97
+ } | null = null
98
+ let suppressClick = false
99
+ const variables = ['--_rad-drawer-shift', '--_rad-drawer-visible'] as const
100
+ const saved = variables.map(key => node.style.getPropertyValue(key))
101
+ const horizontal = () => physical === 'left' || physical === 'right'
102
+ const sign = () => (physical === 'bottom' || physical === 'right' ? 1 : -1)
103
+ function render() {
104
+ // showModal() runs its native focus algorithm before the open observer fires.
105
+ // Leave a closed viewport at its CSS size so those targets are eligible.
106
+ if (!node.hasAttribute('open')) {
107
+ node.style.setProperty('--_rad-drawer-shift', '0%')
108
+ node.style.removeProperty('--_rad-drawer-visible')
109
+ return
110
+ }
111
+ const fraction = Math.max(0, Math.min(1, drag.get()))
112
+ node.style.setProperty(
113
+ '--_rad-drawer-shift',
114
+ `${sign() * (1 - fraction * Math.max(0, Math.min(1, entry.get()))) * 100}%`
115
+ )
116
+ const css = view.getComputedStyle(node)
117
+ const border =
118
+ parseFloat(
119
+ css[
120
+ physical === 'bottom'
121
+ ? 'borderTopWidth'
122
+ : physical === 'top'
123
+ ? 'borderBottomWidth'
124
+ : physical === 'right'
125
+ ? 'borderLeftWidth'
126
+ : 'borderRightWidth'
127
+ ]
128
+ ) || 0
129
+ node.style.setProperty('--_rad-drawer-visible', `${Math.max(0, extent * fraction - border)}px`)
130
+ }
131
+ const offDrag = drag.subscribe(render),
132
+ offEntry = entry.subscribe(render)
133
+ function cancel() {
134
+ const prior = gesture
135
+ gesture = null
136
+ node.removeAttribute('data-dragging')
137
+ if (prior?.active) {
138
+ drag.cancel()
139
+ drag.to(options.snapPoint)
140
+ suppressClick = true
141
+ }
142
+ if (prior?.handle.hasPointerCapture(prior.id)) prior.handle.releasePointerCapture(prior.id)
143
+ }
144
+ function sync() {
145
+ if (destroyed) return
146
+ const rtl = view.getComputedStyle(node).direction === 'rtl'
147
+ const side =
148
+ options.side === 'start'
149
+ ? rtl
150
+ ? 'right'
151
+ : 'left'
152
+ : options.side === 'end'
153
+ ? rtl
154
+ ? 'left'
155
+ : 'right'
156
+ : options.side
157
+ if (side !== physical) {
158
+ cancel()
159
+ physical = side
160
+ }
161
+ node.setAttribute('data-physical-side', physical)
162
+ const nextOpen = node.hasAttribute('open')
163
+ if (nextOpen) {
164
+ const rect = node.getBoundingClientRect()
165
+ extent = Math.max(1, horizontal() ? rect.width : rect.height)
166
+ }
167
+ drag.configure({ min: 0, max: 1, snapPoints: [0, ...options.snapPoints] })
168
+ if (nextOpen !== open) {
169
+ cancel()
170
+ drag.jump(options.snapPoint)
171
+ entry.jump(nextOpen ? 0 : 1)
172
+ if (nextOpen) entry.to(1)
173
+ open = nextOpen
174
+ } else if (!gesture?.active) drag.to(options.snapPoint)
175
+ render()
176
+ }
177
+ function targetHandle(event: Event) {
178
+ const target = event.target
179
+ if (!(target instanceof view.Element)) return null
180
+ const handle = target.closest<HTMLElement>('[data-slot~="drawer-handle"]')
181
+ return handle?.closest('dialog') === node ? handle : null
182
+ }
183
+ function down(event: PointerEvent) {
184
+ const handle = targetHandle(event)
185
+ if (!handle || !open) return
186
+ suppressClick = false
187
+ if (
188
+ event.defaultPrevented ||
189
+ event.button !== 0 ||
190
+ !event.isPrimary ||
191
+ handle.matches(':disabled,[aria-disabled="true"],[aria-busy="true"]')
192
+ )
193
+ return
194
+ cancel()
195
+ gesture = {
196
+ id: event.pointerId,
197
+ x: event.clientX,
198
+ y: event.clientY,
199
+ point: drag.get(),
200
+ handle,
201
+ active: false
202
+ }
203
+ }
204
+ function move(event: PointerEvent) {
205
+ const current = gesture
206
+ if (!current || current.id !== event.pointerId) return
207
+ if (event.defaultPrevented) {
208
+ cancel()
209
+ return
210
+ }
211
+ const dx = event.clientX - current.x,
212
+ dy = event.clientY - current.y
213
+ const delta = horizontal() ? dx : dy,
214
+ cross = horizontal() ? dy : dx
215
+ if (!current.active) {
216
+ if (Math.abs(cross) > 8 && Math.abs(cross) > Math.abs(delta)) {
217
+ cancel()
218
+ return
219
+ }
220
+ if (Math.abs(delta) < 4) return
221
+ current.active = true
222
+ drag.begin()
223
+ current.handle.setPointerCapture(event.pointerId)
224
+ node.setAttribute('data-dragging', '')
225
+ }
226
+ event.preventDefault()
227
+ drag.move(current.point - (sign() * delta) / extent, event.timeStamp)
228
+ }
229
+ function up(event: PointerEvent) {
230
+ const current = gesture
231
+ if (!current || current.id !== event.pointerId) return
232
+ if (event.defaultPrevented) {
233
+ cancel()
234
+ return
235
+ }
236
+ gesture = null
237
+ node.removeAttribute('data-dragging')
238
+ if (current.active) {
239
+ suppressClick = true
240
+ const { target } = drag.release(event.timeStamp)
241
+ if (target === 0) options.dismiss()
242
+ else options.request(target)
243
+ // Controlled owners may refuse without causing another render.
244
+ view.queueMicrotask(() => {
245
+ if (!destroyed && node.hasAttribute('open')) drag.to(options.snapPoint)
246
+ })
247
+ }
248
+ if (current.handle.hasPointerCapture(current.id))
249
+ current.handle.releasePointerCapture(current.id)
250
+ }
251
+ function click(event: MouseEvent) {
252
+ if (suppressClick && targetHandle(event)) {
253
+ suppressClick = false
254
+ event.preventDefault()
255
+ event.stopImmediatePropagation()
256
+ }
257
+ }
258
+ function key(event: KeyboardEvent) {
259
+ if (event.key === 'Escape' && gesture?.active && !event.defaultPrevented) {
260
+ event.preventDefault()
261
+ cancel()
262
+ }
263
+ }
264
+ function lost(event: PointerEvent) {
265
+ // Touch implicitly captures the inner span first; transferring that capture
266
+ // to the actual Button must not cancel the new gesture.
267
+ if (event.type === 'lostpointercapture' && event.target !== gesture?.handle) return
268
+ if (gesture?.id === event.pointerId) cancel()
269
+ }
270
+ const observer = new view.MutationObserver(sync)
271
+ observer.observe(node, { attributes: true, attributeFilter: ['open'] })
272
+ const direction = new view.MutationObserver(() => {
273
+ const rtl = view.getComputedStyle(node).direction === 'rtl'
274
+ const expected =
275
+ options.side === 'start'
276
+ ? rtl
277
+ ? 'right'
278
+ : 'left'
279
+ : options.side === 'end'
280
+ ? rtl
281
+ ? 'left'
282
+ : 'right'
283
+ : options.side
284
+ if (physical !== expected) sync()
285
+ })
286
+ for (let ancestor: HTMLElement | null = node; ancestor; ancestor = ancestor.parentElement)
287
+ direction.observe(ancestor, { attributes: true, attributeFilter: ['dir', 'class', 'style'] })
288
+ const resize = new view.ResizeObserver(sync)
289
+ resize.observe(node)
290
+ doc.addEventListener('pointerdown', down)
291
+ doc.addEventListener('pointermove', move, { passive: false })
292
+ doc.addEventListener('pointerup', up)
293
+ doc.addEventListener('pointercancel', lost)
294
+ doc.addEventListener('lostpointercapture', lost)
295
+ doc.addEventListener('click', click, true)
296
+ doc.addEventListener('keydown', key)
297
+ view.addEventListener('blur', cancel)
298
+ view.addEventListener('resize', sync)
299
+ sync()
300
+ return {
301
+ update(next) {
302
+ if (
303
+ next.side !== options.side ||
304
+ next.snapPoint !== options.snapPoint ||
305
+ next.snapPoints.join() !== options.snapPoints.join()
306
+ )
307
+ cancel()
308
+ options = next
309
+ sync()
310
+ },
311
+ destroy() {
312
+ destroyed = true
313
+ cancel()
314
+ observer.disconnect()
315
+ direction.disconnect()
316
+ resize.disconnect()
317
+ doc.removeEventListener('pointerdown', down)
318
+ doc.removeEventListener('pointermove', move)
319
+ doc.removeEventListener('pointerup', up)
320
+ doc.removeEventListener('pointercancel', lost)
321
+ doc.removeEventListener('lostpointercapture', lost)
322
+ doc.removeEventListener('click', click, true)
323
+ doc.removeEventListener('keydown', key)
324
+ view.removeEventListener('blur', cancel)
325
+ view.removeEventListener('resize', sync)
326
+ offDrag()
327
+ offEntry()
328
+ drag.destroy()
329
+ entry.destroy()
330
+ variables.forEach((key, index) =>
331
+ saved[index] ? node.style.setProperty(key, saved[index]) : node.style.removeProperty(key)
332
+ )
333
+ node.removeAttribute('data-physical-side')
334
+ }
335
+ }
336
+ }
@@ -0,0 +1,43 @@
1
+ import { bindDrawer, type DrawerOptions } from '../internal/drawer.js'
2
+ import { createSpringDrag } from './drag.js'
3
+ import { createSpring } from './runtime.js'
4
+ import { resolveMotion, type MotionProfile } from './config.js'
5
+
6
+ export function bindDrawerMotion(
7
+ node: HTMLDialogElement,
8
+ initial: MotionProfile,
9
+ initialOptions: DrawerOptions
10
+ ) {
11
+ let profile = initial,
12
+ options = initialOptions
13
+ const query = node.ownerDocument.defaultView!.matchMedia('(prefers-reduced-motion: reduce)')
14
+ const policy = (interaction: 'drag' | 'dialog') =>
15
+ resolveMotion(profile, interaction, query.matches || options.motion === 'off')
16
+ const drag = createSpringDrag(options.snapPoint, {
17
+ min: 0,
18
+ max: 1,
19
+ snapPoints: [0, ...options.snapPoints],
20
+ ...policy('drag')
21
+ })
22
+ const entry = createSpring(1, policy('dialog'))
23
+ const binding = bindDrawer(node, options, drag, entry)
24
+ function configure(next: MotionProfile) {
25
+ profile = next
26
+ drag.configure(policy('drag'))
27
+ entry.configure(policy('dialog'))
28
+ }
29
+ const preference = () => configure(profile)
30
+ query.addEventListener('change', preference)
31
+ return {
32
+ configure,
33
+ update(next: DrawerOptions) {
34
+ options = next
35
+ configure(profile)
36
+ binding.update(next)
37
+ },
38
+ destroy() {
39
+ query.removeEventListener('change', preference)
40
+ binding.destroy()
41
+ }
42
+ }
43
+ }
@@ -3,6 +3,7 @@ import { createContext, useContext, useEffect, useRef, useState, type Ref } from
3
3
  import type { MotionProfile } from './config.js'
4
4
  import { MotionProfileContext } from './context.js'
5
5
  import { useComposedRefs } from '../react/useComposedRefs.js'
6
+ import { bindDrawer, type DrawerBinding, type DrawerOptions } from '../internal/drawer.js'
6
7
 
7
8
  export interface MotionHostBinding {
8
9
  update(profile: MotionProfile): void
@@ -11,6 +12,11 @@ export interface MotionHostBinding {
11
12
  destroy(): void
12
13
  }
13
14
  export interface MotionHostRuntime {
15
+ drawer?(
16
+ node: HTMLDialogElement,
17
+ profile: MotionProfile,
18
+ options: DrawerOptions
19
+ ): DrawerBinding & { configure(profile: MotionProfile): void }
14
20
  press(node: HTMLElement, profile: MotionProfile): MotionHostBinding
15
21
  disclosure?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
16
22
  dialog?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
@@ -18,6 +24,44 @@ export interface MotionHostRuntime {
18
24
  indicator?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
19
25
  }
20
26
  export const MotionHostContext = createContext<MotionHostRuntime | null>(null)
27
+
28
+ /** Geometry and gestures remain functional without the optional engine. */
29
+ export function useDrawerHost(ref: Ref<HTMLDialogElement> | undefined, options: DrawerOptions) {
30
+ const runtime = useContext(MotionHostContext),
31
+ profile = useContext(MotionProfileContext)
32
+ const node = useRef<HTMLDialogElement>(null)
33
+ const binding = useRef<{
34
+ node: HTMLDialogElement
35
+ runtime: typeof runtime
36
+ value: DrawerBinding & { configure?(profile: MotionProfile): void }
37
+ } | null>(null)
38
+ const composed = useComposedRefs(node, ref)
39
+ useEffect(() => {
40
+ if (
41
+ binding.current &&
42
+ (binding.current.node !== node.current || binding.current.runtime !== runtime)
43
+ ) {
44
+ binding.current.value.destroy()
45
+ binding.current = null
46
+ }
47
+ if (!node.current) return
48
+ binding.current ??= {
49
+ node: node.current,
50
+ runtime,
51
+ value: runtime?.drawer?.(node.current, profile, options) ?? bindDrawer(node.current, options)
52
+ }
53
+ binding.current.value.configure?.(profile)
54
+ binding.current.value.update(options)
55
+ })
56
+ useEffect(
57
+ () => () => {
58
+ binding.current?.value.destroy()
59
+ binding.current = null
60
+ },
61
+ []
62
+ )
63
+ return composed
64
+ }
21
65
  /** Engine-free bridge: only the opt-in provider supplies implementation. */
22
66
  export function usePressHost<Node extends HTMLElement>(
23
67
  ref: Ref<Node> | undefined,
@@ -14,6 +14,7 @@ import { bindDisclosureMotion, bindDialogMotion } from './transitions.js'
14
14
  import { bindPressMotion } from './press.js'
15
15
  import { bindPopupMotion } from './popup.js'
16
16
  import { bindIndicatorMotion } from './indicator.js'
17
+ import { bindDrawerMotion } from './drawer.js'
17
18
  import { MotionProfileContext } from './context.js'
18
19
  import { resolveMotion, type MotionInteraction } from './config.js'
19
20
  import { createSpringDrag, type SpringDragController, type SpringDragOptions } from './drag.js'
@@ -134,6 +135,7 @@ export function useSpringStyle(
134
135
  }
135
136
 
136
137
  const hostRuntime = {
138
+ drawer: bindDrawerMotion,
137
139
  press: bindPressMotion,
138
140
  disclosure: bindDisclosureMotion,
139
141
  dialog: bindDialogMotion,
@@ -20,6 +20,7 @@ type Options<Node extends HTMLElement> = {
20
20
  loop: boolean
21
21
  active?: boolean
22
22
  deferTo?: string
23
+ includeAriaDisabled?: boolean
23
24
  }
24
25
 
25
26
  /** Native hosts and DOM order stay authoritative, including dynamic disabled items. */
@@ -31,7 +32,8 @@ export function useRovingFocus<Node extends HTMLElement>({
31
32
  orientation,
32
33
  loop,
33
34
  active = true,
34
- deferTo
35
+ deferTo,
36
+ includeAriaDisabled = false
35
37
  }: Options<Node>) {
36
38
  const ownsFocus = (node: Node) => {
37
39
  if (!active) return false
@@ -50,7 +52,7 @@ export function useRovingFocus<Node extends HTMLElement>({
50
52
  return items(node).filter(
51
53
  item =>
52
54
  !item.matches(':disabled') &&
53
- item.getAttribute('aria-disabled') !== 'true' &&
55
+ (includeAriaDisabled || item.getAttribute('aria-disabled') !== 'true') &&
54
56
  !item.closest('[inert], [hidden]') &&
55
57
  item.getClientRects().length > 0 &&
56
58
  !['hidden', 'collapse'].includes(view.getComputedStyle(item).visibility)
@@ -123,6 +125,7 @@ export function useRovingFocus<Node extends HTMLElement>({
123
125
  }
124
126
  })
125
127
  return {
128
+ items: enabled,
126
129
  onFocus(event: FocusEvent<Node>) {
127
130
  if (
128
131
  ownsFocus(event.currentTarget) &&
@@ -2,6 +2,8 @@ import { kebabCase } from './tokens.js'
2
2
 
3
3
  /** Explicit component slots; omitted slots reset to CSS fallback semantics. */
4
4
  export const componentRegistry = {
5
+ drawer: { size: 'css' },
6
+ menubar: { gap: 'css', padding: 'css', radius: 'css', background: 'css' },
5
7
  contextMenu: { width: 'css' },
6
8
  hoverCard: { width: 'css' },
7
9
  button: {