@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.
Files changed (51) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/components/Button/Button.js.map +1 -1
  3. package/dist/cjs/components/Drawer/Drawer.js +20 -126
  4. package/dist/cjs/components/Drawer/Drawer.js.map +1 -1
  5. package/dist/cjs/components/Modal/Modal.js +19 -125
  6. package/dist/cjs/components/Modal/Modal.js.map +1 -1
  7. package/dist/cjs/hooks/index.js +1 -0
  8. package/dist/cjs/hooks/index.js.map +1 -1
  9. package/dist/cjs/hooks/useDialog/index.js +20 -0
  10. package/dist/cjs/hooks/useDialog/index.js.map +1 -0
  11. package/dist/cjs/hooks/useDialog/useDialog.js +94 -0
  12. package/dist/cjs/hooks/useDialog/useDialog.js.map +1 -0
  13. package/dist/cjs/hooks/useDialogPolyfill/index.js +20 -0
  14. package/dist/cjs/hooks/useDialogPolyfill/index.js.map +1 -0
  15. package/dist/cjs/hooks/useDialogPolyfill/useDialogPolyfill.js +77 -0
  16. package/dist/cjs/hooks/useDialogPolyfill/useDialogPolyfill.js.map +1 -0
  17. package/dist/esm/components/Button/Button.js.map +1 -1
  18. package/dist/esm/components/Drawer/Drawer.js +22 -87
  19. package/dist/esm/components/Drawer/Drawer.js.map +1 -1
  20. package/dist/esm/components/Modal/Modal.js +20 -85
  21. package/dist/esm/components/Modal/Modal.js.map +1 -1
  22. package/dist/esm/hooks/index.js +1 -0
  23. package/dist/esm/hooks/index.js.map +1 -1
  24. package/dist/esm/hooks/useDialog/index.js +3 -0
  25. package/dist/esm/hooks/useDialog/index.js.map +1 -0
  26. package/dist/esm/hooks/useDialog/useDialog.js +84 -0
  27. package/dist/esm/hooks/useDialog/useDialog.js.map +1 -0
  28. package/dist/esm/hooks/useDialogPolyfill/index.js +3 -0
  29. package/dist/esm/hooks/useDialogPolyfill/index.js.map +1 -0
  30. package/dist/esm/hooks/useDialogPolyfill/useDialogPolyfill.js +27 -0
  31. package/dist/esm/hooks/useDialogPolyfill/useDialogPolyfill.js.map +1 -0
  32. package/dist/types/components/Button/Button.d.ts +1 -1
  33. package/dist/types/components/Button/Button.stories.d.ts +2 -1
  34. package/dist/types/hooks/index.d.ts +1 -0
  35. package/dist/types/hooks/useDialog/index.d.ts +1 -0
  36. package/dist/types/hooks/useDialog/useDialog.d.ts +16 -0
  37. package/dist/types/hooks/useDialogPolyfill/index.d.ts +1 -0
  38. package/dist/types/hooks/useDialogPolyfill/useDialogPolyfill.d.ts +2 -0
  39. package/package.json +2 -1
  40. package/src/components/Button/Button.stories.tsx +14 -1
  41. package/src/components/Button/Button.test.tsx +8 -0
  42. package/src/components/Button/Button.tsx +2 -1
  43. package/src/components/Drawer/Drawer.stories.tsx +1 -1
  44. package/src/components/Drawer/Drawer.tsx +22 -100
  45. package/src/components/Modal/Modal.tsx +20 -98
  46. package/src/hooks/index.tsx +1 -0
  47. package/src/hooks/useDialog/index.ts +1 -0
  48. package/src/hooks/useDialog/useDialog.ts +98 -0
  49. package/src/hooks/useDialogPolyfill/index.ts +1 -0
  50. package/src/hooks/useDialogPolyfill/useDialogPolyfill.ts +32 -0
  51. 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;AAyCpC,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
