@zealakit/overlay-react 0.0.0-stage → 1.0.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 +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 +47 -0
- package/dist/vendor/core/animation/keyframes.js +105 -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,180 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { forwardRef, useCallback, useEffect, useId, useMemo, useRef, useState, } from "react";
|
|
4
|
+
import { Slot, composeRefs } from "../slot.js";
|
|
5
|
+
import { OverlayPresenceContext, useOverlayPresence, } from "../../animation/presence.js";
|
|
6
|
+
import { MenuSurface } from "./menu-surface.js";
|
|
7
|
+
import { ContextMenuSubContext, useContextMenuContext, useContextMenuSubContext, } from "./context.js";
|
|
8
|
+
const SUBMENU_CLOSE_DELAY = 100;
|
|
9
|
+
/** Uncontrolled submenu state. It inherits direction and layer context. */
|
|
10
|
+
export function ContextMenuSub({ children, defaultOpen = false, }) {
|
|
11
|
+
const menu = useContextMenuContext("ContextMenuSub");
|
|
12
|
+
const [isOpen, setIsOpen] = useState(defaultOpen);
|
|
13
|
+
const [triggerElement, setTriggerElement] = useState(null);
|
|
14
|
+
const closeTimerRef = useRef(null);
|
|
15
|
+
const id = useId();
|
|
16
|
+
const clearClose = useCallback(() => {
|
|
17
|
+
if (closeTimerRef.current !== null) {
|
|
18
|
+
clearTimeout(closeTimerRef.current);
|
|
19
|
+
closeTimerRef.current = null;
|
|
20
|
+
}
|
|
21
|
+
}, []);
|
|
22
|
+
const open = useCallback(() => {
|
|
23
|
+
clearClose();
|
|
24
|
+
setIsOpen(true);
|
|
25
|
+
}, [clearClose]);
|
|
26
|
+
const close = useCallback(() => {
|
|
27
|
+
clearClose();
|
|
28
|
+
setIsOpen(false);
|
|
29
|
+
}, [clearClose]);
|
|
30
|
+
const toggle = useCallback(() => {
|
|
31
|
+
if (isOpen) {
|
|
32
|
+
close();
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
open();
|
|
36
|
+
}
|
|
37
|
+
}, [close, isOpen, open]);
|
|
38
|
+
const scheduleClose = useCallback(() => {
|
|
39
|
+
clearClose();
|
|
40
|
+
closeTimerRef.current = setTimeout(() => {
|
|
41
|
+
closeTimerRef.current = null;
|
|
42
|
+
setIsOpen(false);
|
|
43
|
+
}, SUBMENU_CLOSE_DELAY);
|
|
44
|
+
}, [clearClose]);
|
|
45
|
+
const cancelClose = useCallback(() => {
|
|
46
|
+
clearClose();
|
|
47
|
+
}, [clearClose]);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
return () => {
|
|
50
|
+
clearClose();
|
|
51
|
+
};
|
|
52
|
+
}, [clearClose]);
|
|
53
|
+
const contextValue = useMemo(() => ({
|
|
54
|
+
isOpen,
|
|
55
|
+
direction: menu.direction,
|
|
56
|
+
triggerElement,
|
|
57
|
+
setTriggerElement,
|
|
58
|
+
triggerId: `${id}-trigger`,
|
|
59
|
+
contentId: `${id}-content`,
|
|
60
|
+
open,
|
|
61
|
+
close,
|
|
62
|
+
toggle,
|
|
63
|
+
scheduleClose,
|
|
64
|
+
cancelClose,
|
|
65
|
+
}), [
|
|
66
|
+
cancelClose,
|
|
67
|
+
close,
|
|
68
|
+
id,
|
|
69
|
+
isOpen,
|
|
70
|
+
menu.direction,
|
|
71
|
+
open,
|
|
72
|
+
scheduleClose,
|
|
73
|
+
toggle,
|
|
74
|
+
triggerElement,
|
|
75
|
+
]);
|
|
76
|
+
return (_jsx(ContextMenuSubContext.Provider, { value: contextValue, children: children }));
|
|
77
|
+
}
|
|
78
|
+
export const ContextMenuSubTrigger = forwardRef(function ContextMenuSubTrigger({ asChild = false, disabled = false, onPointerEnter, onPointerLeave, onClick, onKeyDown, children, ...rest }, forwardedRef) {
|
|
79
|
+
const sub = useContextMenuSubContext("ContextMenuSubTrigger");
|
|
80
|
+
const refs = useMemo(() => composeRefs(forwardedRef, sub.setTriggerElement), [forwardedRef, sub.setTriggerElement]);
|
|
81
|
+
const handlePointerEnter = (event) => {
|
|
82
|
+
onPointerEnter?.(event);
|
|
83
|
+
if (event.defaultPrevented || disabled) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
sub.cancelClose();
|
|
87
|
+
sub.open();
|
|
88
|
+
};
|
|
89
|
+
const handlePointerLeave = (event) => {
|
|
90
|
+
onPointerLeave?.(event);
|
|
91
|
+
if (event.defaultPrevented || disabled) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
sub.scheduleClose();
|
|
95
|
+
};
|
|
96
|
+
const handleClick = (event) => {
|
|
97
|
+
onClick?.(event);
|
|
98
|
+
if (event.defaultPrevented || disabled) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
sub.toggle();
|
|
102
|
+
};
|
|
103
|
+
const handleKeyDown = (event) => {
|
|
104
|
+
onKeyDown?.(event);
|
|
105
|
+
if (event.defaultPrevented || disabled) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const openKey = sub.direction === "rtl" ? "ArrowLeft" : "ArrowRight";
|
|
109
|
+
const closeKey = sub.direction === "rtl" ? "ArrowRight" : "ArrowLeft";
|
|
110
|
+
if (event.key === openKey) {
|
|
111
|
+
event.preventDefault();
|
|
112
|
+
event.stopPropagation();
|
|
113
|
+
sub.cancelClose();
|
|
114
|
+
sub.open();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (event.key === closeKey && sub.isOpen) {
|
|
118
|
+
event.preventDefault();
|
|
119
|
+
event.stopPropagation();
|
|
120
|
+
sub.close();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (event.key === "Enter" || event.key === " ") {
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
event.stopPropagation();
|
|
126
|
+
sub.open();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const shared = {
|
|
130
|
+
...rest,
|
|
131
|
+
role: "menuitem",
|
|
132
|
+
tabIndex: -1,
|
|
133
|
+
id: rest.id ?? sub.triggerId,
|
|
134
|
+
"aria-haspopup": "menu",
|
|
135
|
+
"aria-expanded": sub.isOpen,
|
|
136
|
+
"aria-controls": sub.isOpen ? sub.contentId : undefined,
|
|
137
|
+
"aria-disabled": disabled ? true : undefined,
|
|
138
|
+
"data-state": sub.isOpen ? "open" : "closed",
|
|
139
|
+
"data-disabled": disabled ? "" : undefined,
|
|
140
|
+
"data-context-menu-subtrigger": "",
|
|
141
|
+
onPointerEnter: handlePointerEnter,
|
|
142
|
+
onPointerLeave: handlePointerLeave,
|
|
143
|
+
onClick: handleClick,
|
|
144
|
+
onKeyDown: handleKeyDown,
|
|
145
|
+
};
|
|
146
|
+
if (asChild) {
|
|
147
|
+
return (_jsx(Slot, { ...shared, ref: refs, children: children }));
|
|
148
|
+
}
|
|
149
|
+
return (_jsx("div", { ...shared, ref: refs, children: children }));
|
|
150
|
+
});
|
|
151
|
+
export const ContextMenuSubContent = forwardRef(function ContextMenuSubContent({ side, align = "start", sideOffset = 0, alignOffset = 0, collisionBoundary, collisionPadding = 0, collisionAvoidance, positionMethod, portal = true, animation, ...rest }, forwardedRef) {
|
|
152
|
+
const sub = useContextMenuSubContext("ContextMenuSubContent");
|
|
153
|
+
const resolvedSide = side ?? (sub.direction === "rtl" ? "left" : "right");
|
|
154
|
+
const placement = (align === "center" ? resolvedSide : `${resolvedSide}-${align}`);
|
|
155
|
+
const handleDismiss = useCallback((_reason) => {
|
|
156
|
+
sub.close();
|
|
157
|
+
}, [sub]);
|
|
158
|
+
const handleSubmenuKeyDown = (event) => {
|
|
159
|
+
const closeKey = sub.direction === "rtl" ? "ArrowRight" : "ArrowLeft";
|
|
160
|
+
if (event.key !== closeKey) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
event.stopPropagation();
|
|
165
|
+
sub.close();
|
|
166
|
+
if (sub.triggerElement instanceof HTMLElement) {
|
|
167
|
+
sub.triggerElement.focus();
|
|
168
|
+
}
|
|
169
|
+
return true;
|
|
170
|
+
};
|
|
171
|
+
const presence = useOverlayPresence(sub.isOpen, animation);
|
|
172
|
+
if (!presence.present) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
return (_jsx(OverlayPresenceContext.Provider, { value: presence, children: _jsx(MenuSurface, { ...rest, ref: forwardedRef, reference: sub.triggerElement, boundaryReference: sub.triggerElement, focusRestoreTarget: sub.triggerElement, menuId: sub.contentId, labelledBy: sub.triggerId, placement: placement, offset: sideOffset, alignOffset: alignOffset, padding: collisionPadding, boundary: collisionBoundary, strategy: positionMethod, flip: collisionAvoidance?.flip ?? true, shift: collisionAvoidance?.shift ?? true, portal: portal, dir: sub.direction, submenu: true, open: sub.isOpen, onDismiss: handleDismiss, onSubmenuKeyDown: handleSubmenuKeyDown, onPointerEnterSurface: () => {
|
|
176
|
+
sub.cancelClose();
|
|
177
|
+
}, onPointerLeaveSurface: () => {
|
|
178
|
+
sub.scheduleClose();
|
|
179
|
+
}, onContextMenu: (event) => event.preventDefault() }) }));
|
|
180
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ComponentPropsWithoutRef } from "react";
|
|
2
|
+
import type { ContextMenuDataAttributes } from "./types.js";
|
|
3
|
+
export type ContextMenuTriggerProps = ComponentPropsWithoutRef<"div"> & ContextMenuDataAttributes & {
|
|
4
|
+
asChild?: boolean;
|
|
5
|
+
/** Disable this trigger without disabling other ContextMenu triggers. */
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Right-click/keyboard context-menu target. It intentionally renders a
|
|
10
|
+
* focusable generic element by default rather than a button: a ContextMenu is
|
|
11
|
+
* not a click-triggered menu. Consumers can supply role/label or use asChild.
|
|
12
|
+
*/
|
|
13
|
+
export declare const ContextMenuTrigger: import("react").ForwardRefExoticComponent<Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & ContextMenuDataAttributes & {
|
|
14
|
+
asChild?: boolean;
|
|
15
|
+
/** Disable this trigger without disabling other ContextMenu triggers. */
|
|
16
|
+
disabled?: boolean;
|
|
17
|
+
} & import("react").RefAttributes<HTMLElement>>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { forwardRef, useMemo, useRef, } from "react";
|
|
4
|
+
import { Slot, composeRefs } from "../slot.js";
|
|
5
|
+
import { useContextMenuContext } from "./context.js";
|
|
6
|
+
const KEYBOARD_CONTEXT_MENU_WINDOW = 500;
|
|
7
|
+
/**
|
|
8
|
+
* Right-click/keyboard context-menu target. It intentionally renders a
|
|
9
|
+
* focusable generic element by default rather than a button: a ContextMenu is
|
|
10
|
+
* not a click-triggered menu. Consumers can supply role/label or use asChild.
|
|
11
|
+
*/
|
|
12
|
+
export const ContextMenuTrigger = forwardRef(function ContextMenuTrigger({ asChild = false, disabled = false, onContextMenu, onKeyDown, tabIndex, children, ...rest }, forwardedRef) {
|
|
13
|
+
const menu = useContextMenuContext("ContextMenuTrigger");
|
|
14
|
+
const keyboardInvocationRef = useRef(0);
|
|
15
|
+
const refs = useMemo(() => composeRefs(forwardedRef, menu.setTriggerElement), [forwardedRef, menu.setTriggerElement]);
|
|
16
|
+
const handleContextMenu = (event) => {
|
|
17
|
+
if (event.button === 0 &&
|
|
18
|
+
keyboardInvocationRef.current > 0 &&
|
|
19
|
+
Date.now() - keyboardInvocationRef.current < KEYBOARD_CONTEXT_MENU_WINDOW) {
|
|
20
|
+
keyboardInvocationRef.current = 0;
|
|
21
|
+
event.preventDefault();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
onContextMenu?.(event);
|
|
25
|
+
if (event.defaultPrevented || disabled || menu.disabled) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
event.preventDefault();
|
|
29
|
+
keyboardInvocationRef.current = 0;
|
|
30
|
+
menu.openAt({ x: event.clientX, y: event.clientY }, "trigger");
|
|
31
|
+
};
|
|
32
|
+
const handleKeyDown = (event) => {
|
|
33
|
+
onKeyDown?.(event);
|
|
34
|
+
if (event.defaultPrevented || disabled || menu.disabled) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const isContextMenuKey = event.key === "ContextMenu";
|
|
38
|
+
const isShiftF10 = event.shiftKey && event.key === "F10";
|
|
39
|
+
if (!isContextMenuKey && !isShiftF10) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
keyboardInvocationRef.current = Date.now();
|
|
44
|
+
menu.openAt(null, "trigger");
|
|
45
|
+
};
|
|
46
|
+
const shared = {
|
|
47
|
+
...rest,
|
|
48
|
+
id: rest.id ?? menu.triggerId,
|
|
49
|
+
tabIndex: tabIndex ?? 0,
|
|
50
|
+
"aria-haspopup": "menu",
|
|
51
|
+
"aria-expanded": menu.isOpen,
|
|
52
|
+
"aria-controls": menu.isOpen ? menu.contentId : undefined,
|
|
53
|
+
"data-state": menu.isOpen ? "open" : "closed",
|
|
54
|
+
"data-context-menu-trigger": "",
|
|
55
|
+
onContextMenu: handleContextMenu,
|
|
56
|
+
onKeyDown: handleKeyDown,
|
|
57
|
+
};
|
|
58
|
+
if (asChild) {
|
|
59
|
+
return (_jsx(Slot, { ...shared, ...(disabled ? { disabled: true, "aria-disabled": true } : {}), ref: refs, children: children }));
|
|
60
|
+
}
|
|
61
|
+
return (_jsx("div", { ...shared, ref: refs, children: children }));
|
|
62
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type ContextMenuSide = "top" | "right" | "bottom" | "left";
|
|
2
|
+
export type ContextMenuAlign = "start" | "center" | "end";
|
|
3
|
+
export type ContextMenuCollisionAvoidance = {
|
|
4
|
+
flip?: boolean;
|
|
5
|
+
shift?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export type ContextMenuDataAttributes = {
|
|
8
|
+
[key: `data-${string}`]: string | number | boolean | undefined;
|
|
9
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type HTMLAttributes, type ReactNode } from "react";
|
|
2
|
+
import { type AnimationSetting } from "../../vendor/core/index.js";
|
|
3
|
+
export type DialogFocusTarget = "container" | "first" | Element | (() => Element | null) | null;
|
|
4
|
+
export type DialogPortalProps = {
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
/** Defaults to document.body. */
|
|
7
|
+
container?: Element | null;
|
|
8
|
+
/** Render in place without creating a dialog portal root. */
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
/** Keep portal DOM mounted while closed for consumer transitions. */
|
|
11
|
+
keepMounted?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export type DialogDataAttributes = {
|
|
14
|
+
[key: `data-${string}`]: string | number | boolean | undefined;
|
|
15
|
+
};
|
|
16
|
+
export type DialogBackdropProps = HTMLAttributes<HTMLDivElement> & DialogDataAttributes & {
|
|
17
|
+
/** Keep the backdrop mounted while closed for consumer transitions. */
|
|
18
|
+
keepMounted?: boolean;
|
|
19
|
+
};
|
|
20
|
+
export type DialogViewportProps = HTMLAttributes<HTMLDivElement> & DialogDataAttributes;
|
|
21
|
+
export type DialogPopupProps = HTMLAttributes<HTMLDivElement> & DialogDataAttributes & {
|
|
22
|
+
/**
|
|
23
|
+
* Modal default is "container" (the popup itself, tabIndex=-1).
|
|
24
|
+
* Non-modal default is no initial focus unless this is supplied.
|
|
25
|
+
*/
|
|
26
|
+
initialFocus?: DialogFocusTarget;
|
|
27
|
+
/** Final focus target override; defaults to the trigger/captured element. */
|
|
28
|
+
finalFocus?: Element | (() => Element | null) | null;
|
|
29
|
+
/** Defaults to true. */
|
|
30
|
+
restoreFocus?: boolean;
|
|
31
|
+
/** Keep the popup mounted while closed for consumer transitions. */
|
|
32
|
+
keepMounted?: boolean;
|
|
33
|
+
/** Fade/custom enter and exit animation. Disabled by default; pass true or a config. */
|
|
34
|
+
animation?: AnimationSetting;
|
|
35
|
+
};
|
|
36
|
+
export type DialogContentProps = Omit<DialogPopupProps, "children"> & {
|
|
37
|
+
children?: ReactNode;
|
|
38
|
+
/** Defaults to true; modal dialogs also render a backdrop by default. */
|
|
39
|
+
portal?: boolean | Element | null;
|
|
40
|
+
/** Defaults to the Dialog modal setting. */
|
|
41
|
+
backdrop?: boolean;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Portal wrapper used by DialogContent and available for advanced composition.
|
|
45
|
+
* The wrapper is a body-level interaction boundary for background inertness.
|
|
46
|
+
*/
|
|
47
|
+
export declare function DialogPortal({ children, container, disabled, keepMounted, }: DialogPortalProps): import("react").JSX.Element | null;
|
|
48
|
+
/**
|
|
49
|
+
* Optional structural backdrop. It has no styling and no built-in close button.
|
|
50
|
+
* Clicking it is outside the Dialog popup and follows outside-dismissal rules.
|
|
51
|
+
*/
|
|
52
|
+
export declare const DialogBackdrop: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & DialogDataAttributes & {
|
|
53
|
+
/** Keep the backdrop mounted while closed for consumer transitions. */
|
|
54
|
+
keepMounted?: boolean;
|
|
55
|
+
} & import("react").RefAttributes<HTMLDivElement>>;
|
|
56
|
+
/** Structural layout wrapper. No display, padding, or sizing styles are applied. */
|
|
57
|
+
export declare const DialogViewport: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & DialogDataAttributes & import("react").RefAttributes<HTMLDivElement>>;
|
|
58
|
+
/**
|
|
59
|
+
* Dialog popup. Owns layer registration, modal focus containment, initial/final
|
|
60
|
+
* focus, scroll locking, and dialog ARIA/data attributes.
|
|
61
|
+
*/
|
|
62
|
+
export declare const DialogPopup: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & DialogDataAttributes & {
|
|
63
|
+
/**
|
|
64
|
+
* Modal default is "container" (the popup itself, tabIndex=-1).
|
|
65
|
+
* Non-modal default is no initial focus unless this is supplied.
|
|
66
|
+
*/
|
|
67
|
+
initialFocus?: DialogFocusTarget;
|
|
68
|
+
/** Final focus target override; defaults to the trigger/captured element. */
|
|
69
|
+
finalFocus?: Element | (() => Element | null) | null;
|
|
70
|
+
/** Defaults to true. */
|
|
71
|
+
restoreFocus?: boolean;
|
|
72
|
+
/** Keep the popup mounted while closed for consumer transitions. */
|
|
73
|
+
keepMounted?: boolean;
|
|
74
|
+
/** Fade/custom enter and exit animation. Disabled by default; pass true or a config. */
|
|
75
|
+
animation?: AnimationSetting;
|
|
76
|
+
} & import("react").RefAttributes<HTMLDivElement>>;
|
|
77
|
+
/**
|
|
78
|
+
* Convenience composition: Portal -> optional Backdrop -> Viewport -> Popup.
|
|
79
|
+
* No layout, sizing, colors, or overflow styles are applied.
|
|
80
|
+
*/
|
|
81
|
+
export declare const DialogContent: import("react").ForwardRefExoticComponent<Omit<DialogPopupProps, "children"> & {
|
|
82
|
+
children?: ReactNode;
|
|
83
|
+
/** Defaults to true; modal dialogs also render a backdrop by default. */
|
|
84
|
+
portal?: boolean | Element | null;
|
|
85
|
+
/** Defaults to the Dialog modal setting. */
|
|
86
|
+
backdrop?: boolean;
|
|
87
|
+
} & import("react").RefAttributes<HTMLDivElement>>;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { forwardRef, useEffect, useMemo, useRef, useState, } from "react";
|
|
4
|
+
import { createFocusContainment, focusElement, focusFirst, getActiveElement, lockBodyScroll, restoreFocus, } from "../../vendor/core/index.js";
|
|
5
|
+
import { useLayer } from "../../hooks/use-layer.js";
|
|
6
|
+
import { Portal } from "../portal.js";
|
|
7
|
+
import { OverlayLayerContext, OverlayPortalContainerContext, } from "../overlay-context.js";
|
|
8
|
+
import { DialogContentContext, DialogNestingContext, DialogPortalContext, useDialogContext, useDialogPortalContext, } from "./context.js";
|
|
9
|
+
import { useModalBackground } from "./modal-background.js";
|
|
10
|
+
import { composeRefs } from "../slot.js";
|
|
11
|
+
import { OverlayPresenceContext, useOverlayElementAnimation, useOverlayPresence, useOverlayPresenceContext, } from "../../animation/presence.js";
|
|
12
|
+
function toDialogReason(reason) {
|
|
13
|
+
switch (reason) {
|
|
14
|
+
case "escape-key":
|
|
15
|
+
return "escape";
|
|
16
|
+
case "outside-press":
|
|
17
|
+
return "outside-press";
|
|
18
|
+
case "reference-press":
|
|
19
|
+
return "trigger";
|
|
20
|
+
default:
|
|
21
|
+
return "programmatic";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function resolveTarget(target) {
|
|
25
|
+
return typeof target === "function" ? target() : (target ?? null);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Portal wrapper used by DialogContent and available for advanced composition.
|
|
29
|
+
* The wrapper is a body-level interaction boundary for background inertness.
|
|
30
|
+
*/
|
|
31
|
+
export function DialogPortal({ children, container, disabled = false, keepMounted = false, }) {
|
|
32
|
+
const dialog = useDialogContext("DialogPortal");
|
|
33
|
+
const presence = useOverlayPresenceContext();
|
|
34
|
+
const [root, setRoot] = useState(null);
|
|
35
|
+
if (!dialog.isOpen && !keepMounted && !presence?.present) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
if (disabled) {
|
|
39
|
+
return (_jsx(DialogPortalContext.Provider, { value: { root: null }, children: children }));
|
|
40
|
+
}
|
|
41
|
+
return (_jsx(Portal, { container: container, children: _jsx(DialogPortalContext.Provider, { value: { root }, children: _jsx("div", { ref: setRoot, "data-dialog-portal": "", children: children }) }) }));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Optional structural backdrop. It has no styling and no built-in close button.
|
|
45
|
+
* Clicking it is outside the Dialog popup and follows outside-dismissal rules.
|
|
46
|
+
*/
|
|
47
|
+
export const DialogBackdrop = forwardRef(function DialogBackdrop({ children, keepMounted = false, ...rest }, forwardedRef) {
|
|
48
|
+
const dialog = useDialogContext("DialogBackdrop");
|
|
49
|
+
const presence = useOverlayPresenceContext();
|
|
50
|
+
if (!dialog.isOpen && !keepMounted && !presence?.present) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return (_jsx("div", { ...rest, ref: forwardedRef, hidden: !dialog.isOpen && !presence?.present ? true : undefined, "aria-hidden": "true", "data-dialog-backdrop": "", "data-state": dialog.isOpen ? "open" : "closed", "data-open": dialog.isOpen ? "" : undefined, "data-closed": dialog.isOpen ? undefined : "", "data-modal": dialog.modal ? "" : undefined, "data-non-modal": dialog.modal ? undefined : "", children: children }));
|
|
54
|
+
});
|
|
55
|
+
/** Structural layout wrapper. No display, padding, or sizing styles are applied. */
|
|
56
|
+
export const DialogViewport = forwardRef(function DialogViewport(props, forwardedRef) {
|
|
57
|
+
const dialog = useDialogContext("DialogViewport");
|
|
58
|
+
return (_jsx("div", { ...props, ref: forwardedRef, "data-dialog-viewport": "", "data-modal": dialog.modal ? "" : undefined, "data-non-modal": dialog.modal ? undefined : "" }));
|
|
59
|
+
});
|
|
60
|
+
/**
|
|
61
|
+
* Dialog popup. Owns layer registration, modal focus containment, initial/final
|
|
62
|
+
* focus, scroll locking, and dialog ARIA/data attributes.
|
|
63
|
+
*/
|
|
64
|
+
export const DialogPopup = forwardRef(function DialogPopup({ children, initialFocus, finalFocus, restoreFocus: restoreOnClose = true, keepMounted = false, animation, ...rest }, forwardedRef) {
|
|
65
|
+
const dialog = useDialogContext("DialogPopup");
|
|
66
|
+
const inheritedPresence = useOverlayPresenceContext();
|
|
67
|
+
const localPresence = useOverlayPresence(dialog.isOpen, animation);
|
|
68
|
+
const presence = inheritedPresence ?? localPresence;
|
|
69
|
+
const { root: portalRoot } = useDialogPortalContext();
|
|
70
|
+
const [popupElement, setPopupElement] = useState(null);
|
|
71
|
+
const [titleId, setTitleId] = useState(null);
|
|
72
|
+
const [descriptionId, setDescriptionId] = useState(null);
|
|
73
|
+
const [nestedOpenCount, setNestedOpenCount] = useState(0);
|
|
74
|
+
const focusInsideRef = useRef(false);
|
|
75
|
+
const initialFocusAppliedRef = useRef(false);
|
|
76
|
+
const capturedFocusRef = useRef(null);
|
|
77
|
+
const triggerElementRef = useRef(dialog.triggerElement);
|
|
78
|
+
const finalFocusRef = useRef(finalFocus);
|
|
79
|
+
const restoreOnCloseRef = useRef(restoreOnClose);
|
|
80
|
+
triggerElementRef.current = dialog.triggerElement;
|
|
81
|
+
finalFocusRef.current = finalFocus;
|
|
82
|
+
restoreOnCloseRef.current = restoreOnClose;
|
|
83
|
+
const layer = useLayer({
|
|
84
|
+
element: popupElement,
|
|
85
|
+
reference: dialog.triggerElement,
|
|
86
|
+
parent: dialog.parentLayer,
|
|
87
|
+
dismissOnEscape: dialog.dismissOnEscape,
|
|
88
|
+
dismissOnOutsidePress: dialog.dismissOnOutsidePress,
|
|
89
|
+
disabled: !dialog.isOpen,
|
|
90
|
+
onDismiss: (reason) => {
|
|
91
|
+
dialog.close(toDialogReason(reason));
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
const refs = useMemo(() => composeRefs(forwardedRef, setPopupElement), [forwardedRef]);
|
|
95
|
+
useOverlayElementAnimation({
|
|
96
|
+
element: popupElement,
|
|
97
|
+
phase: presence.phase,
|
|
98
|
+
animation: presence.animation,
|
|
99
|
+
onExitComplete: presence.finishExit,
|
|
100
|
+
});
|
|
101
|
+
useModalBackground(portalRoot, dialog.isOpen && dialog.modal);
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
if (!dialog.isOpen ||
|
|
104
|
+
!dialog.modal ||
|
|
105
|
+
!dialog.lockScroll ||
|
|
106
|
+
!popupElement) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
return lockBodyScroll(popupElement.ownerDocument);
|
|
110
|
+
}, [dialog.isOpen, dialog.lockScroll, dialog.modal, popupElement]);
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
if (!dialog.isOpen || !popupElement) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const handleFocusIn = () => {
|
|
116
|
+
focusInsideRef.current = true;
|
|
117
|
+
};
|
|
118
|
+
const handleFocusOut = (event) => {
|
|
119
|
+
const next = event.relatedTarget;
|
|
120
|
+
if (!next || !popupElement.contains(next)) {
|
|
121
|
+
focusInsideRef.current = false;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
popupElement.addEventListener("focusin", handleFocusIn);
|
|
125
|
+
popupElement.addEventListener("focusout", handleFocusOut);
|
|
126
|
+
return () => {
|
|
127
|
+
popupElement.removeEventListener("focusin", handleFocusIn);
|
|
128
|
+
popupElement.removeEventListener("focusout", handleFocusOut);
|
|
129
|
+
};
|
|
130
|
+
}, [dialog.isOpen, popupElement]);
|
|
131
|
+
// Capture the restore target before initial focus moves into the Dialog.
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
if (!dialog.isOpen || !popupElement) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
capturedFocusRef.current =
|
|
137
|
+
triggerElementRef.current ??
|
|
138
|
+
getActiveElement(popupElement.ownerDocument);
|
|
139
|
+
return () => {
|
|
140
|
+
const documentRef = popupElement.ownerDocument;
|
|
141
|
+
const active = getActiveElement(documentRef);
|
|
142
|
+
const focusWasInside = focusInsideRef.current ||
|
|
143
|
+
(active !== null && popupElement.contains(active));
|
|
144
|
+
focusInsideRef.current = false;
|
|
145
|
+
if (!restoreOnCloseRef.current) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (!focusWasInside && active !== null && active !== documentRef.body) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const target = resolveTarget(finalFocusRef.current) ??
|
|
152
|
+
triggerElementRef.current ??
|
|
153
|
+
capturedFocusRef.current;
|
|
154
|
+
if (!target) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (!restoreFocus(target)) {
|
|
158
|
+
queueMicrotask(() => {
|
|
159
|
+
restoreFocus(target);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}, [dialog.isOpen, popupElement]);
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
if (!dialog.isOpen || !popupElement || initialFocusAppliedRef.current) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (dialog.modal && !layer.isTopmost) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const requested = initialFocus ?? (dialog.modal ? "container" : null);
|
|
172
|
+
if (requested === null) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
initialFocusAppliedRef.current = true;
|
|
176
|
+
if (requested === "container") {
|
|
177
|
+
focusElement(popupElement);
|
|
178
|
+
}
|
|
179
|
+
else if (requested === "first") {
|
|
180
|
+
focusFirst(popupElement);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
focusElement(resolveTarget(requested));
|
|
184
|
+
}
|
|
185
|
+
}, [
|
|
186
|
+
dialog.isOpen,
|
|
187
|
+
dialog.modal,
|
|
188
|
+
initialFocus,
|
|
189
|
+
layer.isTopmost,
|
|
190
|
+
popupElement,
|
|
191
|
+
]);
|
|
192
|
+
useEffect(() => {
|
|
193
|
+
if (!dialog.isOpen) {
|
|
194
|
+
initialFocusAppliedRef.current = false;
|
|
195
|
+
focusInsideRef.current = false;
|
|
196
|
+
}
|
|
197
|
+
}, [dialog.isOpen]);
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
if (!dialog.modal || !layer.isTopmost || !popupElement) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const containment = createFocusContainment({
|
|
203
|
+
container: popupElement,
|
|
204
|
+
enabled: true,
|
|
205
|
+
restoreFocus: false,
|
|
206
|
+
});
|
|
207
|
+
return () => {
|
|
208
|
+
containment.dispose();
|
|
209
|
+
};
|
|
210
|
+
}, [dialog.modal, layer.isTopmost, popupElement]);
|
|
211
|
+
const labelContext = useMemo(() => ({ setTitleId, setDescriptionId }), []);
|
|
212
|
+
const nestingContext = useMemo(() => ({
|
|
213
|
+
reportNestedOpen(open) {
|
|
214
|
+
setNestedOpenCount((count) => Math.max(0, count + (open ? 1 : -1)));
|
|
215
|
+
},
|
|
216
|
+
}), []);
|
|
217
|
+
const layerContext = useMemo(() => ({ handle: layer.handle, isTopmost: layer.isTopmost }), [layer.handle, layer.isTopmost]);
|
|
218
|
+
if (!dialog.isOpen && !keepMounted && !presence.present) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
const style = { ...rest.style };
|
|
222
|
+
return (_jsx(OverlayLayerContext.Provider, { value: layerContext, children: _jsx(OverlayPortalContainerContext.Provider, { value: popupElement, children: _jsx(DialogNestingContext.Provider, { value: nestingContext, children: _jsx(DialogContentContext.Provider, { value: labelContext, children: _jsx("div", { ...rest, ref: refs, id: dialog.contentId, hidden: !dialog.isOpen && !presence.present ? true : undefined, role: rest.role ?? "dialog", "aria-modal": dialog.isOpen && dialog.modal ? true : undefined, "aria-labelledby": rest["aria-labelledby"] ?? titleId ?? undefined, "aria-describedby": rest["aria-describedby"] ?? descriptionId ?? undefined, tabIndex: rest.tabIndex ?? -1, dir: rest.dir ?? (dialog.direction === "rtl" ? "rtl" : undefined), "data-dialog-popup": "", "data-state": dialog.isOpen ? "open" : "closed", "data-open": dialog.isOpen ? "" : undefined, "data-closed": dialog.isOpen ? undefined : "", "data-modal": dialog.modal ? "" : undefined, "data-non-modal": dialog.modal ? undefined : "", "data-nested": dialog.nested ? "" : undefined, "data-nested-dialog-open": nestedOpenCount > 0 ? "" : undefined, style: style, children: children }) }) }) }) }));
|
|
223
|
+
});
|
|
224
|
+
/**
|
|
225
|
+
* Convenience composition: Portal -> optional Backdrop -> Viewport -> Popup.
|
|
226
|
+
* No layout, sizing, colors, or overflow styles are applied.
|
|
227
|
+
*/
|
|
228
|
+
export const DialogContent = forwardRef(function DialogContent({ portal = true, backdrop, animation, children, ...popupProps }, forwardedRef) {
|
|
229
|
+
const dialog = useDialogContext("DialogContent");
|
|
230
|
+
const keepMounted = popupProps.keepMounted ?? false;
|
|
231
|
+
const presence = useOverlayPresence(dialog.isOpen, animation);
|
|
232
|
+
if (!dialog.isOpen && !keepMounted && !presence.present) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
const showBackdrop = backdrop ?? dialog.modal;
|
|
236
|
+
const content = (_jsxs(_Fragment, { children: [showBackdrop ? _jsx(DialogBackdrop, { keepMounted: keepMounted }) : null, _jsx(DialogViewport, { children: _jsx(DialogPopup, { ...popupProps, animation: animation, ref: forwardedRef, children: children }) })] }));
|
|
237
|
+
return (_jsx(OverlayPresenceContext.Provider, { value: presence, children: portal === false ? (content) : (_jsx(DialogPortal, { container: typeof portal === "object" && portal !== null ? portal : undefined, keepMounted: keepMounted, children: content })) }));
|
|
238
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { LayerStackHandle } from "../../vendor/core/index.js";
|
|
2
|
+
export type DialogChangeReason = "trigger" | "outside-press" | "escape" | "close-button" | "programmatic";
|
|
3
|
+
export type DialogDirection = "ltr" | "rtl";
|
|
4
|
+
export type DialogContextValue = {
|
|
5
|
+
isOpen: boolean;
|
|
6
|
+
open(): void;
|
|
7
|
+
close(reason?: DialogChangeReason): void;
|
|
8
|
+
toggle(reason?: DialogChangeReason): 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: DialogDirection;
|
|
16
|
+
modal: boolean;
|
|
17
|
+
nested: boolean;
|
|
18
|
+
lockScroll: boolean;
|
|
19
|
+
dismissOnEscape: boolean;
|
|
20
|
+
dismissOnOutsidePress: boolean;
|
|
21
|
+
parentLayer: LayerStackHandle | null;
|
|
22
|
+
};
|
|
23
|
+
export declare const DialogContext: import("react").Context<DialogContextValue | null>;
|
|
24
|
+
export declare function useDialogContext(part: string): DialogContextValue;
|
|
25
|
+
export type DialogNestingContextValue = {
|
|
26
|
+
reportNestedOpen(open: boolean): void;
|
|
27
|
+
};
|
|
28
|
+
/** Supplied by a DialogPopup to descendants so nested Dialog roots can report state. */
|
|
29
|
+
export declare const DialogNestingContext: import("react").Context<DialogNestingContextValue | null>;
|
|
30
|
+
export type DialogContentContextValue = {
|
|
31
|
+
setTitleId(id: string | null): void;
|
|
32
|
+
setDescriptionId(id: string | null): void;
|
|
33
|
+
};
|
|
34
|
+
export declare const DialogContentContext: import("react").Context<DialogContentContextValue | null>;
|
|
35
|
+
export declare function useDialogContentContext(part: string): DialogContentContextValue;
|
|
36
|
+
export type DialogPortalContextValue = {
|
|
37
|
+
/** Top-level dialog portal wrapper, when rendered through DialogPortal. */
|
|
38
|
+
root: HTMLElement | null;
|
|
39
|
+
};
|
|
40
|
+
export declare const DialogPortalContext: import("react").Context<DialogPortalContextValue>;
|
|
41
|
+
export declare function useDialogPortalContext(): DialogPortalContextValue;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
export const DialogContext = createContext(null);
|
|
4
|
+
export function useDialogContext(part) {
|
|
5
|
+
const context = useContext(DialogContext);
|
|
6
|
+
if (!context) {
|
|
7
|
+
throw new Error(`[overlay-react] ${part} must be rendered inside <Dialog>.`);
|
|
8
|
+
}
|
|
9
|
+
return context;
|
|
10
|
+
}
|
|
11
|
+
/** Supplied by a DialogPopup to descendants so nested Dialog roots can report state. */
|
|
12
|
+
export const DialogNestingContext = createContext(null);
|
|
13
|
+
export const DialogContentContext = createContext(null);
|
|
14
|
+
export function useDialogContentContext(part) {
|
|
15
|
+
const context = useContext(DialogContentContext);
|
|
16
|
+
if (!context) {
|
|
17
|
+
throw new Error(`[overlay-react] ${part} must be rendered inside <DialogPopup> or <DialogContent>.`);
|
|
18
|
+
}
|
|
19
|
+
return context;
|
|
20
|
+
}
|
|
21
|
+
export const DialogPortalContext = createContext({
|
|
22
|
+
root: null,
|
|
23
|
+
});
|
|
24
|
+
export function useDialogPortalContext() {
|
|
25
|
+
return useContext(DialogPortalContext);
|
|
26
|
+
}
|