@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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.6.0
4
+
5
+ ### Minor Changes
6
+
7
+ - b0d7505: Add new variant "link" to `<Button>`
8
+
9
+ ## 4.5.1
10
+
11
+ ### Patch Changes
12
+
13
+ - 8a50b45: Fix `<Drawer>`, `<Modal>` transition-in not working in Safari
14
+
3
15
  ## 4.5.0
4
16
 
5
17
  ### Minor Changes
@@ -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":["Button","HAS_DEPRECATION_WARNING_SHOWN","forwardRef","props","ref","children","elementType","Component","isDisabled","isLoading","isSuccess","variant","size","icon","iconPosition","onPress","onClick","otherProps","buttonProps","useButton","console","warn","classes","classNames","className","Loading","displayName"],"mappings":"AAAA;;;;;+BAqGSA;;;eAAAA;;;;uBAnGiD;+DACnC;yBAGC;2BACkB;;;;;;AAE1C,IAAIC,gCAAgC;AAyCpC,MAAMD,uBACJE,IAAAA,iBAAU,EAAC,CAACC,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,GAAGC,IAAAA,oBAAS,EAAChB;IAElC,IAAI,AAACU,CAAAA,QAAQC,YAAW,KAAM,CAACb,+BAA+B;QAC5D,sCAAsC;QACtCmB,QAAQC,IAAI,CACV;QAEFpB,gCAAgC;IAClC;IAEA,gDAAgD;IAChD,MAAMqB,UAAUC,IAAAA,eAAU,EACxB,UACA,iBACA,CAAC,UAAU,EAAEZ,QAAQ,CAAC,EACtB,CAAC,OAAO,EAAEC,KAAK,CAAC,EAChB;QACE,iBAAiBJ;QACjB,gBAAgBC;QAChB,gBAAgBC,aAAa,CAACD;IAChC,GACAQ,WAAWO,SAAS;IAEtBP,WAAWO,SAAS,GAAGF;IAEvB,qBACE,qBAACf;QAAUH,KAAKA;QAAM,GAAGc,WAAW;QAAG,GAAGD,UAAU;kBACjDR,0BAAY,qBAACgB,gBAAO,QAAMpB,YAAYA;;AAG7C;AAEFL,OAAO0B,WAAW,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":["Button","HAS_DEPRECATION_WARNING_SHOWN","forwardRef","props","ref","children","elementType","Component","isDisabled","isLoading","isSuccess","variant","size","icon","iconPosition","onPress","onClick","otherProps","buttonProps","useButton","console","warn","classes","classNames","className","Loading","displayName"],"mappings":"AAAA;;;;;+BAsGSA;;;eAAAA;;;;uBApGiD;+DACnC;yBAGC;2BACkB;;;;;;AAE1C,IAAIC,gCAAgC;AA0CpC,MAAMD,uBACJE,IAAAA,iBAAU,EAAC,CAACC,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,GAAGC,IAAAA,oBAAS,EAAChB;IAElC,IAAI,AAACU,CAAAA,QAAQC,YAAW,KAAM,CAACb,+BAA+B;QAC5D,sCAAsC;QACtCmB,QAAQC,IAAI,CACV;QAEFpB,gCAAgC;IAClC;IAEA,gDAAgD;IAChD,MAAMqB,UAAUC,IAAAA,eAAU,EACxB,UACA,iBACA,CAAC,UAAU,EAAEZ,QAAQ,CAAC,EACtB,CAAC,OAAO,EAAEC,KAAK,CAAC,EAChB;QACE,iBAAiBJ;QACjB,gBAAgBC;QAChB,gBAAgBC,aAAa,CAACD;IAChC,GACAQ,WAAWO,SAAS;IAEtBP,WAAWO,SAAS,GAAGF;IAEvB,qBACE,qBAACf;QAAUH,KAAKA;QAAM,GAAGc,WAAW;QAAG,GAAGD,UAAU;kBACjDR,0BAAY,qBAACgB,gBAAO,QAAMpB,YAAYA;;AAG7C;AAEFL,OAAO0B,WAAW,GAAG"}
@@ -12,153 +12,47 @@ Object.defineProperty(exports, "Drawer", {
12
12
  const _jsxruntime = require("react/jsx-runtime");
13
13
  const _dedupe = /*#__PURE__*/ _interop_require_default(require("classnames/dedupe"));
14
14
  const _react = require("react");
15
- const _useBodyScrollLock = require("../../hooks/useBodyScrollLock");
16
- const _polyfilltests = require("../../utils/polyfill-tests");
17
15
  const _VisuallyHidden = require("../VisuallyHidden");
18
16
  const _utils = require("../../utils");
19
17
  const _DrawerContext = require("./DrawerContext");
18
+ const _hooks = require("../../hooks");
20
19
  function _interop_require_default(obj) {
21
20
  return obj && obj.__esModule ? obj : {
22
21
  default: obj
23
22
  };
24
23
  }
25
- function _getRequireWildcardCache(nodeInterop) {
26
- if (typeof WeakMap !== "function") return null;
27
- var cacheBabelInterop = new WeakMap();
28
- var cacheNodeInterop = new WeakMap();
29
- return (_getRequireWildcardCache = function(nodeInterop) {
30
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
31
- })(nodeInterop);
32
- }
33
- function _interop_require_wildcard(obj, nodeInterop) {
34
- if (!nodeInterop && obj && obj.__esModule) {
35
- return obj;
36
- }
37
- if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
38
- return {
39
- default: obj
40
- };
41
- }
42
- var cache = _getRequireWildcardCache(nodeInterop);
43
- if (cache && cache.has(obj)) {
44
- return cache.get(obj);
45
- }
46
- var newObj = {
47
- __proto__: null
48
- };
49
- var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
50
- for(var key in obj){
51
- if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
52
- var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
53
- if (desc && (desc.get || desc.set)) {
54
- Object.defineProperty(newObj, key, desc);
55
- } else {
56
- newObj[key] = obj[key];
57
- }
58
- }
59
- }
60
- newObj.default = obj;
61
- if (cache) {
62
- cache.set(obj, newObj);
63
- }
64
- return newObj;
65
- }
66
- const TRANSITION_CLASS_NAME = "--transition";
24
+ const TRANSITION_CSS_VARIABLE = "--drawer-transition-duration";
67
25
  const Drawer = /*#__PURE__*/ (0, _react.forwardRef)((props, ref)=>{
68
26
  const { isOpen, className, closeLabel, direction, announce = "Drawer opened on screen", onOpen, onClose, children } = props;
69
- const hasOpened = (0, _react.useRef)(false);
70
- const modalRef = (0, _react.useRef)(null);
71
- const hasDialogSupport = (0, _polyfilltests.supportsDialog)();
72
- // Fire onOpen once
73
- if (onOpen && !hasOpened.current) {
74
- onOpen();
75
- hasOpened.current = true;
76
- }
77
- (0, _useBodyScrollLock.useBodyScrollLock)({
78
- enabled: isOpen
79
- });
80
- // Add close handler, to enable closing transitions
81
- const handleClose = (0, _react.useCallback)((event)=>{
82
- if (event) {
83
- event.preventDefault();
84
- event.stopPropagation();
85
- }
86
- // Name the callback function, so we can add and remove event listener
87
- const transitionCallback = (e)=>{
88
- // Close drawer only if the transition is on the dialog element
89
- // As it can be on a child element (ie `<Button>` inside the drawer)
90
- if (e.target === modalRef.current) {
91
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
92
- doClose();
93
- }
94
- };
95
- const doClose = ()=>{
96
- var _modalRef_current, _modalRef_current1;
97
- (_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.close();
98
- onClose === null || onClose === void 0 ? void 0 : onClose();
99
- (_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.removeEventListener("transitionend", transitionCallback);
100
- };
101
- // Delay close to allow backdrop exit transition
102
- if (hasDialogSupport) {
103
- var _modalRef_current, _modalRef_current1;
104
- (_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.classList.remove(TRANSITION_CLASS_NAME);
105
- (_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.addEventListener("transitionend", transitionCallback);
106
- } else {
107
- doClose();
108
- }
109
- }, [
110
- onClose,
111
- hasDialogSupport
112
- ]);
113
- const modalClasses = (0, _dedupe.default)("mobius", "mobius/Drawer", `--${direction}`, className, {
114
- "--should-transition": hasDialogSupport
115
- });
116
- // Add polyfill for HTML Dialog in old browsers
117
- (0, _react.useEffect)(()=>{
118
- async function toggleModal() {
119
- var _modalRef_current, _modalRef_current1;
120
- if (!hasDialogSupport && typeof window !== "undefined" && modalRef.current !== null) {
121
- // eslint-disable-next-line import/no-extraneous-dependencies
122
- const { default: dialogPolyfill } = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard(require("dialog-polyfill")));
123
- try {
124
- dialogPolyfill.registerDialog(modalRef.current);
125
- } catch (error) {
126
- // In iOS 15 <= 15.2 this intermittently fails with
127
- // TypeError: null is not an object (evaluating 'element.showModal')
128
- // Checking showModal presence through hasOwnProperty is falsy natively, truthy with polyfill 🤷🏼‍♂️
129
- console.error("Failed to load dialog-polyfill", error);
130
- }
131
- }
132
- if (isOpen && !((_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.open)) {
133
- var _modalRef_current2, _modalRef_current3;
134
- (_modalRef_current2 = modalRef.current) === null || _modalRef_current2 === void 0 ? void 0 : _modalRef_current2.showModal();
135
- (_modalRef_current3 = modalRef.current) === null || _modalRef_current3 === void 0 ? void 0 : _modalRef_current3.classList.add(TRANSITION_CLASS_NAME);
136
- onOpen === null || onOpen === void 0 ? void 0 : onOpen();
137
- } else if (!isOpen && ((_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.open)) {
138
- handleClose();
139
- }
140
- }
141
- toggleModal();
142
- }, [
27
+ const shouldTransition = (0, _utils.supportsDialog)();
28
+ const dialogRef = (0, _react.useRef)(null);
29
+ const { close } = (0, _hooks.useDialog)({
30
+ ref: dialogRef,
143
31
  isOpen,
144
32
  onOpen,
145
- handleClose,
146
- hasDialogSupport
147
- ]);
33
+ onClose,
34
+ transition: {
35
+ isEnabled: true,
36
+ CSSVariable: TRANSITION_CSS_VARIABLE
37
+ }
38
+ });
39
+ const dialogClasses = (0, _dedupe.default)("mobius", "mobius/Drawer", `--${direction}`, className, {
40
+ "--should-transition": shouldTransition
41
+ });
148
42
  const contextValue = (0, _react.useMemo)(()=>({
149
- onClose: handleClose,
43
+ onClose: close,
150
44
  closeLabel
151
45
  }), [
152
- handleClose,
46
+ close,
153
47
  closeLabel
154
48
  ]);
155
49
  return /*#__PURE__*/ (0, _jsxruntime.jsxs)("dialog", {
156
50
  ref: (0, _utils.mergeRefs)([
157
- modalRef,
51
+ dialogRef,
158
52
  ref
159
53
  ]),
160
- onCancel: handleClose,
161
- className: modalClasses,
54
+ onCancel: close,
55
+ className: dialogClasses,
162
56
  "aria-describedby": "screen-reader-announce",
163
57
  children: [
164
58
  /*#__PURE__*/ (0, _jsxruntime.jsx)(_VisuallyHidden.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":["Drawer","TRANSITION_CLASS_NAME","forwardRef","props","ref","isOpen","className","closeLabel","direction","announce","onOpen","onClose","children","hasOpened","useRef","modalRef","hasDialogSupport","supportsDialog","current","useBodyScrollLock","enabled","handleClose","useCallback","event","preventDefault","stopPropagation","transitionCallback","e","target","doClose","close","removeEventListener","classList","remove","addEventListener","modalClasses","classNames","useEffect","toggleModal","window","default","dialogPolyfill","registerDialog","error","console","open","showModal","add","contextValue","useMemo","dialog","mergeRefs","onCancel","aria-describedby","VisuallyHidden","div","id","DrawerContext","Provider","value","displayName"],"mappings":"AAAA;;;;;+BAyJSA;;;eAAAA;;;;+DAvJc;uBAShB;mCAC2B;+BACH;gCACA;uBAEL;+BACI;;;;;;;;;;;;;;;;;;;;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EjB,qBAACC,8BAAc;0BACb,cAAA,qBAACC;oBAAIC,IAAG;8BAA0B/C;;;0BAEpC,qBAACgD,4BAAa,CAACC,QAAQ;gBAACC,OAAOX;0BAC5BpC;;;;AAIT;AAEAZ,OAAO4D,WAAW,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":["Drawer","TRANSITION_CSS_VARIABLE","forwardRef","props","ref","isOpen","className","closeLabel","direction","announce","onOpen","onClose","children","shouldTransition","supportsDialog","dialogRef","useRef","close","useDialog","transition","isEnabled","CSSVariable","dialogClasses","classNames","contextValue","useMemo","dialog","mergeRefs","onCancel","aria-describedby","VisuallyHidden","div","id","DrawerContext","Provider","value","displayName"],"mappings":"AAAA;;;;;+BA2ESA;;;eAAAA;;;;+DAzEc;uBAC0B;gCAClB;uBAEW;+BACZ;uBACJ;;;;;;AAK1B,MAAMC,0BAA0B;AAEhC,MAAMD,uBAASE,IAAAA,iBAAU,EAAC,CAACC,OAAoBC;IAC7C,MAAM,EACJC,MAAM,EACNC,SAAS,EACTC,UAAU,EACVC,SAAS,EACTC,WAAW,yBAAyB,EACpCC,MAAM,EACNC,OAAO,EACPC,QAAQ,EACT,GAAGT;IACJ,MAAMU,mBAAmBC,IAAAA,qBAAc;IACvC,MAAMC,YAAYC,IAAAA,aAAM,EAA2B;IACnD,MAAM,EAAEC,KAAK,EAAE,GAAGC,IAAAA,gBAAS,EAAC;QAC1Bd,KAAKW;QACLV;QACAK;QACAC;QACAQ,YAAY;YACVC,WAAW;YACXC,aAAapB;QACf;IACF;IAEA,MAAMqB,gBAAgBC,IAAAA,eAAU,EAC9B,UACA,iBACA,CAAC,EAAE,EAAEf,UAAU,CAAC,EAChBF,WACA;QACE,uBAAuBO;IACzB;IAGF,MAAMW,eAAeC,IAAAA,cAAO,EAC1B,IAAO,CAAA;YACLd,SAASM;YACTV;QACF,CAAA,GACA;QAACU;QAAOV;KAAW;IAGrB,qBACE,sBAACmB;QACCtB,KAAKuB,IAAAA,gBAAS,EAAC;YAACZ;YAAWX;SAAI;QAC/BwB,UAAUX;QACVX,WAAWgB;QACXO,oBAAiB;;0BAEjB,qBAACC,8BAAc;0BACb,cAAA,qBAACC;oBAAIC,IAAG;8BAA0BvB;;;0BAEpC,qBAACwB,4BAAa,CAACC,QAAQ;gBAACC,OAAOX;0BAC5BZ;;;;AAIT;AAEAZ,OAAOoC,WAAW,GAAG"}
@@ -12,57 +12,16 @@ Object.defineProperty(exports, "Modal", {
12
12
  const _jsxruntime = require("react/jsx-runtime");
13
13
  const _dedupe = /*#__PURE__*/ _interop_require_default(require("classnames/dedupe"));
14
14
  const _react = require("react");
15
- const _useBodyScrollLock = require("../../hooks/useBodyScrollLock");
16
15
  const _polyfilltests = require("../../utils/polyfill-tests");
17
16
  const _utils = require("../../utils");
18
17
  const _ModalContext = require("./ModalContext");
18
+ const _hooks = require("../../hooks");
19
19
  function _interop_require_default(obj) {
20
20
  return obj && obj.__esModule ? obj : {
21
21
  default: obj
22
22
  };
23
23
  }
24
- function _getRequireWildcardCache(nodeInterop) {
25
- if (typeof WeakMap !== "function") return null;
26
- var cacheBabelInterop = new WeakMap();
27
- var cacheNodeInterop = new WeakMap();
28
- return (_getRequireWildcardCache = function(nodeInterop) {
29
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
30
- })(nodeInterop);
31
- }
32
- function _interop_require_wildcard(obj, nodeInterop) {
33
- if (!nodeInterop && obj && obj.__esModule) {
34
- return obj;
35
- }
36
- if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
37
- return {
38
- default: obj
39
- };
40
- }
41
- var cache = _getRequireWildcardCache(nodeInterop);
42
- if (cache && cache.has(obj)) {
43
- return cache.get(obj);
44
- }
45
- var newObj = {
46
- __proto__: null
47
- };
48
- var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
49
- for(var key in obj){
50
- if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
51
- var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
52
- if (desc && (desc.get || desc.set)) {
53
- Object.defineProperty(newObj, key, desc);
54
- } else {
55
- newObj[key] = obj[key];
56
- }
57
- }
58
- }
59
- newObj.default = obj;
60
- if (cache) {
61
- cache.set(obj, newObj);
62
- }
63
- return newObj;
64
- }
65
- const TRANSITION_CLASS_NAME = "--transition";
24
+ const TRANSITION_CSS_VARIABLE = "--modal-transition-duration";
66
25
  const Modal = /*#__PURE__*/ (0, _react.forwardRef)((props, ref)=>{
67
26
  const { isOpen, onClose, onOpen, children, className, closeLabel, isFullScreen, animation, // Deprecated props below
68
27
  size, appElement, preventCloseOnEsc, shouldFocusAfterRender, parentSelector } = props;
@@ -74,103 +33,38 @@ const Modal = /*#__PURE__*/ (0, _react.forwardRef)((props, ref)=>{
74
33
  hasWarnedAboutMissingLabels.current = true;
75
34
  }
76
35
  }
77
- const hasOpened = (0, _react.useRef)(false);
78
- const modalRef = (0, _react.useRef)(null);
79
- const hasDialogSupport = (0, _polyfilltests.supportsDialog)();
80
- // Fire onOpen once
81
- if (onOpen && !hasOpened.current) {
82
- onOpen();
83
- hasOpened.current = true;
84
- }
85
- (0, _useBodyScrollLock.useBodyScrollLock)({
86
- enabled: isOpen
87
- });
88
- // Add close handler, to enable closing animations
89
- const handleClose = (0, _react.useCallback)((event)=>{
90
- if (event) {
91
- event.preventDefault();
92
- event.stopPropagation();
93
- }
94
- // Name the callback function, so we can add and remove event listener
95
- const transitionCallback = (e)=>{
96
- // Close modal only if the transition is on the dialog element
97
- // As it can be on a child element (ie `<Button>` inside the drawer)
98
- if (e.target === modalRef.current) {
99
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
100
- doClose();
101
- }
102
- };
103
- const doClose = ()=>{
104
- var _modalRef_current, _modalRef_current1;
105
- (_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.close();
106
- onClose === null || onClose === void 0 ? void 0 : onClose();
107
- (_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.removeEventListener("transitionend", transitionCallback);
108
- };
109
- // Delay close to allow backdrop exit transition
110
- if (hasDialogSupport && animation) {
111
- var _modalRef_current, _modalRef_current1;
112
- (_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.classList.remove(TRANSITION_CLASS_NAME);
113
- (_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.addEventListener("transitionend", transitionCallback);
114
- } else {
115
- doClose();
116
- }
117
- }, [
36
+ const shouldTransition = (0, _polyfilltests.supportsDialog)();
37
+ const dialogRef = (0, _react.useRef)(null);
38
+ const { close } = (0, _hooks.useDialog)({
39
+ ref: dialogRef,
40
+ isOpen,
41
+ onOpen,
118
42
  onClose,
119
- hasDialogSupport,
120
- animation
121
- ]);
43
+ transition: {
44
+ isEnabled: !!animation,
45
+ CSSVariable: TRANSITION_CSS_VARIABLE
46
+ }
47
+ });
122
48
  const modalClasses = (0, _dedupe.default)("mobius", "mobius/Modal", {
123
- "--no-dialog-support": !hasDialogSupport,
124
- "--should-transition": hasDialogSupport && animation,
49
+ "--no-dialog-support": !shouldTransition,
50
+ "--should-transition": shouldTransition && animation,
125
51
  "--slide-up": animation === "slideUp",
126
52
  "--fade": animation === "fade",
127
53
  "--is-fullscreen": isFullScreen
128
54
  }, className);
129
- // Add polyfill for HTML Dialog in old browsers
130
- (0, _react.useEffect)(()=>{
131
- async function toggleModal() {
132
- var _modalRef_current, _modalRef_current1;
133
- if (!hasDialogSupport && typeof window !== "undefined" && modalRef.current !== null) {
134
- // eslint-disable-next-line import/no-extraneous-dependencies
135
- const { default: dialogPolyfill } = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard(require("dialog-polyfill")));
136
- try {
137
- dialogPolyfill.registerDialog(modalRef.current);
138
- } catch (error) {
139
- // In iOS 15 <= 15.2 this intermittently fails with
140
- // TypeError: null is not an object (evaluating 'element.showModal')
141
- // Checking showModal presence through hasOwnProperty is falsy natively, truthy with polyfill 🤷🏼‍♂️
142
- console.error("Failed to load dialog-polyfill", error);
143
- }
144
- }
145
- if (isOpen && !((_modalRef_current = modalRef.current) === null || _modalRef_current === void 0 ? void 0 : _modalRef_current.open)) {
146
- var _modalRef_current2, _modalRef_current3;
147
- (_modalRef_current2 = modalRef.current) === null || _modalRef_current2 === void 0 ? void 0 : _modalRef_current2.showModal();
148
- (_modalRef_current3 = modalRef.current) === null || _modalRef_current3 === void 0 ? void 0 : _modalRef_current3.classList.add(TRANSITION_CLASS_NAME);
149
- onOpen === null || onOpen === void 0 ? void 0 : onOpen();
150
- } else if (!isOpen && ((_modalRef_current1 = modalRef.current) === null || _modalRef_current1 === void 0 ? void 0 : _modalRef_current1.open)) {
151
- handleClose();
152
- }
153
- }
154
- toggleModal();
155
- }, [
156
- isOpen,
157
- onOpen,
158
- handleClose,
159
- hasDialogSupport
160
- ]);
161
55
  const contextValue = (0, _react.useMemo)(()=>({
162
- onClose: handleClose,
56
+ onClose: close,
163
57
  closeLabel
164
58
  }), [
165
- handleClose,
59
+ close,
166
60
  closeLabel
167
61
  ]);
168
62
  return /*#__PURE__*/ (0, _jsxruntime.jsx)("dialog", {
169
63
  ref: (0, _utils.mergeRefs)([
170
- modalRef,
64
+ dialogRef,
171
65
  ref
172
66
  ]),
173
- onCancel: handleClose,
67
+ onCancel: close,
174
68
  className: modalClasses,
175
69
  children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_ModalContext.ModalContext.Provider, {
176
70
  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":["Modal","TRANSITION_CLASS_NAME","forwardRef","props","ref","isOpen","onClose","onOpen","children","className","closeLabel","isFullScreen","animation","size","appElement","preventCloseOnEsc","shouldFocusAfterRender","parentSelector","hasWarnedAboutMissingLabels","useRef","current","console","warn","hasOpened","modalRef","hasDialogSupport","supportsDialog","useBodyScrollLock","enabled","handleClose","useCallback","event","preventDefault","stopPropagation","transitionCallback","e","target","doClose","close","removeEventListener","classList","remove","addEventListener","modalClasses","classNames","useEffect","toggleModal","window","default","dialogPolyfill","registerDialog","error","open","showModal","add","contextValue","useMemo","dialog","mergeRefs","onCancel","ModalContext","Provider","value","displayName"],"mappings":"AAAA;;;;;+BA+KSA;;;eAAAA;;;;+DA7Kc;uBAShB;mCAC2B;+BACH;uBAEL;8BACG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAK7B,MAAMC,wBAAwB;AAE9B,MAAMD,sBAAQE,IAAAA,iBAAU,EAAC,CAACC,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,8BAA8BC,IAAAA,aAAM,EAAU;IACpD,0BAA0B;IAC1B,IAAI,CAACD,4BAA4BE,OAAO,EAAE;QACxC,IACEP,QACAC,cACAC,qBACAC,0BACAC,gBACA;YACAI,QAAQC,IAAI,CACV,CAAC,yJAAyJ,CAAC;YAE7JJ,4BAA4BE,OAAO,GAAG;QACxC;IACF;IAEA,MAAMG,YAAYJ,IAAAA,aAAM,EAAU;IAClC,MAAMK,WAAWL,IAAAA,aAAM,EAA2B;IAClD,MAAMM,mBAAmBC,IAAAA,6BAAc;IAEvC,mBAAmB;IACnB,IAAInB,UAAU,CAACgB,UAAUH,OAAO,EAAE;QAChCb;QACAgB,UAAUH,OAAO,GAAG;IACtB;IAEAO,IAAAA,oCAAiB,EAAC;QAAEC,SAASvB;IAAO;IAEpC,kDAAkD;IAClD,MAAMwB,cAAcC,IAAAA,kBAAW,EAC7B,CAACC;QACC,IAAIA,OAAO;YACTA,MAAMC,cAAc;YACpBD,MAAME,eAAe;QACvB;QAEA,sEAAsE;QACtE,MAAMC,qBAAqB,CAACC;YAC1B,8DAA8D;YAC9D,oEAAoE;YACpE,IAAIA,EAAEC,MAAM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,oBACAA;iBADAA,qBAAAA,SAASJ,OAAO,cAAhBI,yCAAAA,mBAAkB6B,SAAS;iBAC3B7B,qBAAAA,SAASJ,OAAO,cAAhBI,yCAAAA,mBAAkBgB,SAAS,CAACc,GAAG,CAACrD;gBAChCM,mBAAAA,6BAAAA;YACF,OAAO,IAAI,CAACF,YAAUmB,qBAAAA,SAASJ,OAAO,cAAhBI,yCAAAA,mBAAkB4B,IAAI,GAAE;gBAC5CvB;YACF;QACF;QAEAiB;IACF,GAAG;QAACzC;QAAQE;QAAQsB;QAAaJ;KAAiB;IAElD,MAAM8B,eAAeC,IAAAA,cAAO,EAC1B,IAAO,CAAA;YACLlD,SAASuB;YACTnB;QACF,CAAA,GACA;QAACmB;QAAanB;KAAW;IAG3B,qBACE,qBAAC+C;QACCrD,KAAKsD,IAAAA,gBAAS,EAAC;YAAClC;YAAUpB;SAAI;QAC9BuD,UAAU9B;QACVpB,WAAWkC;kBAEX,cAAA,qBAACiB,0BAAY,CAACC,QAAQ;YAACC,OAAOP;sBAC3B/C;;;AAIT;AAEAR,MAAM+D,WAAW,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":["Modal","TRANSITION_CSS_VARIABLE","forwardRef","props","ref","isOpen","onClose","onOpen","children","className","closeLabel","isFullScreen","animation","size","appElement","preventCloseOnEsc","shouldFocusAfterRender","parentSelector","hasWarnedAboutMissingLabels","useRef","current","console","warn","shouldTransition","supportsDialog","dialogRef","close","useDialog","transition","isEnabled","CSSVariable","modalClasses","classNames","contextValue","useMemo","dialog","mergeRefs","onCancel","ModalContext","Provider","value","displayName"],"mappings":"AAAA;;;;;+BAiGSA;;;eAAAA;;;;+DA/Fc;uBAC0B;+BAClB;uBAEL;8B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AEAR,MAAMyC,WAAW,GAAG"}
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  _export_star(require("./useBodyScrollLock"), exports);
6
6
  _export_star(require("./useBreakpoint"), exports);
7
7
  _export_star(require("./useButton"), exports);
8
+ _export_star(require("./useDialog"), exports);
8
9
  _export_star(require("./useLabel"), exports);
9
10
  _export_star(require("./useOnClickOutside"), exports);
10
11
  _export_star(require("./useTextField"), exports);
@@ -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":";;;;qBAAc;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA"}
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":";;;;qBAAc;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA"}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ _export_star(require("./useDialog"), 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/useDialog/index.ts"],"sourcesContent":["export * from \"./useDialog\";\n"],"names":[],"mappings":";;;;qBAAc"}
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useDialog", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useDialog;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _useDialogPolyfill = require("../useDialogPolyfill");
13
+ const _useBodyScrollLock = require("../useBodyScrollLock");
14
+ const _utils = require("../../utils");
15
+ const TRANSITION_CLASS_NAME = "--transition";
16
+ const FALLBACK_TRANSITION_DURATION = 0;
17
+ const useDialog = (props)=>{
18
+ const { ref, isOpen, transition, onOpen, onClose } = props;
19
+ const shouldTransition = (0, _utils.supportsDialog)() && transition.isEnabled;
20
+ // Read CSS variable value as number
21
+ const TRANSITION_DURATION_IN_MS = ref.current && Number(getComputedStyle(ref.current).getPropertyValue(transition.CSSVariable).replace("ms", "")) || FALLBACK_TRANSITION_DURATION;
22
+ (0, _useDialogPolyfill.useDialogPolyfill)(ref);
23
+ (0, _useBodyScrollLock.useBodyScrollLock)({
24
+ enabled: isOpen
25
+ });
26
+ const open = (0, _react.useCallback)(()=>{
27
+ var _ref_current;
28
+ (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.showModal();
29
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
30
+ }, [
31
+ onOpen,
32
+ ref
33
+ ]);
34
+ const close = (0, _react.useCallback)(()=>{
35
+ var _ref_current;
36
+ (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.close();
37
+ onClose === null || onClose === void 0 ? void 0 : onClose();
38
+ }, [
39
+ onClose,
40
+ ref
41
+ ]);
42
+ // Add close handler, to enable closing transitions
43
+ const handleClose = (0, _react.useCallback)((event)=>{
44
+ if (event) {
45
+ // Prevent default event coming from onCancel,
46
+ // which is triggered by pressing ESC key
47
+ event.preventDefault();
48
+ // Ensure that nested `<dialog>` elements
49
+ // don't close the parent
50
+ event.stopPropagation();
51
+ }
52
+ if (shouldTransition) {
53
+ var _ref_current;
54
+ (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.classList.remove(TRANSITION_CLASS_NAME);
55
+ // Delay close to allow exit transition
56
+ setTimeout(()=>close(), TRANSITION_DURATION_IN_MS);
57
+ } else {
58
+ close();
59
+ }
60
+ }, [
61
+ TRANSITION_DURATION_IN_MS,
62
+ close,
63
+ ref,
64
+ shouldTransition
65
+ ]);
66
+ (0, _react.useEffect)(()=>{
67
+ async function toggleDialog() {
68
+ var _ref_current, _ref_current1;
69
+ if (isOpen && !((_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.open)) {
70
+ open();
71
+ // Fix opening transition in Safari being skipped
72
+ // by wrapping with setTimeout
73
+ setTimeout(()=>{
74
+ var _ref_current;
75
+ (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.classList.add(TRANSITION_CLASS_NAME);
76
+ }, 0);
77
+ } else if (!isOpen && ((_ref_current1 = ref.current) === null || _ref_current1 === void 0 ? void 0 : _ref_current1.open)) {
78
+ handleClose();
79
+ }
80
+ }
81
+ toggleDialog();
82
+ }, [
83
+ handleClose,
84
+ isOpen,
85
+ open,
86
+ ref
87
+ ]);
88
+ return {
89
+ open,
90
+ close: handleClose
91
+ };
92
+ };
93
+
94
+ //# 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":["useDialog","TRANSITION_CLASS_NAME","FALLBACK_TRANSITION_DURATION","props","ref","isOpen","transition","onOpen","onClose","shouldTransition","supportsDialog","isEnabled","TRANSITION_DURATION_IN_MS","current","Number","getComputedStyle","getPropertyValue","CSSVariable","replace","useDialogPolyfill","useBodyScrollLock","enabled","open","useCallback","showModal","close","handleClose","event","preventDefault","stopPropagation","classList","remove","setTimeout","useEffect","toggleDialog","add"],"mappings":";;;;+BA0BaA;;;eAAAA;;;uBArBN;mCAC2B;mCACA;uBACH;AAe/B,MAAMC,wBAAwB;AAC9B,MAAMC,+BAA+B;AAE9B,MAAMF,YAAY,CAACG;IACxB,MAAM,EAAEC,GAAG,EAAEC,MAAM,EAAEC,UAAU,EAAEC,MAAM,EAAEC,OAAO,EAAE,GAAGL;IACrD,MAAMM,mBAAmBC,IAAAA,qBAAc,OAAMJ,WAAWK,SAAS;IAEjE,oCAAoC;IACpC,MAAMC,4BACJ,AAACR,IAAIS,OAAO,IACVC,OACEC,iBAAiBX,IAAIS,OAAO,EACzBG,gBAAgB,CAACV,WAAWW,WAAW,EACvCC,OAAO,CAAC,MAAM,QAErBhB;IAEFiB,IAAAA,oCAAiB,EAACf;IAClBgB,IAAAA,oCAAiB,EAAC;QAAEC,SAAShB;IAAO;IAEpC,MAAMiB,OAAOC,IAAAA,kBAAW,EAAC;YACvBnB;SAAAA,eAAAA,IAAIS,OAAO,cAAXT,mCAAAA,aAAaoB,SAAS;QACtBjB,mBAAAA,6BAAAA;IACF,GAAG;QAACA;QAAQH;KAAI;IAEhB,MAAMqB,QAAQF,IAAAA,kBAAW,EAAC;YACxBnB;SAAAA,eAAAA,IAAIS,OAAO,cAAXT,mCAAAA,aAAaqB,KAAK;QAClBjB,oBAAAA,8BAAAA;IACF,GAAG;QAACA;QAASJ;KAAI;IAEjB,mDAAmD;IACnD,MAAMsB,cAAcH,IAAAA,kBAAW,EAC7B,CAACI;QACC,IAAIA,OAAO;YACT,8CAA8C;YAC9C,yCAAyC;YACzCA,MAAMC,cAAc;YACpB,yCAAyC;YACzC,yBAAyB;YACzBD,MAAME,eAAe;QACvB;QAEA,IAAIpB,kBAAkB;gBACpBL;aAAAA,eAAAA,IA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