+ {"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, useCallback, useEffect, useMemo, useRef } from "react";
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
- const TRANSITION_CLASS_NAME = "--transition";
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 hasOpened = useRef(false);
14
- const modalRef = useRef(null);
15
- const hasDialogSupport = supportsDialog();
16
- // Fire onOpen once
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
- handleClose,
90
- hasDialogSupport
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: handleClose,
28
+ onClose: close,
94
29
  closeLabel
95
30
  }), [
96
- handleClose,
31
+ close,
97
32
  closeLabel
98
33
  ]);
99
34
  return /*#__PURE__*/ _jsxs("dialog", {
100
35
  ref: mergeRefs([
101
- modalRef,
36
+ dialogRef,
102
37
  ref
103
38
  ]),
104
- onCancel: handleClose,
105
- className: modalClasses,
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 {\n Ref,\n SyntheticEvent,\n forwardRef,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n} from \"react\";\nimport { useBodyScrollLock } from \"../../hooks/useBodyScrollLock\";\nimport { supportsDialog } from \"../../utils/polyfill-tests\";\nimport { VisuallyHidden } from \"../VisuallyHidden\";\nimport { DrawerProps } from \"./types\";\nimport { mergeRefs } from \"../../utils\";\nimport { DrawerContext } from \"./DrawerContext\";\n\nexport type DialogElementType = HTMLDialogElement;\nexport type DialogRef = Ref<DialogElementType>;\n\nconst TRANSITION_CLASS_NAME = \"--transition\";\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 hasOpened = useRef<boolean>(false);\n const modalRef = useRef<HTMLDialogElement | null>(null);\n const hasDialogSupport = supportsDialog();\n\n // Fire onOpen once\n if (onOpen && !hasOpened.current) {\n onOpen();\n hasOpened.current = true;\n }\n\n useBodyScrollLock({ enabled: isOpen });\n\n // Add close handler, to enable closing transitions\n const handleClose = useCallback(\n (event?: SyntheticEvent<HTMLElement, Event>) => {\n if (event) {\n event.preventDefault();\n event.stopPropagation();\n }\n\n // Name the callback function, so we can add and remove event listener\n const transitionCallback = (e: Event) => {\n // Close drawer only if the transition is on the dialog element\n // As it can be on a child element (ie `<Button>` inside the drawer)\n if (e.target === modalRef.current) {\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n doClose();\n }\n };\n\n const doClose = () => {\n modalRef.current?.close();\n onClose?.();\n modalRef.current?.removeEventListener(\n \"transitionend\",\n transitionCallback,\n );\n };\n\n // Delay close to allow backdrop exit transition\n if (hasDialogSupport) {\n modalRef.current?.classList.remove(TRANSITION_CLASS_NAME);\n modalRef.current?.addEventListener(\"transitionend\", transitionCallback);\n } else {\n doClose();\n }\n },\n [onClose, hasDialogSupport],\n );\n\n const modalClasses = classNames(\n \"mobius\",\n \"mobius/Drawer\",\n `--${direction}`,\n className,\n {\n \"--should-transition\": hasDialogSupport,\n },\n );\n\n // Add polyfill for HTML Dialog in old browsers\n useEffect(() => {\n async function toggleModal() {\n if (\n !hasDialogSupport &&\n typeof window !== \"undefined\" &&\n modalRef.current !== null\n ) {\n // eslint-disable-next-line import/no-extraneous-dependencies\n const { default: dialogPolyfill } = await import(\"dialog-polyfill\");\n try {\n dialogPolyfill.registerDialog(modalRef.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 if (isOpen && !modalRef.current?.open) {\n modalRef.current?.showModal();\n modalRef.current?.classList.add(TRANSITION_CLASS_NAME);\n onOpen?.();\n } else if (!isOpen && modalRef.current?.open) {\n handleClose();\n }\n }\n\n toggleModal();\n }, [isOpen, onOpen, handleClose, hasDialogSupport]);\n\n const contextValue = useMemo(\n () => ({\n onClose: handleClose,\n closeLabel,\n }),\n [handleClose, closeLabel],\n );\n\n return (\n <dialog\n ref={mergeRefs([modalRef, ref])}\n onCancel={handleClose}\n className={modalClasses}\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","useCallback","useEffect","useMemo","useRef","useBodyScrollLock","supportsDialog","VisuallyHidden","mergeRefs","DrawerContext","TRANSITION_CLASS_NAME","Drawer","props","ref","isOpen","className","closeLabel","direction","announce","onOpen","onClose","children","hasOpened","modalRef","hasDialogSupport","current","enabled","handleClose","event","preventDefault","stopPropagation","transitionCallback","e","target","doClose","close","removeEventListener","classList","remove","addEventListener","modalClasses","toggleModal","window","default","dialogPolyfill","registerDialog","error","console","open","showModal","add","contextValue","dialog","onCancel","aria-describedby","div","id","Provider","value","displayName"],"mappings":"AAAA;;AAEA,OAAOA,gBAAgB,oBAAoB;AAC3C,SAGEC,UAAU,EACVC,WAAW,EACXC,SAAS,EACTC,OAAO,EACPC,MAAM,QACD,QAAQ;AACf,SAASC,iBAAiB,QAAQ,gCAAgC;AAClE,SAASC,cAAc,QAAQ,6BAA6B;AAC5D,SAASC,cAAc,QAAQ,oBAAoB;AAEnD,SAASC,SAAS,QAAQ,cAAc;AACxC,SAASC,aAAa,QAAQ,kBAAkB;AAKhD,MAAMC,wBAAwB;AAE9B,MAAMC,uBAASX,WAAW,CAACY,OAAoBC;IAC7C,MAAM,EACJC,MAAM,EACNC,SAAS,EACTC,UAAU,EACVC,SAAS,EACTC,WAAW,y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OAAO,KAAK,MACrB;gBACA,6DAA6D;gBAC7D,MAAM,EAAEkB,SAASC,cAAc,EAAE,GAAG,MAAM,MAAM,CAAC;gBACjD,IAAI;oBACFA,eAAeC,cAAc,CAACtB,SAASE,OAAO;gBAChD,EAAE,OAAOqB,OAAO;oBACd,mDAAmD;oBACnD,oEAAoE;oBACpE,qGAAqG;oBACrGC,QAAQD,KAAK,CAAC,kCAAkCA;gBAClD;YACF;YAEA,IAAIhC,UAAU,GAACS,oBAAAA,SAASE,OAAO,cAAhBF,wCAAAA,kBAAkByB,IAAI,GAAE;oBACrCzB,oBACAA;iBADAA,qBAAAA,SAASE,OAAO,cAAhBF,yCAAAA,mBAAkB0B,SAAS;iBAC3B1B,qBAAAA,SAASE,OAAO,cAAhBF,yCAAAA,mBAAkBc,SAAS,CAACa,GAAG,CAACxC;gBAChCS,mBAAAA,6BAAAA;YACF,OAAO,IAAI,CAACL,YAAUS,qBAAAA,SAASE,OAAO,cAAhBF,yCAAAA,mBAAkByB,IAAI,GAAE;gBAC5CrB;YACF;QACF;QAEAc;IACF,GAAG;QAAC3B;QAAQK;QAAQQ;QAAaH;KAAiB;IAElD,MAAM2B,eAAehD,QACnB,IAAO,CAAA;YACLiB,SAASO;YACTX;QACF,CAAA,GACA;QAACW;QAAaX;KAAW;IAG3B,qBACE,MAACoC;QACCvC,KAAKL,UAAU;YAACe;YAAUV;SAAI;QAC9BwC,UAAU1B;QACVZ,WAAWyB;QACXc,oBAAiB;;0BAEjB,KAAC/C;0BACC,cAAA,KAACgD;oBAAIC,IAAG;8BAA0BtC;;;0BAEpC,KAACT,cAAcgD,QAAQ;gBAACC,OAAOP;0BAC5B9B;;;;AAIT;AAEAV,OAAOgD,WAAW,GAAG;AACrB,SAAShD,MAAM,GAAG"}
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, useCallback, useEffect, useMemo, useRef } from "react";
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
- const TRANSITION_CLASS_NAME = "--transition";
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 hasOpened = useRef(false);
22
- const modalRef = useRef(null);
23
- const hasDialogSupport = supportsDialog();
24
- // Fire onOpen once
25
- if (onOpen && !hasOpened.current) {
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
- hasDialogSupport,
64
- animation
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": !hasDialogSupport,
68
- "--should-transition": hasDialogSupport && animation,
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: handleClose,
41
+ onClose: close,
107
42
  closeLabel
108
43
  }), [
109
- handleClose,
44
+ close,
110
45
  closeLabel
111
46
  ]);
