@uxf/core 10.0.0-beta.28 → 10.0.0-beta.37

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.
@@ -1,4 +1,4 @@
1
- import { BodyScrollOptions } from "body-scroll-lock";
1
+ import { BodyScrollOptions } from "../utils/bodyScrollLock";
2
2
  import { RefObject } from "react";
3
3
  export interface UseBodyScrollLockOptions extends BodyScrollOptions {
4
4
  clearAllOnClose?: boolean;
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useBodyScrollLock = void 0;
4
- const body_scroll_lock_1 = require("body-scroll-lock");
4
+ const bodyScrollLock_1 = require("../utils/bodyScrollLock");
5
5
  const react_1 = require("react");
6
6
  function useBodyScrollLock(containerRef, isOpen, { allowTouchMove, clearAllOnClose, reserveScrollBarGap } = {}) {
7
7
  (0, react_1.useEffect)(() => {
8
8
  const node = containerRef.current;
9
9
  if (isOpen && node) {
10
- (0, body_scroll_lock_1.disableBodyScroll)(node, {
10
+ (0, bodyScrollLock_1.disableBodyScroll)(node, {
11
11
  allowTouchMove: allowTouchMove ||
12
12
  ((element) => {
13
13
  var _a;
@@ -24,10 +24,10 @@ function useBodyScrollLock(containerRef, isOpen, { allowTouchMove, clearAllOnClo
24
24
  }
25
25
  return () => {
26
26
  if (node) {
27
- (0, body_scroll_lock_1.enableBodyScroll)(node);
27
+ (0, bodyScrollLock_1.enableBodyScroll)(node);
28
28
  }
29
29
  if (clearAllOnClose) {
30
- (0, body_scroll_lock_1.clearAllBodyScrollLocks)();
30
+ (0, bodyScrollLock_1.clearAllBodyScrollLocks)();
31
31
  }
32
32
  };
33
33
  }, [allowTouchMove, clearAllOnClose, containerRef, isOpen, reserveScrollBarGap]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/core",
3
- "version": "10.0.0-beta.28",
3
+ "version": "10.0.0-beta.37",
4
4
  "description": "UXF Core",
5
5
  "author": "Petr Vejvoda <vejvoda@uxf.cz>",
6
6
  "homepage": "https://gitlab.com/uxf-npm/core#readme",
@@ -25,16 +25,13 @@
25
25
  "url": "https://gitlab.com/uxf-npm/core/issues"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/body-scroll-lock": "^3.1.0",
29
28
  "@types/jest": "^27.4.0",
30
29
  "@types/react": "^18.2.6",
31
- "body-scroll-lock": "^4.0.0-beta.0",
32
30
  "jest": "^27.4.5",
33
31
  "react": "^18.2.0",
34
32
  "ts-jest": "^27.1.2"
35
33
  },
36
34
  "peerDependencies": {
37
- "body-scroll-lock": "^4.0.0-beta.0",
38
35
  "react": "^17.0.0 || ^18.0.0"
39
36
  },
40
37
  "gitHead": "a2dee6aa67621f6eda17e63e88d0edf14f88455a"
@@ -0,0 +1,7 @@
1
+ export interface BodyScrollOptions {
2
+ reserveScrollBarGap?: boolean;
3
+ allowTouchMove?: (el: any) => boolean;
4
+ }
5
+ export declare const disableBodyScroll: (targetElement: any, options?: BodyScrollOptions) => void;
6
+ export declare const clearAllBodyScrollLocks: () => void;
7
+ export declare const enableBodyScroll: (targetElement: any) => void;
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+ // Forked from "body-scroll-lock" https://github.com/willmcpo/body-scroll-lock
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.enableBodyScroll = exports.clearAllBodyScrollLocks = exports.disableBodyScroll = void 0;
5
+ function noop() {
6
+ return void null;
7
+ }
8
+ // Older browsers don't support event options, feature detect it.
9
+ let hasPassiveEvents = false;
10
+ if (typeof window !== "undefined") {
11
+ const passiveTestOptions = {
12
+ get passive() {
13
+ hasPassiveEvents = true;
14
+ return undefined;
15
+ },
16
+ };
17
+ window.addEventListener("testPassive", noop, passiveTestOptions);
18
+ window.removeEventListener("testPassive", noop);
19
+ }
20
+ const isIosDevice = typeof window !== "undefined" &&
21
+ typeof window.navigator !== "undefined" &&
22
+ typeof window.navigator.platform !== "undefined" &&
23
+ (/iP(ad|hone|od)/.test(window.navigator.platform) ||
24
+ (window.navigator.platform === "MacIntel" && window.navigator.maxTouchPoints > 1));
25
+ let locks = [];
26
+ let documentListenerAdded = false;
27
+ let initialClientY = -1;
28
+ let previousBodyOverflowSetting;
29
+ let previousBodyPosition;
30
+ let previousBodyPaddingRight;
31
+ // returns true if `el` should be allowed to receive touchmove events.
32
+ const allowTouchMove = (el) => locks.some((lock) => !!(lock.options.allowTouchMove && lock.options.allowTouchMove(el)));
33
+ const preventDefault = (rawEvent) => {
34
+ const e = typeof rawEvent !== "undefined" ? rawEvent : window.event;
35
+ // For the case whereby consumers adds a touchmove event listener to document.
36
+ // Recall that we do document.addEventListener('touchmove', preventDefault, { passive: false })
37
+ // in disableBodyScroll - so if we provide this opportunity to allowTouchMove, then
38
+ // the touchmove event on document will break.
39
+ if ((e === null || e === void 0 ? void 0 : e.target) && allowTouchMove(e.target)) {
40
+ return true;
41
+ }
42
+ // Do not prevent if the event has more than one touch (usually meaning this is a multi touch gesture like pinch to zoom).
43
+ if ((e === null || e === void 0 ? void 0 : e.touches.length) > 1) {
44
+ return true;
45
+ }
46
+ if (e === null || e === void 0 ? void 0 : e.preventDefault) {
47
+ e.preventDefault();
48
+ }
49
+ return false;
50
+ };
51
+ const setOverflowHidden = (options) => {
52
+ // If previousBodyPaddingRight is already set, don't set it again.
53
+ if (previousBodyPaddingRight === undefined) {
54
+ const reserveScrollBarGap = !!options && options.reserveScrollBarGap === true;
55
+ const scrollBarGap = window.innerWidth - document.documentElement.clientWidth;
56
+ if (reserveScrollBarGap && scrollBarGap > 0) {
57
+ const computedBodyPaddingRight = parseInt(window.getComputedStyle(document.body).getPropertyValue("padding-right"), 10);
58
+ previousBodyPaddingRight = document.body.style.paddingRight;
59
+ document.body.style.paddingRight = `${computedBodyPaddingRight + scrollBarGap}px`;
60
+ }
61
+ }
62
+ // If previousBodyOverflowSetting is already set, don't set it again.
63
+ if (previousBodyOverflowSetting === undefined) {
64
+ previousBodyOverflowSetting = document.body.style.overflow;
65
+ document.body.style.overflow = "hidden";
66
+ }
67
+ };
68
+ const restoreOverflowSetting = () => {
69
+ if (previousBodyPaddingRight !== undefined) {
70
+ document.body.style.paddingRight = previousBodyPaddingRight;
71
+ // Restore previousBodyPaddingRight to undefined so setOverflowHidden knows it
72
+ // can be set again.
73
+ previousBodyPaddingRight = undefined;
74
+ }
75
+ if (previousBodyOverflowSetting !== undefined) {
76
+ document.body.style.overflow = previousBodyOverflowSetting;
77
+ // Restore previousBodyOverflowSetting to undefined
78
+ // so setOverflowHidden knows it can be set again.
79
+ previousBodyOverflowSetting = undefined;
80
+ }
81
+ };
82
+ const setPositionFixed = () => window.requestAnimationFrame(() => {
83
+ // If previousBodyPosition is already set, don't set it again.
84
+ if (previousBodyPosition === undefined) {
85
+ previousBodyPosition = {
86
+ position: document.body.style.position,
87
+ top: document.body.style.top,
88
+ left: document.body.style.left,
89
+ };
90
+ // Update the dom inside an animation frame
91
+ const { scrollY, scrollX, innerHeight } = window;
92
+ document.body.style.position = "fixed";
93
+ document.body.style.top = `${-scrollY}px`;
94
+ document.body.style.left = `${-scrollX}px`;
95
+ setTimeout(() => window.requestAnimationFrame(() => {
96
+ // Attempt to check if the bottom bar appeared due to the position change
97
+ const bottomBarHeight = innerHeight - window.innerHeight;
98
+ if (bottomBarHeight && scrollY >= innerHeight) {
99
+ // Move the content further up so that the bottom bar doesn't hide it
100
+ document.body.style.top = `${-(scrollY + bottomBarHeight)}px`;
101
+ }
102
+ }), 300);
103
+ }
104
+ });
105
+ const restorePositionSetting = () => {
106
+ if (previousBodyPosition !== undefined) {
107
+ // Convert the position from "px" to Int
108
+ const y = -parseInt(document.body.style.top, 10);
109
+ const x = -parseInt(document.body.style.left, 10);
110
+ // Restore styles
111
+ document.body.style.position = previousBodyPosition.position;
112
+ document.body.style.top = previousBodyPosition.top;
113
+ document.body.style.left = previousBodyPosition.left;
114
+ // Restore scroll
115
+ window.scrollTo(x, y);
116
+ previousBodyPosition = undefined;
117
+ }
118
+ };
119
+ // https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
120
+ const isTargetElementTotallyScrolled = (targetElement) => targetElement ? targetElement.scrollHeight - targetElement.scrollTop <= targetElement.clientHeight : false;
121
+ const handleScroll = (event, targetElement) => {
122
+ const clientY = event.targetTouches[0].clientY - initialClientY;
123
+ if (event.target && allowTouchMove(event.target)) {
124
+ return false;
125
+ }
126
+ if (targetElement && targetElement.scrollTop === 0 && clientY > 0) {
127
+ // element is at the top of its scroll.
128
+ return preventDefault(event);
129
+ }
130
+ if (isTargetElementTotallyScrolled(targetElement) && clientY < 0) {
131
+ // element is at the bottom of its scroll.
132
+ return preventDefault(event);
133
+ }
134
+ event.stopPropagation();
135
+ return true;
136
+ };
137
+ const disableBodyScroll = (targetElement, options) => {
138
+ // targetElement must be provided
139
+ if (!targetElement) {
140
+ // eslint-disable-next-line no-console
141
+ console.error("disableBodyScroll unsuccessful - targetElement must be provided when calling disableBodyScroll on IOS devices.");
142
+ return;
143
+ }
144
+ // disableBodyScroll must not have been called on this targetElement before
145
+ if (locks.some((lock) => lock.targetElement === targetElement)) {
146
+ return;
147
+ }
148
+ const lock = {
149
+ targetElement,
150
+ options: options || {},
151
+ };
152
+ locks = [...locks, lock];
153
+ if (isIosDevice) {
154
+ setPositionFixed();
155
+ }
156
+ else {
157
+ setOverflowHidden(options);
158
+ }
159
+ if (isIosDevice) {
160
+ targetElement.ontouchstart = (event) => {
161
+ if (event.targetTouches.length === 1) {
162
+ // detect single touch.
163
+ initialClientY = event.targetTouches[0].clientY;
164
+ }
165
+ };
166
+ targetElement.ontouchmove = (event) => {
167
+ if (event.targetTouches.length === 1) {
168
+ // detect single touch.
169
+ handleScroll(event, targetElement);
170
+ }
171
+ };
172
+ if (!documentListenerAdded) {
173
+ document.addEventListener("touchmove", preventDefault, hasPassiveEvents ? { passive: false } : undefined);
174
+ documentListenerAdded = true;
175
+ }
176
+ }
177
+ };
178
+ exports.disableBodyScroll = disableBodyScroll;
179
+ const clearAllBodyScrollLocks = () => {
180
+ if (isIosDevice) {
181
+ // Clear all locks ontouchstart/ontouchmove handlers, and the references.
182
+ locks.forEach((lock) => {
183
+ lock.targetElement.ontouchstart = null;
184
+ lock.targetElement.ontouchmove = null;
185
+ });
186
+ if (documentListenerAdded) {
187
+ document.removeEventListener("touchmove", preventDefault);
188
+ documentListenerAdded = false;
189
+ }
190
+ // Reset initial clientY.
191
+ initialClientY = -1;
192
+ }
193
+ if (isIosDevice) {
194
+ restorePositionSetting();
195
+ }
196
+ else {
197
+ restoreOverflowSetting();
198
+ }
199
+ locks = [];
200
+ };
201
+ exports.clearAllBodyScrollLocks = clearAllBodyScrollLocks;
202
+ const enableBodyScroll = (targetElement) => {
203
+ if (!targetElement) {
204
+ // eslint-disable-next-line no-console
205
+ console.error("enableBodyScroll unsuccessful - targetElement must be provided when calling enableBodyScroll on IOS devices.");
206
+ return;
207
+ }
208
+ locks = locks.filter((lock) => lock.targetElement !== targetElement);
209
+ if (isIosDevice) {
210
+ targetElement.ontouchstart = null;
211
+ targetElement.ontouchmove = null;
212
+ if (documentListenerAdded && locks.length === 0) {
213
+ document.removeEventListener("touchmove", preventDefault);
214
+ documentListenerAdded = false;
215
+ }
216
+ }
217
+ if (isIosDevice) {
218
+ restorePositionSetting();
219
+ }
220
+ else {
221
+ restoreOverflowSetting();
222
+ }
223
+ };
224
+ exports.enableBodyScroll = enableBodyScroll;
@@ -9,7 +9,7 @@ function copyToClipboardFallback(text) {
9
9
  textArea.style.left = "0";
10
10
  textArea.style.position = "fixed";
11
11
  document.body.appendChild(textArea);
12
- textArea.focus();
12
+ textArea.focus({ preventScroll: true });
13
13
  textArea.select();
14
14
  document.execCommand("copy");
15
15
  document.body.removeChild(textArea);