@sb1/ffe-modals-react 0.5.1 → 0.5.3
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/es/Modal.js +30 -22
- package/lib/Modal.js +30 -22
- package/package.json +5 -5
package/es/Modal.js
CHANGED
|
@@ -25,10 +25,12 @@ import { createPortal } from 'react-dom';
|
|
|
25
25
|
import classnames from 'classnames';
|
|
26
26
|
import { CloseButton } from './CloseButton';
|
|
27
27
|
export var Modal = React.forwardRef(function (_a, ref) {
|
|
28
|
-
var children = _a.children, onClick = _a.onClick, ariaLabelledby = _a.ariaLabelledby, className = _a.className, _b = _a.locale, locale = _b === void 0 ? 'nb' : _b, onClose = _a.onClose,
|
|
28
|
+
var children = _a.children, onClick = _a.onClick, ariaLabelledby = _a.ariaLabelledby, className = _a.className, _b = _a.locale, locale = _b === void 0 ? 'nb' : _b, onClose = _a.onClose, portalContainer = _a.portalContainer, rest = __rest(_a, ["children", "onClick", "ariaLabelledby", "className", "locale", "onClose", "portalContainer"]);
|
|
29
|
+
// SSR does not have access to document/window etc.
|
|
30
|
+
var _c = useState(false), isClient = _c[0], setIsClient = _c[1];
|
|
29
31
|
var _d = useState(false), isOpen = _d[0], setIsOpen = _d[1];
|
|
30
32
|
var dialogRef = useRef(null);
|
|
31
|
-
var htmlOverflowY = useRef(document.documentElement.style.overflowY);
|
|
33
|
+
var htmlOverflowY = useRef(isClient ? document.documentElement.style.overflowY : '');
|
|
32
34
|
useImperativeHandle(ref, function () { return ({
|
|
33
35
|
open: function () {
|
|
34
36
|
var _a;
|
|
@@ -40,11 +42,15 @@ export var Modal = React.forwardRef(function (_a, ref) {
|
|
|
40
42
|
(_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.close();
|
|
41
43
|
},
|
|
42
44
|
}); });
|
|
45
|
+
useEffect(function () {
|
|
46
|
+
setIsClient(true);
|
|
47
|
+
}, []);
|
|
43
48
|
useEffect(function () {
|
|
44
49
|
var _a;
|
|
45
|
-
var
|
|
46
|
-
|
|
50
|
+
var inShadow = isClient &&
|
|
51
|
+
((_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.getRootNode()) instanceof ShadowRoot;
|
|
47
52
|
if (inShadow) {
|
|
53
|
+
var html = document.documentElement;
|
|
48
54
|
if (isOpen) {
|
|
49
55
|
htmlOverflowY.current = html.style.overflowY;
|
|
50
56
|
html.style.overflowY = 'hidden';
|
|
@@ -53,9 +59,9 @@ export var Modal = React.forwardRef(function (_a, ref) {
|
|
|
53
59
|
html.style.overflowY = htmlOverflowY.current;
|
|
54
60
|
}
|
|
55
61
|
}
|
|
56
|
-
}, [isOpen]);
|
|
62
|
+
}, [isClient, isOpen]);
|
|
57
63
|
useEffect(function () {
|
|
58
|
-
if (
|
|
64
|
+
if (isClient &&
|
|
59
65
|
dialogRef.current &&
|
|
60
66
|
typeof dialogRef.current.showModal !== 'function') {
|
|
61
67
|
import('dialog-polyfill').then(function (_a) {
|
|
@@ -65,20 +71,22 @@ export var Modal = React.forwardRef(function (_a, ref) {
|
|
|
65
71
|
}
|
|
66
72
|
});
|
|
67
73
|
}
|
|
68
|
-
}, []);
|
|
69
|
-
return
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
target.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
React.createElement(
|
|
83
|
-
|
|
74
|
+
}, [isClient]);
|
|
75
|
+
return isClient
|
|
76
|
+
? createPortal(
|
|
77
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
|
|
78
|
+
React.createElement("dialog", __assign({}, rest, { ref: dialogRef, className: classnames('ffe-modal', className), "aria-labelledby": ariaLabelledby, onClick: function (event) {
|
|
79
|
+
var target = event.target;
|
|
80
|
+
if (target.nodeName === 'DIALOG') {
|
|
81
|
+
target.close();
|
|
82
|
+
}
|
|
83
|
+
onClick === null || onClick === void 0 ? void 0 : onClick(event);
|
|
84
|
+
}, onClose: function () {
|
|
85
|
+
onClose === null || onClose === void 0 ? void 0 : onClose();
|
|
86
|
+
setIsOpen(false);
|
|
87
|
+
} }),
|
|
88
|
+
React.createElement("div", { className: "ffe-modal__body" },
|
|
89
|
+
React.createElement(CloseButton, { onClick: function () { var _a; return (_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.close(); }, locale: locale }),
|
|
90
|
+
children)), portalContainer !== null && portalContainer !== void 0 ? portalContainer : document.body)
|
|
91
|
+
: null;
|
|
84
92
|
});
|
package/lib/Modal.js
CHANGED
|
@@ -54,10 +54,12 @@ var react_dom_1 = require("react-dom");
|
|
|
54
54
|
var classnames_1 = __importDefault(require("classnames"));
|
|
55
55
|
var CloseButton_1 = require("./CloseButton");
|
|
56
56
|
exports.Modal = react_1.default.forwardRef(function (_a, ref) {
|
|
57
|
-
var children = _a.children, onClick = _a.onClick, ariaLabelledby = _a.ariaLabelledby, className = _a.className, _b = _a.locale, locale = _b === void 0 ? 'nb' : _b, onClose = _a.onClose,
|
|
57
|
+
var children = _a.children, onClick = _a.onClick, ariaLabelledby = _a.ariaLabelledby, className = _a.className, _b = _a.locale, locale = _b === void 0 ? 'nb' : _b, onClose = _a.onClose, portalContainer = _a.portalContainer, rest = __rest(_a, ["children", "onClick", "ariaLabelledby", "className", "locale", "onClose", "portalContainer"]);
|
|
58
|
+
// SSR does not have access to document/window etc.
|
|
59
|
+
var _c = (0, react_1.useState)(false), isClient = _c[0], setIsClient = _c[1];
|
|
58
60
|
var _d = (0, react_1.useState)(false), isOpen = _d[0], setIsOpen = _d[1];
|
|
59
61
|
var dialogRef = (0, react_1.useRef)(null);
|
|
60
|
-
var htmlOverflowY = (0, react_1.useRef)(document.documentElement.style.overflowY);
|
|
62
|
+
var htmlOverflowY = (0, react_1.useRef)(isClient ? document.documentElement.style.overflowY : '');
|
|
61
63
|
(0, react_1.useImperativeHandle)(ref, function () { return ({
|
|
62
64
|
open: function () {
|
|
63
65
|
var _a;
|
|
@@ -69,11 +71,15 @@ exports.Modal = react_1.default.forwardRef(function (_a, ref) {
|
|
|
69
71
|
(_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.close();
|
|
70
72
|
},
|
|
71
73
|
}); });
|
|
74
|
+
(0, react_1.useEffect)(function () {
|
|
75
|
+
setIsClient(true);
|
|
76
|
+
}, []);
|
|
72
77
|
(0, react_1.useEffect)(function () {
|
|
73
78
|
var _a;
|
|
74
|
-
var
|
|
75
|
-
|
|
79
|
+
var inShadow = isClient &&
|
|
80
|
+
((_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.getRootNode()) instanceof ShadowRoot;
|
|
76
81
|
if (inShadow) {
|
|
82
|
+
var html = document.documentElement;
|
|
77
83
|
if (isOpen) {
|
|
78
84
|
htmlOverflowY.current = html.style.overflowY;
|
|
79
85
|
html.style.overflowY = 'hidden';
|
|
@@ -82,9 +88,9 @@ exports.Modal = react_1.default.forwardRef(function (_a, ref) {
|
|
|
82
88
|
html.style.overflowY = htmlOverflowY.current;
|
|
83
89
|
}
|
|
84
90
|
}
|
|
85
|
-
}, [isOpen]);
|
|
91
|
+
}, [isClient, isOpen]);
|
|
86
92
|
(0, react_1.useEffect)(function () {
|
|
87
|
-
if (
|
|
93
|
+
if (isClient &&
|
|
88
94
|
dialogRef.current &&
|
|
89
95
|
typeof dialogRef.current.showModal !== 'function') {
|
|
90
96
|
Promise.resolve().then(function () { return __importStar(require('dialog-polyfill')); }).then(function (_a) {
|
|
@@ -94,20 +100,22 @@ exports.Modal = react_1.default.forwardRef(function (_a, ref) {
|
|
|
94
100
|
}
|
|
95
101
|
});
|
|
96
102
|
}
|
|
97
|
-
}, []);
|
|
98
|
-
return
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
target.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
react_1.default.createElement(
|
|
112
|
-
|
|
103
|
+
}, [isClient]);
|
|
104
|
+
return isClient
|
|
105
|
+
? (0, react_dom_1.createPortal)(
|
|
106
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
|
|
107
|
+
react_1.default.createElement("dialog", __assign({}, rest, { ref: dialogRef, className: (0, classnames_1.default)('ffe-modal', className), "aria-labelledby": ariaLabelledby, onClick: function (event) {
|
|
108
|
+
var target = event.target;
|
|
109
|
+
if (target.nodeName === 'DIALOG') {
|
|
110
|
+
target.close();
|
|
111
|
+
}
|
|
112
|
+
onClick === null || onClick === void 0 ? void 0 : onClick(event);
|
|
113
|
+
}, onClose: function () {
|
|
114
|
+
onClose === null || onClose === void 0 ? void 0 : onClose();
|
|
115
|
+
setIsOpen(false);
|
|
116
|
+
} }),
|
|
117
|
+
react_1.default.createElement("div", { className: "ffe-modal__body" },
|
|
118
|
+
react_1.default.createElement(CloseButton_1.CloseButton, { onClick: function () { var _a; return (_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.close(); }, locale: locale }),
|
|
119
|
+
children)), portalContainer !== null && portalContainer !== void 0 ? portalContainer : document.body)
|
|
120
|
+
: null;
|
|
113
121
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sb1/ffe-modals-react",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "SpareBank 1",
|
|
6
6
|
"files": [
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"test:watch": "ffe-buildtool jest --watch"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@sb1/ffe-icons-react": "^11.0.
|
|
29
|
-
"@sb1/ffe-modals": "^0.
|
|
28
|
+
"@sb1/ffe-icons-react": "^11.0.3",
|
|
29
|
+
"@sb1/ffe-modals": "^0.3.0",
|
|
30
30
|
"dialog-polyfill": "^0.5.6"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@sb1/ffe-buildtool": "^0.
|
|
33
|
+
"@sb1/ffe-buildtool": "^0.8.0",
|
|
34
34
|
"eslint": "^8.57.0",
|
|
35
35
|
"react": "^18.2.0",
|
|
36
36
|
"react-dom": "^18.2.0"
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"publishConfig": {
|
|
42
42
|
"access": "public"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "6a59fb8e29f1d533f772abe85464718674e43187"
|
|
45
45
|
}
|