@cognitap/radiant 0.0.14 → 0.0.15
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 -1
- package/dist/library/Popover.d.ts +44 -0
- package/dist/library/Popover.d.ts.map +1 -0
- package/dist/library/Popover.js +94 -0
- package/dist/library/Popover.js.map +1 -0
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +22 -2
- 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/internal/popover.d.ts +36 -0
- package/dist/library/internal/popover.d.ts.map +1 -0
- package/dist/library/internal/popover.js +45 -0
- package/dist/library/internal/popover.js.map +1 -0
- package/dist/library/react/usePopoverSurface.d.ts +10 -0
- package/dist/library/react/usePopoverSurface.d.ts.map +1 -0
- package/dist/library/react/usePopoverSurface.js +193 -0
- package/dist/library/react/usePopoverSurface.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +18 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +18 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +6 -3
- package/docs/theming.md +1 -1
- package/package.json +1 -1
- package/src/Popover.tsx +201 -0
- package/src/cn.ts +22 -2
- package/src/index.ts +19 -0
- package/src/internal/popover.ts +66 -0
- package/src/react/usePopoverSurface.ts +207 -0
- package/src/themes/components.ts +18 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useLayoutEffect, useRef, useState } from 'react';
|
|
3
|
+
import { positionPopover } from '../internal/popover.js';
|
|
4
|
+
// Each document owns its layers; imports never inspect a browser global.
|
|
5
|
+
const layers = new WeakMap();
|
|
6
|
+
export function usePopoverSurface(nodeRef, trigger, open, close, restoreFocus, options) {
|
|
7
|
+
const latest = useRef({ ...options, open, close });
|
|
8
|
+
const reposition = useRef(() => { });
|
|
9
|
+
const ancestorClosing = useRef(false);
|
|
10
|
+
const [generation, setGeneration] = useState(0);
|
|
11
|
+
useLayoutEffect(() => {
|
|
12
|
+
latest.current = { ...options, open, close };
|
|
13
|
+
});
|
|
14
|
+
const { side, align, sideOffset, collisionPadding } = options;
|
|
15
|
+
useLayoutEffect(() => {
|
|
16
|
+
const node = nodeRef.current;
|
|
17
|
+
if (!node || !trigger)
|
|
18
|
+
return;
|
|
19
|
+
const ancestors = [];
|
|
20
|
+
for (let parent = trigger.parentElement; parent; parent = parent.parentElement) {
|
|
21
|
+
if (parent.matches('dialog, [popover]'))
|
|
22
|
+
ancestors.push(parent);
|
|
23
|
+
}
|
|
24
|
+
const reconcile = () => {
|
|
25
|
+
if (!latest.current.open)
|
|
26
|
+
return;
|
|
27
|
+
const visible = trigger.checkVisibility();
|
|
28
|
+
if (!visible && node.matches(':popover-open')) {
|
|
29
|
+
ancestorClosing.current = true;
|
|
30
|
+
latest.current.close(false);
|
|
31
|
+
node.hidePopover();
|
|
32
|
+
setGeneration(value => value + 1);
|
|
33
|
+
}
|
|
34
|
+
else if (visible && !node.matches(':popover-open'))
|
|
35
|
+
setGeneration(value => value + 1);
|
|
36
|
+
};
|
|
37
|
+
const observer = new node.ownerDocument.defaultView.MutationObserver(reconcile);
|
|
38
|
+
for (const ancestor of ancestors) {
|
|
39
|
+
observer.observe(ancestor, { attributes: true, attributeFilter: ['open', 'data-state'] });
|
|
40
|
+
ancestor.addEventListener('toggle', reconcile);
|
|
41
|
+
}
|
|
42
|
+
return () => {
|
|
43
|
+
observer.disconnect();
|
|
44
|
+
for (const ancestor of ancestors)
|
|
45
|
+
ancestor.removeEventListener('toggle', reconcile);
|
|
46
|
+
};
|
|
47
|
+
}, [nodeRef, trigger]);
|
|
48
|
+
useLayoutEffect(() => {
|
|
49
|
+
const node = nodeRef.current;
|
|
50
|
+
if (!node || !trigger || !open || !trigger.checkVisibility() || generation < 0)
|
|
51
|
+
return;
|
|
52
|
+
const doc = node.ownerDocument;
|
|
53
|
+
const win = doc.defaultView;
|
|
54
|
+
const shouldRestore = () => restoreFocus.current;
|
|
55
|
+
const place = () => {
|
|
56
|
+
if (!node.isConnected || !node.matches(':popover-open'))
|
|
57
|
+
return;
|
|
58
|
+
const { side, align, sideOffset, collisionPadding } = latest.current;
|
|
59
|
+
const viewport = win.visualViewport;
|
|
60
|
+
const view = {
|
|
61
|
+
top: viewport?.offsetTop ?? 0,
|
|
62
|
+
left: viewport?.offsetLeft ?? 0,
|
|
63
|
+
width: viewport?.width ?? win.innerWidth,
|
|
64
|
+
height: viewport?.height ?? win.innerHeight
|
|
65
|
+
};
|
|
66
|
+
node.style.setProperty('--_rad-popover-max-width', `${Math.max(0, view.width - 2 * collisionPadding)}px`);
|
|
67
|
+
node.style.setProperty('--_rad-popover-max-height', `${Math.max(0, view.height - 2 * collisionPadding)}px`);
|
|
68
|
+
const css = win.getComputedStyle(node);
|
|
69
|
+
const next = positionPopover({
|
|
70
|
+
anchor: trigger.getBoundingClientRect(),
|
|
71
|
+
width: node.offsetWidth,
|
|
72
|
+
height: node.scrollHeight + parseFloat(css.borderTopWidth) + parseFloat(css.borderBottomWidth),
|
|
73
|
+
viewport: view,
|
|
74
|
+
side,
|
|
75
|
+
align,
|
|
76
|
+
offset: sideOffset,
|
|
77
|
+
padding: collisionPadding,
|
|
78
|
+
rtl: win.getComputedStyle(trigger).direction === 'rtl'
|
|
79
|
+
});
|
|
80
|
+
for (const key of ['left', 'top', 'maxWidth', 'maxHeight']) {
|
|
81
|
+
const property = key === 'maxWidth' ? 'max-width' : key === 'maxHeight' ? 'max-height' : key;
|
|
82
|
+
node.style.setProperty(`--_rad-popover-${property}`, `${next[key]}px`);
|
|
83
|
+
}
|
|
84
|
+
node.dataset.side = next.side;
|
|
85
|
+
node.dataset.align = next.align;
|
|
86
|
+
};
|
|
87
|
+
// A manual native popover keeps controlled owners authoritative and avoids auto-stack reopen loops.
|
|
88
|
+
// The source preserves the browser's logical Tab order without assigning content semantics.
|
|
89
|
+
node.showPopover({ source: trigger });
|
|
90
|
+
ancestorClosing.current = false;
|
|
91
|
+
reposition.current = place;
|
|
92
|
+
place();
|
|
93
|
+
const target = latest.current.initialFocusRef?.current;
|
|
94
|
+
if (target && node.contains(target))
|
|
95
|
+
target.focus({ preventScroll: true });
|
|
96
|
+
const stack = layers.get(doc) ?? [];
|
|
97
|
+
layers.set(doc, stack);
|
|
98
|
+
const descendant = stack.findIndex(layer => node.contains(layer));
|
|
99
|
+
if (descendant < 0)
|
|
100
|
+
stack.push(node);
|
|
101
|
+
else
|
|
102
|
+
stack.splice(descendant, 0, node);
|
|
103
|
+
let frame = 0;
|
|
104
|
+
const schedule = () => {
|
|
105
|
+
if (!frame)
|
|
106
|
+
frame = win.requestAnimationFrame(() => {
|
|
107
|
+
frame = 0;
|
|
108
|
+
place();
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
const inside = (target) => target instanceof win.Node && (node.contains(target) || trigger.contains(target));
|
|
112
|
+
let pointerDismissed = false;
|
|
113
|
+
const pointerEnd = () => {
|
|
114
|
+
pointerDismissed = false;
|
|
115
|
+
};
|
|
116
|
+
const outside = (event) => {
|
|
117
|
+
if (event.type === 'focusin' &&
|
|
118
|
+
(event.target === doc.body || event.target === doc.documentElement))
|
|
119
|
+
return;
|
|
120
|
+
if (event.type === 'focusin' && pointerDismissed)
|
|
121
|
+
return;
|
|
122
|
+
if (event.type === 'pointerdown')
|
|
123
|
+
pointerDismissed = !inside(event.target);
|
|
124
|
+
if (!inside(event.target))
|
|
125
|
+
latest.current.close(false);
|
|
126
|
+
};
|
|
127
|
+
const escape = (event) => {
|
|
128
|
+
if (event.key !== 'Escape' ||
|
|
129
|
+
event.defaultPrevented ||
|
|
130
|
+
stack.filter(layer => layer.matches(':popover-open')).at(-1) !== node)
|
|
131
|
+
return;
|
|
132
|
+
const modal = Array.from(doc.querySelectorAll('dialog:modal')).at(-1);
|
|
133
|
+
if (modal && !modal.contains(node))
|
|
134
|
+
return;
|
|
135
|
+
const nested = event.target instanceof win.Element ? event.target.closest('[popover]:popover-open') : null;
|
|
136
|
+
if (nested &&
|
|
137
|
+
nested !== node &&
|
|
138
|
+
node.contains(nested) &&
|
|
139
|
+
nested.getAttribute('popover') !== 'manual')
|
|
140
|
+
return;
|
|
141
|
+
event.preventDefault();
|
|
142
|
+
event.stopPropagation();
|
|
143
|
+
latest.current.close(true);
|
|
144
|
+
};
|
|
145
|
+
const toggle = () => {
|
|
146
|
+
if (ancestorClosing.current || node.matches(':popover-open') || !latest.current.open)
|
|
147
|
+
return;
|
|
148
|
+
latest.current.close(true);
|
|
149
|
+
// Native hide is an observation; a controlled owner may refuse the request.
|
|
150
|
+
setGeneration(value => value + 1);
|
|
151
|
+
};
|
|
152
|
+
const observer = new win.ResizeObserver(schedule);
|
|
153
|
+
const mutations = new win.MutationObserver(schedule);
|
|
154
|
+
observer.observe(trigger);
|
|
155
|
+
observer.observe(node);
|
|
156
|
+
mutations.observe(node, { childList: true, subtree: true, characterData: true });
|
|
157
|
+
node.addEventListener('toggle', toggle);
|
|
158
|
+
doc.addEventListener('pointerdown', outside, true);
|
|
159
|
+
doc.addEventListener('pointerup', pointerEnd, true);
|
|
160
|
+
doc.addEventListener('pointercancel', pointerEnd, true);
|
|
161
|
+
doc.addEventListener('focusin', outside);
|
|
162
|
+
doc.addEventListener('keydown', escape);
|
|
163
|
+
doc.addEventListener('scroll', schedule, true);
|
|
164
|
+
win.addEventListener('resize', schedule);
|
|
165
|
+
win.visualViewport?.addEventListener('resize', schedule);
|
|
166
|
+
win.visualViewport?.addEventListener('scroll', schedule);
|
|
167
|
+
return () => {
|
|
168
|
+
observer.disconnect();
|
|
169
|
+
mutations.disconnect();
|
|
170
|
+
win.cancelAnimationFrame(frame);
|
|
171
|
+
node.removeEventListener('toggle', toggle);
|
|
172
|
+
doc.removeEventListener('pointerdown', outside, true);
|
|
173
|
+
doc.removeEventListener('pointerup', pointerEnd, true);
|
|
174
|
+
doc.removeEventListener('pointercancel', pointerEnd, true);
|
|
175
|
+
doc.removeEventListener('focusin', outside);
|
|
176
|
+
doc.removeEventListener('keydown', escape);
|
|
177
|
+
doc.removeEventListener('scroll', schedule, true);
|
|
178
|
+
win.removeEventListener('resize', schedule);
|
|
179
|
+
win.visualViewport?.removeEventListener('resize', schedule);
|
|
180
|
+
win.visualViewport?.removeEventListener('scroll', schedule);
|
|
181
|
+
stack.splice(stack.indexOf(node), 1);
|
|
182
|
+
if (!stack.length)
|
|
183
|
+
layers.delete(doc);
|
|
184
|
+
if (shouldRestore() && node.contains(doc.activeElement) && trigger.isConnected)
|
|
185
|
+
trigger.focus({ preventScroll: true });
|
|
186
|
+
if (node.matches(':popover-open'))
|
|
187
|
+
node.hidePopover();
|
|
188
|
+
reposition.current = () => { };
|
|
189
|
+
};
|
|
190
|
+
}, [nodeRef, trigger, open, generation, restoreFocus]);
|
|
191
|
+
useLayoutEffect(() => reposition.current(), [side, align, sideOffset, collisionPadding]);
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=usePopoverSurface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePopoverSurface.js","sourceRoot":"","sources":["../../../src/react/usePopoverSurface.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,QAAQ,EAAkB,MAAM,OAAO,CAAA;AACzE,OAAO,EAAE,eAAe,EAAuC,MAAM,wBAAwB,CAAA;AAE7F,yEAAyE;AACzE,MAAM,MAAM,GAAG,IAAI,OAAO,EAA2B,CAAA;AAErD,MAAM,UAAU,iBAAiB,CAC/B,OAAyC,EACzC,OAAiC,EACjC,IAAa,EACb,KAAiC,EACjC,YAAgC,EAChC,OAMC;IAED,MAAM,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAA;IAClD,M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,EAAE,CAAC,CAAA;YACxC,IAAI,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC;gBAAE,IAAI,CAAC,WAAW,EAAE,CAAA;YACrD,UAAU,CAAC,OAAO,GAAG,GAAG,EAAE,GAAE,CAAC,CAAA;QAC/B,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,YAAY,CAAC,CAAC,CAAA;IACtD,eAAe,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,gBAAgB,CAAC,CAAC,CAAA;AAC1F,CAAC","sourcesContent":["'use client'\n\nimport { useLayoutEffect, useRef, useState, type RefObject } from 'react'\nimport { positionPopover, type PopoverAlign, type PopoverSide } from '../internal/popover.js'\n\n// Each document owns its layers; imports never inspect a browser global.\nconst layers = new WeakMap<Document, HTMLElement[]>()\n\nexport function usePopoverSurface(\n nodeRef: RefObject<HTMLDivElement | null>,\n trigger: HTMLButtonElement | null,\n open: boolean,\n close: (restore: boolean) => void,\n restoreFocus: RefObject<boolean>,\n options: {\n initialFocusRef?: RefObject<HTMLElement | null>\n side: PopoverSide\n align: PopoverAlign\n sideOffset: number\n collisionPadding: number\n }\n) {\n const latest = useRef({ ...options, open, close })\n const reposition = useRef(() => {})\n const ancestorClosing = useRef(false)\n const [generation, setGeneration] = useState(0)\n useLayoutEffect(() => {\n latest.current = { ...options, open, close }\n })\n const { side, align, sideOffset, collisionPadding } = options\n\n useLayoutEffect(() => {\n const node = nodeRef.current\n if (!node || !trigger) return\n const ancestors: HTMLElement[] = []\n for (let parent = trigger.parentElement; parent; parent = parent.parentElement) {\n if (parent.matches('dialog, [popover]')) ancestors.push(parent)\n }\n const reconcile = () => {\n if (!latest.current.open) return\n const visible = trigger.checkVisibility()\n if (!visible && node.matches(':popover-open')) {\n ancestorClosing.current = true\n latest.current.close(false)\n node.hidePopover()\n setGeneration(value => value + 1)\n } else if (visible && !node.matches(':popover-open')) setGeneration(value => value + 1)\n }\n const observer = new node.ownerDocument.defaultView!.MutationObserver(reconcile)\n for (const ancestor of ancestors) {\n observer.observe(ancestor, { attributes: true, attributeFilter: ['open', 'data-state'] })\n ancestor.addEventListener('toggle', reconcile)\n }\n return () => {\n observer.disconnect()\n for (const ancestor of ancestors) ancestor.removeEventListener('toggle', reconcile)\n }\n }, [nodeRef, trigger])\n\n useLayoutEffect(() => {\n const node = nodeRef.current\n if (!node || !trigger || !open || !trigger.checkVisibility() || generation < 0) return\n const doc = node.ownerDocument\n const win = doc.defaultView!\n const shouldRestore = () => restoreFocus.current\n const place = () => {\n if (!node.isConnected || !node.matches(':popover-open')) return\n const { side, align, sideOffset, collisionPadding } = latest.current\n const viewport = win.visualViewport\n const view = {\n top: viewport?.offsetTop ?? 0,\n left: viewport?.offsetLeft ?? 0,\n width: viewport?.width ?? win.innerWidth,\n height: viewport?.height ?? win.innerHeight\n }\n node.style.setProperty(\n '--_rad-popover-max-width',\n `${Math.max(0, view.width - 2 * collisionPadding)}px`\n )\n node.style.setProperty(\n '--_rad-popover-max-height',\n `${Math.max(0, view.height - 2 * collisionPadding)}px`\n )\n const css = win.getComputedStyle(node)\n const next = positionPopover({\n anchor: trigger.getBoundingClientRect(),\n width: node.offsetWidth,\n height:\n node.scrollHeight + parseFloat(css.borderTopWidth) + parseFloat(css.borderBottomWidth),\n viewport: view,\n side,\n align,\n offset: sideOffset,\n padding: collisionPadding,\n rtl: win.getComputedStyle(trigger).direction === 'rtl'\n })\n for (const key of ['left', 'top', 'maxWidth', 'maxHeight'] as const) {\n const property = key === 'maxWidth' ? 'max-width' : key === 'maxHeight' ? 'max-height' : key\n node.style.setProperty(`--_rad-popover-${property}`, `${next[key]}px`)\n }\n node.dataset.side = next.side\n node.dataset.align = next.align\n }\n // A manual native popover keeps controlled owners authoritative and avoids auto-stack reopen loops.\n // The source preserves the browser's logical Tab order without assigning content semantics.\n node.showPopover({ source: trigger })\n ancestorClosing.current = false\n reposition.current = place\n place()\n const target = latest.current.initialFocusRef?.current\n if (target && node.contains(target)) target.focus({ preventScroll: true })\n const stack = layers.get(doc) ?? []\n layers.set(doc, stack)\n const descendant = stack.findIndex(layer => node.contains(layer))\n if (descendant < 0) stack.push(node)\n else stack.splice(descendant, 0, node)\n let frame = 0\n const schedule = () => {\n if (!frame)\n frame = win.requestAnimationFrame(() => {\n frame = 0\n place()\n })\n }\n const inside = (target: EventTarget | null) =>\n target instanceof win.Node && (node.contains(target) || trigger.contains(target))\n let pointerDismissed = false\n const pointerEnd = () => {\n pointerDismissed = false\n }\n const outside = (event: PointerEvent | FocusEvent) => {\n if (\n event.type === 'focusin' &&\n (event.target === doc.body || event.target === doc.documentElement)\n )\n return\n if (event.type === 'focusin' && pointerDismissed) return\n if (event.type === 'pointerdown') pointerDismissed = !inside(event.target)\n if (!inside(event.target)) latest.current.close(false)\n }\n const escape = (event: KeyboardEvent) => {\n if (\n event.key !== 'Escape' ||\n event.defaultPrevented ||\n stack.filter(layer => layer.matches(':popover-open')).at(-1) !== node\n )\n return\n const modal = Array.from(doc.querySelectorAll('dialog:modal')).at(-1)\n if (modal && !modal.contains(node)) return\n const nested =\n event.target instanceof win.Element ? event.target.closest('[popover]:popover-open') : null\n if (\n nested &&\n nested !== node &&\n node.contains(nested) &&\n nested.getAttribute('popover') !== 'manual'\n )\n return\n event.preventDefault()\n event.stopPropagation()\n latest.current.close(true)\n }\n const toggle = () => {\n if (ancestorClosing.current || node.matches(':popover-open') || !latest.current.open) return\n latest.current.close(true)\n // Native hide is an observation; a controlled owner may refuse the request.\n setGeneration(value => value + 1)\n }\n const observer = new win.ResizeObserver(schedule)\n const mutations = new win.MutationObserver(schedule)\n observer.observe(trigger)\n observer.observe(node)\n mutations.observe(node, { childList: true, subtree: true, characterData: true })\n node.addEventListener('toggle', toggle)\n doc.addEventListener('pointerdown', outside, true)\n doc.addEventListener('pointerup', pointerEnd, true)\n doc.addEventListener('pointercancel', pointerEnd, true)\n doc.addEventListener('focusin', outside)\n doc.addEventListener('keydown', escape)\n doc.addEventListener('scroll', schedule, true)\n win.addEventListener('resize', schedule)\n win.visualViewport?.addEventListener('resize', schedule)\n win.visualViewport?.addEventListener('scroll', schedule)\n return () => {\n observer.disconnect()\n mutations.disconnect()\n win.cancelAnimationFrame(frame)\n node.removeEventListener('toggle', toggle)\n doc.removeEventListener('pointerdown', outside, true)\n doc.removeEventListener('pointerup', pointerEnd, true)\n doc.removeEventListener('pointercancel', pointerEnd, true)\n doc.removeEventListener('focusin', outside)\n doc.removeEventListener('keydown', escape)\n doc.removeEventListener('scroll', schedule, true)\n win.removeEventListener('resize', schedule)\n win.visualViewport?.removeEventListener('resize', schedule)\n win.visualViewport?.removeEventListener('scroll', schedule)\n stack.splice(stack.indexOf(node), 1)\n if (!stack.length) layers.delete(doc)\n if (shouldRestore() && node.contains(doc.activeElement) && trigger.isConnected)\n trigger.focus({ preventScroll: true })\n if (node.matches(':popover-open')) node.hidePopover()\n reposition.current = () => {}\n }\n }, [nodeRef, trigger, open, generation, restoreFocus])\n useLayoutEffect(() => reposition.current(), [side, align, sideOffset, collisionPadding])\n}\n"]}
|