@cognitap/radiant 0.0.34 → 0.0.36
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 +22 -8
- package/dist/library/ContextMenu.d.ts +38 -0
- package/dist/library/ContextMenu.d.ts.map +1 -0
- package/dist/library/ContextMenu.js +169 -0
- package/dist/library/ContextMenu.js.map +1 -0
- package/dist/library/HoverCard.d.ts +26 -0
- package/dist/library/HoverCard.d.ts.map +1 -0
- package/dist/library/HoverCard.js +89 -0
- package/dist/library/HoverCard.js.map +1 -0
- package/dist/library/Menu.d.ts +83 -3
- package/dist/library/Menu.d.ts.map +1 -1
- package/dist/library/Menu.js +209 -25
- package/dist/library/Menu.js.map +1 -1
- package/dist/library/Popover.d.ts +32 -3
- package/dist/library/Popover.d.ts.map +1 -1
- package/dist/library/Popover.js +34 -9
- package/dist/library/Popover.js.map +1 -1
- package/dist/library/Tooltip.d.ts.map +1 -1
- package/dist/library/Tooltip.js +10 -88
- package/dist/library/Tooltip.js.map +1 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +2 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +6 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +3 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/react/useHoverSurface.d.ts +18 -0
- package/dist/library/react/useHoverSurface.d.ts.map +1 -0
- package/dist/library/react/useHoverSurface.js +109 -0
- package/dist/library/react/useHoverSurface.js.map +1 -0
- package/dist/library/react/usePopoverSurface.d.ts +6 -1
- package/dist/library/react/usePopoverSurface.d.ts.map +1 -1
- package/dist/library/react/usePopoverSurface.js +27 -6
- package/dist/library/react/usePopoverSurface.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +6 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +2 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +35 -30
- package/package.json +1 -1
- package/src/ContextMenu.tsx +354 -0
- package/src/HoverCard.tsx +179 -0
- package/src/Menu.tsx +496 -40
- package/src/Popover.tsx +69 -14
- package/src/Tooltip.tsx +9 -80
- package/src/cn.ts +2 -0
- package/src/index.ts +72 -0
- package/src/react/useHoverSurface.ts +119 -0
- package/src/react/usePopoverSurface.ts +33 -6
- package/src/themes/components.ts +2 -0
package/src/Popover.tsx
CHANGED
|
@@ -32,8 +32,9 @@ export type PopoverProps = (
|
|
|
32
32
|
}
|
|
33
33
|
export type PopoverState = { open: boolean; setOpen: (open: boolean) => void; contentId: string }
|
|
34
34
|
type Context = PopoverState & {
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
anchorPoint?: { x: number; y: number }
|
|
36
|
+
trigger: HTMLElement | null
|
|
37
|
+
setTrigger: (node: HTMLElement | null) => void
|
|
37
38
|
content: RefObject<HTMLDivElement | null>
|
|
38
39
|
restoreFocus: RefObject<boolean>
|
|
39
40
|
close: (restore: boolean) => void
|
|
@@ -42,12 +43,28 @@ const PopoverContext = createContext<Context | null>(null)
|
|
|
42
43
|
export function usePopover(): PopoverState | null {
|
|
43
44
|
return useContext(PopoverContext)
|
|
44
45
|
}
|
|
45
|
-
|
|
46
|
+
/** Private native-anchor seam for composed surface families; absent from package exports. */
|
|
47
|
+
export function usePopoverContext() {
|
|
46
48
|
const value = useContext(PopoverContext)
|
|
47
49
|
if (!value) throw new Error('Popover parts must render inside Popover.')
|
|
48
50
|
return value
|
|
49
51
|
}
|
|
50
|
-
export function Popover(
|
|
52
|
+
export function Popover(props: PopoverProps) {
|
|
53
|
+
return (
|
|
54
|
+
<PopoverRoot
|
|
55
|
+
{...props}
|
|
56
|
+
anchorPoint={undefined}
|
|
57
|
+
/>
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
/** Private pointer anchor for composed context menus; the real source retains focus ownership. */
|
|
61
|
+
export function PopoverRoot({
|
|
62
|
+
open,
|
|
63
|
+
defaultOpen,
|
|
64
|
+
onOpenChange,
|
|
65
|
+
children,
|
|
66
|
+
anchorPoint
|
|
67
|
+
}: PopoverProps & { anchorPoint?: { x: number; y: number } }) {
|
|
51
68
|
const state = useControllableState({
|
|
52
69
|
name: 'Popover',
|
|
53
70
|
value: open,
|
|
@@ -56,7 +73,7 @@ export function Popover({ open, defaultOpen, onOpenChange, children }: PopoverPr
|
|
|
56
73
|
onChange: onOpenChange
|
|
57
74
|
})
|
|
58
75
|
const contentId = useId()
|
|
59
|
-
const [trigger, setTrigger] = useState<
|
|
76
|
+
const [trigger, setTrigger] = useState<HTMLElement | null>(null)
|
|
60
77
|
const content = useRef<HTMLDivElement>(null)
|
|
61
78
|
const restoreFocus = useRef(true)
|
|
62
79
|
const setOpen = (next: boolean) => {
|
|
@@ -73,6 +90,7 @@ export function Popover({ open, defaultOpen, onOpenChange, children }: PopoverPr
|
|
|
73
90
|
open: state.value,
|
|
74
91
|
setOpen,
|
|
75
92
|
contentId,
|
|
93
|
+
anchorPoint,
|
|
76
94
|
trigger,
|
|
77
95
|
setTrigger,
|
|
78
96
|
content,
|
|
@@ -103,7 +121,7 @@ export type PopoverContentProps = Omit<ComponentProps<'div'>, 'style' | 'popover
|
|
|
103
121
|
|
|
104
122
|
export function PopoverTrigger({ ref, onClick, ...props }: PopoverTriggerProps) {
|
|
105
123
|
const popover = usePopoverContext()
|
|
106
|
-
const hostRef = useComposedRefs(popover.setTrigger, ref)
|
|
124
|
+
const hostRef = useComposedRefs<HTMLButtonElement>(popover.setTrigger, ref)
|
|
107
125
|
return (
|
|
108
126
|
<Button
|
|
109
127
|
{...props}
|
|
@@ -117,7 +135,22 @@ export function PopoverTrigger({ ref, onClick, ...props }: PopoverTriggerProps)
|
|
|
117
135
|
/>
|
|
118
136
|
)
|
|
119
137
|
}
|
|
120
|
-
export function PopoverClose(
|
|
138
|
+
export function PopoverClose(props: PopoverCloseProps) {
|
|
139
|
+
return (
|
|
140
|
+
<PopoverCloseRoot
|
|
141
|
+
{...props}
|
|
142
|
+
requestClose={undefined}
|
|
143
|
+
closeOnClick={undefined}
|
|
144
|
+
/>
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
/** Private close-action policy for composed menus; keeps the real Button and identities. */
|
|
148
|
+
export function PopoverCloseRoot({
|
|
149
|
+
onClick,
|
|
150
|
+
requestClose,
|
|
151
|
+
closeOnClick = true,
|
|
152
|
+
...props
|
|
153
|
+
}: PopoverCloseProps & { requestClose?: () => void; closeOnClick?: boolean }) {
|
|
121
154
|
const popover = usePopoverContext()
|
|
122
155
|
return (
|
|
123
156
|
<Button
|
|
@@ -125,16 +158,20 @@ export function PopoverClose({ onClick, ...props }: PopoverCloseProps) {
|
|
|
125
158
|
popoverTarget={undefined}
|
|
126
159
|
popoverTargetAction={undefined}
|
|
127
160
|
data-slot={joinIds('popover-close', props['data-slot'])}
|
|
128
|
-
onClick={composeAction(onClick, () =>
|
|
161
|
+
onClick={composeAction(onClick, () => {
|
|
162
|
+
if (closeOnClick) (requestClose ?? (() => popover.close(true)))()
|
|
163
|
+
})}
|
|
129
164
|
/>
|
|
130
165
|
)
|
|
131
166
|
}
|
|
132
167
|
/** The local portal preserves theme, native form ownership and modal ancestry. */
|
|
133
168
|
export function PopoverPortal({ children }: PopoverPortalProps) {
|
|
134
169
|
const popover = usePopoverContext()
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
170
|
+
// Rich surfaces cannot be inserted into a link's phrasing-only parent (p, span, label).
|
|
171
|
+
const container = popover.trigger?.parentElement?.closest<HTMLElement>(
|
|
172
|
+
'div, section, article, aside, main, nav, form, dialog, [popover], li, dd, td, th, fieldset, figure, body'
|
|
173
|
+
)
|
|
174
|
+
return container ? createPortal(children, container) : null
|
|
138
175
|
}
|
|
139
176
|
|
|
140
177
|
const surfaceClass = [
|
|
@@ -151,7 +188,17 @@ const surfaceClass = [
|
|
|
151
188
|
'forced-colors:border-[CanvasText] forced-colors:bg-[Canvas] forced-colors:text-[CanvasText]'
|
|
152
189
|
].join(' ')
|
|
153
190
|
|
|
154
|
-
export function PopoverContent({
|
|
191
|
+
export function PopoverContent(props: PopoverContentProps) {
|
|
192
|
+
return (
|
|
193
|
+
<PopoverContentRoot
|
|
194
|
+
{...props}
|
|
195
|
+
onFocusOutside={undefined}
|
|
196
|
+
/>
|
|
197
|
+
)
|
|
198
|
+
}
|
|
199
|
+
/** Private focus-leave timing seam; native pointer/Escape dismissal stays shared. */
|
|
200
|
+
export function PopoverContentRoot({
|
|
201
|
+
onFocusOutside,
|
|
155
202
|
ref,
|
|
156
203
|
className,
|
|
157
204
|
side = 'bottom',
|
|
@@ -161,7 +208,7 @@ export function PopoverContent({
|
|
|
161
208
|
initialFocusRef,
|
|
162
209
|
motion = 'inherit',
|
|
163
210
|
...props
|
|
164
|
-
}: PopoverContentProps) {
|
|
211
|
+
}: PopoverContentProps & { onFocusOutside?: () => void }) {
|
|
165
212
|
const popover = usePopoverContext()
|
|
166
213
|
if (![sideOffset, collisionPadding].every(value => Number.isFinite(value) && value >= 0))
|
|
167
214
|
throw new TypeError('Popover offsets and padding must be finite, nonnegative numbers.')
|
|
@@ -177,7 +224,15 @@ export function PopoverContent({
|
|
|
177
224
|
popover.open,
|
|
178
225
|
popover.close,
|
|
179
226
|
popover.restoreFocus,
|
|
180
|
-
{
|
|
227
|
+
{
|
|
228
|
+
side,
|
|
229
|
+
align,
|
|
230
|
+
sideOffset,
|
|
231
|
+
collisionPadding,
|
|
232
|
+
initialFocusRef,
|
|
233
|
+
onFocusOutside,
|
|
234
|
+
anchorPoint: popover.anchorPoint
|
|
235
|
+
}
|
|
181
236
|
)
|
|
182
237
|
return (
|
|
183
238
|
<div
|
package/src/Tooltip.tsx
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
createContext,
|
|
5
|
-
useCallback,
|
|
6
5
|
useContext,
|
|
7
|
-
useEffect,
|
|
8
6
|
useId,
|
|
9
7
|
useRef,
|
|
10
8
|
useState,
|
|
@@ -20,6 +18,7 @@ import { composeAction } from './internal/events.js'
|
|
|
20
18
|
import type { PopoverSide, PopoverAlign } from './internal/popover.js'
|
|
21
19
|
import { useControllableState } from './react/useControllableState.js'
|
|
22
20
|
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
21
|
+
import { useHoverSurface } from './react/useHoverSurface.js'
|
|
23
22
|
import { usePopoverSurface } from './react/usePopoverSurface.js'
|
|
24
23
|
import { useTransitionHost } from './motion/host.js'
|
|
25
24
|
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
@@ -73,81 +72,14 @@ export function Tooltip({
|
|
|
73
72
|
const contentId = useId()
|
|
74
73
|
const [trigger, setTrigger] = useState<HTMLButtonElement | null>(null)
|
|
75
74
|
const content = useRef<HTMLSpanElement>(null)
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
useEffect(() => {
|
|
84
|
-
latest.current = state.request
|
|
75
|
+
const { enter, leave, dismiss, touch } = useHoverSurface({
|
|
76
|
+
trigger,
|
|
77
|
+
content,
|
|
78
|
+
open: state.value,
|
|
79
|
+
request: state.request,
|
|
80
|
+
openDelay,
|
|
81
|
+
closeDelay
|
|
85
82
|
})
|
|
86
|
-
useEffect(() => () => clear(), [])
|
|
87
|
-
const schedule = useCallback(
|
|
88
|
-
(next: boolean, delay: number) => {
|
|
89
|
-
clear()
|
|
90
|
-
const view = trigger?.ownerDocument.defaultView
|
|
91
|
-
if (!view) return
|
|
92
|
-
timer.current = {
|
|
93
|
-
view,
|
|
94
|
-
id: view.setTimeout(() => {
|
|
95
|
-
timer.current = null
|
|
96
|
-
latest.current(next)
|
|
97
|
-
}, delay)
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
[trigger]
|
|
101
|
-
)
|
|
102
|
-
const dismiss = () => {
|
|
103
|
-
clear()
|
|
104
|
-
activity.current.suppressed = true
|
|
105
|
-
state.request(false)
|
|
106
|
-
}
|
|
107
|
-
const enter = (focus = false) => {
|
|
108
|
-
if (focus && !activity.current.focused) activity.current.suppressed = false
|
|
109
|
-
if (focus) activity.current.focused = true
|
|
110
|
-
else activity.current.hovered = true
|
|
111
|
-
clear()
|
|
112
|
-
if (activity.current.suppressed || activity.current.touch) return
|
|
113
|
-
if (focus) state.request(true)
|
|
114
|
-
else if (!state.value) schedule(true, openDelay)
|
|
115
|
-
}
|
|
116
|
-
const leave = (focus = false) => {
|
|
117
|
-
if (focus) {
|
|
118
|
-
activity.current.focused = false
|
|
119
|
-
activity.current.touch = false
|
|
120
|
-
} else activity.current.hovered = false
|
|
121
|
-
if (activity.current.focused || activity.current.hovered) return
|
|
122
|
-
activity.current.suppressed = false
|
|
123
|
-
schedule(false, closeDelay)
|
|
124
|
-
}
|
|
125
|
-
useEffect(() => {
|
|
126
|
-
if (!trigger || !state.value) return
|
|
127
|
-
const move = (event: PointerEvent) => {
|
|
128
|
-
if (
|
|
129
|
-
event.pointerType === 'touch' ||
|
|
130
|
-
activity.current.focused ||
|
|
131
|
-
activity.current.hovered ||
|
|
132
|
-
activity.current.suppressed
|
|
133
|
-
)
|
|
134
|
-
return
|
|
135
|
-
const panel = content.current
|
|
136
|
-
if (!panel) return
|
|
137
|
-
const a = trigger.getBoundingClientRect()
|
|
138
|
-
const b = panel.getBoundingClientRect()
|
|
139
|
-
// Keep a hoverable bridge across the placement gap, including magnified pointer movement.
|
|
140
|
-
const within =
|
|
141
|
-
event.clientX >= Math.min(a.left, b.left) &&
|
|
142
|
-
event.clientX <= Math.max(a.right, b.right) &&
|
|
143
|
-
event.clientY >= Math.min(a.top, b.top) &&
|
|
144
|
-
event.clientY <= Math.max(a.bottom, b.bottom)
|
|
145
|
-
if (within) clear()
|
|
146
|
-
else if (!timer.current) schedule(false, closeDelay)
|
|
147
|
-
}
|
|
148
|
-
trigger.ownerDocument.addEventListener('pointermove', move)
|
|
149
|
-
return () => trigger.ownerDocument.removeEventListener('pointermove', move)
|
|
150
|
-
}, [trigger, state.value, closeDelay, schedule])
|
|
151
83
|
return (
|
|
152
84
|
<TooltipContext.Provider
|
|
153
85
|
value={{
|
|
@@ -160,10 +92,7 @@ export function Tooltip({
|
|
|
160
92
|
enter,
|
|
161
93
|
leave,
|
|
162
94
|
dismiss,
|
|
163
|
-
touch
|
|
164
|
-
activity.current.touch = true
|
|
165
|
-
dismiss()
|
|
166
|
-
}
|
|
95
|
+
touch
|
|
167
96
|
}}
|
|
168
97
|
>
|
|
169
98
|
{children}
|
package/src/cn.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
+
export {
|
|
2
|
+
MenuGroup,
|
|
3
|
+
MenuLabel,
|
|
4
|
+
MenuSeparator,
|
|
5
|
+
MenuCheckboxItem,
|
|
6
|
+
MenuRadioGroup,
|
|
7
|
+
MenuRadioItem,
|
|
8
|
+
MenuSub,
|
|
9
|
+
MenuSubTrigger,
|
|
10
|
+
MenuSubContent
|
|
11
|
+
} from './Menu.js'
|
|
12
|
+
export type {
|
|
13
|
+
MenuGroupProps,
|
|
14
|
+
MenuLabelProps,
|
|
15
|
+
MenuSeparatorProps,
|
|
16
|
+
MenuCheckboxItemProps,
|
|
17
|
+
MenuRadioGroupProps,
|
|
18
|
+
MenuRadioItemProps,
|
|
19
|
+
MenuSubProps,
|
|
20
|
+
MenuSubTriggerProps,
|
|
21
|
+
MenuSubContentProps
|
|
22
|
+
} from './Menu.js'
|
|
23
|
+
export {
|
|
24
|
+
HoverCard,
|
|
25
|
+
HoverCardTrigger,
|
|
26
|
+
HoverCardContent,
|
|
27
|
+
HoverCardPortal,
|
|
28
|
+
useHoverCard
|
|
29
|
+
} from './HoverCard.js'
|
|
30
|
+
export type {
|
|
31
|
+
HoverCardProps,
|
|
32
|
+
HoverCardState,
|
|
33
|
+
HoverCardTriggerProps,
|
|
34
|
+
HoverCardContentProps,
|
|
35
|
+
HoverCardPortalProps,
|
|
36
|
+
HoverCardRenderProps
|
|
37
|
+
} from './HoverCard.js'
|
|
1
38
|
export {
|
|
2
39
|
Combobox,
|
|
3
40
|
ComboboxInput,
|
|
@@ -397,3 +434,38 @@ export type {
|
|
|
397
434
|
|
|
398
435
|
export { Toolbar } from './Toolbar.js'
|
|
399
436
|
export type { ToolbarProps, ToolbarOrientation } from './Toolbar.js'
|
|
437
|
+
|
|
438
|
+
export {
|
|
439
|
+
ContextMenu,
|
|
440
|
+
ContextMenuTrigger,
|
|
441
|
+
ContextMenuPortal,
|
|
442
|
+
ContextMenuContent,
|
|
443
|
+
ContextMenuItem,
|
|
444
|
+
ContextMenuGroup,
|
|
445
|
+
ContextMenuLabel,
|
|
446
|
+
ContextMenuSeparator,
|
|
447
|
+
ContextMenuCheckboxItem,
|
|
448
|
+
ContextMenuRadioGroup,
|
|
449
|
+
ContextMenuRadioItem,
|
|
450
|
+
ContextMenuSub,
|
|
451
|
+
ContextMenuSubTrigger,
|
|
452
|
+
ContextMenuSubContent,
|
|
453
|
+
useContextMenu
|
|
454
|
+
} from './ContextMenu.js'
|
|
455
|
+
export type {
|
|
456
|
+
ContextMenuProps,
|
|
457
|
+
ContextMenuTriggerProps,
|
|
458
|
+
ContextMenuPortalProps,
|
|
459
|
+
ContextMenuContentProps,
|
|
460
|
+
ContextMenuItemProps,
|
|
461
|
+
ContextMenuGroupProps,
|
|
462
|
+
ContextMenuLabelProps,
|
|
463
|
+
ContextMenuSeparatorProps,
|
|
464
|
+
ContextMenuCheckboxItemProps,
|
|
465
|
+
ContextMenuRadioGroupProps,
|
|
466
|
+
ContextMenuRadioItemProps,
|
|
467
|
+
ContextMenuSubProps,
|
|
468
|
+
ContextMenuSubTriggerProps,
|
|
469
|
+
ContextMenuSubContentProps,
|
|
470
|
+
ContextMenuState
|
|
471
|
+
} from './ContextMenu.js'
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useRef, type RefObject } from 'react'
|
|
4
|
+
|
|
5
|
+
/** Shared hover/focus timing, dismissal suppression and a bridge across the placement gap. */
|
|
6
|
+
export function useHoverSurface({
|
|
7
|
+
trigger,
|
|
8
|
+
content,
|
|
9
|
+
open,
|
|
10
|
+
request,
|
|
11
|
+
openDelay,
|
|
12
|
+
focusDelay = 0,
|
|
13
|
+
closeDelay
|
|
14
|
+
}: {
|
|
15
|
+
trigger: HTMLElement | null
|
|
16
|
+
content: RefObject<HTMLElement | null>
|
|
17
|
+
open: boolean
|
|
18
|
+
request: (open: boolean) => void
|
|
19
|
+
openDelay: number
|
|
20
|
+
focusDelay?: number
|
|
21
|
+
closeDelay: number
|
|
22
|
+
}) {
|
|
23
|
+
const timer = useRef<{ view: Window; id: number } | null>(null)
|
|
24
|
+
const activity = useRef({ focused: false, hovered: false, suppressed: false, touch: false })
|
|
25
|
+
const latest = useRef(request)
|
|
26
|
+
const clear = () => {
|
|
27
|
+
if (timer.current) timer.current.view.clearTimeout(timer.current.id)
|
|
28
|
+
timer.current = null
|
|
29
|
+
}
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
latest.current = request
|
|
32
|
+
})
|
|
33
|
+
useEffect(() => () => clear(), [])
|
|
34
|
+
const previousOpen = useRef(open)
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!open) {
|
|
37
|
+
clear()
|
|
38
|
+
if (previousOpen.current)
|
|
39
|
+
activity.current.suppressed = activity.current.focused || activity.current.hovered
|
|
40
|
+
}
|
|
41
|
+
previousOpen.current = open
|
|
42
|
+
}, [open])
|
|
43
|
+
const schedule = useCallback(
|
|
44
|
+
(next: boolean, delay: number) => {
|
|
45
|
+
clear()
|
|
46
|
+
const view = trigger?.ownerDocument.defaultView
|
|
47
|
+
if (!view) return
|
|
48
|
+
timer.current = {
|
|
49
|
+
view,
|
|
50
|
+
id: view.setTimeout(() => {
|
|
51
|
+
timer.current = null
|
|
52
|
+
latest.current(next)
|
|
53
|
+
}, delay)
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
[trigger]
|
|
57
|
+
)
|
|
58
|
+
const setOpen = (next: boolean) => {
|
|
59
|
+
clear()
|
|
60
|
+
activity.current.suppressed = !next
|
|
61
|
+
request(next)
|
|
62
|
+
}
|
|
63
|
+
const dismiss = () => setOpen(false)
|
|
64
|
+
const enter = (focus = false) => {
|
|
65
|
+
const wasFocused = activity.current.focused
|
|
66
|
+
if (focus && !wasFocused) activity.current.suppressed = false
|
|
67
|
+
if (focus) activity.current.focused = true
|
|
68
|
+
else activity.current.hovered = true
|
|
69
|
+
clear()
|
|
70
|
+
if (activity.current.suppressed || activity.current.touch || open || (focus && wasFocused))
|
|
71
|
+
return
|
|
72
|
+
if (focus && focusDelay === 0) request(true)
|
|
73
|
+
else schedule(true, focus ? focusDelay : openDelay)
|
|
74
|
+
}
|
|
75
|
+
const leave = (focus = false) => {
|
|
76
|
+
if (focus) {
|
|
77
|
+
activity.current.focused = false
|
|
78
|
+
activity.current.touch = false
|
|
79
|
+
} else activity.current.hovered = false
|
|
80
|
+
if (activity.current.focused || activity.current.hovered) return
|
|
81
|
+
activity.current.suppressed = false
|
|
82
|
+
schedule(false, closeDelay)
|
|
83
|
+
}
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (!trigger || !open) return
|
|
86
|
+
const move = (event: PointerEvent) => {
|
|
87
|
+
if (
|
|
88
|
+
event.pointerType === 'touch' ||
|
|
89
|
+
activity.current.focused ||
|
|
90
|
+
activity.current.hovered ||
|
|
91
|
+
activity.current.suppressed
|
|
92
|
+
)
|
|
93
|
+
return
|
|
94
|
+
const panel = content.current
|
|
95
|
+
if (!panel) return
|
|
96
|
+
const a = trigger.getBoundingClientRect(),
|
|
97
|
+
b = panel.getBoundingClientRect()
|
|
98
|
+
const within =
|
|
99
|
+
event.clientX >= Math.min(a.left, b.left) &&
|
|
100
|
+
event.clientX <= Math.max(a.right, b.right) &&
|
|
101
|
+
event.clientY >= Math.min(a.top, b.top) &&
|
|
102
|
+
event.clientY <= Math.max(a.bottom, b.bottom)
|
|
103
|
+
if (within) clear()
|
|
104
|
+
else if (!timer.current) schedule(false, closeDelay)
|
|
105
|
+
}
|
|
106
|
+
trigger.ownerDocument.addEventListener('pointermove', move)
|
|
107
|
+
return () => trigger.ownerDocument.removeEventListener('pointermove', move)
|
|
108
|
+
}, [trigger, content, open, closeDelay, schedule])
|
|
109
|
+
return {
|
|
110
|
+
setOpen,
|
|
111
|
+
enter,
|
|
112
|
+
leave,
|
|
113
|
+
dismiss,
|
|
114
|
+
touch: () => {
|
|
115
|
+
activity.current.touch = true
|
|
116
|
+
dismiss()
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -8,12 +8,14 @@ const layers = new WeakMap<Document, HTMLElement[]>()
|
|
|
8
8
|
|
|
9
9
|
export function usePopoverSurface<Node extends HTMLElement>(
|
|
10
10
|
nodeRef: RefObject<Node | null>,
|
|
11
|
-
trigger:
|
|
11
|
+
trigger: HTMLElement | null,
|
|
12
12
|
open: boolean,
|
|
13
13
|
close: (restore: boolean) => void,
|
|
14
14
|
restoreFocus: RefObject<boolean>,
|
|
15
15
|
options: {
|
|
16
|
+
anchorPoint?: { x: number; y: number }
|
|
16
17
|
initialFocusRef?: RefObject<HTMLElement | null>
|
|
18
|
+
onFocusOutside?: () => void
|
|
17
19
|
side: PopoverSide
|
|
18
20
|
align: PopoverAlign
|
|
19
21
|
sideOffset: number
|
|
@@ -27,7 +29,7 @@ export function usePopoverSurface<Node extends HTMLElement>(
|
|
|
27
29
|
useLayoutEffect(() => {
|
|
28
30
|
latest.current = { ...options, open, close }
|
|
29
31
|
})
|
|
30
|
-
const { side, align, sideOffset, collisionPadding } = options
|
|
32
|
+
const { side, align, sideOffset, collisionPadding, anchorPoint } = options
|
|
31
33
|
|
|
32
34
|
useLayoutEffect(() => {
|
|
33
35
|
const node = nodeRef.current
|
|
@@ -65,7 +67,7 @@ export function usePopoverSurface<Node extends HTMLElement>(
|
|
|
65
67
|
const shouldRestore = () => restoreFocus.current
|
|
66
68
|
const place = () => {
|
|
67
69
|
if (!node.isConnected || !node.matches(':popover-open')) return
|
|
68
|
-
const { side, align, sideOffset, collisionPadding } = latest.current
|
|
70
|
+
const { side, align, sideOffset, collisionPadding, anchorPoint } = latest.current
|
|
69
71
|
const viewport = win.visualViewport
|
|
70
72
|
const view = {
|
|
71
73
|
top: viewport?.offsetTop ?? 0,
|
|
@@ -83,7 +85,16 @@ export function usePopoverSurface<Node extends HTMLElement>(
|
|
|
83
85
|
)
|
|
84
86
|
const css = win.getComputedStyle(node)
|
|
85
87
|
const next = positionPopover({
|
|
86
|
-
anchor:
|
|
88
|
+
anchor: anchorPoint
|
|
89
|
+
? {
|
|
90
|
+
top: anchorPoint.y,
|
|
91
|
+
bottom: anchorPoint.y,
|
|
92
|
+
left: anchorPoint.x,
|
|
93
|
+
right: anchorPoint.x,
|
|
94
|
+
width: 0,
|
|
95
|
+
height: 0
|
|
96
|
+
}
|
|
97
|
+
: trigger.getBoundingClientRect(),
|
|
87
98
|
width: node.offsetWidth,
|
|
88
99
|
height:
|
|
89
100
|
node.scrollHeight + parseFloat(css.borderTopWidth) + parseFloat(css.borderBottomWidth),
|
|
@@ -136,7 +147,11 @@ export function usePopoverSurface<Node extends HTMLElement>(
|
|
|
136
147
|
return
|
|
137
148
|
if (event.type === 'focusin' && pointerDismissed) return
|
|
138
149
|
if (event.type === 'pointerdown') pointerDismissed = !inside(event.target)
|
|
139
|
-
if (!inside(event.target))
|
|
150
|
+
if (!inside(event.target)) {
|
|
151
|
+
if (event.type === 'focusin' && latest.current.onFocusOutside)
|
|
152
|
+
latest.current.onFocusOutside()
|
|
153
|
+
else latest.current.close(false)
|
|
154
|
+
}
|
|
140
155
|
}
|
|
141
156
|
const escape = (event: KeyboardEvent) => {
|
|
142
157
|
if (
|
|
@@ -171,6 +186,15 @@ export function usePopoverSurface<Node extends HTMLElement>(
|
|
|
171
186
|
observer.observe(trigger)
|
|
172
187
|
observer.observe(node)
|
|
173
188
|
mutations.observe(node, { childList: true, subtree: true, characterData: true })
|
|
189
|
+
// Inline anchors do not report text-size changes through ResizeObserver.
|
|
190
|
+
mutations.observe(trigger, {
|
|
191
|
+
childList: true,
|
|
192
|
+
subtree: true,
|
|
193
|
+
characterData: true,
|
|
194
|
+
attributes: true,
|
|
195
|
+
attributeFilter: ['class', 'style', 'dir']
|
|
196
|
+
})
|
|
197
|
+
|
|
174
198
|
node.addEventListener('toggle', toggle)
|
|
175
199
|
doc.addEventListener('pointerdown', outside, true)
|
|
176
200
|
doc.addEventListener('pointerup', pointerEnd, true)
|
|
@@ -203,5 +227,8 @@ export function usePopoverSurface<Node extends HTMLElement>(
|
|
|
203
227
|
reposition.current = () => {}
|
|
204
228
|
}
|
|
205
229
|
}, [nodeRef, trigger, open, generation, restoreFocus])
|
|
206
|
-
useLayoutEffect(
|
|
230
|
+
useLayoutEffect(
|
|
231
|
+
() => reposition.current(),
|
|
232
|
+
[side, align, sideOffset, collisionPadding, anchorPoint?.x, anchorPoint?.y]
|
|
233
|
+
)
|
|
207
234
|
}
|
package/src/themes/components.ts
CHANGED
|
@@ -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
|
+
contextMenu: { width: 'css' },
|
|
6
|
+
hoverCard: { width: 'css' },
|
|
5
7
|
button: {
|
|
6
8
|
radius: 'css',
|
|
7
9
|
radiusPressed: 'css',
|