@zealakit/overlay-react 0.0.0-stage → 1.0.1
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 +547 -2
- package/dist/animation/index.d.ts +1 -0
- package/dist/animation/index.js +1 -0
- package/dist/animation/presence.d.ts +31 -0
- package/dist/animation/presence.js +104 -0
- package/dist/components/arrow.d.ts +30 -0
- package/dist/components/arrow.js +58 -0
- package/dist/components/context-menu/content.d.ts +37 -0
- package/dist/components/context-menu/content.js +52 -0
- package/dist/components/context-menu/context-menu.d.ts +20 -0
- package/dist/components/context-menu/context-menu.js +78 -0
- package/dist/components/context-menu/context.d.ts +47 -0
- package/dist/components/context-menu/context.js +27 -0
- package/dist/components/context-menu/index.d.ts +7 -0
- package/dist/components/context-menu/index.js +6 -0
- package/dist/components/context-menu/items.d.ts +71 -0
- package/dist/components/context-menu/items.js +207 -0
- package/dist/components/context-menu/menu-navigation.d.ts +3 -0
- package/dist/components/context-menu/menu-navigation.js +42 -0
- package/dist/components/context-menu/menu-surface.d.ts +58 -0
- package/dist/components/context-menu/menu-surface.js +172 -0
- package/dist/components/context-menu/sub.d.ts +45 -0
- package/dist/components/context-menu/sub.js +180 -0
- package/dist/components/context-menu/trigger.d.ts +17 -0
- package/dist/components/context-menu/trigger.js +62 -0
- package/dist/components/context-menu/types.d.ts +9 -0
- package/dist/components/context-menu/types.js +1 -0
- package/dist/components/dialog/content.d.ts +87 -0
- package/dist/components/dialog/content.js +238 -0
- package/dist/components/dialog/context.d.ts +41 -0
- package/dist/components/dialog/context.js +26 -0
- package/dist/components/dialog/dialog.d.ts +27 -0
- package/dist/components/dialog/dialog.js +88 -0
- package/dist/components/dialog/helpers.d.ts +24 -0
- package/dist/components/dialog/helpers.js +59 -0
- package/dist/components/dialog/index.d.ts +5 -0
- package/dist/components/dialog/index.js +5 -0
- package/dist/components/dialog/modal-background.d.ts +7 -0
- package/dist/components/dialog/modal-background.js +21 -0
- package/dist/components/dialog/trigger.d.ts +11 -0
- package/dist/components/dialog/trigger.js +29 -0
- package/dist/components/overlay-context.d.ts +13 -0
- package/dist/components/overlay-context.js +10 -0
- package/dist/components/popover/content.d.ts +131 -0
- package/dist/components/popover/content.js +261 -0
- package/dist/components/popover/context.d.ts +27 -0
- package/dist/components/popover/context.js +18 -0
- package/dist/components/popover/helpers.d.ts +23 -0
- package/dist/components/popover/helpers.js +58 -0
- package/dist/components/popover/index.d.ts +4 -0
- package/dist/components/popover/index.js +4 -0
- package/dist/components/popover/popover.d.ts +39 -0
- package/dist/components/popover/popover.js +98 -0
- package/dist/components/portal.d.ts +14 -0
- package/dist/components/portal.js +19 -0
- package/dist/components/slot.d.ts +12 -0
- package/dist/components/slot.js +60 -0
- package/dist/components/tooltip/content.d.ts +21 -0
- package/dist/components/tooltip/content.js +95 -0
- package/dist/components/tooltip/context.d.ts +22 -0
- package/dist/components/tooltip/context.js +10 -0
- package/dist/components/tooltip/index.d.ts +5 -0
- package/dist/components/tooltip/index.js +5 -0
- package/dist/components/tooltip/provider.d.ts +29 -0
- package/dist/components/tooltip/provider.js +29 -0
- package/dist/components/tooltip/tooltip.d.ts +25 -0
- package/dist/components/tooltip/tooltip.js +268 -0
- package/dist/components/tooltip/trigger.d.ts +11 -0
- package/dist/components/tooltip/trigger.js +67 -0
- package/dist/hooks/use-escape-key.d.ts +12 -0
- package/dist/hooks/use-escape-key.js +30 -0
- package/dist/hooks/use-layer.d.ts +33 -0
- package/dist/hooks/use-layer.js +84 -0
- package/dist/hooks/use-overlay.d.ts +31 -0
- package/dist/hooks/use-overlay.js +69 -0
- package/dist/hooks/use-positioner.d.ts +28 -0
- package/dist/hooks/use-positioner.js +127 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/vendor/core/animation/index.d.ts +2 -0
- package/dist/vendor/core/animation/index.js +2 -0
- package/dist/vendor/core/animation/keyframes.d.ts +56 -0
- package/dist/vendor/core/animation/keyframes.js +155 -0
- package/dist/vendor/core/animation/run-animation.d.ts +27 -0
- package/dist/vendor/core/animation/run-animation.js +175 -0
- package/dist/vendor/core/controller/animator.d.ts +19 -0
- package/dist/vendor/core/controller/animator.js +117 -0
- package/dist/vendor/core/controller/context-menu-behavior.d.ts +15 -0
- package/dist/vendor/core/controller/context-menu-behavior.js +123 -0
- package/dist/vendor/core/controller/host.d.ts +10 -0
- package/dist/vendor/core/controller/host.js +1 -0
- package/dist/vendor/core/controller/index.d.ts +2 -0
- package/dist/vendor/core/controller/index.js +1 -0
- package/dist/vendor/core/controller/overlay-controller.d.ts +9 -0
- package/dist/vendor/core/controller/overlay-controller.js +723 -0
- package/dist/vendor/core/controller/tooltip-behavior.d.ts +9 -0
- package/dist/vendor/core/controller/tooltip-behavior.js +123 -0
- package/dist/vendor/core/controller/types.d.ts +92 -0
- package/dist/vendor/core/controller/types.js +1 -0
- package/dist/vendor/core/events/index.d.ts +3 -0
- package/dist/vendor/core/events/index.js +3 -0
- package/dist/vendor/core/events/keyboard.d.ts +2 -0
- package/dist/vendor/core/events/keyboard.js +4 -0
- package/dist/vendor/core/events/outside.d.ts +8 -0
- package/dist/vendor/core/events/outside.js +33 -0
- package/dist/vendor/core/events/pointer.d.ts +13 -0
- package/dist/vendor/core/events/pointer.js +18 -0
- package/dist/vendor/core/focus/focus.d.ts +73 -0
- package/dist/vendor/core/focus/focus.js +312 -0
- package/dist/vendor/core/focus/index.d.ts +1 -0
- package/dist/vendor/core/focus/index.js +1 -0
- package/dist/vendor/core/index.d.ts +13 -0
- package/dist/vendor/core/index.js +13 -0
- package/dist/vendor/core/layer/create-layer-stack.d.ts +25 -0
- package/dist/vendor/core/layer/create-layer-stack.js +247 -0
- package/dist/vendor/core/layer/create-layer.d.ts +10 -0
- package/dist/vendor/core/layer/create-layer.js +78 -0
- package/dist/vendor/core/layer/index.d.ts +3 -0
- package/dist/vendor/core/layer/index.js +3 -0
- package/dist/vendor/core/layer/manager.d.ts +9 -0
- package/dist/vendor/core/layer/manager.js +157 -0
- package/dist/vendor/core/lifecycle/events.d.ts +8 -0
- package/dist/vendor/core/lifecycle/events.js +21 -0
- package/dist/vendor/core/lifecycle/index.d.ts +1 -0
- package/dist/vendor/core/lifecycle/index.js +1 -0
- package/dist/vendor/core/menu/index.d.ts +1 -0
- package/dist/vendor/core/menu/index.js +1 -0
- package/dist/vendor/core/menu/navigation.d.ts +22 -0
- package/dist/vendor/core/menu/navigation.js +107 -0
- package/dist/vendor/core/modal/background.d.ts +10 -0
- package/dist/vendor/core/modal/background.js +115 -0
- package/dist/vendor/core/modal/index.d.ts +1 -0
- package/dist/vendor/core/modal/index.js +1 -0
- package/dist/vendor/core/portal/index.d.ts +1 -0
- package/dist/vendor/core/portal/index.js +1 -0
- package/dist/vendor/core/portal/portal.d.ts +13 -0
- package/dist/vendor/core/portal/portal.js +25 -0
- package/dist/vendor/core/positioning/arrow.d.ts +34 -0
- package/dist/vendor/core/positioning/arrow.js +142 -0
- package/dist/vendor/core/positioning/compute.d.ts +9 -0
- package/dist/vendor/core/positioning/compute.js +22 -0
- package/dist/vendor/core/positioning/coordinates.d.ts +11 -0
- package/dist/vendor/core/positioning/coordinates.js +45 -0
- package/dist/vendor/core/positioning/create-positioner.d.ts +8 -0
- package/dist/vendor/core/positioning/create-positioner.js +251 -0
- package/dist/vendor/core/positioning/index.d.ts +6 -0
- package/dist/vendor/core/positioning/index.js +6 -0
- package/dist/vendor/core/positioning/placement.d.ts +8 -0
- package/dist/vendor/core/positioning/placement.js +30 -0
- package/dist/vendor/core/positioning/same-result.d.ts +8 -0
- package/dist/vendor/core/positioning/same-result.js +13 -0
- package/dist/vendor/core/positioning/strategy.d.ts +12 -0
- package/dist/vendor/core/positioning/strategy.js +46 -0
- package/dist/vendor/core/scroll-lock/index.d.ts +1 -0
- package/dist/vendor/core/scroll-lock/index.js +1 -0
- package/dist/vendor/core/scroll-lock/scroll-lock.d.ts +12 -0
- package/dist/vendor/core/scroll-lock/scroll-lock.js +68 -0
- package/dist/vendor/core/types/animation.d.ts +81 -0
- package/dist/vendor/core/types/animation.js +93 -0
- package/dist/vendor/core/types/dismissal.d.ts +2 -0
- package/dist/vendor/core/types/dismissal.js +1 -0
- package/dist/vendor/core/types/element.d.ts +20 -0
- package/dist/vendor/core/types/element.js +1 -0
- package/dist/vendor/core/types/geometry.d.ts +22 -0
- package/dist/vendor/core/types/geometry.js +1 -0
- package/dist/vendor/core/types/index.d.ts +9 -0
- package/dist/vendor/core/types/index.js +8 -0
- package/dist/vendor/core/types/layer.d.ts +126 -0
- package/dist/vendor/core/types/layer.js +1 -0
- package/dist/vendor/core/types/lifecycle.d.ts +2 -0
- package/dist/vendor/core/types/lifecycle.js +1 -0
- package/dist/vendor/core/types/positioner.d.ts +74 -0
- package/dist/vendor/core/types/positioner.js +1 -0
- package/dist/vendor/core/types/positioning.d.ts +23 -0
- package/dist/vendor/core/types/positioning.js +1 -0
- package/dist/vendor/core/utils/dom.d.ts +11 -0
- package/dist/vendor/core/utils/dom.js +21 -0
- package/dist/vendor/core/utils/element.d.ts +15 -0
- package/dist/vendor/core/utils/element.js +21 -0
- package/dist/vendor/core/utils/global-state.d.ts +1 -0
- package/dist/vendor/core/utils/global-state.js +24 -0
- package/dist/vendor/core/utils/index.d.ts +2 -0
- package/dist/vendor/core/utils/index.js +2 -0
- package/package.json +74 -4
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, forwardRef, useContext, useEffect, useMemo, useRef, useState, } from "react";
|
|
4
|
+
import { createFocusContainment, focusFirst, getActiveElement, parsePlacement, restoreFocus, } from "../../vendor/core/index.js";
|
|
5
|
+
import { useLayer } from "../../hooks/use-layer.js";
|
|
6
|
+
import { usePositioner } from "../../hooks/use-positioner.js";
|
|
7
|
+
import { Portal } from "../portal.js";
|
|
8
|
+
import { PopoverContentContext, usePopoverContext, } from "./context.js";
|
|
9
|
+
import { OverlayLayerContext, OverlayPortalContainerContext, } from "../overlay-context.js";
|
|
10
|
+
import { composeRefs } from "../slot.js";
|
|
11
|
+
import { OverlayPresenceContext, useOverlayElementAnimation, useOverlayPresence, useOverlayPresenceContext, } from "../../animation/presence.js";
|
|
12
|
+
import { DefaultArrow } from "../arrow.js";
|
|
13
|
+
const PopoverPositionerContext = createContext(null);
|
|
14
|
+
export function usePopoverPositionerContext(part) {
|
|
15
|
+
const context = useContext(PopoverPositionerContext);
|
|
16
|
+
if (!context) {
|
|
17
|
+
throw new Error(`[overlay-react] ${part} must be rendered inside <PopoverPositioner>.`);
|
|
18
|
+
}
|
|
19
|
+
return context;
|
|
20
|
+
}
|
|
21
|
+
function toPopoverReason(reason) {
|
|
22
|
+
switch (reason) {
|
|
23
|
+
case "escape-key":
|
|
24
|
+
return "escape";
|
|
25
|
+
case "outside-press":
|
|
26
|
+
return "outside-press";
|
|
27
|
+
case "reference-press":
|
|
28
|
+
return "trigger";
|
|
29
|
+
default:
|
|
30
|
+
return "programmatic";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function sameAnchorSize(previous, next) {
|
|
34
|
+
return (previous !== null &&
|
|
35
|
+
previous.width === next.width &&
|
|
36
|
+
previous.height === next.height);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Portal part for advanced composition. The convenience Content uses it by
|
|
40
|
+
* default. It renders nothing while the Popover is closed.
|
|
41
|
+
*/
|
|
42
|
+
export function PopoverPortal({ children, container, disabled = false, }) {
|
|
43
|
+
const popover = usePopoverContext("PopoverPortal");
|
|
44
|
+
const presence = useOverlayPresenceContext();
|
|
45
|
+
const inheritedContainer = useContext(OverlayPortalContainerContext);
|
|
46
|
+
if (!popover.isOpen && !presence?.present) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
if (disabled) {
|
|
50
|
+
return _jsx(_Fragment, { children: children });
|
|
51
|
+
}
|
|
52
|
+
const target = container === undefined ? inheritedContainer : container;
|
|
53
|
+
return _jsx(Portal, { container: target, children: children });
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Advanced positioning part. Renders no DOM; provides the positioner state to
|
|
57
|
+
* PopoverPopup. Positioning props are consumed here and never reach the popup.
|
|
58
|
+
*/
|
|
59
|
+
export function PopoverPositioner({ children, anchor, side = "bottom", align = "center", sideOffset = 0, alignOffset = 0, collisionBoundary, collisionPadding = 0, collisionAvoidance, positionMethod, arrow = false, arrowSize = 8, arrowPadding = 0, arrowClassName, arrowStyle, }) {
|
|
60
|
+
const popover = usePopoverContext("PopoverPositioner");
|
|
61
|
+
const placement = (align === "center" ? side : `${side}-${align}`);
|
|
62
|
+
const reference = anchor ?? popover.triggerElement;
|
|
63
|
+
const [arrowElement, setArrowElement] = useState(null);
|
|
64
|
+
const [activeArrowPadding, setActiveArrowPadding] = useState(arrowPadding);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
setActiveArrowPadding(arrowPadding);
|
|
67
|
+
}, [arrowPadding]);
|
|
68
|
+
const arrowOptions = useMemo(() => arrowElement
|
|
69
|
+
? { element: arrowElement, padding: activeArrowPadding }
|
|
70
|
+
: null, [activeArrowPadding, arrowElement]);
|
|
71
|
+
const { referenceRef, floatingRef, referenceElement, floatingElement, result, } = usePositioner({
|
|
72
|
+
placement,
|
|
73
|
+
offset: sideOffset,
|
|
74
|
+
alignOffset,
|
|
75
|
+
padding: collisionPadding,
|
|
76
|
+
boundary: collisionBoundary,
|
|
77
|
+
strategy: positionMethod,
|
|
78
|
+
flip: collisionAvoidance?.flip ?? true,
|
|
79
|
+
shift: collisionAvoidance?.shift ?? true,
|
|
80
|
+
arrow: arrowOptions,
|
|
81
|
+
});
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
referenceRef(reference);
|
|
84
|
+
}, [reference, referenceRef]);
|
|
85
|
+
const value = useMemo(() => ({
|
|
86
|
+
result,
|
|
87
|
+
referenceElement,
|
|
88
|
+
floatingElement,
|
|
89
|
+
floatingRef,
|
|
90
|
+
placement,
|
|
91
|
+
arrow: {
|
|
92
|
+
enabled: arrow,
|
|
93
|
+
shorthand: arrow,
|
|
94
|
+
size: arrowSize,
|
|
95
|
+
padding: activeArrowPadding,
|
|
96
|
+
className: arrowClassName,
|
|
97
|
+
style: arrowStyle,
|
|
98
|
+
},
|
|
99
|
+
setArrowElement,
|
|
100
|
+
setArrowPadding: setActiveArrowPadding,
|
|
101
|
+
}), [
|
|
102
|
+
activeArrowPadding,
|
|
103
|
+
arrow,
|
|
104
|
+
arrowClassName,
|
|
105
|
+
arrowSize,
|
|
106
|
+
arrowStyle,
|
|
107
|
+
floatingElement,
|
|
108
|
+
floatingRef,
|
|
109
|
+
placement,
|
|
110
|
+
referenceElement,
|
|
111
|
+
result,
|
|
112
|
+
]);
|
|
113
|
+
return (_jsx(PopoverPositionerContext.Provider, { value: value, children: children }));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Advanced popup part. Owns the floating element, layer registration, ARIA
|
|
117
|
+
* title/description wiring, focus policy, and state/data attributes.
|
|
118
|
+
*/
|
|
119
|
+
export const PopoverPopup = forwardRef(function PopoverPopup({ children, initialFocus = false, trapFocus = false, restoreFocus: restoreOnClose = true, animation, ...rest }, forwardedRef) {
|
|
120
|
+
const popover = usePopoverContext("PopoverPopup");
|
|
121
|
+
const positioner = usePopoverPositionerContext("PopoverPopup");
|
|
122
|
+
const inheritedPresence = useOverlayPresenceContext();
|
|
123
|
+
const localPresence = useOverlayPresence(popover.isOpen, animation);
|
|
124
|
+
const presence = inheritedPresence ?? localPresence;
|
|
125
|
+
const [popupElement, setPopupElement] = useState(null);
|
|
126
|
+
const [titleId, setTitleId] = useState(null);
|
|
127
|
+
const [descriptionId, setDescriptionId] = useState(null);
|
|
128
|
+
const focusInsideRef = useRef(false);
|
|
129
|
+
const initialFocusAppliedRef = useRef(false);
|
|
130
|
+
const triggerElementRef = useRef(popover.triggerElement);
|
|
131
|
+
triggerElementRef.current = popover.triggerElement;
|
|
132
|
+
const layer = useLayer({
|
|
133
|
+
element: popupElement,
|
|
134
|
+
reference: positioner.referenceElement,
|
|
135
|
+
parent: popover.parentLayer,
|
|
136
|
+
dismissOnEscape: popover.dismissOnEscape,
|
|
137
|
+
dismissOnOutsidePress: popover.dismissOnOutsidePress,
|
|
138
|
+
disabled: !popover.isOpen,
|
|
139
|
+
onDismiss: (reason) => {
|
|
140
|
+
popover.close(toPopoverReason(reason));
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
useOverlayElementAnimation({
|
|
144
|
+
element: positioner.result ? popupElement : null,
|
|
145
|
+
phase: presence.phase,
|
|
146
|
+
animation: presence.animation,
|
|
147
|
+
placement: positioner.result?.placement ?? positioner.placement,
|
|
148
|
+
onExitComplete: presence.finishExit,
|
|
149
|
+
});
|
|
150
|
+
const refs = useMemo(() => composeRefs(forwardedRef, positioner.floatingRef, setPopupElement), [forwardedRef, positioner.floatingRef]);
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (!popover.isOpen || !popupElement) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const handleFocusIn = () => {
|
|
156
|
+
focusInsideRef.current = true;
|
|
157
|
+
};
|
|
158
|
+
const handleFocusOut = (event) => {
|
|
159
|
+
const next = event.relatedTarget;
|
|
160
|
+
if (!next || !popupElement.contains(next)) {
|
|
161
|
+
focusInsideRef.current = false;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
popupElement.addEventListener("focusin", handleFocusIn);
|
|
165
|
+
popupElement.addEventListener("focusout", handleFocusOut);
|
|
166
|
+
return () => {
|
|
167
|
+
popupElement.removeEventListener("focusin", handleFocusIn);
|
|
168
|
+
popupElement.removeEventListener("focusout", handleFocusOut);
|
|
169
|
+
};
|
|
170
|
+
}, [popover.isOpen, popupElement]);
|
|
171
|
+
useEffect(() => {
|
|
172
|
+
if (!popover.isOpen ||
|
|
173
|
+
!initialFocus ||
|
|
174
|
+
!popupElement ||
|
|
175
|
+
!positioner.result ||
|
|
176
|
+
initialFocusAppliedRef.current) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
initialFocusAppliedRef.current = true;
|
|
180
|
+
focusFirst(popupElement);
|
|
181
|
+
}, [initialFocus, popover.isOpen, popupElement, positioner.result]);
|
|
182
|
+
useEffect(() => {
|
|
183
|
+
if (!popover.isOpen || !restoreOnClose || !popupElement) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
return () => {
|
|
187
|
+
const active = getActiveElement();
|
|
188
|
+
const focusWasInside = focusInsideRef.current ||
|
|
189
|
+
(active !== null && popupElement.contains(active));
|
|
190
|
+
focusInsideRef.current = false;
|
|
191
|
+
if (focusWasInside) {
|
|
192
|
+
restoreFocus(triggerElementRef.current);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}, [popover.isOpen, popupElement, restoreOnClose]);
|
|
196
|
+
useEffect(() => {
|
|
197
|
+
if (!popover.isOpen || !trapFocus || !popupElement) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const containment = createFocusContainment({
|
|
201
|
+
container: popupElement,
|
|
202
|
+
enabled: layer.isTopmost,
|
|
203
|
+
restoreFocus: false,
|
|
204
|
+
});
|
|
205
|
+
return () => {
|
|
206
|
+
containment.dispose();
|
|
207
|
+
};
|
|
208
|
+
}, [layer.isTopmost, popover.isOpen, popupElement, trapFocus]);
|
|
209
|
+
const [anchorSize, setAnchorSize] = useState(null);
|
|
210
|
+
useEffect(() => {
|
|
211
|
+
const reference = positioner.referenceElement;
|
|
212
|
+
if (!reference || !positioner.result) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const rect = reference.getBoundingClientRect();
|
|
216
|
+
const next = { width: rect.width, height: rect.height };
|
|
217
|
+
setAnchorSize((previous) => sameAnchorSize(previous, next) ? previous : next);
|
|
218
|
+
}, [positioner.referenceElement, positioner.result]);
|
|
219
|
+
const labelContext = useMemo(() => ({ setTitleId, setDescriptionId }), []);
|
|
220
|
+
const layerContext = useMemo(() => ({ handle: layer.handle, isTopmost: layer.isTopmost }), [layer.handle, layer.isTopmost]);
|
|
221
|
+
if (!presence.present) {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
const placement = positioner.result?.placement ?? positioner.placement;
|
|
225
|
+
const parsed = parsePlacement(placement);
|
|
226
|
+
const style = {
|
|
227
|
+
...rest.style,
|
|
228
|
+
...(positioner.result
|
|
229
|
+
? {
|
|
230
|
+
position: positioner.result.strategy,
|
|
231
|
+
left: `${positioner.result.x}px`,
|
|
232
|
+
top: `${positioner.result.y}px`,
|
|
233
|
+
}
|
|
234
|
+
: {
|
|
235
|
+
position: "fixed",
|
|
236
|
+
left: 0,
|
|
237
|
+
top: 0,
|
|
238
|
+
visibility: "hidden",
|
|
239
|
+
}),
|
|
240
|
+
...(anchorSize
|
|
241
|
+
? {
|
|
242
|
+
"--popover-anchor-width": `${anchorSize.width}px`,
|
|
243
|
+
"--popover-anchor-height": `${anchorSize.height}px`,
|
|
244
|
+
}
|
|
245
|
+
: {}),
|
|
246
|
+
};
|
|
247
|
+
return (_jsx(OverlayLayerContext.Provider, { value: layerContext, children: _jsx(PopoverContentContext.Provider, { value: labelContext, children: _jsxs("div", { ...rest, ref: refs, id: popover.contentId, dir: rest.dir ?? (popover.direction === "rtl" ? "rtl" : undefined), "data-state": popover.isOpen ? "open" : "closed", "data-open": popover.isOpen ? "" : undefined, "data-closed": popover.isOpen ? undefined : "", "data-side": parsed.side, "data-align": parsed.align, "aria-labelledby": rest["aria-labelledby"] ?? titleId ?? undefined, "aria-describedby": rest["aria-describedby"] ?? descriptionId ?? undefined, style: style, children: [positioner.arrow.shorthand ? (_jsx(DefaultArrow, { size: positioner.arrow.size, padding: positioner.arrow.padding, className: positioner.arrow.className, style: positioner.arrow.style, part: "PopoverArrow" })) : null, children] }) }) }));
|
|
248
|
+
});
|
|
249
|
+
/**
|
|
250
|
+
* Convenience composition: Portal -> Positioner -> Popup. Positioning props are
|
|
251
|
+
* consumed by the Positioner; native props go to the Popup.
|
|
252
|
+
*/
|
|
253
|
+
export const PopoverContent = forwardRef(function PopoverContent({ anchor, side, align, sideOffset, alignOffset, collisionBoundary, collisionPadding, collisionAvoidance, positionMethod, arrow, arrowSize, arrowPadding, arrowClassName, arrowStyle, animation, portal = true, children, ...popupProps }, forwardedRef) {
|
|
254
|
+
const popover = usePopoverContext("PopoverContent");
|
|
255
|
+
const presence = useOverlayPresence(popover.isOpen, animation);
|
|
256
|
+
if (!presence.present) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
const content = (_jsx(PopoverPositioner, { anchor: anchor, side: side, align: align, sideOffset: sideOffset, alignOffset: alignOffset, collisionBoundary: collisionBoundary, collisionPadding: collisionPadding, collisionAvoidance: collisionAvoidance, positionMethod: positionMethod, arrow: arrow, arrowSize: arrowSize, arrowPadding: arrowPadding, arrowClassName: arrowClassName, arrowStyle: arrowStyle, children: _jsx(PopoverPopup, { ...popupProps, animation: animation, ref: forwardedRef, children: children }) }));
|
|
260
|
+
return (_jsx(OverlayPresenceContext.Provider, { value: presence, children: portal === false ? (content) : (_jsx(PopoverPortal, { container: typeof portal === "object" && portal !== null ? portal : undefined, children: content })) }));
|
|
261
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { LayerStackHandle } from "../../vendor/core/index.js";
|
|
2
|
+
export type PopoverChangeReason = "trigger" | "outside-press" | "escape" | "close-button" | "programmatic";
|
|
3
|
+
export type PopoverDirection = "ltr" | "rtl";
|
|
4
|
+
export type PopoverContextValue = {
|
|
5
|
+
isOpen: boolean;
|
|
6
|
+
open(): void;
|
|
7
|
+
close(reason?: PopoverChangeReason): void;
|
|
8
|
+
toggle(reason?: PopoverChangeReason): void;
|
|
9
|
+
triggerElement: Element | null;
|
|
10
|
+
setTriggerElement(element: Element | null): void;
|
|
11
|
+
triggerId: string;
|
|
12
|
+
contentId: string;
|
|
13
|
+
titleId: string;
|
|
14
|
+
descriptionId: string;
|
|
15
|
+
direction: PopoverDirection;
|
|
16
|
+
dismissOnEscape: boolean;
|
|
17
|
+
dismissOnOutsidePress: boolean;
|
|
18
|
+
parentLayer: LayerStackHandle | null;
|
|
19
|
+
};
|
|
20
|
+
export declare const PopoverContext: import("react").Context<PopoverContextValue | null>;
|
|
21
|
+
export declare function usePopoverContext(part: string): PopoverContextValue;
|
|
22
|
+
export type PopoverContentContextValue = {
|
|
23
|
+
setTitleId(id: string | null): void;
|
|
24
|
+
setDescriptionId(id: string | null): void;
|
|
25
|
+
};
|
|
26
|
+
export declare const PopoverContentContext: import("react").Context<PopoverContentContextValue | null>;
|
|
27
|
+
export declare function usePopoverContentContext(part: string): PopoverContentContextValue;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
export const PopoverContext = createContext(null);
|
|
4
|
+
export function usePopoverContext(part) {
|
|
5
|
+
const context = useContext(PopoverContext);
|
|
6
|
+
if (!context) {
|
|
7
|
+
throw new Error(`[overlay-react] ${part} must be rendered inside <Popover>.`);
|
|
8
|
+
}
|
|
9
|
+
return context;
|
|
10
|
+
}
|
|
11
|
+
export const PopoverContentContext = createContext(null);
|
|
12
|
+
export function usePopoverContentContext(part) {
|
|
13
|
+
const context = useContext(PopoverContentContext);
|
|
14
|
+
if (!context) {
|
|
15
|
+
throw new Error(`[overlay-react] ${part} must be rendered inside <PopoverPopup> or <PopoverContent>.`);
|
|
16
|
+
}
|
|
17
|
+
return context;
|
|
18
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ComponentPropsWithoutRef, type HTMLAttributes } from "react";
|
|
2
|
+
export type PopoverHeaderProps = HTMLAttributes<HTMLDivElement>;
|
|
3
|
+
/** Optional layout wrapper only; it carries no accessibility semantics. */
|
|
4
|
+
export declare const PopoverHeader: import("react").ForwardRefExoticComponent<PopoverHeaderProps & import("react").RefAttributes<HTMLDivElement>>;
|
|
5
|
+
export type PopoverTitleProps = HTMLAttributes<HTMLHeadingElement>;
|
|
6
|
+
/**
|
|
7
|
+
* Optional semantic title. It sets the Popover popup's `aria-labelledby`
|
|
8
|
+
* relationship when used.
|
|
9
|
+
*/
|
|
10
|
+
export declare const PopoverTitle: import("react").ForwardRefExoticComponent<PopoverTitleProps & import("react").RefAttributes<HTMLHeadingElement>>;
|
|
11
|
+
export type PopoverDescriptionProps = HTMLAttributes<HTMLParagraphElement>;
|
|
12
|
+
/**
|
|
13
|
+
* Optional semantic description. It sets the popup's `aria-describedby`
|
|
14
|
+
* relationship when used.
|
|
15
|
+
*/
|
|
16
|
+
export declare const PopoverDescription: import("react").ForwardRefExoticComponent<PopoverDescriptionProps & import("react").RefAttributes<HTMLParagraphElement>>;
|
|
17
|
+
export type PopoverCloseProps = ComponentPropsWithoutRef<"button"> & {
|
|
18
|
+
asChild?: boolean;
|
|
19
|
+
};
|
|
20
|
+
/** Optional close control. Consumers are not required to render one. */
|
|
21
|
+
export declare const PopoverClose: import("react").ForwardRefExoticComponent<Omit<import("react").DetailedHTMLProps<import("react").ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "ref"> & {
|
|
22
|
+
asChild?: boolean;
|
|
23
|
+
} & import("react").RefAttributes<HTMLElement>>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { forwardRef, useEffect, useMemo, } from "react";
|
|
4
|
+
import { usePopoverContentContext, usePopoverContext } from "./context.js";
|
|
5
|
+
import { Slot, composeRefs } from "../slot.js";
|
|
6
|
+
/** Optional layout wrapper only; it carries no accessibility semantics. */
|
|
7
|
+
export const PopoverHeader = forwardRef(function PopoverHeader(props, ref) {
|
|
8
|
+
return _jsx("div", { ...props, ref: ref });
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Optional semantic title. It sets the Popover popup's `aria-labelledby`
|
|
12
|
+
* relationship when used.
|
|
13
|
+
*/
|
|
14
|
+
export const PopoverTitle = forwardRef(function PopoverTitle({ id, ...rest }, ref) {
|
|
15
|
+
const popover = usePopoverContext("PopoverTitle");
|
|
16
|
+
const { setTitleId } = usePopoverContentContext("PopoverTitle");
|
|
17
|
+
const titleId = id ?? popover.titleId;
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
setTitleId(titleId);
|
|
20
|
+
return () => {
|
|
21
|
+
setTitleId(null);
|
|
22
|
+
};
|
|
23
|
+
}, [setTitleId, titleId]);
|
|
24
|
+
return _jsx("h2", { ...rest, id: titleId, ref: ref });
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* Optional semantic description. It sets the popup's `aria-describedby`
|
|
28
|
+
* relationship when used.
|
|
29
|
+
*/
|
|
30
|
+
export const PopoverDescription = forwardRef(function PopoverDescription({ id, ...rest }, ref) {
|
|
31
|
+
const popover = usePopoverContext("PopoverDescription");
|
|
32
|
+
const { setDescriptionId } = usePopoverContentContext("PopoverDescription");
|
|
33
|
+
const descriptionId = id ?? popover.descriptionId;
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
setDescriptionId(descriptionId);
|
|
36
|
+
return () => {
|
|
37
|
+
setDescriptionId(null);
|
|
38
|
+
};
|
|
39
|
+
}, [descriptionId, setDescriptionId]);
|
|
40
|
+
return _jsx("p", { ...rest, id: descriptionId, ref: ref });
|
|
41
|
+
});
|
|
42
|
+
/** Optional close control. Consumers are not required to render one. */
|
|
43
|
+
export const PopoverClose = forwardRef(function PopoverClose({ asChild = false, onClick, type, children, ...rest }, forwardedRef) {
|
|
44
|
+
const popover = usePopoverContext("PopoverClose");
|
|
45
|
+
const refs = useMemo(() => composeRefs(forwardedRef), [forwardedRef]);
|
|
46
|
+
const handleClick = (event) => {
|
|
47
|
+
onClick?.(event);
|
|
48
|
+
if (event.defaultPrevented) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
popover.close("close-button");
|
|
52
|
+
};
|
|
53
|
+
const shared = { ...rest, onClick: handleClick };
|
|
54
|
+
if (asChild) {
|
|
55
|
+
return (_jsx(Slot, { ...shared, ref: refs, children: children }));
|
|
56
|
+
}
|
|
57
|
+
return (_jsx("button", { ...shared, ref: refs, type: type ?? "button", children: children }));
|
|
58
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Popover, PopoverTrigger, type PopoverProps, type PopoverTriggerProps, } from "./popover.js";
|
|
2
|
+
export type { PopoverChangeReason, PopoverDirection, } from "./context.js";
|
|
3
|
+
export { PopoverContent, PopoverPopup, PopoverPortal, PopoverPositioner, type PopoverAlign, type PopoverArrowConfig, type PopoverCollisionAvoidance, type PopoverContentProps, type PopoverPopupProps, type PopoverPortalProps, type PopoverPositionerProps, type PopoverSide, } from "./content.js";
|
|
4
|
+
export { PopoverClose, PopoverDescription, PopoverHeader, PopoverTitle, type PopoverCloseProps, type PopoverDescriptionProps, type PopoverHeaderProps, type PopoverTitleProps, } from "./helpers.js";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type ComponentPropsWithoutRef, type ReactNode } from "react";
|
|
2
|
+
import type { LayerStackHandle } from "../../vendor/core/index.js";
|
|
3
|
+
import { type PopoverChangeReason, type PopoverDirection } from "./context.js";
|
|
4
|
+
export type PopoverProps = {
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
/** Controlled visibility. When set, the Popover does not own the state. */
|
|
7
|
+
open?: boolean;
|
|
8
|
+
/** Initial visibility in uncontrolled mode. Defaults to false. */
|
|
9
|
+
defaultOpen?: boolean;
|
|
10
|
+
onOpenChange?: (open: boolean, reason: PopoverChangeReason) => void;
|
|
11
|
+
/** Defaults to true. */
|
|
12
|
+
dismissOnEscape?: boolean;
|
|
13
|
+
/** Defaults to true. */
|
|
14
|
+
dismissOnOutsidePress?: boolean;
|
|
15
|
+
/** Direction used for start/end alignment. Defaults to ltr. */
|
|
16
|
+
dir?: PopoverDirection;
|
|
17
|
+
/** Optional parent layer for integration with a Dialog/other layer. */
|
|
18
|
+
parentLayer?: LayerStackHandle | null;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Headless, non-modal Popover root.
|
|
22
|
+
*
|
|
23
|
+
* Owns controlled/uncontrolled open state and generates the trigger/content
|
|
24
|
+
* relationship IDs. It renders no DOM.
|
|
25
|
+
*/
|
|
26
|
+
export declare function Popover({ children, open: controlledOpen, defaultOpen, onOpenChange, dismissOnEscape, dismissOnOutsidePress, dir, parentLayer, }: PopoverProps): import("react").JSX.Element;
|
|
27
|
+
export type PopoverTriggerProps = ComponentPropsWithoutRef<"button"> & {
|
|
28
|
+
/** Merge trigger behavior onto the single child element instead of wrapping it. */
|
|
29
|
+
asChild?: boolean;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Trigger button. Renders a native button by default. With `asChild`, the
|
|
33
|
+
* consumer's button/link receives the behavior, refs, ARIA, and handlers
|
|
34
|
+
* without an extra wrapper.
|
|
35
|
+
*/
|
|
36
|
+
export declare const PopoverTrigger: import("react").ForwardRefExoticComponent<Omit<import("react").DetailedHTMLProps<import("react").ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "ref"> & {
|
|
37
|
+
/** Merge trigger behavior onto the single child element instead of wrapping it. */
|
|
38
|
+
asChild?: boolean;
|
|
39
|
+
} & import("react").RefAttributes<HTMLElement>>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { forwardRef, useCallback, useEffect, useId, useMemo, useRef, useState, useContext, } from "react";
|
|
4
|
+
import { OverlayLayerContext } from "../overlay-context.js";
|
|
5
|
+
import { PopoverContext, usePopoverContext, } from "./context.js";
|
|
6
|
+
import { Slot, composeRefs } from "../slot.js";
|
|
7
|
+
/**
|
|
8
|
+
* Headless, non-modal Popover root.
|
|
9
|
+
*
|
|
10
|
+
* Owns controlled/uncontrolled open state and generates the trigger/content
|
|
11
|
+
* relationship IDs. It renders no DOM.
|
|
12
|
+
*/
|
|
13
|
+
export function Popover({ children, open: controlledOpen, defaultOpen = false, onOpenChange, dismissOnEscape = true, dismissOnOutsidePress = true, dir = "ltr", parentLayer, }) {
|
|
14
|
+
const inheritedLayer = useContext(OverlayLayerContext);
|
|
15
|
+
const resolvedParentLayer = parentLayer ?? inheritedLayer?.handle ?? null;
|
|
16
|
+
const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen);
|
|
17
|
+
const isControlled = controlledOpen !== undefined;
|
|
18
|
+
const isOpen = isControlled ? controlledOpen : uncontrolledOpen;
|
|
19
|
+
const onOpenChangeRef = useRef(onOpenChange);
|
|
20
|
+
const id = useId();
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
onOpenChangeRef.current = onOpenChange;
|
|
23
|
+
}, [onOpenChange]);
|
|
24
|
+
const setOpen = useCallback((next, reason) => {
|
|
25
|
+
if (next === isOpen) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (!isControlled) {
|
|
29
|
+
setUncontrolledOpen(next);
|
|
30
|
+
}
|
|
31
|
+
onOpenChangeRef.current?.(next, reason);
|
|
32
|
+
}, [isControlled, isOpen]);
|
|
33
|
+
const open = useCallback(() => {
|
|
34
|
+
setOpen(true, "programmatic");
|
|
35
|
+
}, [setOpen]);
|
|
36
|
+
const close = useCallback((reason = "programmatic") => {
|
|
37
|
+
setOpen(false, reason);
|
|
38
|
+
}, [setOpen]);
|
|
39
|
+
const toggle = useCallback((reason = "trigger") => {
|
|
40
|
+
setOpen(!isOpen, reason);
|
|
41
|
+
}, [isOpen, setOpen]);
|
|
42
|
+
const [triggerElement, setTriggerElement] = useState(null);
|
|
43
|
+
const contextValue = useMemo(() => ({
|
|
44
|
+
isOpen,
|
|
45
|
+
open,
|
|
46
|
+
close,
|
|
47
|
+
toggle,
|
|
48
|
+
triggerElement,
|
|
49
|
+
setTriggerElement,
|
|
50
|
+
triggerId: `${id}-trigger`,
|
|
51
|
+
contentId: `${id}-content`,
|
|
52
|
+
titleId: `${id}-title`,
|
|
53
|
+
descriptionId: `${id}-description`,
|
|
54
|
+
direction: dir,
|
|
55
|
+
dismissOnEscape,
|
|
56
|
+
dismissOnOutsidePress,
|
|
57
|
+
parentLayer: resolvedParentLayer,
|
|
58
|
+
}), [
|
|
59
|
+
close,
|
|
60
|
+
dir,
|
|
61
|
+
dismissOnEscape,
|
|
62
|
+
dismissOnOutsidePress,
|
|
63
|
+
id,
|
|
64
|
+
isOpen,
|
|
65
|
+
open,
|
|
66
|
+
resolvedParentLayer,
|
|
67
|
+
toggle,
|
|
68
|
+
triggerElement,
|
|
69
|
+
]);
|
|
70
|
+
return (_jsx(PopoverContext.Provider, { value: contextValue, children: children }));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Trigger button. Renders a native button by default. With `asChild`, the
|
|
74
|
+
* consumer's button/link receives the behavior, refs, ARIA, and handlers
|
|
75
|
+
* without an extra wrapper.
|
|
76
|
+
*/
|
|
77
|
+
export const PopoverTrigger = forwardRef(function PopoverTrigger({ asChild = false, disabled = false, onClick, type, id, children, ...rest }, forwardedRef) {
|
|
78
|
+
const popover = usePopoverContext("PopoverTrigger");
|
|
79
|
+
const handleClick = (event) => {
|
|
80
|
+
onClick?.(event);
|
|
81
|
+
if (event.defaultPrevented || disabled) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
popover.toggle("trigger");
|
|
85
|
+
};
|
|
86
|
+
const refs = useMemo(() => composeRefs(forwardedRef, popover.setTriggerElement), [forwardedRef, popover.setTriggerElement]);
|
|
87
|
+
const shared = {
|
|
88
|
+
...rest,
|
|
89
|
+
id: id ?? popover.triggerId,
|
|
90
|
+
"aria-expanded": popover.isOpen,
|
|
91
|
+
"aria-controls": popover.isOpen ? popover.contentId : undefined,
|
|
92
|
+
onClick: handleClick,
|
|
93
|
+
};
|
|
94
|
+
if (asChild) {
|
|
95
|
+
return (_jsx(Slot, { ...shared, ...(disabled ? { disabled: true, "aria-disabled": true } : {}), ref: refs, children: children }));
|
|
96
|
+
}
|
|
97
|
+
return (_jsx("button", { ...shared, ref: refs, type: type ?? "button", disabled: disabled, children: children }));
|
|
98
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
export type PortalProps = {
|
|
3
|
+
children: ReactNode;
|
|
4
|
+
/** Defaults to `document.body`. */
|
|
5
|
+
container?: Element | null;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Render children into a portal target after mount.
|
|
10
|
+
*
|
|
11
|
+
* Returns `null` on the server and during the first client render, which keeps
|
|
12
|
+
* SSR markup stable. Prefer this over touching `document` during render.
|
|
13
|
+
*/
|
|
14
|
+
export declare function Portal({ children, container, disabled }: PortalProps): import("react").ReactPortal | null;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { createPortal } from "react-dom";
|
|
4
|
+
/**
|
|
5
|
+
* Render children into a portal target after mount.
|
|
6
|
+
*
|
|
7
|
+
* Returns `null` on the server and during the first client render, which keeps
|
|
8
|
+
* SSR markup stable. Prefer this over touching `document` during render.
|
|
9
|
+
*/
|
|
10
|
+
export function Portal({ children, container, disabled = false }) {
|
|
11
|
+
const [mounted, setMounted] = useState(false);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
setMounted(true);
|
|
14
|
+
}, []);
|
|
15
|
+
if (disabled || !mounted) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return createPortal(children, container ?? document.body);
|
|
19
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ReactNode, type Ref } from "react";
|
|
2
|
+
export declare function composeRefs<T>(...refs: Array<Ref<T> | undefined>): (node: T | null) => void;
|
|
3
|
+
export type SlotProps = {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Merge Popover behavior onto a single consumer element instead of wrapping it.
|
|
9
|
+
* Consumer handlers run first; the Popover handler is skipped when the consumer
|
|
10
|
+
* calls `preventDefault()`.
|
|
11
|
+
*/
|
|
12
|
+
export declare const Slot: import("react").ForwardRefExoticComponent<Omit<SlotProps, "ref"> & import("react").RefAttributes<HTMLElement>>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cloneElement, forwardRef, isValidElement, useMemo, } from "react";
|
|
3
|
+
export function composeRefs(...refs) {
|
|
4
|
+
return (node) => {
|
|
5
|
+
for (const ref of refs) {
|
|
6
|
+
if (!ref) {
|
|
7
|
+
continue;
|
|
8
|
+
}
|
|
9
|
+
if (typeof ref === "function") {
|
|
10
|
+
ref(node);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
ref.current = node;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Merge Popover behavior onto a single consumer element instead of wrapping it.
|
|
20
|
+
* Consumer handlers run first; the Popover handler is skipped when the consumer
|
|
21
|
+
* calls `preventDefault()`.
|
|
22
|
+
*/
|
|
23
|
+
export const Slot = forwardRef(function Slot({ children, ...rest }, forwardedRef) {
|
|
24
|
+
const childRef = isValidElement(children)
|
|
25
|
+
? children.ref
|
|
26
|
+
: undefined;
|
|
27
|
+
const mergedRef = useMemo(() => composeRefs(childRef, forwardedRef), [childRef, forwardedRef]);
|
|
28
|
+
if (!isValidElement(children)) {
|
|
29
|
+
throw new Error("[overlay-react] asChild requires exactly one React element child.");
|
|
30
|
+
}
|
|
31
|
+
const child = children;
|
|
32
|
+
const childProps = child.props;
|
|
33
|
+
const merged = { ...childProps, ...rest };
|
|
34
|
+
const childClassName = typeof childProps.className === "string" ? childProps.className : "";
|
|
35
|
+
const slotClassName = typeof rest.className === "string" ? rest.className : "";
|
|
36
|
+
const className = [childClassName, slotClassName]
|
|
37
|
+
.filter(Boolean)
|
|
38
|
+
.join(" ")
|
|
39
|
+
.trim();
|
|
40
|
+
if (className) {
|
|
41
|
+
merged.className = className;
|
|
42
|
+
}
|
|
43
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
44
|
+
if (key.startsWith("on") &&
|
|
45
|
+
typeof value === "function" &&
|
|
46
|
+
typeof childProps[key] === "function") {
|
|
47
|
+
const childHandler = childProps[key];
|
|
48
|
+
const slotHandler = value;
|
|
49
|
+
merged[key] = (...args) => {
|
|
50
|
+
childHandler(...args);
|
|
51
|
+
const event = args[0];
|
|
52
|
+
if (!event?.defaultPrevented) {
|
|
53
|
+
slotHandler(...args);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
merged.ref = mergedRef;
|
|
59
|
+
return cloneElement(child, merged);
|
|
60
|
+
});
|