@sb1/ffe-modals-react 0.5.2 → 0.5.4

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 (3) hide show
  1. package/es/Modal.js +30 -22
  2. package/lib/Modal.js +30 -22
  3. package/package.json +3 -3
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, _c = _a.portalContainer, portalContainer = _c === void 0 ? document.body : _c, rest = __rest(_a, ["children", "onClick", "ariaLabelledby", "className", "locale", "onClose", "portalContainer"]);
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 html = document.documentElement;
46
- var inShadow = ((_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.getRootNode()) instanceof ShadowRoot;
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 (typeof window !== 'undefined' && // to support Gatsby build(server side rendering)
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 createPortal(
70
- // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
71
- React.createElement("dialog", __assign({}, rest, { ref: dialogRef, className: classnames('ffe-modal', className), "aria-labelledby": ariaLabelledby, onClick: function (event) {
72
- var target = event.target;
73
- if (target.nodeName === 'DIALOG') {
74
- target.close();
75
- }
76
- onClick === null || onClick === void 0 ? void 0 : onClick(event);
77
- }, onClose: function () {
78
- onClose === null || onClose === void 0 ? void 0 : onClose();
79
- setIsOpen(false);
80
- } }),
81
- React.createElement("div", { className: "ffe-modal__body" },
82
- React.createElement(CloseButton, { onClick: function () { var _a; return (_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.close(); }, locale: locale }),
83
- children)), portalContainer);
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, _c = _a.portalContainer, portalContainer = _c === void 0 ? document.body : _c, rest = __rest(_a, ["children", "onClick", "ariaLabelledby", "className", "locale", "onClose", "portalContainer"]);
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 html = document.documentElement;
75
- var inShadow = ((_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.getRootNode()) instanceof ShadowRoot;
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 (typeof window !== 'undefined' && // to support Gatsby build(server side rendering)
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 (0, react_dom_1.createPortal)(
99
- // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
100
- react_1.default.createElement("dialog", __assign({}, rest, { ref: dialogRef, className: (0, classnames_1.default)('ffe-modal', className), "aria-labelledby": ariaLabelledby, onClick: function (event) {
101
- var target = event.target;
102
- if (target.nodeName === 'DIALOG') {
103
- target.close();
104
- }
105
- onClick === null || onClick === void 0 ? void 0 : onClick(event);
106
- }, onClose: function () {
107
- onClose === null || onClose === void 0 ? void 0 : onClose();
108
- setIsOpen(false);
109
- } }),
110
- react_1.default.createElement("div", { className: "ffe-modal__body" },
111
- 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 }),
112
- children)), portalContainer);
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.2",
3
+ "version": "0.5.4",
4
4
  "license": "MIT",
5
5
  "author": "SpareBank 1",
6
6
  "files": [
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@sb1/ffe-icons-react": "^11.0.3",
29
- "@sb1/ffe-modals": "^0.3.0",
29
+ "@sb1/ffe-modals": "^0.3.1",
30
30
  "dialog-polyfill": "^0.5.6"
31
31
  },
32
32
  "devDependencies": {
@@ -41,5 +41,5 @@
41
41
  "publishConfig": {
42
42
  "access": "public"
43
43
  },
44
- "gitHead": "98d33cf2e867ce718139edf1adf97336b8da6b68"
44
+ "gitHead": "ff16d78f24c2bf9dd5b7da1cf33a52e09ea3a523"
45
45
  }