@simplybusiness/mobius 4.5.0 → 4.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/cjs/components/Button/Button.js.map +1 -1
- package/dist/cjs/components/Drawer/Drawer.js +20 -126
- package/dist/cjs/components/Drawer/Drawer.js.map +1 -1
- package/dist/cjs/components/Modal/Modal.js +19 -125
- package/dist/cjs/components/Modal/Modal.js.map +1 -1
- package/dist/cjs/hooks/index.js +1 -0
- package/dist/cjs/hooks/index.js.map +1 -1
- package/dist/cjs/hooks/useDialog/index.js +20 -0
- package/dist/cjs/hooks/useDialog/index.js.map +1 -0
- package/dist/cjs/hooks/useDialog/useDialog.js +94 -0
- package/dist/cjs/hooks/useDialog/useDialog.js.map +1 -0
- package/dist/cjs/hooks/useDialogPolyfill/index.js +20 -0
- package/dist/cjs/hooks/useDialogPolyfill/index.js.map +1 -0
- package/dist/cjs/hooks/useDialogPolyfill/useDialogPolyfill.js +77 -0
- package/dist/cjs/hooks/useDialogPolyfill/useDialogPolyfill.js.map +1 -0
- package/dist/esm/components/Button/Button.js.map +1 -1
- package/dist/esm/components/Drawer/Drawer.js +22 -87
- package/dist/esm/components/Drawer/Drawer.js.map +1 -1
- package/dist/esm/components/Modal/Modal.js +20 -85
- package/dist/esm/components/Modal/Modal.js.map +1 -1
- package/dist/esm/hooks/index.js +1 -0
- package/dist/esm/hooks/index.js.map +1 -1
- package/dist/esm/hooks/useDialog/index.js +3 -0
- package/dist/esm/hooks/useDialog/index.js.map +1 -0
- package/dist/esm/hooks/useDialog/useDialog.js +84 -0
- package/dist/esm/hooks/useDialog/useDialog.js.map +1 -0
- package/dist/esm/hooks/useDialogPolyfill/index.js +3 -0
- package/dist/esm/hooks/useDialogPolyfill/index.js.map +1 -0
- package/dist/esm/hooks/useDialogPolyfill/useDialogPolyfill.js +27 -0
- package/dist/esm/hooks/useDialogPolyfill/useDialogPolyfill.js.map +1 -0
- package/dist/types/components/Button/Button.d.ts +1 -1
- package/dist/types/components/Button/Button.stories.d.ts +2 -1
- package/dist/types/hooks/index.d.ts +1 -0
- package/dist/types/hooks/useDialog/index.d.ts +1 -0
- package/dist/types/hooks/useDialog/useDialog.d.ts +16 -0
- package/dist/types/hooks/useDialogPolyfill/index.d.ts +1 -0
- package/dist/types/hooks/useDialogPolyfill/useDialogPolyfill.d.ts +2 -0
- package/package.json +2 -1
- package/src/components/Button/Button.stories.tsx +14 -1
- package/src/components/Button/Button.test.tsx +8 -0
- package/src/components/Button/Button.tsx +2 -1
- package/src/components/Drawer/Drawer.stories.tsx +1 -1
- package/src/components/Drawer/Drawer.tsx +22 -100
- package/src/components/Modal/Modal.tsx +20 -98
- package/src/hooks/index.tsx +1 -0
- package/src/hooks/useDialog/index.ts +1 -0
- package/src/hooks/useDialog/useDialog.ts +98 -0
- package/src/hooks/useDialogPolyfill/index.ts +1 -0
- package/src/hooks/useDialogPolyfill/useDialogPolyfill.ts +32 -0
- package/dist/cjs/tsconfig.tsbuildinfo +0 -1
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;;;;;+
|
|
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
|
-
|
|
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
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
|
|
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
|
-
|
|
146
|
-
|
|
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:
|
|
43
|
+
onClose: close,
|
|
150
44
|
closeLabel
|
|
151
45
|
}), [
|
|
152
|
-
|
|
46
|
+
close,
|
|
153
47
|
closeLabel
|
|
154
48
|
]);
|
|
155
49
|
return /*#__PURE__*/ (0, _jsxruntime.jsxs)("dialog", {
|
|
156
50
|
ref: (0, _utils.mergeRefs)([
|
|
157
|
-
|
|
51
|
+
dialogRef,
|
|
158
52
|
ref
|
|
159
53
|
]),
|
|
160
|
-
onCancel:
|
|
161
|
-
className:
|
|
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 {
|
|
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
|
-
|
|
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
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
120
|
-
|
|
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": !
|
|
124
|
-
"--should-transition":
|
|
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:
|
|
56
|
+
onClose: close,
|
|
163
57
|
closeLabel
|
|
164
58
|
}), [
|
|
165
|
-
|
|
59
|
+
close,
|
|
166
60
|
closeLabel
|
|
167
61
|
]);
|
|
168
62
|
return /*#__PURE__*/ (0, _jsxruntime.jsx)("dialog", {
|
|
169
63
|
ref: (0, _utils.mergeRefs)([
|
|
170
|
-
|
|
64
|
+
dialogRef,
|
|
171
65
|
ref
|
|
172
66
|
]),
|
|
173
|
-
onCancel:
|
|
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 {
|
|
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;8BACG;uBACH;;;;;;AAK1B,MAAMC,0BAA0B;AAEhC,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,mBAAmBC,IAAAA,6BAAc;IACvC,MAAMC,YAAYN,IAAAA,aAAM,EAA2B;IACnD,MAAM,EAAEO,KAAK,EAAE,GAAGC,IAAAA,gBAAS,EAAC;QAC1BvB,KAAKqB;QACLpB;QACAE;QACAD;QACAsB,YAAY;YACVC,WAAW,CAAC,CAACjB;YACbkB,aAAa7B;QACf;IACF;IAEA,MAAM8B,eAAeC,IAAAA,eAAU,EAC7B,UACA,gBACA;QACE,uBAAuB,CAACT;QACxB,uBAAuBA,oBAAoBX;QAC3C,cAAcA,cAAc;QAC5B,UAAUA,cAAc;QACxB,mBAAmBD;IACrB,GACAF;IAGF,MAAMwB,eAAeC,IAAAA,cAAO,EAC1B,IAAO,CAAA;YACL5B,SAASoB;YACThB;QACF,CAAA,GACA;QAACgB;QAAOhB;KAAW;IAGrB,qBACE,qBAACyB;QACC/B,KAAKgC,IAAAA,gBAAS,EAAC;YAACX;YAAWrB;SAAI;QAC/BiC,UAAUX;QACVjB,WAAWsB;kBAEX,cAAA,qBAACO,0BAAY,CAACC,QAAQ;YAACC,OAAOP;sBAC3BzB;;;AAIT;AAEAR,MAAMyC,WAAW,GAAG"}
|
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -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,IAAIS,OAAO,cAAXT,mCAAAA,aAAa0B,SAAS,CAACC,MAAM,CAAC9B;YAC9B,uCAAuC;YACvC+B,WAAW,IAAMP,SAASb;QAC5B,OAAO;YACLa;QACF;IACF,GACA;QAACb;QAA2Ba;QAAOrB;QAAKK;KAAiB;IAG3DwB,IAAAA,gBAAS,EAAC;QACR,eAAeC;gBACE9B,cAOOA;YAPtB,IAAIC,UAAU,GAACD,eAAAA,IAAIS,OAAO,cAAXT,mCAAAA,aAAakB,IAAI,GAAE;gBAChCA;gBACA,iDAAiD;gBACjD,8BAA8B;gBAC9BU,WAAW;wBACT5B;qBAAAA,eAAAA,IAAIS,OAAO,cAAXT,mCAAAA,aAAa0B,SAAS,CAACK,GAAG,CAAClC;gBAC7B,GAAG;YACL,OAAO,IAAI,CAACI,YAAUD,gBAAAA,IAAIS,OAAO,cAAXT,oCAAAA,cAAakB,IAAI,GAAE;gBACvCI;YACF;QACF;QAEAQ;IACF,GAAG;QAACR;QAAarB;QAAQiB;QAAMlB;KAAI;IAEnC,OAAO;QACLkB;QACAG,OAAOC;IACT;AACF"}
|