@spaced-out/ui-design-system 0.1.7 → 0.1.8

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 CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [0.1.8](https://github.com/spaced-out/ui-design-system/compare/v0.1.7...v0.1.8) (2023-04-05)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * modal scroll fix ([47a2349](https://github.com/spaced-out/ui-design-system/commit/47a23496c8b9328ab6cbe3f7736635776fb4f797))
11
+ * modal scroll fix ([c33b8bd](https://github.com/spaced-out/ui-design-system/commit/c33b8bd6b4a7cf4c4a51c2abe8a3211c829b054b))
12
+
5
13
  ### [0.1.7](https://github.com/spaced-out/ui-design-system/compare/v0.1.6...v0.1.7) (2023-04-05)
6
14
 
7
15
 
@@ -67,6 +67,34 @@ const createPortalRoot = id => {
67
67
  return modalRoot;
68
68
  };
69
69
  const getModalRoot = id => document.getElementById(`modal-root-${id}`);
70
+ function hasChildNode(nodeList) {
71
+ for (let i = 0, len = nodeList.length; i < len; i++) {
72
+ if (nodeList[i].firstChild !== null) {
73
+ return true;
74
+ }
75
+ }
76
+ return false;
77
+ }
78
+ const fixBody = bodyEl => {
79
+ document.getElementsByTagName('body')[0].classList.add('fixed');
80
+ bodyEl.style.overflow = 'hidden';
81
+ };
82
+ const unfixBody = bodyEl => {
83
+ document.getElementsByTagName('body')[0].classList.remove('fixed');
84
+ bodyEl.style.overflow = '';
85
+ };
86
+ const checkAndAddBodyOverflow = bodyEl => {
87
+ const nodes = document.querySelectorAll('[id^="modal-root"]');
88
+ let isParentModalPresent = false;
89
+ if (nodes.length) {
90
+ isParentModalPresent = hasChildNode(nodes);
91
+ }
92
+ if (isParentModalPresent) {
93
+ fixBody(bodyEl);
94
+ } else {
95
+ unfixBody(bodyEl);
96
+ }
97
+ };
70
98
  const Modal = _ref4 => {
71
99
  let {
72
100
  classNames,
@@ -74,7 +102,6 @@ const Modal = _ref4 => {
74
102
  isOpen = false,
75
103
  onClose,
76
104
  hideBackdrop = false,
77
- removeWhenClosed = true,
78
105
  tapOutsideToClose = true,
79
106
  initialFocus = -1
80
107
  } = _ref4;
@@ -97,7 +124,7 @@ const Modal = _ref4 => {
97
124
  portal.remove();
98
125
  // Ensure scroll overflow is removed
99
126
  if (bodyEl) {
100
- bodyEl.style.overflow = '';
127
+ unfixBody(bodyEl);
101
128
  }
102
129
  };
103
130
  }, []);
@@ -107,11 +134,11 @@ const Modal = _ref4 => {
107
134
  const updatePageScroll = () => {
108
135
  if (isOpen) {
109
136
  if (bodyRef.current) {
110
- bodyRef.current.style.overflow = 'hidden';
137
+ fixBody(bodyRef.current);
111
138
  }
112
139
  } else {
113
140
  if (bodyRef.current) {
114
- bodyRef.current.style.overflow = '';
141
+ unfixBody(bodyRef.current);
115
142
  }
116
143
  }
117
144
  };
@@ -132,7 +159,14 @@ const Modal = _ref4 => {
132
159
  window.removeEventListener('keyup', onKeyPress);
133
160
  };
134
161
  }, [isOpen, onClose]);
135
- if (!isTransitioning && removeWhenClosed && !isOpen) {
162
+ if (!isTransitioning && !isOpen) {
163
+ // Check overflow after resetting the DOM for modal. This should always happen after DOM reset
164
+ // TODO(Nishant): Better way to do this?
165
+ setTimeout(() => {
166
+ if (bodyRef && !!bodyRef.current) {
167
+ checkAndAddBodyOverflow(bodyRef.current);
168
+ }
169
+ });
136
170
  return null;
137
171
  }
138
172
  const onBackdropClick = e => {
@@ -37,6 +37,7 @@ export type ModalProps = {
37
37
  isOpen?: boolean,
38
38
  onClose?: ?(SyntheticEvent<HTMLElement>) => mixed,
39
39
  hideBackdrop?: boolean,
40
+ // This prop is removed now
40
41
  removeWhenClosed?: boolean,
41
42
  tapOutsideToClose?: boolean,
42
43
  initialFocus?: number,
@@ -116,13 +117,44 @@ const createPortalRoot = (id: string) => {
116
117
  const getModalRoot = (id: string) =>
117
118
  document.getElementById(`modal-root-${id}`);
118
119
 
120
+ function hasChildNode(nodeList) {
121
+ for (let i = 0, len = nodeList.length; i < len; i++) {
122
+ if (nodeList[i].firstChild !== null) {
123
+ return true;
124
+ }
125
+ }
126
+ return false;
127
+ }
128
+
129
+ const fixBody = (bodyEl: HTMLBodyElement) => {
130
+ document.getElementsByTagName('body')[0].classList.add('fixed');
131
+ bodyEl.style.overflow = 'hidden';
132
+ };
133
+
134
+ const unfixBody = (bodyEl: HTMLBodyElement) => {
135
+ document.getElementsByTagName('body')[0].classList.remove('fixed');
136
+ bodyEl.style.overflow = '';
137
+ };
138
+
139
+ const checkAndAddBodyOverflow = (bodyEl: HTMLBodyElement) => {
140
+ const nodes = document.querySelectorAll('[id^="modal-root"]');
141
+ let isParentModalPresent = false;
142
+ if (nodes.length) {
143
+ isParentModalPresent = hasChildNode(nodes);
144
+ }
145
+ if (isParentModalPresent) {
146
+ fixBody(bodyEl);
147
+ } else {
148
+ unfixBody(bodyEl);
149
+ }
150
+ };
151
+
119
152
  export const Modal = ({
120
153
  classNames,
121
154
  children,
122
155
  isOpen = false,
123
156
  onClose,
124
157
  hideBackdrop = false,
125
- removeWhenClosed = true,
126
158
  tapOutsideToClose = true,
127
159
  initialFocus = -1,
128
160
  }: ModalProps): React.Node => {
@@ -150,7 +182,7 @@ export const Modal = ({
150
182
  portal.remove();
151
183
  // Ensure scroll overflow is removed
152
184
  if (bodyEl) {
153
- bodyEl.style.overflow = '';
185
+ unfixBody(bodyEl);
154
186
  }
155
187
  };
156
188
  }, []);
@@ -160,11 +192,11 @@ export const Modal = ({
160
192
  const updatePageScroll = () => {
161
193
  if (isOpen) {
162
194
  if (bodyRef.current) {
163
- bodyRef.current.style.overflow = 'hidden';
195
+ fixBody(bodyRef.current);
164
196
  }
165
197
  } else {
166
198
  if (bodyRef.current) {
167
- bodyRef.current.style.overflow = '';
199
+ unfixBody(bodyRef.current);
168
200
  }
169
201
  }
170
202
  };
@@ -189,7 +221,15 @@ export const Modal = ({
189
221
  };
190
222
  }, [isOpen, onClose]);
191
223
 
192
- if (!isTransitioning && removeWhenClosed && !isOpen) {
224
+ if (!isTransitioning && !isOpen) {
225
+ // Check overflow after resetting the DOM for modal. This should always happen after DOM reset
226
+ // TODO(Nishant): Better way to do this?
227
+ setTimeout(() => {
228
+ if (bodyRef && !!bodyRef.current) {
229
+ checkAndAddBodyOverflow(bodyRef.current);
230
+ }
231
+ });
232
+
193
233
  return null;
194
234
  }
195
235
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spaced-out/ui-design-system",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "main": "index.js",
5
5
  "description": "Sense UI components library",
6
6
  "author": {