112
47
  return /*#__PURE__*/ _jsx("dialog", {
113
48
  ref: mergeRefs([
114
- modalRef,
49
+ dialogRef,
115
50
  ref
116
51
  ]),
117
- onCancel: handleClose,
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 {\n Ref,\n SyntheticEvent,\n forwardRef,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n} from \"react\";\nimport { useBodyScrollLock } from \"../../hooks/useBodyScrollLock\";\nimport { supportsDialog } from \"../../utils/polyfill-tests\";\nimport { ModalProps } from \"./types\";\nimport { mergeRefs } from \"../../utils\";\nimport { ModalContext } from \"./ModalContext\";\n\nexport type ModalElementType = HTMLDialogElement;\nexport type ModalRef = Ref<ModalElementType>;\n\nconst TRANSITION_CLASS_NAME = \"--transition\";\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 hasOpened = useRef<boolean>(false);\n const modalRef = useRef<HTMLDialogElement | null>(null);\n const hasDialogSupport = supportsDialog();\n\n // Fire onOpen once\n if (onOpen && !hasOpened.current) {\n onOpen();\n hasOpened.current = true;\n }\n\n useBodyScrollLock({ enabled: isOpen });\n\n // Add close handler, to enable closing animations\n const handleClose = useCallback(\n (event?: SyntheticEvent<HTMLElement, Event>) => {\n if (event) {\n event.preventDefault();\n event.stopPropagation();\n }\n\n // Name the callback function, so we can add and remove event listener\n const transitionCallback = (e: Event) => {\n // Close modal only if the transition is on the dialog element\n // As it can be on a child element (ie `<Button>` inside the drawer)\n if (e.target === modalRef.current) {\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n doClose();\n }\n };\n\n const doClose = () => {\n modalRef.current?.close();\n onClose?.();\n modalRef.current?.removeEventListener(\n \"transitionend\",\n transitionCallback,\n );\n };\n\n // Delay close to allow backdrop exit transition\n if (hasDialogSupport && animation) {\n modalRef.current?.classList.remove(TRANSITION_CLASS_NAME);\n modalRef.current?.addEventListener(\"transitionend\", transitionCallback);\n } else {\n doClose();\n }\n },\n [onClose, hasDialogSupport, animation],\n );\n\n const modalClasses = classNames(\n \"mobius\",\n \"mobius/Modal\",\n {\n \"--no-dialog-support\": !hasDialogSupport, // This class is used to correctly position modal in x/y middle on iOS <= 15.2\n \"--should-transition\": hasDialogSupport && animation,\n \"--slide-up\": animation === \"slideUp\",\n \"--fade\": animation === \"fade\",\n \"--is-fullscreen\": isFullScreen,\n },\n className,\n );\n\n // Add polyfill for HTML Dialog in old browsers\n useEffect(() => {\n async function toggleModal() {\n if (\n !hasDialogSupport &&\n typeof window !== \"undefined\" &&\n modalRef.current !== null\n ) {\n // eslint-disable-next-line import/no-extraneous-dependencies\n const { default: dialogPolyfill } = await import(\"dialog-polyfill\");\n\n try {\n dialogPolyfill.registerDialog(modalRef.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 if (isOpen && !modalRef.current?.open) {\n modalRef.current?.showModal();\n modalRef.current?.classList.add(TRANSITION_CLASS_NAME);\n onOpen?.();\n } else if (!isOpen && modalRef.current?.open) {\n handleClose();\n }\n }\n\n toggleModal();\n }, [isOpen, onOpen, handleClose, hasDialogSupport]);\n\n const contextValue = useMemo(\n () => ({\n onClose: handleClose,\n closeLabel,\n }),\n [handleClose, closeLabel],\n );\n\n return (\n <dialog\n ref={mergeRefs([modalRef, ref])}\n onCancel={handleClose}\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","useCallback","useEffect","useMemo","useRef","useBodyScrollLock","supportsDialog","mergeRefs","ModalContext","TRANSITION_CLASS_NAME","Modal","props","ref","isOpen","onClose","onOpen","children","className","closeLabel","isFullScreen","animation","size","appElement","preventCloseOnEsc","shouldFocusAfterRender","parentSelector","hasWarnedAboutMissingLabels","current","console","warn","hasOpened","modalRef","hasDialogSupport","enabled","handleClose","event","preventDefault","stopPropagation","transitionCallback","e","target","doClose","close","removeEventListener","classList","remove","addEventListener","modalClasses","toggleModal","window","default","dialogPolyfill","registerDialog","error","open","showModal","add","contextValue","dialog","onCancel","Prov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S;iBAC3BxB,qBAAAA,SAASJ,OAAO,cAAhBI,yCAAAA,mBAAkBa,SAAS,CAACY,GAAG,CAAC/C;gBAChCM,mBAAAA,6BAAAA;YACF,OAAO,IAAI,CAACF,YAAUkB,qBAAAA,SAASJ,OAAO,cAAhBI,yCAAAA,mBAAkBuB,IAAI,GAAE;gBAC5CpB;YACF;QACF;QAEAc;IACF,GAAG;QAACnC;QAAQE;QAAQmB;QAAaF;KAAiB;IAElD,MAAMyB,eAAetD,QACnB,IAAO,CAAA;YACLW,SAASoB;YACThB;QACF,CAAA,GACA;QAACgB;QAAahB;KAAW;IAG3B,qBACE,KAACwC;QACC9C,KAAKL,UAAU;YAACwB;YAAUnB;SAAI;QAC9B+C,UAAUzB;QACVjB,WAAW8B;kBAEX,cAAA,KAACvC,aAAaoD,QAAQ;YAACC,OAAOJ;sBAC3BzC;;;AAIT;AAEAN,MAAMoD,WAAW,GAAG;AACpB,SAASpD,KAAK,GAAG"}
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,QA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
@@ -1,6 +1,7 @@
1
1
  export * from "./useBodyScrollLock";
2
2
  export * from "./useBreakpoint";
3
3
  export * from "./useButton";
4
+ export * from "./useDialog";
4
5
  export * from "./useLabel";
5
6
  export * from "./useOnClickOutside";
6
7
  export * from "./useTextField";
@@ -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,3 @@
1
+ export * from "./useDialog";
2
+
3
+ //# sourceMappingURL=index.js.map
@@ -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,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AW;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,3 @@
1
+ export * from "./useDialogPolyfill";
2
+
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/hooks/useDialogPolyfill/index.ts"],"sourcesContent":["export * from \"./useDialogPolyfill\";\n"],"names":[],"mappings":"AAAA,cAAc,sBAAsB"}