@simplybusiness/mobius 4.5.0 → 4.6.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/CHANGELOG.md +12 -0
- package/dist/cjs/components/Button/Button.js.map +1 -1
- package/dist/cjs/components/Drawer/Drawer.js +20 -126
- package/dist/cjs/components/Drawer/Drawer.js.map +1 -1
- package/dist/cjs/components/Modal/Modal.js +19 -125
- package/dist/cjs/components/Modal/Modal.js.map +1 -1
- package/dist/cjs/hooks/index.js +1 -0
- package/dist/cjs/hooks/index.js.map +1 -1
- package/dist/cjs/hooks/useDialog/index.js +20 -0
- package/dist/cjs/hooks/useDialog/index.js.map +1 -0
- package/dist/cjs/hooks/useDialog/useDialog.js +94 -0
- package/dist/cjs/hooks/useDialog/useDialog.js.map +1 -0
- package/dist/cjs/hooks/useDialogPolyfill/index.js +20 -0
- package/dist/cjs/hooks/useDialogPolyfill/index.js.map +1 -0
- package/dist/cjs/hooks/useDialogPolyfill/useDialogPolyfill.js +77 -0
- package/dist/cjs/hooks/useDialogPolyfill/useDialogPolyfill.js.map +1 -0
- package/dist/esm/components/Button/Button.js.map +1 -1
- package/dist/esm/components/Drawer/Drawer.js +22 -87
- package/dist/esm/components/Drawer/Drawer.js.map +1 -1
- package/dist/esm/components/Modal/Modal.js +20 -85
- package/dist/esm/components/Modal/Modal.js.map +1 -1
- package/dist/esm/hooks/index.js +1 -0
- package/dist/esm/hooks/index.js.map +1 -1
- package/dist/esm/hooks/useDialog/index.js +3 -0
- package/dist/esm/hooks/useDialog/index.js.map +1 -0
- package/dist/esm/hooks/useDialog/useDialog.js +84 -0
- package/dist/esm/hooks/useDialog/useDialog.js.map +1 -0
- package/dist/esm/hooks/useDialogPolyfill/index.js +3 -0
- package/dist/esm/hooks/useDialogPolyfill/index.js.map +1 -0
- package/dist/esm/hooks/useDialogPolyfill/useDialogPolyfill.js +27 -0
- package/dist/esm/hooks/useDialogPolyfill/useDialogPolyfill.js.map +1 -0
- package/dist/types/components/Button/Button.d.ts +1 -1
- package/dist/types/components/Button/Button.stories.d.ts +2 -1
- package/dist/types/hooks/index.d.ts +1 -0
- package/dist/types/hooks/useDialog/index.d.ts +1 -0
- package/dist/types/hooks/useDialog/useDialog.d.ts +16 -0
- package/dist/types/hooks/useDialogPolyfill/index.d.ts +1 -0
- package/dist/types/hooks/useDialogPolyfill/useDialogPolyfill.d.ts +2 -0
- package/package.json +2 -1
- package/src/components/Button/Button.stories.tsx +14 -1
- package/src/components/Button/Button.test.tsx +8 -0
- package/src/components/Button/Button.tsx +2 -1
- package/src/components/Drawer/Drawer.stories.tsx +1 -1
- package/src/components/Drawer/Drawer.tsx +22 -100
- package/src/components/Modal/Modal.tsx +20 -98
- package/src/hooks/index.tsx +1 -0
- package/src/hooks/useDialog/index.ts +1 -0
- package/src/hooks/useDialog/useDialog.ts +98 -0
- package/src/hooks/useDialogPolyfill/index.ts +1 -0
- package/src/hooks/useDialogPolyfill/useDialogPolyfill.ts +32 -0
- package/dist/cjs/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_export_star(require("./useDialogPolyfill"), exports);
|
|
6
|
+
function _export_star(from, to) {
|
|
7
|
+
Object.keys(from).forEach(function(k) {
|
|
8
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
9
|
+
Object.defineProperty(to, k, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function() {
|
|
12
|
+
return from[k];
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return from;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/hooks/useDialogPolyfill/index.ts"],"sourcesContent":["export * from \"./useDialogPolyfill\";\n"],"names":[],"mappings":";;;;qBAAc"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "useDialogPolyfill", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return useDialogPolyfill;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _react = require("react");
|
|
12
|
+
const _utils = require("../../utils");
|
|
13
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
14
|
+
if (typeof WeakMap !== "function") return null;
|
|
15
|
+
var cacheBabelInterop = new WeakMap();
|
|
16
|
+
var cacheNodeInterop = new WeakMap();
|
|
17
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
18
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
19
|
+
})(nodeInterop);
|
|
20
|
+
}
|
|
21
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
22
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
23
|
+
return obj;
|
|
24
|
+
}
|
|
25
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
26
|
+
return {
|
|
27
|
+
default: obj
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
31
|
+
if (cache && cache.has(obj)) {
|
|
32
|
+
return cache.get(obj);
|
|
33
|
+
}
|
|
34
|
+
var newObj = {
|
|
35
|
+
__proto__: null
|
|
36
|
+
};
|
|
37
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
38
|
+
for(var key in obj){
|
|
39
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
40
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
41
|
+
if (desc && (desc.get || desc.set)) {
|
|
42
|
+
Object.defineProperty(newObj, key, desc);
|
|
43
|
+
} else {
|
|
44
|
+
newObj[key] = obj[key];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
newObj.default = obj;
|
|
49
|
+
if (cache) {
|
|
50
|
+
cache.set(obj, newObj);
|
|
51
|
+
}
|
|
52
|
+
return newObj;
|
|
53
|
+
}
|
|
54
|
+
const useDialogPolyfill = (ref)=>{
|
|
55
|
+
const hasDialogSupport = (0, _utils.supportsDialog)();
|
|
56
|
+
(0, _react.useEffect)(()=>{
|
|
57
|
+
async function polyfillDialog() {
|
|
58
|
+
if (!hasDialogSupport && typeof window !== "undefined" && ref.current !== null) {
|
|
59
|
+
const { default: dialogPolyfill } = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard(require("dialog-polyfill")));
|
|
60
|
+
try {
|
|
61
|
+
dialogPolyfill.registerDialog(ref.current);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
// In iOS 15 <= 15.2 this intermittently fails with
|
|
64
|
+
// TypeError: null is not an object (evaluating 'element.showModal')
|
|
65
|
+
// Checking showModal presence through hasOwnProperty is falsy natively, truthy with polyfill 🤷🏼♂️
|
|
66
|
+
console.error("Failed to load dialog-polyfill", error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
polyfillDialog();
|
|
71
|
+
}, [
|
|
72
|
+
ref,
|
|
73
|
+
hasDialogSupport
|
|
74
|
+
]);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
//# sourceMappingURL=useDialogPolyfill.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/hooks/useDialogPolyfill/useDialogPolyfill.ts"],"sourcesContent":["import { MutableRefObject, useEffect } from \"react\";\nimport { supportsDialog } from \"../../utils\";\n\n// Add polyfill for HTML Dialog in old browsers\nexport const useDialogPolyfill = (\n ref: MutableRefObject<HTMLDialogElement | null>,\n) => {\n const hasDialogSupport = supportsDialog();\n\n useEffect(() => {\n async function polyfillDialog() {\n if (\n !hasDialogSupport &&\n typeof window !== \"undefined\" &&\n ref.current !== null\n ) {\n const { default: dialogPolyfill } = await import(\"dialog-polyfill\");\n\n try {\n dialogPolyfill.registerDialog(ref.current);\n } catch (error) {\n // In iOS 15 <= 15.2 this intermittently fails with\n // TypeError: null is not an object (evaluating 'element.showModal')\n // Checking showModal presence through hasOwnProperty is falsy natively, truthy with polyfill 🤷🏼♂️\n console.error(\"Failed to load dialog-polyfill\", error);\n }\n }\n }\n\n polyfillDialog();\n }, [ref, hasDialogSupport]);\n};\n"],"names":["useDialogPolyfill","ref","hasDialogSupport","supportsDialog","useEffect","polyfillDialog","window","current","default","dialogPolyfill","registerDialog","error","console"],"mappings":";;;;+BAIaA;;;eAAAA;;;uBAJ+B;uBACb;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGxB,MAAMA,oBAAoB,CAC/BC;IAEA,MAAMC,mBAAmBC,IAAAA,qBAAc;IAEvCC,IAAAA,gBAAS,EAAC;QACR,eAAeC;YACb,IACE,CAACH,oBACD,OAAOI,WAAW,eAClBL,IAAIM,OAAO,KAAK,MAChB;gBACA,MAAM,EAAEC,SAASC,cAAc,EAAE,GAAG,MAAM,mEAAA,QAAO;gBAEjD,IAAI;oBACFA,eAAeC,cAAc,CAACT,IAAIM,OAAO;gBAC3C,EAAE,OAAOI,OAAO;oBACd,mDAAmD;oBACnD,oEAAoE;oBACpE,qGAAqG;oBACrGC,QAAQD,KAAK,CAAC,kCAAkCA;gBAClD;YACF;QACF;QAEAN;IACF,GAAG;QAACJ;QAAKC;KAAiB;AAC5B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/components/Button/Button.tsx"],"sourcesContent":["\"use client\";\n\nimport { Ref, forwardRef, RefAttributes, ReactNode } from \"react\";\nimport classNames from \"classnames/dedupe\";\nimport { DOMProps } from \"../../types/dom\";\nimport { ForwardedRefComponent } from \"../../types/components\";\nimport { Loading } from \"./Loading\";\nimport { UseButtonProps, useButton } from \"../../hooks/useButton\";\n\nlet HAS_DEPRECATION_WARNING_SHOWN = false;\n\nexport type ButtonElementType = HTMLButtonElement;\n\nexport type Variant =\n | \"primary\"\n | \"secondary\"\n | \"ghost\"\n | \"inverse\"\n | \"inverse-ghost\"\n | \"basic\";\n\nexport type Size = \"sm\" | \"md\" | \"lg\";\n\nexport interface ButtonProps\n extends UseButtonProps,\n DOMProps,\n RefAttributes<ButtonElementType> {\n /** Custom class name for setting specific CSS */\n className?: string;\n /** Shortlist of styles */\n variant?: Variant;\n size?: Size;\n /** Display loading spinner */\n isLoading?: boolean;\n /** Display success style */\n isSuccess?: boolean;\n onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void;\n /**\n * **Deprecated:** Please pass icon as part of component's children instead.\n */\n icon?: string;\n /**\n * **Deprecated:** Please pass icon as part of component's children instead.\n */\n iconPosition?: \"left\" | \"right\";\n children?: ReactNode;\n}\n\nexport type ButtonRef = Ref<ButtonElementType>;\n\nconst Button: ForwardedRefComponent<ButtonProps, ButtonElementType> =\n forwardRef((props: ButtonProps, ref: ButtonRef) => {\n const {\n children,\n elementType: Component = \"button\",\n isDisabled,\n isLoading,\n isSuccess,\n variant = \"primary\",\n size = \"md\",\n icon,\n iconPosition,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onPress,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onClick,\n ...otherProps\n } = props;\n const { buttonProps } = useButton(props);\n\n if ((icon || iconPosition) && !HAS_DEPRECATION_WARNING_SHOWN) {\n // eslint-disable-next-line no-console\n console.warn(\n \"icon, iconPosition props have been deprecated in <Button> and will be removed in Mobius v4. Please pass icon as part of component's children instead.\",\n );\n HAS_DEPRECATION_WARNING_SHOWN = true;\n }\n\n // Reshape class name and apply to outer element\n const classes = classNames(\n \"mobius\",\n \"mobius/Button\",\n `--variant-${variant}`,\n `--size-${size}`,\n {\n \"--is-disabled\": isDisabled,\n \"--is-loading\": isLoading,\n \"--is-success\": isSuccess && !isLoading,\n },\n otherProps.className,\n );\n otherProps.className = classes;\n\n return (\n <Component ref={ref} {...buttonProps} {...otherProps}>\n {isLoading ? <Loading /> : children && children}\n </Component>\n );\n });\n\nButton.displayName = \"Button\";\nexport { Button };\n"],"names":["forwardRef","classNames","Loading","useButton","HAS_DEPRECATION_WARNING_SHOWN","Button","props","ref","children","elementType","Component","isDisabled","isLoading","isSuccess","variant","size","icon","iconPosition","onPress","onClick","otherProps","buttonProps","console","warn","classes","className","displayName"],"mappings":"AAAA;;AAEA,SAAcA,UAAU,QAAkC,QAAQ;AAClE,OAAOC,gBAAgB,oBAAoB;AAG3C,SAASC,OAAO,QAAQ,YAAY;AACpC,SAAyBC,SAAS,QAAQ,wBAAwB;AAElE,IAAIC,gCAAgC;
|
|
1
|
+
{"version":3,"sources":["../../../../src/components/Button/Button.tsx"],"sourcesContent":["\"use client\";\n\nimport { Ref, forwardRef, RefAttributes, ReactNode } from \"react\";\nimport classNames from \"classnames/dedupe\";\nimport { DOMProps } from \"../../types/dom\";\nimport { ForwardedRefComponent } from \"../../types/components\";\nimport { Loading } from \"./Loading\";\nimport { UseButtonProps, useButton } from \"../../hooks/useButton\";\n\nlet HAS_DEPRECATION_WARNING_SHOWN = false;\n\nexport type ButtonElementType = HTMLButtonElement;\n\nexport type Variant =\n | \"primary\"\n | \"secondary\"\n | \"ghost\"\n | \"inverse\"\n | \"inverse-ghost\"\n | \"basic\"\n | \"link\";\n\nexport type Size = \"sm\" | \"md\" | \"lg\";\n\nexport interface ButtonProps\n extends UseButtonProps,\n DOMProps,\n RefAttributes<ButtonElementType> {\n /** Custom class name for setting specific CSS */\n className?: string;\n /** Shortlist of styles */\n variant?: Variant;\n size?: Size;\n /** Display loading spinner */\n isLoading?: boolean;\n /** Display success style */\n isSuccess?: boolean;\n onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void;\n /**\n * **Deprecated:** Please pass icon as part of component's children instead.\n */\n icon?: string;\n /**\n * **Deprecated:** Please pass icon as part of component's children instead.\n */\n iconPosition?: \"left\" | \"right\";\n children?: ReactNode;\n}\n\nexport type ButtonRef = Ref<ButtonElementType>;\n\nconst Button: ForwardedRefComponent<ButtonProps, ButtonElementType> =\n forwardRef((props: ButtonProps, ref: ButtonRef) => {\n const {\n children,\n elementType: Component = \"button\",\n isDisabled,\n isLoading,\n isSuccess,\n variant = \"primary\",\n size = \"md\",\n icon,\n iconPosition,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onPress,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onClick,\n ...otherProps\n } = props;\n const { buttonProps } = useButton(props);\n\n if ((icon || iconPosition) && !HAS_DEPRECATION_WARNING_SHOWN) {\n // eslint-disable-next-line no-console\n console.warn(\n \"icon, iconPosition props have been deprecated in <Button> and will be removed in Mobius v4. Please pass icon as part of component's children instead.\",\n );\n HAS_DEPRECATION_WARNING_SHOWN = true;\n }\n\n // Reshape class name and apply to outer element\n const classes = classNames(\n \"mobius\",\n \"mobius/Button\",\n `--variant-${variant}`,\n `--size-${size}`,\n {\n \"--is-disabled\": isDisabled,\n \"--is-loading\": isLoading,\n \"--is-success\": isSuccess && !isLoading,\n },\n otherProps.className,\n );\n otherProps.className = classes;\n\n return (\n <Component ref={ref} {...buttonProps} {...otherProps}>\n {isLoading ? <Loading /> : children && children}\n </Component>\n );\n });\n\nButton.displayName = \"Button\";\nexport { Button };\n"],"names":["forwardRef","classNames","Loading","useButton","HAS_DEPRECATION_WARNING_SHOWN","Button","props","ref","children","elementType","Component","isDisabled","isLoading","isSuccess","variant","size","icon","iconPosition","onPress","onClick","otherProps","buttonProps","console","warn","classes","className","displayName"],"mappings":"AAAA;;AAEA,SAAcA,UAAU,QAAkC,QAAQ;AAClE,OAAOC,gBAAgB,oBAAoB;AAG3C,SAASC,OAAO,QAAQ,YAAY;AACpC,SAAyBC,SAAS,QAAQ,wBAAwB;AAElE,IAAIC,gCAAgC;AA0CpC,MAAMC,uBACJL,WAAW,CAACM,OAAoBC;IAC9B,MAAM,EACJC,QAAQ,EACRC,aAAaC,YAAY,QAAQ,EACjCC,UAAU,EACVC,SAAS,EACTC,SAAS,EACTC,UAAU,SAAS,EACnBC,OAAO,IAAI,EACXC,IAAI,EACJC,YAAY,EACZ,6DAA6D;IAC7DC,OAAO,EACP,6DAA6D;IAC7DC,OAAO,EACP,GAAGC,YACJ,GAAGd;IACJ,MAAM,EAAEe,WAAW,EAAE,GAAGlB,UAAUG;IAElC,IAAI,AAACU,CAAAA,QAAQC,YAAW,KAAM,CAACb,+BAA+B;QAC5D,sCAAsC;QACtCkB,QAAQC,IAAI,CACV;QAEFnB,gCAAgC;IAClC;IAEA,gDAAgD;IAChD,MAAMoB,UAAUvB,WACd,UACA,iBACA,CAAC,UAAU,EAAEa,QAAQ,CAAC,EACtB,CAAC,OAAO,EAAEC,KAAK,CAAC,EAChB;QACE,iBAAiBJ;QACjB,gBAAgBC;QAChB,gBAAgBC,aAAa,CAACD;IAChC,GACAQ,WAAWK,SAAS;IAEtBL,WAAWK,SAAS,GAAGD;IAEvB,qBACE,KAACd;QAAUH,KAAKA;QAAM,GAAGc,WAAW;QAAG,GAAGD,UAAU;kBACjDR,0BAAY,KAACV,eAAaM,YAAYA;;AAG7C;AAEFH,OAAOqB,WAAW,GAAG;AACrB,SAASrB,MAAM,GAAG"}
|
|
@@ -1,108 +1,43 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import classNames from "classnames/dedupe";
|
|
4
|
-
import { forwardRef,
|
|
5
|
-
import { useBodyScrollLock } from "../../hooks/useBodyScrollLock";
|
|
6
|
-
import { supportsDialog } from "../../utils/polyfill-tests";
|
|
4
|
+
import { forwardRef, useMemo, useRef } from "react";
|
|
7
5
|
import { VisuallyHidden } from "../VisuallyHidden";
|
|
8
|
-
import { mergeRefs } from "../../utils";
|
|
6
|
+
import { mergeRefs, supportsDialog } from "../../utils";
|
|
9
7
|
import { DrawerContext } from "./DrawerContext";
|
|
10
|
-
|
|
8
|
+
import { useDialog } from "../../hooks";
|
|
9
|
+
const TRANSITION_CSS_VARIABLE = "--drawer-transition-duration";
|
|
11
10
|
const Drawer = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
12
11
|
const { isOpen, className, closeLabel, direction, announce = "Drawer opened on screen", onOpen, onClose, children } = props;
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
if (onOpen && !hasOpened.current) {
|
|
18
|
-
onOpen();
|
|
19
|
-
hasOpened.current = true;
|
|
20
|
-
}
|
|
21
|
-
useBodyScrollLock({
|
|
22
|
-
enabled: isOpen
|
|
23
|
-
});
|
|
24
|
-
// Add close handler, to enable closing transitions
|
|
25
|
-
const handleClose = useCallback((event)=>{
|
|
26
|
-
if (event) {
|
|
27
|
-
event.preventDefault();
|
|
28
|
-
event.stopPropagation();
|
|
29
|
-
}
|
|
30
|
-
// Name the callback function, so we can add and remove event listener
|
|
31
|
-
const transitionCallback = (e)=>{
|
|
32
|
-
// Close drawer only if the transition is on the dialog element
|
|
33
|
-
// As it can be on a child element (ie `<Button>` inside the drawer)
|
|
34
|
-
if (e.target === modalRef.current) {
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
36
|
-
doClose();
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
const doClose = ()=>{
|
|
40
|
-
var _modalRef_current, _modalRef_current1;
|
|
41
|
-
(_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.close();
|
|
42
|
-
onClose === null || onClose === void 0 ? void 0 : onClose();
|
|
43
|
-
(_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.removeEventListener("transitionend", transitionCallback);
|
|
44
|
-
};
|
|
45
|
-
// Delay close to allow backdrop exit transition
|
|
46
|
-
if (hasDialogSupport) {
|
|
47
|
-
var _modalRef_current, _modalRef_current1;
|
|
48
|
-
(_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.classList.remove(TRANSITION_CLASS_NAME);
|
|
49
|
-
(_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.addEventListener("transitionend", transitionCallback);
|
|
50
|
-
} else {
|
|
51
|
-
doClose();
|
|
52
|
-
}
|
|
53
|
-
}, [
|
|
54
|
-
onClose,
|
|
55
|
-
hasDialogSupport
|
|
56
|
-
]);
|
|
57
|
-
const modalClasses = classNames("mobius", "mobius/Drawer", `--${direction}`, className, {
|
|
58
|
-
"--should-transition": hasDialogSupport
|
|
59
|
-
});
|
|
60
|
-
// Add polyfill for HTML Dialog in old browsers
|
|
61
|
-
useEffect(()=>{
|
|
62
|
-
async function toggleModal() {
|
|
63
|
-
var _modalRef_current, _modalRef_current1;
|
|
64
|
-
if (!hasDialogSupport && typeof window !== "undefined" && modalRef.current !== null) {
|
|
65
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
66
|
-
const { default: dialogPolyfill } = await import("dialog-polyfill");
|
|
67
|
-
try {
|
|
68
|
-
dialogPolyfill.registerDialog(modalRef.current);
|
|
69
|
-
} catch (error) {
|
|
70
|
-
// In iOS 15 <= 15.2 this intermittently fails with
|
|
71
|
-
// TypeError: null is not an object (evaluating 'element.showModal')
|
|
72
|
-
// Checking showModal presence through hasOwnProperty is falsy natively, truthy with polyfill 🤷🏼♂️
|
|
73
|
-
console.error("Failed to load dialog-polyfill", error);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
if (isOpen && !((_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.open)) {
|
|
77
|
-
var _modalRef_current2, _modalRef_current3;
|
|
78
|
-
(_modalRef_current2 = modalRef.current) === null || _modalRef_current2 === void 0 ? void 0 : _modalRef_current2.showModal();
|
|
79
|
-
(_modalRef_current3 = modalRef.current) === null || _modalRef_current3 === void 0 ? void 0 : _modalRef_current3.classList.add(TRANSITION_CLASS_NAME);
|
|
80
|
-
onOpen === null || onOpen === void 0 ? void 0 : onOpen();
|
|
81
|
-
} else if (!isOpen && ((_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.open)) {
|
|
82
|
-
handleClose();
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
toggleModal();
|
|
86
|
-
}, [
|
|
12
|
+
const shouldTransition = supportsDialog();
|
|
13
|
+
const dialogRef = useRef(null);
|
|
14
|
+
const { close } = useDialog({
|
|
15
|
+
ref: dialogRef,
|
|
87
16
|
isOpen,
|
|
88
17
|
onOpen,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
18
|
+
onClose,
|
|
19
|
+
transition: {
|
|
20
|
+
isEnabled: true,
|
|
21
|
+
CSSVariable: TRANSITION_CSS_VARIABLE
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const dialogClasses = classNames("mobius", "mobius/Drawer", `--${direction}`, className, {
|
|
25
|
+
"--should-transition": shouldTransition
|
|
26
|
+
});
|
|
92
27
|
const contextValue = useMemo(()=>({
|
|
93
|
-
onClose:
|
|
28
|
+
onClose: close,
|
|
94
29
|
closeLabel
|
|
95
30
|
}), [
|
|
96
|
-
|
|
31
|
+
close,
|
|
97
32
|
closeLabel
|
|
98
33
|
]);
|
|
99
34
|
return /*#__PURE__*/ _jsxs("dialog", {
|
|
100
35
|
ref: mergeRefs([
|
|
101
|
-
|
|
36
|
+
dialogRef,
|
|
102
37
|
ref
|
|
103
38
|
]),
|
|
104
|
-
onCancel:
|
|
105
|
-
className:
|
|
39
|
+
onCancel: close,
|
|
40
|
+
className: dialogClasses,
|
|
106
41
|
"aria-describedby": "screen-reader-announce",
|
|
107
42
|
children: [
|
|
108
43
|
/*#__PURE__*/ _jsx(VisuallyHidden, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/components/Drawer/Drawer.tsx"],"sourcesContent":["\"use client\";\n\nimport classNames from \"classnames/dedupe\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../src/components/Drawer/Drawer.tsx"],"sourcesContent":["\"use client\";\n\nimport classNames from \"classnames/dedupe\";\nimport { Ref, forwardRef, useMemo, useRef } from \"react\";\nimport { VisuallyHidden } from \"../VisuallyHidden\";\nimport { DrawerProps } from \"./types\";\nimport { mergeRefs, supportsDialog } from \"../../utils\";\nimport { DrawerContext } from \"./DrawerContext\";\nimport { useDialog } from \"../../hooks\";\n\nexport type DialogElementType = HTMLDialogElement;\nexport type DialogRef = Ref<DialogElementType>;\n\nconst TRANSITION_CSS_VARIABLE = \"--drawer-transition-duration\";\n\nconst Drawer = forwardRef((props: DrawerProps, ref: DialogRef) => {\n const {\n isOpen,\n className,\n closeLabel,\n direction,\n announce = \"Drawer opened on screen\",\n onOpen,\n onClose,\n children,\n } = props;\n const shouldTransition = supportsDialog();\n const dialogRef = useRef<HTMLDialogElement | null>(null);\n const { close } = useDialog({\n ref: dialogRef,\n isOpen,\n onOpen,\n onClose,\n transition: {\n isEnabled: true,\n CSSVariable: TRANSITION_CSS_VARIABLE,\n },\n });\n\n const dialogClasses = classNames(\n \"mobius\",\n \"mobius/Drawer\",\n `--${direction}`,\n className,\n {\n \"--should-transition\": shouldTransition,\n },\n );\n\n const contextValue = useMemo(\n () => ({\n onClose: close,\n closeLabel,\n }),\n [close, closeLabel],\n );\n\n return (\n <dialog\n ref={mergeRefs([dialogRef, ref])}\n onCancel={close}\n className={dialogClasses}\n aria-describedby=\"screen-reader-announce\"\n >\n <VisuallyHidden>\n <div id=\"screen-reader-announce\">{announce}</div>\n </VisuallyHidden>\n <DrawerContext.Provider value={contextValue}>\n {children}\n </DrawerContext.Provider>\n </dialog>\n );\n});\n\nDrawer.displayName = \"Drawer\";\nexport { Drawer };\n"],"names":["classNames","forwardRef","useMemo","useRef","VisuallyHidden","mergeRefs","supportsDialog","DrawerContext","useDialog","TRANSITION_CSS_VARIABLE","Drawer","props","ref","isOpen","className","closeLabel","direction","announce","onOpen","onClose","children","shouldTransition","dialogRef","close","transition","isEnabled","CSSVariable","dialogClasses","contextValue","dialog","onCancel","aria-describedby","div","id","Provider","value","displayName"],"mappings":"AAAA;;AAEA,OAAOA,gBAAgB,oBAAoB;AAC3C,SAAcC,UAAU,EAAEC,OAAO,EAAEC,MAAM,QAAQ,QAAQ;AACzD,SAASC,cAAc,QAAQ,oBAAoB;AAEnD,SAASC,SAAS,EAAEC,cAAc,QAAQ,cAAc;AACxD,SAASC,aAAa,QAAQ,kBAAkB;AAChD,SAASC,SAAS,QAAQ,cAAc;AAKxC,MAAMC,0BAA0B;AAEhC,MAAMC,uBAAST,WAAW,CAACU,OAAoBC;IAC7C,MAAM,EACJC,MAAM,EACNC,SAAS,EACTC,UAAU,EACVC,SAAS,EACTC,WAAW,yBAAyB,EACpCC,MAAM,EACNC,OAAO,EACPC,QAAQ,EACT,GAAGT;IACJ,MAAMU,mBAAmBf;IACzB,MAAMgB,YAAYnB,OAAiC;IACnD,MAAM,EAAEoB,KAAK,EAAE,GAAGf,UAAU;QAC1BI,KAAKU;QACLT;QACAK;QACAC;QACAK,YAAY;YACVC,WAAW;YACXC,aAAajB;QACf;IACF;IAEA,MAAMkB,gBAAgB3B,WACpB,UACA,iBACA,CAAC,EAAE,EAAEgB,UAAU,CAAC,EAChBF,WACA;QACE,uBAAuBO;IACzB;IAGF,MAAMO,eAAe1B,QACnB,IAAO,CAAA;YACLiB,SAASI;YACTR;QACF,CAAA,GACA;QAACQ;QAAOR;KAAW;IAGrB,qBACE,MAACc;QACCjB,KAAKP,UAAU;YAACiB;YAAWV;SAAI;QAC/BkB,UAAUP;QACVT,WAAWa;QACXI,oBAAiB;;0BAEjB,KAAC3B;0BACC,cAAA,KAAC4B;oBAAIC,IAAG;8BAA0BhB;;;0BAEpC,KAACV,cAAc2B,QAAQ;gBAACC,OAAOP;0BAC5BR;;;;AAIT;AAEAV,OAAO0B,WAAW,GAAG;AACrB,SAAS1B,MAAM,GAAG"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
import classNames from "classnames/dedupe";
|
|
4
|
-
import { forwardRef,
|
|
5
|
-
import { useBodyScrollLock } from "../../hooks/useBodyScrollLock";
|
|
4
|
+
import { forwardRef, useMemo, useRef } from "react";
|
|
6
5
|
import { supportsDialog } from "../../utils/polyfill-tests";
|
|
7
6
|
import { mergeRefs } from "../../utils";
|
|
8
7
|
import { ModalContext } from "./ModalContext";
|
|
9
|
-
|
|
8
|
+
import { useDialog } from "../../hooks";
|
|
9
|
+
const TRANSITION_CSS_VARIABLE = "--modal-transition-duration";
|
|
10
10
|
const Modal = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
11
11
|
const { isOpen, onClose, onOpen, children, className, closeLabel, isFullScreen, animation, // Deprecated props below
|
|
12
12
|
size, appElement, preventCloseOnEsc, shouldFocusAfterRender, parentSelector } = props;
|
|
@@ -18,103 +18,38 @@ const Modal = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
|
18
18
|
hasWarnedAboutMissingLabels.current = true;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
onOpen
|
|
27
|
-
hasOpened.current = true;
|
|
28
|
-
}
|
|
29
|
-
useBodyScrollLock({
|
|
30
|
-
enabled: isOpen
|
|
31
|
-
});
|
|
32
|
-
// Add close handler, to enable closing animations
|
|
33
|
-
const handleClose = useCallback((event)=>{
|
|
34
|
-
if (event) {
|
|
35
|
-
event.preventDefault();
|
|
36
|
-
event.stopPropagation();
|
|
37
|
-
}
|
|
38
|
-
// Name the callback function, so we can add and remove event listener
|
|
39
|
-
const transitionCallback = (e)=>{
|
|
40
|
-
// Close modal only if the transition is on the dialog element
|
|
41
|
-
// As it can be on a child element (ie `<Button>` inside the drawer)
|
|
42
|
-
if (e.target === modalRef.current) {
|
|
43
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
44
|
-
doClose();
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
const doClose = ()=>{
|
|
48
|
-
var _modalRef_current, _modalRef_current1;
|
|
49
|
-
(_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.close();
|
|
50
|
-
onClose === null || onClose === void 0 ? void 0 : onClose();
|
|
51
|
-
(_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.removeEventListener("transitionend", transitionCallback);
|
|
52
|
-
};
|
|
53
|
-
// Delay close to allow backdrop exit transition
|
|
54
|
-
if (hasDialogSupport && animation) {
|
|
55
|
-
var _modalRef_current, _modalRef_current1;
|
|
56
|
-
(_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.classList.remove(TRANSITION_CLASS_NAME);
|
|
57
|
-
(_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.addEventListener("transitionend", transitionCallback);
|
|
58
|
-
} else {
|
|
59
|
-
doClose();
|
|
60
|
-
}
|
|
61
|
-
}, [
|
|
21
|
+
const shouldTransition = supportsDialog();
|
|
22
|
+
const dialogRef = useRef(null);
|
|
23
|
+
const { close } = useDialog({
|
|
24
|
+
ref: dialogRef,
|
|
25
|
+
isOpen,
|
|
26
|
+
onOpen,
|
|
62
27
|
onClose,
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
28
|
+
transition: {
|
|
29
|
+
isEnabled: !!animation,
|
|
30
|
+
CSSVariable: TRANSITION_CSS_VARIABLE
|
|
31
|
+
}
|
|
32
|
+
});
|
|
66
33
|
const modalClasses = classNames("mobius", "mobius/Modal", {
|
|
67
|
-
"--no-dialog-support": !
|
|
68
|
-
"--should-transition":
|
|
34
|
+
"--no-dialog-support": !shouldTransition,
|
|
35
|
+
"--should-transition": shouldTransition && animation,
|
|
69
36
|
"--slide-up": animation === "slideUp",
|
|
70
37
|
"--fade": animation === "fade",
|
|
71
38
|
"--is-fullscreen": isFullScreen
|
|
72
39
|
}, className);
|
|
73
|
-
// Add polyfill for HTML Dialog in old browsers
|
|
74
|
-
useEffect(()=>{
|
|
75
|
-
async function toggleModal() {
|
|
76
|
-
var _modalRef_current, _modalRef_current1;
|
|
77
|
-
if (!hasDialogSupport && typeof window !== "undefined" && modalRef.current !== null) {
|
|
78
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
79
|
-
const { default: dialogPolyfill } = await import("dialog-polyfill");
|
|
80
|
-
try {
|
|
81
|
-
dialogPolyfill.registerDialog(modalRef.current);
|
|
82
|
-
} catch (error) {
|
|
83
|
-
// In iOS 15 <= 15.2 this intermittently fails with
|
|
84
|
-
// TypeError: null is not an object (evaluating 'element.showModal')
|
|
85
|
-
// Checking showModal presence through hasOwnProperty is falsy natively, truthy with polyfill 🤷🏼♂️
|
|
86
|
-
console.error("Failed to load dialog-polyfill", error);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
if (isOpen && !((_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.open)) {
|
|
90
|
-
var _modalRef_current2, _modalRef_current3;
|
|
91
|
-
(_modalRef_current2 = modalRef.current) === null || _modalRef_current2 === void 0 ? void 0 : _modalRef_current2.showModal();
|
|
92
|
-
(_modalRef_current3 = modalRef.current) === null || _modalRef_current3 === void 0 ? void 0 : _modalRef_current3.classList.add(TRANSITION_CLASS_NAME);
|
|
93
|
-
onOpen === null || onOpen === void 0 ? void 0 : onOpen();
|
|
94
|
-
} else if (!isOpen && ((_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.open)) {
|
|
95
|
-
handleClose();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
toggleModal();
|
|
99
|
-
}, [
|
|
100
|
-
isOpen,
|
|
101
|
-
onOpen,
|
|
102
|
-
handleClose,
|
|
103
|
-
hasDialogSupport
|
|
104
|
-
]);
|
|
105
40
|
const contextValue = useMemo(()=>({
|
|
106
|
-
onClose:
|
|
41
|
+
onClose: close,
|
|
107
42
|
closeLabel
|
|
108
43
|
}), [
|
|
109
|
-
|
|
44
|
+
close,
|
|
110
45
|
closeLabel
|
|
111
46
|
]);
|
|
112
47
|
return /*#__PURE__*/ _jsx("dialog", {
|
|
113
48
|
ref: mergeRefs([
|
|
114
|
-
|
|
49
|
+
dialogRef,
|
|
115
50
|
ref
|
|
116
51
|
]),
|
|
117
|
-
onCancel:
|
|
52
|
+
onCancel: close,
|
|
118
53
|
className: modalClasses,
|
|
119
54
|
children: /*#__PURE__*/ _jsx(ModalContext.Provider, {
|
|
120
55
|
value: contextValue,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/components/Modal/Modal.tsx"],"sourcesContent":["\"use client\";\n\nimport classNames from \"classnames/dedupe\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../src/components/Modal/Modal.tsx"],"sourcesContent":["\"use client\";\n\nimport classNames from \"classnames/dedupe\";\nimport { Ref, forwardRef, useMemo, useRef } from \"react\";\nimport { supportsDialog } from \"../../utils/polyfill-tests\";\nimport { ModalProps } from \"./types\";\nimport { mergeRefs } from \"../../utils\";\nimport { ModalContext } from \"./ModalContext\";\nimport { useDialog } from \"../../hooks\";\n\nexport type ModalElementType = HTMLDialogElement;\nexport type ModalRef = Ref<ModalElementType>;\n\nconst TRANSITION_CSS_VARIABLE = \"--modal-transition-duration\";\n\nconst Modal = forwardRef((props: ModalProps, ref: ModalRef) => {\n const {\n isOpen,\n onClose,\n onOpen,\n children,\n className,\n closeLabel,\n isFullScreen,\n animation,\n // Deprecated props below\n size,\n appElement,\n preventCloseOnEsc,\n shouldFocusAfterRender,\n parentSelector,\n } = props;\n const hasWarnedAboutMissingLabels = useRef<boolean>(false);\n // Handle deprecated props\n if (!hasWarnedAboutMissingLabels.current) {\n if (\n size ||\n appElement ||\n preventCloseOnEsc ||\n shouldFocusAfterRender ||\n parentSelector\n ) {\n console.warn(\n `Deprecation warning: Mobius Modal no longer supports the following props: size, appElement, preventCloseOnEsc, shouldFocusAfterRender and parentSelector.`,\n );\n hasWarnedAboutMissingLabels.current = true;\n }\n }\n\n const shouldTransition = supportsDialog();\n const dialogRef = useRef<HTMLDialogElement | null>(null);\n const { close } = useDialog({\n ref: dialogRef,\n isOpen,\n onOpen,\n onClose,\n transition: {\n isEnabled: !!animation,\n CSSVariable: TRANSITION_CSS_VARIABLE,\n },\n });\n\n const modalClasses = classNames(\n \"mobius\",\n \"mobius/Modal\",\n {\n \"--no-dialog-support\": !shouldTransition, // This class is used to correctly position modal in x/y middle on iOS <= 15.2\n \"--should-transition\": shouldTransition && animation,\n \"--slide-up\": animation === \"slideUp\",\n \"--fade\": animation === \"fade\",\n \"--is-fullscreen\": isFullScreen,\n },\n className,\n );\n\n const contextValue = useMemo(\n () => ({\n onClose: close,\n closeLabel,\n }),\n [close, closeLabel],\n );\n\n return (\n <dialog\n ref={mergeRefs([dialogRef, ref])}\n onCancel={close}\n className={modalClasses}\n >\n <ModalContext.Provider value={contextValue}>\n {children}\n </ModalContext.Provider>\n </dialog>\n );\n});\n\nModal.displayName = \"Modal\";\nexport { Modal };\n"],"names":["classNames","forwardRef","useMemo","useRef","supportsDialog","mergeRefs","ModalContext","useDialog","TRANSITION_CSS_VARIABLE","Modal","props","ref","isOpen","onClose","onOpen","children","className","closeLabel","isFullScreen","animation","size","appElement","preventCloseOnEsc","shouldFocusAfterRender","parentSelector","hasWarnedAboutMissingLabels","current","console","warn","shouldTransition","dialogRef","close","transition","isEnabled","CSSVariable","modalClasses","contextValue","dialog","onCancel","Provider","value","displayName"],"mappings":"AAAA;;AAEA,OAAOA,gBAAgB,oBAAoB;AAC3C,SAAcC,UAAU,EAAEC,OAAO,EAAEC,MAAM,QAAQ,QAAQ;AACzD,SAASC,cAAc,QAAQ,6BAA6B;AAE5D,SAASC,SAAS,QAAQ,cAAc;AACxC,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,SAAS,QAAQ,cAAc;AAKxC,MAAMC,0BAA0B;AAEhC,MAAMC,sBAAQR,WAAW,CAACS,OAAmBC;IAC3C,MAAM,EACJC,MAAM,EACNC,OAAO,EACPC,MAAM,EACNC,QAAQ,EACRC,SAAS,EACTC,UAAU,EACVC,YAAY,EACZC,SAAS,EACT,yBAAyB;IACzBC,IAAI,EACJC,UAAU,EACVC,iBAAiB,EACjBC,sBAAsB,EACtBC,cAAc,EACf,GAAGd;IACJ,MAAMe,8BAA8BtB,OAAgB;IACpD,0BAA0B;IAC1B,IAAI,CAACsB,4BAA4BC,OAAO,EAAE;QACxC,IACEN,QACAC,cACAC,qBACAC,0BACAC,gBACA;YACAG,QAAQC,IAAI,CACV,CAAC,yJAAyJ,CAAC;YAE7JH,4BAA4BC,OAAO,GAAG;QACxC;IACF;IAEA,MAAMG,mBAAmBzB;IACzB,MAAM0B,YAAY3B,OAAiC;IACnD,MAAM,EAAE4B,KAAK,EAAE,GAAGxB,UAAU;QAC1BI,KAAKmB;QACLlB;QACAE;QACAD;QACAmB,YAAY;YACVC,WAAW,CAAC,CAACd;YACbe,aAAa1B;QACf;IACF;IAEA,MAAM2B,eAAenC,WACnB,UACA,gBACA;QACE,uBAAuB,CAAC6B;QACxB,uBAAuBA,oBAAoBV;QAC3C,cAAcA,cAAc;QAC5B,UAAUA,cAAc;QACxB,mBAAmBD;IACrB,GACAF;IAGF,MAAMoB,eAAelC,QACnB,IAAO,CAAA;YACLW,SAASkB;YACTd;QACF,CAAA,GACA;QAACc;QAAOd;KAAW;IAGrB,qBACE,KAACoB;QACC1B,KAAKN,UAAU;YAACyB;YAAWnB;SAAI;QAC/B2B,UAAUP;QACVf,WAAWmB;kBAEX,cAAA,KAAC7B,aAAaiC,QAAQ;YAACC,OAAOJ;sBAC3BrB;;;AAIT;AAEAN,MAAMgC,WAAW,GAAG;AACpB,SAAShC,KAAK,GAAG"}
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/index.tsx"],"sourcesContent":["export * from \"./useBodyScrollLock\";\nexport * from \"./useBreakpoint\";\nexport * from \"./useButton\";\nexport * from \"./useLabel\";\nexport * from \"./useOnClickOutside\";\nexport * from \"./useTextField\";\nexport * from \"./useWindowEvent\";\n"],"names":[],"mappings":"AAAA,cAAc,sBAAsB;AACpC,cAAc,kBAAkB;AAChC,cAAc,cAAc;AAC5B,cAAc,aAAa;AAC3B,cAAc,sBAAsB;AACpC,cAAc,iBAAiB;AAC/B,cAAc,mBAAmB"}
|
|
1
|
+
{"version":3,"sources":["../../../src/hooks/index.tsx"],"sourcesContent":["export * from \"./useBodyScrollLock\";\nexport * from \"./useBreakpoint\";\nexport * from \"./useButton\";\nexport * from \"./useDialog\";\nexport * from \"./useLabel\";\nexport * from \"./useOnClickOutside\";\nexport * from \"./useTextField\";\nexport * from \"./useWindowEvent\";\n"],"names":[],"mappings":"AAAA,cAAc,sBAAsB;AACpC,cAAc,kBAAkB;AAChC,cAAc,cAAc;AAC5B,cAAc,cAAc;AAC5B,cAAc,aAAa;AAC3B,cAAc,sBAAsB;AACpC,cAAc,iBAAiB;AAC/B,cAAc,mBAAmB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/hooks/useDialog/index.ts"],"sourcesContent":["export * from \"./useDialog\";\n"],"names":[],"mappings":"AAAA,cAAc,cAAc"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { useCallback, useEffect } from "react";
|
|
2
|
+
import { useDialogPolyfill } from "../useDialogPolyfill";
|
|
3
|
+
import { useBodyScrollLock } from "../useBodyScrollLock";
|
|
4
|
+
import { supportsDialog } from "../../utils";
|
|
5
|
+
const TRANSITION_CLASS_NAME = "--transition";
|
|
6
|
+
const FALLBACK_TRANSITION_DURATION = 0;
|
|
7
|
+
export const useDialog = (props)=>{
|
|
8
|
+
const { ref, isOpen, transition, onOpen, onClose } = props;
|
|
9
|
+
const shouldTransition = supportsDialog() && transition.isEnabled;
|
|
10
|
+
// Read CSS variable value as number
|
|
11
|
+
const TRANSITION_DURATION_IN_MS = ref.current && Number(getComputedStyle(ref.current).getPropertyValue(transition.CSSVariable).replace("ms", "")) || FALLBACK_TRANSITION_DURATION;
|
|
12
|
+
useDialogPolyfill(ref);
|
|
13
|
+
useBodyScrollLock({
|
|
14
|
+
enabled: isOpen
|
|
15
|
+
});
|
|
16
|
+
const open = useCallback(()=>{
|
|
17
|
+
var _ref_current;
|
|
18
|
+
(_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.showModal();
|
|
19
|
+
onOpen === null || onOpen === void 0 ? void 0 : onOpen();
|
|
20
|
+
}, [
|
|
21
|
+
onOpen,
|
|
22
|
+
ref
|
|
23
|
+
]);
|
|
24
|
+
const close = useCallback(()=>{
|
|
25
|
+
var _ref_current;
|
|
26
|
+
(_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.close();
|
|
27
|
+
onClose === null || onClose === void 0 ? void 0 : onClose();
|
|
28
|
+
}, [
|
|
29
|
+
onClose,
|
|
30
|
+
ref
|
|
31
|
+
]);
|
|
32
|
+
// Add close handler, to enable closing transitions
|
|
33
|
+
const handleClose = useCallback((event)=>{
|
|
34
|
+
if (event) {
|
|
35
|
+
// Prevent default event coming from onCancel,
|
|
36
|
+
// which is triggered by pressing ESC key
|
|
37
|
+
event.preventDefault();
|
|
38
|
+
// Ensure that nested `<dialog>` elements
|
|
39
|
+
// don't close the parent
|
|
40
|
+
event.stopPropagation();
|
|
41
|
+
}
|
|
42
|
+
if (shouldTransition) {
|
|
43
|
+
var _ref_current;
|
|
44
|
+
(_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.classList.remove(TRANSITION_CLASS_NAME);
|
|
45
|
+
// Delay close to allow exit transition
|
|
46
|
+
setTimeout(()=>close(), TRANSITION_DURATION_IN_MS);
|
|
47
|
+
} else {
|
|
48
|
+
close();
|
|
49
|
+
}
|
|
50
|
+
}, [
|
|
51
|
+
TRANSITION_DURATION_IN_MS,
|
|
52
|
+
close,
|
|
53
|
+
ref,
|
|
54
|
+
shouldTransition
|
|
55
|
+
]);
|
|
56
|
+
useEffect(()=>{
|
|
57
|
+
async function toggleDialog() {
|
|
58
|
+
var _ref_current, _ref_current1;
|
|
59
|
+
if (isOpen && !((_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.open)) {
|
|
60
|
+
open();
|
|
61
|
+
// Fix opening transition in Safari being skipped
|
|
62
|
+
// by wrapping with setTimeout
|
|
63
|
+
setTimeout(()=>{
|
|
64
|
+
var _ref_current;
|
|
65
|
+
(_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.classList.add(TRANSITION_CLASS_NAME);
|
|
66
|
+
}, 0);
|
|
67
|
+
} else if (!isOpen && ((_ref_current1 = ref.current) === null || _ref_current1 === void 0 ? void 0 : _ref_current1.open)) {
|
|
68
|
+
handleClose();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
toggleDialog();
|
|
72
|
+
}, [
|
|
73
|
+
handleClose,
|
|
74
|
+
isOpen,
|
|
75
|
+
open,
|
|
76
|
+
ref
|
|
77
|
+
]);
|
|
78
|
+
return {
|
|
79
|
+
open,
|
|
80
|
+
close: handleClose
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
//# sourceMappingURL=useDialog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/hooks/useDialog/useDialog.ts"],"sourcesContent":["import {\n MutableRefObject,\n SyntheticEvent,\n useCallback,\n useEffect,\n} from \"react\";\nimport { useDialogPolyfill } from \"../useDialogPolyfill\";\nimport { useBodyScrollLock } from \"../useBodyScrollLock\";\nimport { supportsDialog } from \"../../utils\";\n\nexport type TransitionProps = {\n isEnabled: boolean;\n CSSVariable: string;\n};\n\nexport type useDialogProps = {\n ref: MutableRefObject<HTMLDialogElement | null>;\n transition: TransitionProps;\n isOpen: boolean;\n onOpen?: () => void;\n onClose?: () => void;\n};\n\nconst TRANSITION_CLASS_NAME = \"--transition\";\nconst FALLBACK_TRANSITION_DURATION = 0;\n\nexport const useDialog = (props: useDialogProps) => {\n const { ref, isOpen, transition, onOpen, onClose } = props;\n const shouldTransition = supportsDialog() && transition.isEnabled;\n\n // Read CSS variable value as number\n const TRANSITION_DURATION_IN_MS =\n (ref.current &&\n Number(\n getComputedStyle(ref.current)\n .getPropertyValue(transition.CSSVariable)\n .replace(\"ms\", \"\"),\n )) ||\n FALLBACK_TRANSITION_DURATION;\n\n useDialogPolyfill(ref);\n useBodyScrollLock({ enabled: isOpen });\n\n const open = useCallback(() => {\n ref.current?.showModal();\n onOpen?.();\n }, [onOpen, ref]);\n\n const close = useCallback(() => {\n ref.current?.close();\n onClose?.();\n }, [onClose, ref]);\n\n // Add close handler, to enable closing transitions\n const handleClose = useCallback(\n (event?: SyntheticEvent<HTMLElement, Event>) => {\n if (event) {\n // Prevent default event coming from onCancel,\n // which is triggered by pressing ESC key\n event.preventDefault();\n // Ensure that nested `<dialog>` elements\n // don't close the parent\n event.stopPropagation();\n }\n\n if (shouldTransition) {\n ref.current?.classList.remove(TRANSITION_CLASS_NAME);\n // Delay close to allow exit transition\n setTimeout(() => close(), TRANSITION_DURATION_IN_MS);\n } else {\n close();\n }\n },\n [TRANSITION_DURATION_IN_MS, close, ref, shouldTransition],\n );\n\n useEffect(() => {\n async function toggleDialog() {\n if (isOpen && !ref.current?.open) {\n open();\n // Fix opening transition in Safari being skipped\n // by wrapping with setTimeout\n setTimeout(() => {\n ref.current?.classList.add(TRANSITION_CLASS_NAME);\n }, 0);\n } else if (!isOpen && ref.current?.open) {\n handleClose();\n }\n }\n\n toggleDialog();\n }, [handleClose, isOpen, open, ref]);\n\n return {\n open,\n close: handleClose,\n };\n};\n"],"names":["useCallback","useEffect","useDialogPolyfill","useBodyScrollLock","supportsDialog","TRANSITION_CLASS_NAME","FALLBACK_TRANSITION_DURATION","useDialog","props","ref","isOpen","transition","onOpen","onClose","shouldTransition","isEnabled","TRANSITION_DURATION_IN_MS","current","Number","getComputedStyle","getPropertyValue","CSSVariable","replace","enabled","open","showModal","close","handleClose","event","preventDefault","stopPropagation","classList","remove","setTimeout","toggleDialog","add"],"mappings":"AAAA,SAGEA,WAAW,EACXC,SAAS,QACJ,QAAQ;AACf,SAASC,iBAAiB,QAAQ,uBAAuB;AACzD,SAASC,iBAAiB,QAAQ,uBAAuB;AACzD,SAASC,cAAc,QAAQ,cAAc;AAe7C,MAAMC,wBAAwB;AAC9B,MAAMC,+BAA+B;AAErC,OAAO,MAAMC,YAAY,CAACC;IACxB,MAAM,EAAEC,GAAG,EAAEC,MAAM,EAAEC,UAAU,EAAEC,MAAM,EAAEC,OAAO,EAAE,GAAGL;IACrD,MAAMM,mBAAmBV,oBAAoBO,WAAWI,SAAS;IAEjE,oCAAoC;IACpC,MAAMC,4BACJ,AAACP,IAAIQ,OAAO,IACVC,OACEC,iBAAiBV,IAAIQ,OAAO,EACzBG,gBAAgB,CAACT,WAAWU,WAAW,EACvCC,OAAO,CAAC,MAAM,QAErBhB;IAEFJ,kBAAkBO;IAClBN,kBAAkB;QAAEoB,SAASb;IAAO;IAEpC,MAAMc,OAAOxB,YAAY;YACvBS;SAAAA,eAAAA,IAAIQ,OAAO,cAAXR,mCAAAA,aAAagB,SAAS;QACtBb,mBAAAA,6BAAAA;IACF,GAAG;QAACA;QAAQH;KAAI;IAEhB,MAAMiB,QAAQ1B,YAAY;YACxBS;SAAAA,eAAAA,IAAIQ,OAAO,cAAXR,mCAAAA,aAAaiB,KAAK;QAClBb,oBAAAA,8BAAAA;IACF,GAAG;QAACA;QAASJ;KAAI;IAEjB,mDAAmD;IACnD,MAAMkB,cAAc3B,YAClB,CAAC4B;QACC,IAAIA,OAAO;YACT,8CAA8C;YAC9C,yCAAyC;YACzCA,MAAMC,cAAc;YACpB,yCAAyC;YACzC,yBAAyB;YACzBD,MAAME,eAAe;QACvB;QAEA,IAAIhB,kBAAkB;gBACpBL;aAAAA,eAAAA,IAAIQ,OAAO,cAAXR,mCAAAA,aAAasB,SAAS,CAACC,MAAM,CAAC3B;YAC9B,uCAAuC;YACvC4B,WAAW,IAAMP,SAASV;QAC5B,OAAO;YACLU;QACF;IACF,GACA;QAACV;QAA2BU;QAAOjB;QAAKK;KAAiB;IAG3Db,UAAU;QACR,eAAeiC;gBACEzB,cAOOA;YAPtB,IAAIC,UAAU,GAACD,eAAAA,IAAIQ,OAAO,cAAXR,mCAAAA,aAAae,IAAI,GAAE;gBAChCA;gBACA,iDAAiD;gBACjD,8BAA8B;gBAC9BS,WAAW;wBACTxB;qBAAAA,eAAAA,IAAIQ,OAAO,cAAXR,mCAAAA,aAAasB,SAAS,CAACI,GAAG,CAAC9B;gBAC7B,GAAG;YACL,OAAO,IAAI,CAACK,YAAUD,gBAAAA,IAAIQ,OAAO,cAAXR,oCAAAA,cAAae,IAAI,GAAE;gBACvCG;YACF;QACF;QAEAO;IACF,GAAG;QAACP;QAAajB;QAAQc;QAAMf;KAAI;IAEnC,OAAO;QACLe;QACAE,OAAOC;IACT;AACF,EAAE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/hooks/useDialogPolyfill/index.ts"],"sourcesContent":["export * from \"./useDialogPolyfill\";\n"],"names":[],"mappings":"AAAA,cAAc,sBAAsB"}
|