@rozie-ui/popover-react 0.1.0
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/LICENSE +21 -0
- package/README.md +92 -0
- package/dist/Popover.css +28 -0
- package/dist/index.cjs +307 -0
- package/dist/index.d.cts +66 -0
- package/dist/index.d.mts +66 -0
- package/dist/index.mjs +302 -0
- package/package.json +82 -0
- package/rozie-manifest.json +112 -0
- package/src/Popover.css +28 -0
- package/src/Popover.d.ts +62 -0
- package/src/Popover.tsx +349 -0
- package/src/index.ts +5 -0
- package/src/internal/middleware.ts +54 -0
- package/src/themes/base.css +24 -0
- package/src/themes/bootstrap.css +19 -0
- package/src/themes/material.css +19 -0
- package/src/themes/shadcn.css +23 -0
package/src/Popover.tsx
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef } from 'react';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import { clsx, rozieAttr, useControllableState, useOutsideClick } from '@rozie/runtime-react';
|
|
4
|
+
import './Popover.css';
|
|
5
|
+
// The `offset` AND `arrow` middleware factories are ALIASED on import: both are
|
|
6
|
+
// ALSO author PROP names (`offset`, `arrow`). A bare `offset`/`arrow` shorthand in
|
|
7
|
+
// the buildMiddleware factories object resolves to the PROP — on Vue/Svelte the
|
|
8
|
+
// destructured prop local shadows the import, and on Angular the emitter rewrites
|
|
9
|
+
// the bare shorthand to the prop signal (`offset: this.offset()`, a number) instead
|
|
10
|
+
// of the middleware function (TS2322). Aliasing both severs the import↔prop clash.
|
|
11
|
+
// (The Cropper import-name==component-name class, applied to imports vs PROP names —
|
|
12
|
+
// two collisions, not one.) computePosition/autoUpdate/flip/shift carry no clash.
|
|
13
|
+
import { computePosition, autoUpdate, offset as offsetMiddleware, flip, shift, arrow as arrowMiddleware } from '@floating-ui/dom';
|
|
14
|
+
import { buildMiddleware } from './internal/middleware';
|
|
15
|
+
|
|
16
|
+
// null-lets so the bundled-leaf typeNeutralize pass annotates them `any`:
|
|
17
|
+
// anchorNode/floatingNode/arrowNode hold the resolved ref ELEMENTS (read ONLY in
|
|
18
|
+
// $onMount/handlers, ROZ123). They are deliberately named DIFFERENTLY from the
|
|
19
|
+
// `ref="anchorEl"` / `ref="floatingEl"` / `ref="arrowEl"` template ref names: the
|
|
20
|
+
// React/Svelte emitters declare a `const anchorEl = useRef(...)` for the ref, and a
|
|
21
|
+
// top-level `let anchorEl` hoisted to its own `useRef` would REDECLARE it (TS2451 —
|
|
22
|
+
// the local-name==ref-name self-shadow class, here in its `let X = null; X = $refs.X`
|
|
23
|
+
// variant, which deconflictRefShadows does NOT auto-rewrite since it only fires on the
|
|
24
|
+
// `const X = $refs.X` init shape).
|
|
25
|
+
// stopAutoUpdate is the autoUpdate teardown handle — a TOP-LEVEL `let` so the Solid
|
|
26
|
+
// onMount→onCleanup split (teardown is a separate closure) can still see it.
|
|
27
|
+
// lastFocusedEl (phase 72-06b) holds whatever had DOM focus at the moment a
|
|
28
|
+
// `trigger="click"` popover opened (natively the clicked trigger element itself,
|
|
29
|
+
// since a mousedown focuses a native `<button>` before its `click` fires) —
|
|
30
|
+
// restored on dismissal so Escape/click-outside don't drop focus to `<body>`.
|
|
31
|
+
// Same null-let convention as the others: read/written only in handlers, `any`
|
|
32
|
+
// via typeNeutralize.
|
|
33
|
+
|
|
34
|
+
interface AnchorCtx { open: any; toggle: any; show: any; hide: any; }
|
|
35
|
+
|
|
36
|
+
interface PopoverProps {
|
|
37
|
+
/**
|
|
38
|
+
* Whether the floating content is open. The sole `model: true` prop — two-way bind it (`r-model:open` / `v-model:open` / `bind:open` / `[(open)]`) and Popover writes the new state back whenever the trigger or a dismissal toggles it. Left unbound it falls back to an uncontrolled default.
|
|
39
|
+
*/
|
|
40
|
+
open?: boolean;
|
|
41
|
+
defaultOpen?: boolean;
|
|
42
|
+
onOpenChange?: (open: boolean) => void;
|
|
43
|
+
/**
|
|
44
|
+
* Floating UI placement of the content relative to the anchor — one of `top`/`right`/`bottom`/`left`, each optionally suffixed `-start`/`-end` (e.g. `bottom-start`). With `disableFlip` off, the content may flip to the opposite side when it would overflow the viewport. Reconciled at runtime.
|
|
45
|
+
*/
|
|
46
|
+
placement?: string;
|
|
47
|
+
/**
|
|
48
|
+
* How the anchor opens the content: `'click'` toggles on click, `'hover'` opens on pointer-enter and closes on pointer-leave (tooltip-style), `'focus'` opens on focus and closes on blur. Drives both the gesture handlers and the ARIA role (`'hover'`/`'focus'` → tooltip, `'click'` → popover dialog).
|
|
49
|
+
*/
|
|
50
|
+
trigger?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Distance in pixels between the anchor and the floating content (the Floating UI `offset` middleware). Reconciled at runtime.
|
|
53
|
+
*/
|
|
54
|
+
offset?: number;
|
|
55
|
+
/**
|
|
56
|
+
* Disable the Floating UI `flip` middleware. By default the content flips to the opposite side of the anchor when it would overflow the viewport; set this to keep it pinned to `placement` regardless.
|
|
57
|
+
*/
|
|
58
|
+
disableFlip?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Disable the Floating UI `shift` middleware. By default the content shifts along its axis to stay within the viewport; set this to keep it strictly aligned to the anchor.
|
|
61
|
+
*/
|
|
62
|
+
disableShift?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Opt in to a positioned arrow element. When set, Popover renders an arrow `<div>` and runs the Floating UI `arrow` middleware against it so it points at the anchor. Style it via the `--rozie-popover-*` arrow CSS custom properties.
|
|
65
|
+
*/
|
|
66
|
+
arrow?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Disable the control entirely: the trigger no longer opens the content and any open content is suppressed.
|
|
69
|
+
*/
|
|
70
|
+
disabled?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Opt in to modal dialog semantics for a `click` popover. **Off by default:** a click popover is a non-modal, click-outside-dismissable layer, so its panel is rendered role-neutral (the slot content owns its own ARIA role — e.g. a `role="menu"`) and carries NO `aria-modal`. Set `modal` for a genuinely modal dialog popover: the panel then gets `role="dialog"` + `aria-modal="true"`. **Note:** Popover ships no focus trap (it stays a minimal headless primitive); if you set `modal`, provide your own focus containment so the `aria-modal` claim holds. Ignored for `hover`/`focus` triggers (always tooltip-flavored).
|
|
73
|
+
*/
|
|
74
|
+
modal?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Floating UI positioning strategy — 'absolute' (default) or 'fixed'. Use 'fixed' to escape a scrollable/overflow-clipping ancestor (e.g. a sticky table header). Reconciled at runtime.
|
|
77
|
+
*/
|
|
78
|
+
strategy?: string;
|
|
79
|
+
onChange?: (...args: any[]) => void;
|
|
80
|
+
renderAnchor?: (ctx: AnchorCtx) => ReactNode;
|
|
81
|
+
children?: ReactNode;
|
|
82
|
+
slots?: Record<string, () => import('react').ReactNode>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface PopoverHandle {
|
|
86
|
+
show: (...args: any[]) => any;
|
|
87
|
+
hide: (...args: any[]) => any;
|
|
88
|
+
toggle: (...args: any[]) => any;
|
|
89
|
+
reposition: (...args: any[]) => any;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const Popover = forwardRef<PopoverHandle, PopoverProps>(function Popover(_props: PopoverProps, ref): JSX.Element {
|
|
93
|
+
const props: Omit<PopoverProps, 'placement' | 'trigger' | 'offset' | 'disableFlip' | 'disableShift' | 'arrow' | 'disabled' | 'modal' | 'strategy'> & { placement: string; trigger: string; offset: number; disableFlip: boolean; disableShift: boolean; arrow: boolean; disabled: boolean; modal: boolean; strategy: string } = {
|
|
94
|
+
..._props,
|
|
95
|
+
placement: _props.placement ?? 'bottom',
|
|
96
|
+
trigger: _props.trigger ?? 'click',
|
|
97
|
+
offset: _props.offset ?? 8,
|
|
98
|
+
disableFlip: _props.disableFlip ?? false,
|
|
99
|
+
disableShift: _props.disableShift ?? false,
|
|
100
|
+
arrow: _props.arrow ?? false,
|
|
101
|
+
disabled: _props.disabled ?? false,
|
|
102
|
+
modal: _props.modal ?? false,
|
|
103
|
+
strategy: _props.strategy ?? 'absolute',
|
|
104
|
+
};
|
|
105
|
+
const attrs: Record<string, unknown> = (() => {
|
|
106
|
+
const { open, placement, trigger, offset, disableFlip, disableShift, arrow, disabled, modal, strategy, defaultValue, onOpenChange, defaultOpen, ...rest } = _props as PopoverProps & Record<string, unknown>;
|
|
107
|
+
void open; void placement; void trigger; void offset; void disableFlip; void disableShift; void arrow; void disabled; void modal; void strategy; void defaultValue; void onOpenChange; void defaultOpen;
|
|
108
|
+
return rest;
|
|
109
|
+
})();
|
|
110
|
+
const anchorNode = useRef<any>(null);
|
|
111
|
+
const floatingNode = useRef<any>(null);
|
|
112
|
+
const arrowNode = useRef<any>(null);
|
|
113
|
+
const stopAutoUpdate = useRef<any>(null);
|
|
114
|
+
const lastFocusedEl = useRef<any>(null);
|
|
115
|
+
const [open, setOpen] = useControllableState({
|
|
116
|
+
value: props.open,
|
|
117
|
+
defaultValue: props.defaultOpen ?? false,
|
|
118
|
+
onValueChange: props.onOpenChange,
|
|
119
|
+
});
|
|
120
|
+
const _openRef = useRef(open);
|
|
121
|
+
_openRef.current = open;
|
|
122
|
+
const anchorEl = useRef<HTMLDivElement | null>(null);
|
|
123
|
+
const floatingEl = useRef<HTMLDivElement | null>(null);
|
|
124
|
+
const arrowEl = useRef<HTMLDivElement | null>(null);
|
|
125
|
+
const _watch0First = useRef(true);
|
|
126
|
+
const _watch1First = useRef(true);
|
|
127
|
+
const _watch2First = useRef(true);
|
|
128
|
+
const _watch3First = useRef(true);
|
|
129
|
+
const _watch4First = useRef(true);
|
|
130
|
+
const _watch5First = useRef(true);
|
|
131
|
+
|
|
132
|
+
function deepActiveElement() {
|
|
133
|
+
let el = document.activeElement;
|
|
134
|
+
while (el && el.shadowRoot && el.shadowRoot.activeElement) {
|
|
135
|
+
el = el.shadowRoot.activeElement;
|
|
136
|
+
}
|
|
137
|
+
return el;
|
|
138
|
+
}
|
|
139
|
+
function requestOpen(next: any) {
|
|
140
|
+
if (open === next) return;
|
|
141
|
+
if (next && props.trigger === 'click') {
|
|
142
|
+
lastFocusedEl.current = deepActiveElement();
|
|
143
|
+
}
|
|
144
|
+
setOpen(next);
|
|
145
|
+
props.onChange && props.onChange(next);
|
|
146
|
+
if (!next && props.trigger === 'click' && lastFocusedEl.current && lastFocusedEl.current.isConnected && typeof lastFocusedEl.current.focus === 'function') {
|
|
147
|
+
lastFocusedEl.current.focus();
|
|
148
|
+
}
|
|
149
|
+
if (!next) {
|
|
150
|
+
lastFocusedEl.current = null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function applyPosition(x: any, y: any, middlewareData: any) {
|
|
154
|
+
if (!floatingNode.current) return;
|
|
155
|
+
floatingNode.current.style.left = x + 'px';
|
|
156
|
+
floatingNode.current.style.top = y + 'px';
|
|
157
|
+
if (arrowNode.current && middlewareData && middlewareData.arrow) {
|
|
158
|
+
const ax = middlewareData.arrow.x;
|
|
159
|
+
const ay = middlewareData.arrow.y;
|
|
160
|
+
arrowNode.current.style.left = ax == null ? '' : ax + 'px';
|
|
161
|
+
arrowNode.current.style.top = ay == null ? '' : ay + 'px';
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function position() {
|
|
165
|
+
if (!anchorNode.current || !floatingNode.current) return;
|
|
166
|
+
const middleware = buildMiddleware({
|
|
167
|
+
offset: offsetMiddleware,
|
|
168
|
+
flip,
|
|
169
|
+
shift,
|
|
170
|
+
arrow: arrowMiddleware
|
|
171
|
+
}, {
|
|
172
|
+
offset: props.offset,
|
|
173
|
+
disableFlip: props.disableFlip,
|
|
174
|
+
disableShift: props.disableShift,
|
|
175
|
+
arrow: props.arrow,
|
|
176
|
+
arrowEl: arrowNode.current
|
|
177
|
+
});
|
|
178
|
+
// 'fixed' inline position MUST be written before computePosition measures the
|
|
179
|
+
// floating element's offset parent (fixed vs absolute changes the containing
|
|
180
|
+
// block). Default 'absolute' explicitly CLEARS any inline position instead of
|
|
181
|
+
// writing `position: absolute` — so a never-fixed popover still writes no
|
|
182
|
+
// visible inline position (byte-identical-off preserved: `style.position = ''`
|
|
183
|
+
// is a no-op when the property was never set), while a live `strategy`
|
|
184
|
+
// reconcile (fixed → absolute, see the $watch below) correctly resets the
|
|
185
|
+
// stale inline `fixed` so the stylesheet's `position: absolute` rule re-takes
|
|
186
|
+
// over instead of positioning `fixed` with absolute-computed coordinates
|
|
187
|
+
// (72-REVIEW.md WR-01).
|
|
188
|
+
if (props.strategy === 'fixed') {
|
|
189
|
+
floatingNode.current.style.position = 'fixed';
|
|
190
|
+
} else {
|
|
191
|
+
floatingNode.current.style.position = '';
|
|
192
|
+
}
|
|
193
|
+
let opts: any = null;
|
|
194
|
+
opts = {
|
|
195
|
+
placement: props.placement,
|
|
196
|
+
strategy: props.strategy,
|
|
197
|
+
middleware
|
|
198
|
+
};
|
|
199
|
+
computePosition(anchorNode.current, floatingNode.current, opts).then((result: any) => {
|
|
200
|
+
applyPosition(result.x, result.y, result.middlewareData);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
const startTracking = useCallback(() => {
|
|
204
|
+
if (!anchorNode.current || !floatingNode.current) return;
|
|
205
|
+
if (stopAutoUpdate.current) {
|
|
206
|
+
stopAutoUpdate.current();
|
|
207
|
+
stopAutoUpdate.current = null;
|
|
208
|
+
}
|
|
209
|
+
stopAutoUpdate.current = autoUpdate(anchorNode.current, floatingNode.current, position);
|
|
210
|
+
}, [position]);
|
|
211
|
+
const stopTracking = useCallback(() => {
|
|
212
|
+
if (stopAutoUpdate.current) {
|
|
213
|
+
stopAutoUpdate.current();
|
|
214
|
+
stopAutoUpdate.current = null;
|
|
215
|
+
}
|
|
216
|
+
}, []);
|
|
217
|
+
const onAnchorClick = useCallback(() => {
|
|
218
|
+
if (props.disabled) return;
|
|
219
|
+
requestOpen(!open);
|
|
220
|
+
}, [open, props.disabled, requestOpen]);
|
|
221
|
+
const onAnchorPointerEnter = useCallback(() => {
|
|
222
|
+
if (props.disabled) return;
|
|
223
|
+
requestOpen(true);
|
|
224
|
+
}, [props.disabled, requestOpen]);
|
|
225
|
+
const onAnchorPointerLeave = useCallback(() => {
|
|
226
|
+
if (props.disabled) return;
|
|
227
|
+
requestOpen(false);
|
|
228
|
+
}, [props.disabled, requestOpen]);
|
|
229
|
+
const onAnchorFocus = useCallback(() => {
|
|
230
|
+
if (props.disabled) return;
|
|
231
|
+
requestOpen(true);
|
|
232
|
+
}, [props.disabled, requestOpen]);
|
|
233
|
+
const onAnchorBlur = useCallback(() => {
|
|
234
|
+
if (props.disabled) return;
|
|
235
|
+
requestOpen(false);
|
|
236
|
+
}, [props.disabled, requestOpen]);
|
|
237
|
+
const dismiss = useCallback(() => {
|
|
238
|
+
requestOpen(false);
|
|
239
|
+
}, [requestOpen]);
|
|
240
|
+
function isTooltip() {
|
|
241
|
+
return props.trigger === 'hover' || props.trigger === 'focus';
|
|
242
|
+
}
|
|
243
|
+
function floatingRole() {
|
|
244
|
+
return isTooltip() ? 'tooltip' : props.modal ? 'dialog' : undefined;
|
|
245
|
+
}
|
|
246
|
+
// ─── imperative handle ($expose) ────────────────────────────────────────────────
|
|
247
|
+
// Verbs: show/hide/toggle/reposition. NOT `update` (reserved Lit lifecycle) → the
|
|
248
|
+
// reposition verb is `reposition`. None collide with the `change` emit, the `open`
|
|
249
|
+
// model, or its React `setOpen` setter, nor with inherited HTMLElement members.
|
|
250
|
+
function show() {
|
|
251
|
+
if (!props.disabled) requestOpen(true);
|
|
252
|
+
}
|
|
253
|
+
function hide() {
|
|
254
|
+
requestOpen(false);
|
|
255
|
+
}
|
|
256
|
+
function toggle() {
|
|
257
|
+
if (!props.disabled) requestOpen(!open);
|
|
258
|
+
}
|
|
259
|
+
function reposition() {
|
|
260
|
+
position();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
useEffect(() => {
|
|
264
|
+
// $refs read ONLY here (ROZ123). The floating + arrow elements live behind r-if
|
|
265
|
+
// and may be null until open; startTracking re-reads via the watch path.
|
|
266
|
+
anchorNode.current = anchorEl.current;
|
|
267
|
+
if (_openRef.current && !props.disabled) {
|
|
268
|
+
// floatingNode is populated by its r-if having rendered; read it lazily inside
|
|
269
|
+
// the watch/handlers too. Position on next tick when it exists.
|
|
270
|
+
floatingNode.current = floatingEl.current;
|
|
271
|
+
arrowNode.current = arrowEl.current;
|
|
272
|
+
startTracking();
|
|
273
|
+
}
|
|
274
|
+
return () => {
|
|
275
|
+
stopTracking();
|
|
276
|
+
};
|
|
277
|
+
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
278
|
+
useEffect(() => {
|
|
279
|
+
if (_watch0First.current) { _watch0First.current = false; return; }
|
|
280
|
+
const isOpen = open;
|
|
281
|
+
if (isOpen && !props.disabled) {
|
|
282
|
+
queueMicrotask(() => {
|
|
283
|
+
if (!open || props.disabled) return;
|
|
284
|
+
floatingNode.current = floatingEl.current;
|
|
285
|
+
arrowNode.current = arrowEl.current;
|
|
286
|
+
startTracking();
|
|
287
|
+
});
|
|
288
|
+
} else {
|
|
289
|
+
stopTracking();
|
|
290
|
+
}
|
|
291
|
+
}, [open]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
292
|
+
useEffect(() => {
|
|
293
|
+
if (_watch1First.current) { _watch1First.current = false; return; }
|
|
294
|
+
if (open) position();
|
|
295
|
+
}, [props.placement]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
296
|
+
useEffect(() => {
|
|
297
|
+
if (_watch2First.current) { _watch2First.current = false; return; }
|
|
298
|
+
if (open) position();
|
|
299
|
+
}, [props.offset]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
300
|
+
useEffect(() => {
|
|
301
|
+
if (_watch3First.current) { _watch3First.current = false; return; }
|
|
302
|
+
if (open) position();
|
|
303
|
+
}, [props.disableFlip]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
304
|
+
useEffect(() => {
|
|
305
|
+
if (_watch4First.current) { _watch4First.current = false; return; }
|
|
306
|
+
if (open) position();
|
|
307
|
+
}, [props.disableShift]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
308
|
+
useEffect(() => {
|
|
309
|
+
if (_watch5First.current) { _watch5First.current = false; return; }
|
|
310
|
+
if (open) position();
|
|
311
|
+
}, [props.strategy]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
312
|
+
|
|
313
|
+
useEffect(() => {
|
|
314
|
+
if (!(open)) return;
|
|
315
|
+
const _rozieHandler = ($event: KeyboardEvent) => {
|
|
316
|
+
if ($event.key !== 'Escape') return;
|
|
317
|
+
((dismiss) as ((...args: any[]) => any))($event);
|
|
318
|
+
};
|
|
319
|
+
document.addEventListener('keydown', _rozieHandler);
|
|
320
|
+
return () => document.removeEventListener('keydown', _rozieHandler);
|
|
321
|
+
}, [dismiss, open]);
|
|
322
|
+
|
|
323
|
+
useOutsideClick(
|
|
324
|
+
[anchorEl, floatingEl],
|
|
325
|
+
dismiss,
|
|
326
|
+
() => !!(open),
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
const _rozieExposeRef = useRef({ show, hide, toggle, reposition });
|
|
330
|
+
_rozieExposeRef.current = { show, hide, toggle, reposition };
|
|
331
|
+
useImperativeHandle(ref, () => ({ show: (...args: Parameters<typeof show>): ReturnType<typeof show> => _rozieExposeRef.current.show(...args), hide: (...args: Parameters<typeof hide>): ReturnType<typeof hide> => _rozieExposeRef.current.hide(...args), toggle: (...args: Parameters<typeof toggle>): ReturnType<typeof toggle> => _rozieExposeRef.current.toggle(...args), reposition: (...args: Parameters<typeof reposition>): ReturnType<typeof reposition> => _rozieExposeRef.current.reposition(...args) }), []);
|
|
332
|
+
|
|
333
|
+
return (
|
|
334
|
+
<>
|
|
335
|
+
<div {...attrs} className={clsx("rozie-popover", (attrs.className as string | undefined))} data-rozie-s-c6cf02ea="">
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
<div className={"rozie-popover-anchor"} ref={anchorEl} aria-haspopup="dialog" aria-expanded={!!open} aria-describedby={rozieAttr(isTooltip() && open ? 'rozie-popover-floating' : undefined)} onClick={($event) => { props.trigger === 'click' && onAnchorClick(); }} onPointerEnter={($event) => { props.trigger === 'hover' && onAnchorPointerEnter(); }} onPointerLeave={($event) => { props.trigger === 'hover' && onAnchorPointerLeave(); }} onFocus={($event) => { props.trigger === 'focus' && onAnchorFocus(); }} onBlur={($event) => { props.trigger === 'focus' && onAnchorBlur(); }} data-rozie-s-c6cf02ea="">
|
|
339
|
+
{(props.renderAnchor ?? props.slots?.['anchor'])?.({ open, toggle, show, hide })}
|
|
340
|
+
</div>
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
{!!(open && !props.disabled) && <div className={"rozie-popover-floating"} ref={floatingEl} id="rozie-popover-floating" role={rozieAttr(floatingRole())} aria-modal={!!(floatingRole() === 'dialog')} data-rozie-s-c6cf02ea="">
|
|
344
|
+
{!!(props.arrow) && <div className={"rozie-popover-arrow"} ref={arrowEl} data-rozie-s-c6cf02ea="" />}{(typeof (props.children ?? props.slots?.['']) === 'function' ? ((props.children ?? props.slots?.['']) as Function)() : (props.children ?? props.slots?.['']))}
|
|
345
|
+
</div>}</div>
|
|
346
|
+
</>
|
|
347
|
+
);
|
|
348
|
+
});
|
|
349
|
+
export default Popover;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* middleware.ts — pure construction of the Floating UI middleware stack from the
|
|
3
|
+
* Popover's first-class props.
|
|
4
|
+
*
|
|
5
|
+
* This is the one piece of BRANCHY engine glue in Popover.rozie (offset always on,
|
|
6
|
+
* flip/shift opt-out via `disableFlip`/`disableShift`, arrow opt-in via `arrow` +
|
|
7
|
+
* an element). Extracted to `src/internal/` so it can be unit-tested in isolation
|
|
8
|
+
* (codegen vendors `src/internal/` into every leaf via copyInternal, excluding
|
|
9
|
+
* `*.test.ts`).
|
|
10
|
+
*
|
|
11
|
+
* It takes the Floating UI middleware FACTORIES as arguments (rather than importing
|
|
12
|
+
* `@floating-ui/dom` itself) so the unit test can pass lightweight stand-ins and
|
|
13
|
+
* assert ORDER + presence without pulling the real engine — and so the vendored
|
|
14
|
+
* copy in each leaf has zero engine import of its own (the leaf's Popover.* owns
|
|
15
|
+
* the single engine import).
|
|
16
|
+
*
|
|
17
|
+
* Floating UI ordering contract: offset → flip → shift → arrow. `offset` first so
|
|
18
|
+
* the gap is measured before collision detection; `arrow` last so it reads the
|
|
19
|
+
* final resolved placement.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export interface MiddlewareFactories {
|
|
23
|
+
offset: (value: number) => unknown;
|
|
24
|
+
flip: () => unknown;
|
|
25
|
+
shift: () => unknown;
|
|
26
|
+
arrow: (opts: { element: Element }) => unknown;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface MiddlewareConfig {
|
|
30
|
+
offset: number;
|
|
31
|
+
disableFlip: boolean;
|
|
32
|
+
disableShift: boolean;
|
|
33
|
+
arrow: boolean;
|
|
34
|
+
arrowEl: Element | null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Build the ordered middleware array for `computePosition`. Pure — no engine
|
|
39
|
+
* import, no DOM read beyond the passed-in arrow element.
|
|
40
|
+
*/
|
|
41
|
+
export function buildMiddleware(
|
|
42
|
+
factories: MiddlewareFactories,
|
|
43
|
+
config: MiddlewareConfig,
|
|
44
|
+
): unknown[] {
|
|
45
|
+
const mw: unknown[] = [factories.offset(config.offset)];
|
|
46
|
+
if (!config.disableFlip) mw.push(factories.flip());
|
|
47
|
+
if (!config.disableShift) mw.push(factories.shift());
|
|
48
|
+
// The arrow middleware needs a real element to position; opt-in only when both
|
|
49
|
+
// the `arrow` prop is set AND the arrow element has mounted.
|
|
50
|
+
if (config.arrow && config.arrowEl) {
|
|
51
|
+
mw.push(factories.arrow({ element: config.arrowEl }));
|
|
52
|
+
}
|
|
53
|
+
return mw;
|
|
54
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @rozie-ui/popover — base token reference.
|
|
3
|
+
*
|
|
4
|
+
* Every visual value the component renders is a `--rozie-popover-*` custom
|
|
5
|
+
* property. The component ships with inline `var(token, fallback)` defaults, so
|
|
6
|
+
* importing this file is OPTIONAL — it simply makes the full token surface
|
|
7
|
+
* explicit in one place so you can copy/override exactly what you need.
|
|
8
|
+
*
|
|
9
|
+
* Override any token at any ancestor scope (`:root`, `.dark`, a wrapper, or the
|
|
10
|
+
* `.rozie-popover` element — custom properties inherit through `display:contents`).
|
|
11
|
+
* See themes/shadcn.css, themes/material.css and themes/bootstrap.css for
|
|
12
|
+
* design-system bridges.
|
|
13
|
+
*/
|
|
14
|
+
.rozie-popover {
|
|
15
|
+
--rozie-popover-z: 1000;
|
|
16
|
+
--rozie-popover-max-width: calc(100vw - 16px);
|
|
17
|
+
--rozie-popover-bg: #fff;
|
|
18
|
+
--rozie-popover-color: inherit;
|
|
19
|
+
--rozie-popover-border: 1px solid rgba(0, 0, 0, 0.12);
|
|
20
|
+
--rozie-popover-radius: 8px;
|
|
21
|
+
--rozie-popover-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
|
|
22
|
+
--rozie-popover-padding: 8px 12px;
|
|
23
|
+
--rozie-popover-arrow-size: 8px;
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @rozie-ui/popover — Bootstrap 5 bridge.
|
|
3
|
+
*
|
|
4
|
+
* Maps the popover tokens onto Bootstrap's `--bs-*` CSS variables (Bootstrap
|
|
5
|
+
* 5.2+). Reading them live means the popover follows `data-bs-theme="dark"`
|
|
6
|
+
* and any custom `$primary` build automatically. Falls back to Bootstrap's
|
|
7
|
+
* default light palette when the vars are absent.
|
|
8
|
+
*
|
|
9
|
+
* import '@rozie-ui/popover-react/themes/bootstrap.css';
|
|
10
|
+
*
|
|
11
|
+
* Colors-only scope — sizing tokens are left unset so the `.rozie-popover`
|
|
12
|
+
* defaults hold.
|
|
13
|
+
*/
|
|
14
|
+
.rozie-popover {
|
|
15
|
+
--rozie-popover-bg: var(--bs-body-bg, #fff);
|
|
16
|
+
--rozie-popover-color: var(--bs-body-color, #212529);
|
|
17
|
+
--rozie-popover-border: 1px solid var(--bs-border-color, #dee2e6);
|
|
18
|
+
--rozie-popover-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @rozie-ui/popover — Material Design 3 bridge.
|
|
3
|
+
*
|
|
4
|
+
* Maps the popover tokens onto the MD3 system color roles (`--md-sys-color-*`)
|
|
5
|
+
* emitted by material-web / @material/material-color-utilities theme builders.
|
|
6
|
+
* Falls back to the baseline M3 light palette when those vars are absent, so
|
|
7
|
+
* it renders correctly even without a generated theme.
|
|
8
|
+
*
|
|
9
|
+
* import '@rozie-ui/popover-react/themes/material.css';
|
|
10
|
+
*
|
|
11
|
+
* Colors-only scope — sizing tokens are left unset so the `.rozie-popover`
|
|
12
|
+
* defaults hold.
|
|
13
|
+
*/
|
|
14
|
+
.rozie-popover {
|
|
15
|
+
--rozie-popover-bg: var(--md-sys-color-surface-container, #f3edf7);
|
|
16
|
+
--rozie-popover-color: var(--md-sys-color-on-surface, #1d1b20);
|
|
17
|
+
--rozie-popover-border: 1px solid var(--md-sys-color-outline-variant, #cac4d0);
|
|
18
|
+
--rozie-popover-shadow: 0 2px 6px 2px rgb(0 0 0 / 0.15), 0 1px 2px rgb(0 0 0 / 0.3);
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @rozie-ui/popover — shadcn/ui (Radix) bridge.
|
|
3
|
+
*
|
|
4
|
+
* Maps the popover tokens onto the shadcn/ui CSS variables defined on `:root`
|
|
5
|
+
* / `.dark` (the classic HSL-channel convention: `--popover: 0 0% 100%`
|
|
6
|
+
* consumed as `hsl(var(--popover))`). Because it reads those vars live, the
|
|
7
|
+
* popover panel automatically follows light/dark theme switches.
|
|
8
|
+
*
|
|
9
|
+
* import '@rozie-ui/popover-react/themes/shadcn.css';
|
|
10
|
+
*
|
|
11
|
+
* If your shadcn setup predates the `hsl()`-wrapping convention (Tailwind v4 /
|
|
12
|
+
* oklch tokens emit pre-formed colors), drop the `hsl(...)` wrappers below.
|
|
13
|
+
*
|
|
14
|
+
* Only the color/shadow tokens are remapped here — sizing tokens (z,
|
|
15
|
+
* max-width, radius, padding, arrow-size) are left unset so the
|
|
16
|
+
* `.rozie-popover` defaults hold.
|
|
17
|
+
*/
|
|
18
|
+
.rozie-popover {
|
|
19
|
+
--rozie-popover-bg: hsl(var(--popover, 0 0% 100%));
|
|
20
|
+
--rozie-popover-color: hsl(var(--popover-foreground, 222.2 47.4% 11.2%));
|
|
21
|
+
--rozie-popover-border: 1px solid hsl(var(--border, 214.3 31.8% 91.4%));
|
|
22
|
+
--rozie-popover-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
|
|
23
|
+
}
|