UNPKG

9.99 kBJavaScriptView Raw
1"use strict";
2/**
3 * @reach/dialog v0.18.0
4 *
5 * Copyright (c) 2018-2022, React Training LLC
6 *
7 * This source code is licensed under the MIT license found in the
8 * LICENSE.md file in the root directory of this source tree.
9 *
10 * @license MIT
11 */
12
13var __create = Object.create;
14var __defProp = Object.defineProperty;
15var __defProps = Object.defineProperties;
16var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
17var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
18var __getOwnPropNames = Object.getOwnPropertyNames;
19var __getOwnPropSymbols = Object.getOwnPropertySymbols;
20var __getProtoOf = Object.getPrototypeOf;
21var __hasOwnProp = Object.prototype.hasOwnProperty;
22var __propIsEnum = Object.prototype.propertyIsEnumerable;
23var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
24var __spreadValues = (a, b) => {
25 for (var prop in b || (b = {}))
26 if (__hasOwnProp.call(b, prop))
27 __defNormalProp(a, prop, b[prop]);
28 if (__getOwnPropSymbols)
29 for (var prop of __getOwnPropSymbols(b)) {
30 if (__propIsEnum.call(b, prop))
31 __defNormalProp(a, prop, b[prop]);
32 }
33 return a;
34};
35var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
36var __objRest = (source, exclude) => {
37 var target = {};
38 for (var prop in source)
39 if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
40 target[prop] = source[prop];
41 if (source != null && __getOwnPropSymbols)
42 for (var prop of __getOwnPropSymbols(source)) {
43 if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
44 target[prop] = source[prop];
45 }
46 return target;
47};
48var __export = (target, all) => {
49 for (var name in all)
50 __defProp(target, name, { get: all[name], enumerable: true });
51};
52var __copyProps = (to, from, except, desc) => {
53 if (from && typeof from === "object" || typeof from === "function") {
54 for (let key of __getOwnPropNames(from))
55 if (!__hasOwnProp.call(to, key) && key !== except)
56 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
57 }
58 return to;
59};
60var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
61var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
62
63// src/reach-dialog.tsx
64var reach_dialog_exports = {};
65__export(reach_dialog_exports, {
66 Dialog: () => Dialog,
67 DialogContent: () => DialogContent,
68 DialogInner: () => DialogInner,
69 DialogOverlay: () => DialogOverlay,
70 unstable_DialogWrapper: () => DialogWrapper
71});
72module.exports = __toCommonJS(reach_dialog_exports);
73var React = __toESM(require("react"));
74var import_portal = require("@reach/portal");
75var import_utils = require("@reach/utils");
76var import_react_focus_lock = __toESM(require("react-focus-lock"));
77var import_react_remove_scroll = require("react-remove-scroll");
78var [DialogContextProvider, useDialogContext] = (0, import_utils.createContext)("DialogContext", {
79 isOpen: false
80});
81function DialogWrapper(_a) {
82 var _b = _a, {
83 isOpen = true,
84 children
85 } = _b, props = __objRest(_b, [
86 "isOpen",
87 "children"
88 ]);
89 React.useEffect(() => {
90 if (isOpen) {
91 window.__REACH_DISABLE_TOOLTIPS = true;
92 } else {
93 window.requestAnimationFrame(() => {
94 window.__REACH_DISABLE_TOOLTIPS = false;
95 });
96 }
97 }, [isOpen]);
98 return /* @__PURE__ */ React.createElement(import_portal.Portal, __spreadValues({
99 "data-reach-dialog-wrapper": "",
100 "data-state": isOpen ? "open" /* Open */ : "closed" /* Closed */
101 }, props), /* @__PURE__ */ React.createElement(DialogContextProvider, {
102 isOpen
103 }, children));
104}
105DialogWrapper.displayName = "unstable_DialogWrapper";
106var DialogInner = React.forwardRef(function DialogInner2(_a, forwardedRef) {
107 var _b = _a, {
108 allowPinchZoom,
109 as: Comp = "div",
110 dangerouslyBypassFocusLock,
111 dangerouslyBypassScrollLock,
112 initialFocusRef,
113 onClick,
114 onDismiss = import_utils.noop,
115 onKeyDown,
116 onMouseDown,
117 unstable_lockFocusAcrossFrames
118 } = _b, props = __objRest(_b, [
119 "allowPinchZoom",
120 "as",
121 "dangerouslyBypassFocusLock",
122 "dangerouslyBypassScrollLock",
123 "initialFocusRef",
124 "onClick",
125 "onDismiss",
126 "onKeyDown",
127 "onMouseDown",
128 "unstable_lockFocusAcrossFrames"
129 ]);
130 let { isOpen } = useDialogContext("DialogInner");
131 let lockFocusAcrossFramesIsDefined = unstable_lockFocusAcrossFrames !== void 0;
132 if (true) {
133 React.useEffect(() => {
134 if (lockFocusAcrossFramesIsDefined) {
135 console.warn(`The unstable_lockFocusAcrossFrames in @reach/dialog is deprecated. It will be removed in the next minor release.`);
136 }
137 }, [lockFocusAcrossFramesIsDefined]);
138 }
139 const mouseDownTarget = React.useRef(null);
140 const overlayNode = React.useRef(null);
141 const ref = (0, import_utils.useComposedRefs)(overlayNode, forwardedRef);
142 const activateFocusLock = React.useCallback(() => {
143 if (initialFocusRef && initialFocusRef.current) {
144 initialFocusRef.current.focus();
145 }
146 }, [initialFocusRef]);
147 function handleClick(event) {
148 if (mouseDownTarget.current === event.target) {
149 event.stopPropagation();
150 onDismiss(event);
151 }
152 }
153 function handleKeyDown(event) {
154 if (event.key === "Escape") {
155 event.stopPropagation();
156 onDismiss(event);
157 }
158 }
159 function handleMouseDown(event) {
160 mouseDownTarget.current = event.target;
161 }
162 React.useEffect(() => {
163 return overlayNode.current ? createAriaHider(overlayNode.current) : void 0;
164 }, []);
165 return /* @__PURE__ */ React.createElement(import_react_focus_lock.default, {
166 autoFocus: true,
167 returnFocus: true,
168 onActivation: activateFocusLock,
169 disabled: dangerouslyBypassFocusLock != null ? dangerouslyBypassFocusLock : !isOpen,
170 crossFrame: unstable_lockFocusAcrossFrames != null ? unstable_lockFocusAcrossFrames : true
171 }, /* @__PURE__ */ React.createElement(import_react_remove_scroll.RemoveScroll, {
172 allowPinchZoom,
173 enabled: dangerouslyBypassScrollLock != null ? !dangerouslyBypassScrollLock : isOpen
174 }, /* @__PURE__ */ React.createElement(Comp, __spreadProps(__spreadValues({}, props), {
175 ref,
176 "data-reach-dialog-inner": "",
177 "data-state": isOpen ? "open" /* Open */ : "closed" /* Closed */,
178 onClick: (0, import_utils.composeEventHandlers)(onClick, handleClick),
179 onKeyDown: (0, import_utils.composeEventHandlers)(onKeyDown, handleKeyDown),
180 onMouseDown: (0, import_utils.composeEventHandlers)(onMouseDown, handleMouseDown)
181 }))));
182});
183DialogInner.displayName = "DialogInner";
184var DialogOverlay = React.forwardRef(function DialogOverlay2(_a, forwardedRef) {
185 var _b = _a, { as: Comp = "div", isOpen = true } = _b, props = __objRest(_b, ["as", "isOpen"]);
186 return isOpen ? /* @__PURE__ */ React.createElement(DialogWrapper, {
187 isOpen
188 }, /* @__PURE__ */ React.createElement(DialogInner, __spreadValues({
189 "data-reach-dialog-overlay": "",
190 ref: forwardedRef,
191 as: Comp
192 }, props))) : null;
193});
194DialogOverlay.displayName = "DialogOverlay";
195var DialogContent = React.forwardRef(function DialogContent2(_a, forwardedRef) {
196 var _b = _a, { as: Comp = "div", onClick, onKeyDown } = _b, props = __objRest(_b, ["as", "onClick", "onKeyDown"]);
197 let { isOpen } = useDialogContext("DialogContent");
198 return /* @__PURE__ */ React.createElement(Comp, __spreadProps(__spreadValues({
199 "aria-modal": "true",
200 role: "dialog",
201 tabIndex: -1
202 }, props), {
203 ref: forwardedRef,
204 "data-reach-dialog-content": "",
205 "data-state": isOpen ? "open" /* Open */ : "closed" /* Closed */,
206 onClick: (0, import_utils.composeEventHandlers)(onClick, (event) => {
207 event.stopPropagation();
208 })
209 }));
210});
211DialogContent.displayName = "DialogContent";
212var Dialog = React.forwardRef(function Dialog2(_a, forwardedRef) {
213 var _b = _a, {
214 allowPinchZoom = false,
215 initialFocusRef,
216 isOpen,
217 onDismiss = import_utils.noop
218 } = _b, props = __objRest(_b, [
219 "allowPinchZoom",
220 "initialFocusRef",
221 "isOpen",
222 "onDismiss"
223 ]);
224 return /* @__PURE__ */ React.createElement(DialogOverlay, {
225 allowPinchZoom,
226 initialFocusRef,
227 isOpen,
228 onDismiss
229 }, /* @__PURE__ */ React.createElement(DialogContent, __spreadValues({
230 ref: forwardedRef
231 }, props)));
232});
233Dialog.displayName = "Dialog";
234function createAriaHider(dialogNode) {
235 let originalValues = [];
236 let rootNodes = [];
237 let ownerDocument = (0, import_utils.getOwnerDocument)(dialogNode);
238 if (!dialogNode) {
239 if (true) {
240 console.warn("A ref has not yet been attached to a dialog node when attempting to call `createAriaHider`.");
241 }
242 return import_utils.noop;
243 }
244 Array.prototype.forEach.call(ownerDocument.querySelectorAll("body > *"), (node) => {
245 var _a, _b;
246 const portalNode = (_b = (_a = dialogNode.parentNode) == null ? void 0 : _a.parentNode) == null ? void 0 : _b.parentNode;
247 if (node === portalNode) {
248 return;
249 }
250 let attr = node.getAttribute("aria-hidden");
251 let alreadyHidden = attr !== null && attr !== "false";
252 if (alreadyHidden) {
253 return;
254 }
255 originalValues.push(attr);
256 rootNodes.push(node);
257 node.setAttribute("aria-hidden", "true");
258 });
259 return () => {
260 rootNodes.forEach((node, index) => {
261 let originalValue = originalValues[index];
262 if (originalValue === null) {
263 node.removeAttribute("aria-hidden");
264 } else {
265 node.setAttribute("aria-hidden", originalValue);
266 }
267 });
268 };
269}
270// Annotate the CommonJS export names for ESM import in node:
2710 && (module.exports = {
272 Dialog,
273 DialogContent,
274 DialogInner,
275 DialogOverlay,
276 unstable_DialogWrapper
277});
278//# sourceMappingURL=reach-dialog.cjs.dev.js.map
\No newline at end of file