@megafon/ui-core 9.3.0 → 9.4.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.
Files changed (41) hide show
  1. package/dist/es/components/Modal/Modal.js +27 -3
  2. package/dist/es/components/Modal/_ModalMobile/ModalMobile.css +1 -1
  3. package/dist/es/components/Modal/_ModalMobile/ModalMobile.d.ts +1 -0
  4. package/dist/es/components/Modal/_ModalMobile/ModalMobile.js +9 -4
  5. package/dist/es/components/Modal/utils/usePreventScroll.d.ts +17 -0
  6. package/dist/es/components/Modal/utils/usePreventScroll.js +252 -0
  7. package/dist/es/components/Modal/utils/utils.d.ts +1 -0
  8. package/dist/es/components/Modal/utils/utils.js +3 -2
  9. package/dist/es/hooks/useEvent.d.ts +3 -0
  10. package/dist/es/hooks/useEvent.js +11 -0
  11. package/dist/es/hooks/useLatest.d.ts +2 -0
  12. package/dist/es/hooks/useLatest.js +8 -0
  13. package/dist/es/hooks/useVisualViewport.d.ts +10 -0
  14. package/dist/es/hooks/useVisualViewport.js +55 -0
  15. package/dist/es/index.d.ts +1 -0
  16. package/dist/es/index.js +1 -0
  17. package/dist/es/utils/browser.d.ts +7 -0
  18. package/dist/es/utils/browser.js +30 -0
  19. package/dist/es/utils/is.d.ts +19 -0
  20. package/dist/es/utils/is.js +70 -0
  21. package/dist/lib/components/Modal/Modal.js +27 -3
  22. package/dist/lib/components/Modal/_ModalMobile/ModalMobile.css +1 -1
  23. package/dist/lib/components/Modal/_ModalMobile/ModalMobile.d.ts +1 -0
  24. package/dist/lib/components/Modal/_ModalMobile/ModalMobile.js +9 -4
  25. package/dist/lib/components/Modal/utils/usePreventScroll.d.ts +17 -0
  26. package/dist/lib/components/Modal/utils/usePreventScroll.js +263 -0
  27. package/dist/lib/components/Modal/utils/utils.d.ts +1 -0
  28. package/dist/lib/components/Modal/utils/utils.js +3 -2
  29. package/dist/lib/hooks/useEvent.d.ts +3 -0
  30. package/dist/lib/hooks/useEvent.js +17 -0
  31. package/dist/lib/hooks/useLatest.d.ts +2 -0
  32. package/dist/lib/hooks/useLatest.js +14 -0
  33. package/dist/lib/hooks/useVisualViewport.d.ts +10 -0
  34. package/dist/lib/hooks/useVisualViewport.js +62 -0
  35. package/dist/lib/index.d.ts +1 -0
  36. package/dist/lib/index.js +7 -0
  37. package/dist/lib/utils/browser.d.ts +7 -0
  38. package/dist/lib/utils/browser.js +42 -0
  39. package/dist/lib/utils/is.d.ts +19 -0
  40. package/dist/lib/utils/is.js +92 -0
  41. package/package.json +2 -2
@@ -5,10 +5,14 @@ import { cnCreate, breakpoints } from '@megafon/ui-helpers';
5
5
  import throttle from 'lodash.throttle';
6
6
  import ReactModal from 'react-modal';
7
7
  import THROTTLE_TIME from "../../constants/throttleTime";
8
+ import { useEvent } from "../../hooks/useEvent";
8
9
  import usePrevious from "../../hooks/usePrevious";
9
10
  import useResolution from "../../hooks/useResolution";
11
+ import useVisualViewport from "../../hooks/useVisualViewport";
12
+ import { isIOS } from "../../utils/browser";
10
13
  import ModalDesktop from "./_ModalDesktop/ModalDesktop";
11
14
  import ModalMobile from "./_ModalMobile/ModalMobile";
15
+ import usePreventScroll from "./utils/usePreventScroll";
12
16
  import { changeBodyStyles, getCorrectMaxHeight } from "./utils/utils";
13
17
  import "./Modal.css";
14
18
  var MOBILE_BIG_START = breakpoints.MOBILE_BIG_START;
@@ -97,6 +101,18 @@ var Modal = function Modal(_ref) {
97
101
  var resizeObserver = React.useRef(null);
98
102
  var _useResolution = useResolution(),
99
103
  isMobile = _useResolution.isMobile;
104
+ var _useVisualViewport = useVisualViewport({
105
+ enabled: isOpened && isIOS(),
106
+ onChange: function onChange(metrics) {
107
+ document.documentElement.style.setProperty('--keyboard-offset', "".concat(metrics.keyboardOffset, "px"));
108
+ document.documentElement.style.setProperty('--viewport-height', "".concat(metrics.viewportHeight, "px"));
109
+ }
110
+ }),
111
+ isKeyboardOpen = _useVisualViewport.isKeyboardOpen,
112
+ viewportHeight = _useVisualViewport.viewportHeight;
113
+ usePreventScroll({
114
+ isDisabled: !isOpened
115
+ });
100
116
  var isFullScreenView = mobileView === 'fullScreen' && isMobile;
101
117
  var isBottomView = mobileView === 'bottom' && isMobile;
102
118
  var isBottomTransitionIn = transitionStep !== MODAL_TRANSITIONS_STEPS_ENUM.END_STEP && transitionStep !== MODAL_TRANSITIONS_STEPS_ENUM.MOVE_END_STEP;
@@ -121,7 +137,7 @@ var Modal = function Modal(_ref) {
121
137
  overlayRef.current = node;
122
138
  getModalOverlayRef === null || getModalOverlayRef === void 0 ? void 0 : getModalOverlayRef(node);
123
139
  };
124
- var handleSetScrollContentMaxHeight = React.useCallback(function () {
140
+ var handleSetScrollContentMaxHeight = useEvent(function () {
125
141
  var correctMaxHeight = getCorrectMaxHeight({
126
142
  refs: {
127
143
  overlayNode: overlayRef.current,
@@ -132,11 +148,12 @@ var Modal = function Modal(_ref) {
132
148
  props: {
133
149
  isStickyFooter: isStickyFooter,
134
150
  isBottomView: isBottomView,
135
- isStickyHeader: isStickyHeader
151
+ isStickyHeader: isStickyHeader,
152
+ availableHeight: isBottomView && isIOS() && viewportHeight ? viewportHeight : undefined
136
153
  }
137
154
  });
138
155
  setScrollMaxHeight(correctMaxHeight);
139
- }, [isStickyFooter, isBottomView, isStickyHeader]);
156
+ });
140
157
  var handleSetContainerWrapHeight = React.useCallback(function () {
141
158
  var containerInnerNode = containerInnerRef.current;
142
159
  if (!containerInnerNode) {
@@ -237,6 +254,12 @@ var Modal = function Modal(_ref) {
237
254
  }
238
255
  handleSetScrollContentMaxHeight();
239
256
  }, [isMobile, isChangeMobileView, handleSetScrollContentMaxHeight]);
257
+ React.useEffect(function () {
258
+ if (!isOpened || !isMobile || !isBottomView || !isIOS()) {
259
+ return;
260
+ }
261
+ handleSetScrollContentMaxHeight();
262
+ }, [isOpened, isMobile, isBottomView, viewportHeight, handleSetScrollContentMaxHeight]);
240
263
  React.useEffect(function () {
241
264
  if (isEnabledNativeScroll || !(isRecalculateHeight && (isHeaderPropsDiff || isFooterPropsDiff))) {
242
265
  return;
@@ -297,6 +320,7 @@ var Modal = function Modal(_ref) {
297
320
  return /*#__PURE__*/React.createElement(ModalDesktop, contextProps, children);
298
321
  }
299
322
  return /*#__PURE__*/React.createElement(ModalMobile, _extends({}, contextProps, {
323
+ isKeyboardOpen: isKeyboardOpen,
300
324
  isSwipeDisabled: isSwipeDisabled,
301
325
  isFullView: isFullScreenView,
302
326
  onChangeTransitionStep: handleChangeTransitionStep,
@@ -1 +1 @@
1
- h1,h2,h3,h4,h5{margin:0}.mfui-9-modal-mobile{width:100%}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__container-wrap{display:block;height:calc(var(--vh)*100 - 72px);width:100%}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__container-inner{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;height:100%}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__container-body{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;overflow-y:auto}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__children{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;height:100%}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__children-content{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__scroll-content,.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__scroll-init,.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__scroll-root{height:100%}.mfui-9-modal-mobile__container-wrap{bottom:0;display:-webkit-box;display:-ms-flexbox;display:flex;position:fixed;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;-webkit-box-pack:end;-ms-flex-pack:end;-webkit-animation:show-popup-from-bottom .3s linear;animation:show-popup-from-bottom .3s linear;height:auto;justify-content:flex-end;max-height:100%;overflow:hidden;-webkit-transition:-webkit-transform .3s;transition:-webkit-transform .3s;transition:transform .3s;transition:transform .3s,-webkit-transform .3s}.mfui-9-modal-mobile__container-wrap:before{content:"";display:block;-ms-flex-item-align:end;align-self:flex-end;background-color:var(--spbSky2);border-radius:10px;height:6px;margin:0 auto 8px;width:40px}.mfui-9-modal-mobile__container-wrap_move{-webkit-transition:none;transition:none}.mfui-9-modal-mobile__container-wrap_transition-end{-webkit-animation:hide-popup-to-bottom .2s linear forwards;animation:hide-popup-to-bottom .2s linear forwards}.mfui-9-modal-mobile__container-inner{-webkit-box-flex:0;-ms-flex-positive:0;border-top-left-radius:32px;border-top-right-radius:32px;flex-grow:0;height:auto;overflow:hidden}@-webkit-keyframes show-popup-from-bottom{0%{opacity:0;-webkit-transform:translateY(100%);transform:translateY(100%)}to{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}}@keyframes show-popup-from-bottom{0%{opacity:0;-webkit-transform:translateY(100%);transform:translateY(100%)}to{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}}@-webkit-keyframes hide-popup-to-bottom{0%{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}to{opacity:0;-webkit-transform:translateY(100%);transform:translateY(100%)}}@keyframes hide-popup-to-bottom{0%{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}to{opacity:0;-webkit-transform:translateY(100%);transform:translateY(100%)}}
1
+ h1,h2,h3,h4,h5{margin:0}.mfui-9-modal-mobile{--top-offset:72px;width:100%}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__container-wrap{display:block;height:calc(var(--vh, 1vh)*100 - var(--top-offset));width:100%}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__container-inner{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;height:100%}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__container-body{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;overflow-y:auto}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__children{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;height:100%}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__children-content{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1}.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__scroll-content,.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__scroll-init,.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__scroll-root{height:100%}.mfui-9-modal-mobile__container-wrap{bottom:0;display:-webkit-box;display:-ms-flexbox;display:flex;position:fixed;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;-webkit-box-pack:end;-ms-flex-pack:end;-webkit-animation:show-popup-from-bottom .3s linear;animation:show-popup-from-bottom .3s linear;height:auto;justify-content:flex-end;max-height:100%;overflow:hidden;-webkit-transition:-webkit-transform .3s;transition:-webkit-transform .3s;transition:transform .3s;transition:transform .3s,-webkit-transform .3s}.mfui-9-modal-mobile__container-wrap:before{content:"";display:block;-ms-flex-item-align:end;align-self:flex-end;background-color:var(--spbSky2);border-radius:10px;height:6px;margin:0 auto 8px;width:40px}.mfui-9-modal-mobile__container-wrap_move{-webkit-transition:none;transition:none}.mfui-9-modal-mobile__container-wrap_transition-end{-webkit-animation:hide-popup-to-bottom .2s linear forwards;animation:hide-popup-to-bottom .2s linear forwards}.mfui-9-modal-mobile__container-inner{-webkit-box-flex:0;-ms-flex-positive:0;border-top-left-radius:32px;border-top-right-radius:32px;flex-grow:0;height:auto;overflow:hidden}.mfui-9-modal-mobile_ios.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__container-wrap,.mfui-9-modal-mobile_ios.mfui-9-modal-mobile_keyboard-open .mfui-9-modal-mobile__container-wrap{bottom:var(--keyboard-offset,0);height:calc(var(--viewport-height) - var(--top-offset))}.mfui-9-modal-mobile_ios.mfui-9-modal-mobile_full-view .mfui-9-modal-mobile__container-wrap:after,.mfui-9-modal-mobile_ios.mfui-9-modal-mobile_keyboard-open .mfui-9-modal-mobile__container-wrap:after{background-color:var(--background);bottom:auto;content:"";display:block;height:200%;left:0;position:fixed;right:0;top:var(--viewport-height)}@-webkit-keyframes show-popup-from-bottom{0%{opacity:0;-webkit-transform:translateY(100%);transform:translateY(100%)}to{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}}@keyframes show-popup-from-bottom{0%{opacity:0;-webkit-transform:translateY(100%);transform:translateY(100%)}to{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}}@-webkit-keyframes hide-popup-to-bottom{0%{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}to{opacity:0;-webkit-transform:translateY(100%);transform:translateY(100%)}}@keyframes hide-popup-to-bottom{0%{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}to{opacity:0;-webkit-transform:translateY(100%);transform:translateY(100%)}}
@@ -8,6 +8,7 @@ export interface IModalMobileProps extends ModalContextPropsType {
8
8
  children?: React.ReactNode;
9
9
  onChangeTransitionStep: (value: MODAL_TRANSITIONS_STEPS_ENUM) => void;
10
10
  onChangeContainerWrapTransform: (value: string) => void;
11
+ isKeyboardOpen: boolean;
11
12
  }
12
13
  declare const ModalMobile: React.FC<IModalMobileProps>;
13
14
  export default ModalMobile;
@@ -12,6 +12,7 @@ var __rest = this && this.__rest || function (s, e) {
12
12
  };
13
13
  import * as React from 'react';
14
14
  import { cnCreate } from '@megafon/ui-helpers';
15
+ import { isIOS } from "../../../utils/browser";
15
16
  import ModalContent from "../_ModalContent/ModalContent";
16
17
  import { MODAL_TRANSITIONS_STEPS_ENUM } from "../Modal";
17
18
  import "./ModalMobile.css";
@@ -34,19 +35,21 @@ var ModalMobile = function ModalMobile(_a) {
34
35
  scrollContent = _f.scrollContent,
35
36
  restClassesScrollBar = __rest(_f, ["root", "init", "scrollContent"]),
36
37
  restScrollBarParams = __rest(_d, ["classes"]),
37
- isSwipeDisabled = _a.isSwipeDisabled,
38
38
  _a$isFullView = _a.isFullView,
39
39
  isFullView = _a$isFullView === void 0 ? true : _a$isFullView,
40
40
  children = _a.children,
41
41
  onChangeTransitionStep = _a.onChangeTransitionStep,
42
42
  onChangeContainerWrapTransform = _a.onChangeContainerWrapTransform,
43
- restProps = __rest(_a, ["classes", "scrollBarParams", "isSwipeDisabled", "isFullView", "children", "onChangeTransitionStep", "onChangeContainerWrapTransform"]);
43
+ isKeyboardOpen = _a.isKeyboardOpen,
44
+ restProps = __rest(_a, ["classes", "scrollBarParams", "isFullView", "children", "onChangeTransitionStep", "onChangeContainerWrapTransform", "isKeyboardOpen"]);
44
45
  var _restProps$refs = restProps.refs,
45
46
  containerWrapRef = _restProps$refs.containerWrapRef,
46
47
  containerInnerRef = _restProps$refs.containerInnerRef,
47
48
  scrollBarScrollableRef = _restProps$refs.scrollBarScrollableRef,
48
49
  isTransitionMoveStep = restProps.isTransitionMoveStep,
49
- isTransitionEndStep = restProps.isTransitionEndStep;
50
+ isTransitionEndStep = restProps.isTransitionEndStep,
51
+ isSwipeDisabledProps = restProps.isSwipeDisabled;
52
+ var isSwipeDisabled = isKeyboardOpen && isIOS() || isSwipeDisabledProps;
50
53
  var _React$useState = React.useState(null),
51
54
  _React$useState2 = _slicedToArray(_React$useState, 2),
52
55
  initialTouchPosition = _React$useState2[0],
@@ -118,7 +121,9 @@ var ModalMobile = function ModalMobile(_a) {
118
121
  }, [handleWindowTouchEnd, handleWindowTouchMove, isSwipeDisabled]);
119
122
  return /*#__PURE__*/React.createElement("div", {
120
123
  className: cn({
121
- 'full-view': isFullView
124
+ 'full-view': isFullView,
125
+ ios: isIOS(),
126
+ 'keyboard-open': isKeyboardOpen
122
127
  })
123
128
  }, /*#__PURE__*/React.createElement(ModalContent, _extends({}, restProps, {
124
129
  classes: _extends(_extends({}, restClasses), {
@@ -0,0 +1,17 @@
1
+ import { useEffect } from 'react';
2
+ export declare const useIsomorphicLayoutEffect: typeof useEffect;
3
+ interface PreventScrollOptions {
4
+ /** Whether the scroll lock is disabled. */
5
+ isDisabled?: boolean;
6
+ focusCallback?: () => void;
7
+ }
8
+ export declare function isScrollable(node: Element): boolean;
9
+ export declare function getScrollParent(node: Element): Element;
10
+ /**
11
+ * Prevents scrolling on the document body on mount, and
12
+ * restores it on unmount. Also ensures that content does not
13
+ * shift due to the scrollbars disappearing.
14
+ */
15
+ declare function usePreventScroll(options?: PreventScrollOptions): void;
16
+ export declare function isInput(target: Element): boolean;
17
+ export default usePreventScroll;
@@ -0,0 +1,252 @@
1
+ import "core-js/modules/es.array.iterator.js";
2
+ import "core-js/modules/es.object.to-string.js";
3
+ import "core-js/modules/es.regexp.exec.js";
4
+ import "core-js/modules/es.regexp.test.js";
5
+ import "core-js/modules/es.set.js";
6
+ import "core-js/modules/es.string.iterator.js";
7
+ import "core-js/modules/web.dom-collections.iterator.js";
8
+ // This code comes from https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/overlays/src/usePreventScroll.ts
9
+ /* eslint-disable */
10
+ import { useEffect, useLayoutEffect } from 'react';
11
+ import { isIOS } from "../../../utils/browser";
12
+ var KEYBOARD_BUFFER = 24;
13
+ export var useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
14
+ function chain() {
15
+ for (var _len = arguments.length, callbacks = new Array(_len), _key = 0; _key < _len; _key++) {
16
+ callbacks[_key] = arguments[_key];
17
+ }
18
+ return function () {
19
+ for (var _i = 0, _callbacks = callbacks; _i < _callbacks.length; _i++) {
20
+ var callback = _callbacks[_i];
21
+ if (typeof callback === 'function') {
22
+ callback.apply(void 0, arguments);
23
+ }
24
+ }
25
+ };
26
+ }
27
+ // @ts-ignore
28
+ var visualViewport = typeof document !== 'undefined' && window.visualViewport;
29
+ export function isScrollable(node) {
30
+ var style = window.getComputedStyle(node);
31
+ return /(auto|scroll)/.test(style.overflow + style.overflowX + style.overflowY);
32
+ }
33
+ export function getScrollParent(node) {
34
+ if (isScrollable(node)) {
35
+ node = node.parentElement;
36
+ }
37
+ while (node && !isScrollable(node)) {
38
+ node = node.parentElement;
39
+ }
40
+ return node || document.scrollingElement || document.documentElement;
41
+ }
42
+ // HTML input types that do not cause the software keyboard to appear.
43
+ var nonTextInputTypes = new Set(['checkbox', 'radio', 'range', 'color', 'file', 'image', 'button', 'submit', 'reset']);
44
+ // The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
45
+ var preventScrollCount = 0;
46
+ var restore;
47
+ /**
48
+ * Prevents scrolling on the document body on mount, and
49
+ * restores it on unmount. Also ensures that content does not
50
+ * shift due to the scrollbars disappearing.
51
+ */
52
+ function usePreventScroll() {
53
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
54
+ var isDisabled = options.isDisabled;
55
+ useIsomorphicLayoutEffect(function () {
56
+ if (isDisabled) {
57
+ return;
58
+ }
59
+ preventScrollCount++;
60
+ if (preventScrollCount === 1) {
61
+ if (isIOS()) {
62
+ restore = preventScrollMobileSafari();
63
+ }
64
+ }
65
+ return function () {
66
+ preventScrollCount--;
67
+ if (preventScrollCount === 0) {
68
+ restore === null || restore === void 0 ? void 0 : restore();
69
+ }
70
+ };
71
+ }, [isDisabled]);
72
+ }
73
+ // Mobile Safari is a whole different beast. Even with overflow: hidden,
74
+ // it still scrolls the page in many situations:
75
+ //
76
+ // 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.
77
+ // 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of
78
+ // it, so it becomes scrollable.
79
+ // 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.
80
+ // This may cause even fixed position elements to scroll off the screen.
81
+ // 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always
82
+ // scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.
83
+ //
84
+ // In order to work around these cases, and prevent scrolling without jankiness, we do a few things:
85
+ //
86
+ // 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling
87
+ // on the window.
88
+ // 2. Prevent default on `touchmove` events inside a scrollable element when the scroll position is at the
89
+ // top or bottom. This avoids the whole page scrolling instead, but does prevent overscrolling.
90
+ // 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
91
+ // 4. When focusing an input, apply a transform to trick Safari into thinking the input is at the top
92
+ // of the page, which prevents it from scrolling the page. After the input is focused, scroll the element
93
+ // into view ourselves, without scrolling the whole page.
94
+ // 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the
95
+ // same visually, but makes the actual scroll position always zero. This is required to make all of the
96
+ // above work or Safari will still try to scroll the page when focusing an input.
97
+ // 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
98
+ // to navigate to an input with the next/previous buttons that's outside a modal.
99
+ function preventScrollMobileSafari() {
100
+ var scrollable;
101
+ var lastY = 0;
102
+ var onTouchStart = function onTouchStart(e) {
103
+ // Store the nearest scrollable parent element from the element that the user touched.
104
+ scrollable = getScrollParent(e.target);
105
+ if (scrollable === document.documentElement && scrollable === document.body) {
106
+ return;
107
+ }
108
+ lastY = e.changedTouches[0].pageY;
109
+ };
110
+ var onTouchMove = function onTouchMove(e) {
111
+ // Prevent scrolling the window.
112
+ if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {
113
+ e.preventDefault();
114
+ return;
115
+ }
116
+ // Prevent scrolling up when at the top and scrolling down when at the bottom
117
+ // of a nested scrollable area, otherwise mobile Safari will start scrolling
118
+ // the window instead. Unfortunately, this disables bounce scrolling when at
119
+ // the top but it's the best we can do.
120
+ var y = e.changedTouches[0].pageY;
121
+ var _scrollable = scrollable,
122
+ scrollTop = _scrollable.scrollTop;
123
+ var bottom = scrollable.scrollHeight - scrollable.clientHeight;
124
+ if (bottom === 0) {
125
+ return;
126
+ }
127
+ if (scrollTop <= 0 && y > lastY || scrollTop >= bottom && y < lastY) {
128
+ e.preventDefault();
129
+ }
130
+ lastY = y;
131
+ };
132
+ var onTouchEnd = function onTouchEnd(e) {
133
+ var target = e.target;
134
+ // Apply this change if we're not already focused on the target element
135
+ if (isInput(target) && target !== document.activeElement) {
136
+ e.preventDefault();
137
+ // Apply a transform to trick Safari into thinking the input is at the top of the page
138
+ // so it doesn't try to scroll it into view. When tapping on an input, this needs to
139
+ // be done before the "focus" event, so we have to focus the element ourselves.
140
+ target.style.transform = 'translateY(-2000px)';
141
+ target.focus();
142
+ requestAnimationFrame(function () {
143
+ target.style.transform = '';
144
+ });
145
+ }
146
+ };
147
+ var onFocus = function onFocus(e) {
148
+ var target = e.target;
149
+ if (isInput(target)) {
150
+ // Transform also needs to be applied in the focus event in cases where focus moves
151
+ // other than tapping on an input directly, e.g. the next/previous buttons in the
152
+ // software keyboard. In these cases, it seems applying the transform in the focus event
153
+ // is good enough, whereas when tapping an input, it must be done before the focus event. 🤷‍♂️
154
+ target.style.transform = 'translateY(-2000px)';
155
+ requestAnimationFrame(function () {
156
+ target.style.transform = '';
157
+ // This will have prevented the browser from scrolling the focused element into view,
158
+ // so we need to do this ourselves in a way that doesn't cause the whole page to scroll.
159
+ if (visualViewport) {
160
+ if (visualViewport.height < window.innerHeight) {
161
+ // If the keyboard is already visible, do this after one additional frame
162
+ // to wait for the transform to be removed.
163
+ requestAnimationFrame(function () {
164
+ scrollIntoView(target);
165
+ });
166
+ } else {
167
+ // Otherwise, wait for the visual viewport to resize before scrolling so we can
168
+ // measure the correct position to scroll to.
169
+ visualViewport.addEventListener('resize', function () {
170
+ return scrollIntoView(target);
171
+ }, {
172
+ once: true
173
+ });
174
+ }
175
+ }
176
+ });
177
+ }
178
+ };
179
+ var onWindowScroll = function onWindowScroll() {
180
+ // Last resort. If the window scrolled, scroll it back to the top.
181
+ // It should always be at the top because the body will have a negative margin (see below).
182
+ window.scrollTo(0, 0);
183
+ };
184
+ // Record the original scroll position so we can restore it.
185
+ // Then apply a negative margin to the body to offset it by the scroll position. This will
186
+ // enable us to scroll the window to the top, which is required for the rest of this to work.
187
+ var scrollX = window.pageXOffset;
188
+ var scrollY = window.pageYOffset;
189
+ var restoreStyles = chain(setStyle(document.documentElement, 'paddingRight', "".concat(window.innerWidth - document.documentElement.clientWidth, "px")));
190
+ // Scroll to the top. The negative margin on the body will make this appear the same.
191
+ window.scrollTo(0, 0);
192
+ var removeEvents = chain(addEvent(document, 'touchstart', onTouchStart, {
193
+ passive: false,
194
+ capture: true
195
+ }), addEvent(document, 'touchmove', onTouchMove, {
196
+ passive: false,
197
+ capture: true
198
+ }), addEvent(document, 'touchend', onTouchEnd, {
199
+ passive: false,
200
+ capture: true
201
+ }), addEvent(document, 'focus', onFocus, true), addEvent(window, 'scroll', onWindowScroll));
202
+ return function () {
203
+ // Restore styles and scroll the page back to where it was.
204
+ restoreStyles();
205
+ removeEvents();
206
+ window.scrollTo(scrollX, scrollY);
207
+ };
208
+ }
209
+ // Sets a CSS property on an element, and returns a function to revert it to the previous value.
210
+ function setStyle(element, style, value) {
211
+ // https://github.com/microsoft/TypeScript/issues/17827#issuecomment-391663310
212
+ // @ts-ignore
213
+ var cur = element.style[style];
214
+ // @ts-ignore
215
+ element.style[style] = value;
216
+ return function () {
217
+ // @ts-ignore
218
+ element.style[style] = cur;
219
+ };
220
+ }
221
+ // Adds an event listener to an element, and returns a function to remove it.
222
+ function addEvent(target, event, handler, options) {
223
+ // @ts-ignore
224
+ target.addEventListener(event, handler, options);
225
+ return function () {
226
+ // @ts-ignore
227
+ target.removeEventListener(event, handler, options);
228
+ };
229
+ }
230
+ function scrollIntoView(target) {
231
+ var root = document.scrollingElement || document.documentElement;
232
+ while (target && target !== root) {
233
+ // Find the parent scrollable element and adjust the scroll position if the target is not already in view.
234
+ var scrollable = getScrollParent(target);
235
+ if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== target) {
236
+ var scrollableTop = scrollable.getBoundingClientRect().top;
237
+ var targetTop = target.getBoundingClientRect().top;
238
+ var targetBottom = target.getBoundingClientRect().bottom;
239
+ // Buffer is needed for some edge cases
240
+ var keyboardHeight = scrollable.getBoundingClientRect().bottom + KEYBOARD_BUFFER;
241
+ if (targetBottom > keyboardHeight) {
242
+ scrollable.scrollTop += targetTop - scrollableTop;
243
+ }
244
+ }
245
+ // @ts-ignore
246
+ target = scrollable.parentElement;
247
+ }
248
+ }
249
+ export function isInput(target) {
250
+ return target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type) || target instanceof HTMLTextAreaElement || target instanceof HTMLElement && target.isContentEditable;
251
+ }
252
+ export default usePreventScroll;
@@ -10,6 +10,7 @@ type CorrectMaxHeightArgsType = {
10
10
  isStickyFooter?: boolean;
11
11
  isBottomView?: boolean;
12
12
  isStickyHeader?: boolean;
13
+ availableHeight?: number;
13
14
  };
14
15
  };
15
16
  export declare const getCorrectMaxHeight: ({ refs, props }: CorrectMaxHeightArgsType) => number;
@@ -31,8 +31,9 @@ export var getCorrectMaxHeight = function getCorrectMaxHeight(_ref) {
31
31
  props = _ref.props;
32
32
  var isStickyFooter = props.isStickyFooter,
33
33
  isStickyHeader = props.isStickyHeader,
34
- isBottomView = props.isBottomView;
35
- var windowHeight = window.innerHeight;
34
+ isBottomView = props.isBottomView,
35
+ availableHeight = props.availableHeight;
36
+ var windowHeight = availableHeight !== null && availableHeight !== void 0 ? availableHeight : window.innerHeight;
36
37
  var windowWidth = window.innerWidth;
37
38
  var isMobile = windowWidth < breakpoints.MOBILE_BIG_START;
38
39
  var _getAdditionalSize = getAdditionalSize(refs),
@@ -0,0 +1,3 @@
1
+ type Fn<ARGS extends unknown[], R> = (...args: ARGS) => R;
2
+ export declare const useEvent: <A extends unknown[], R>(fn: Fn<A, R>) => Fn<A, R>;
3
+ export {};
@@ -0,0 +1,11 @@
1
+ import { useMemo } from 'react';
2
+ import { useLatest } from "./useLatest";
3
+ export var useEvent = function useEvent(fn) {
4
+ var ref = useLatest(fn);
5
+ return useMemo(function () {
6
+ return function () {
7
+ var current = ref.current;
8
+ return current.apply(void 0, arguments);
9
+ };
10
+ }, [ref]);
11
+ };
@@ -0,0 +1,2 @@
1
+ import type { RefObject } from 'react';
2
+ export declare const useLatest: <T>(value: T) => RefObject<T>;
@@ -0,0 +1,8 @@
1
+ import { useInsertionEffect, useRef } from 'react';
2
+ export var useLatest = function useLatest(value) {
3
+ var valueRef = useRef(value);
4
+ useInsertionEffect(function () {
5
+ valueRef.current = value;
6
+ }, [value]);
7
+ return valueRef;
8
+ };
@@ -0,0 +1,10 @@
1
+ type Metrics = {
2
+ keyboardOffset: number;
3
+ viewportHeight: number;
4
+ isKeyboardOpen: boolean;
5
+ };
6
+ declare function useVisualViewport({ enabled, onChange, }: {
7
+ enabled?: boolean;
8
+ onChange: (metrics: Metrics) => void;
9
+ }): Metrics;
10
+ export default useVisualViewport;
@@ -0,0 +1,55 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import { useEffect, useState } from 'react';
3
+ import { useEvent } from "./useEvent";
4
+ var getDefaultMetrics = function getDefaultMetrics() {
5
+ return {
6
+ keyboardOffset: 0,
7
+ viewportHeight: window.innerHeight,
8
+ isKeyboardOpen: false
9
+ };
10
+ };
11
+ var getMetrics = function getMetrics() {
12
+ var viewport = window.visualViewport;
13
+ if (!viewport) {
14
+ return getDefaultMetrics();
15
+ }
16
+ var keyboardOffset = Math.max(0, window.innerHeight - viewport.height - viewport.offsetTop);
17
+ var isKeyboardOpen = viewport.height < window.innerHeight && window.innerHeight - viewport.height > 150;
18
+ return {
19
+ keyboardOffset: keyboardOffset,
20
+ viewportHeight: viewport.height,
21
+ isKeyboardOpen: isKeyboardOpen
22
+ };
23
+ };
24
+ function useVisualViewport(_ref) {
25
+ var _ref$enabled = _ref.enabled,
26
+ enabled = _ref$enabled === void 0 ? true : _ref$enabled,
27
+ onChange = _ref.onChange;
28
+ var _useState = useState(function () {
29
+ return getMetrics();
30
+ }),
31
+ _useState2 = _slicedToArray(_useState, 2),
32
+ metrics = _useState2[0],
33
+ setMetrics = _useState2[1];
34
+ var onChangeMetrics = useEvent(onChange);
35
+ useEffect(function () {
36
+ if (!enabled) {
37
+ return undefined;
38
+ }
39
+ var updateMetrics = function updateMetrics() {
40
+ var currentMetrics = getMetrics();
41
+ setMetrics(currentMetrics);
42
+ onChangeMetrics(currentMetrics);
43
+ };
44
+ updateMetrics();
45
+ var viewport = window.visualViewport;
46
+ viewport === null || viewport === void 0 ? void 0 : viewport.addEventListener('resize', updateMetrics);
47
+ viewport === null || viewport === void 0 ? void 0 : viewport.addEventListener('scroll', updateMetrics);
48
+ return function () {
49
+ viewport === null || viewport === void 0 ? void 0 : viewport.removeEventListener('resize', updateMetrics);
50
+ viewport === null || viewport === void 0 ? void 0 : viewport.removeEventListener('scroll', updateMetrics);
51
+ };
52
+ }, [enabled, onChangeMetrics]);
53
+ return metrics;
54
+ }
55
+ export default useVisualViewport;
@@ -89,5 +89,6 @@ export { default as UploadFileItemIcon } from './components/UploadForm/UploadFil
89
89
  export { default as useDragAndDrop } from './components/UploadForm/UploadField/components/useDragAndDrop';
90
90
  export { default as useGradient } from './components/Carousel/useGradient';
91
91
  export { default as usePagination } from './components/Pagination/usePagination';
92
+ export { default as usePreventScroll } from './components/Modal/utils/usePreventScroll';
92
93
  export { default as utils } from './components/Modal/utils/utils';
93
94
  export { default as ValueField } from './components/ValueField/ValueField';
package/dist/es/index.js CHANGED
@@ -89,5 +89,6 @@ export { default as UploadFileItemIcon } from "./components/UploadForm/UploadFil
89
89
  export { default as useDragAndDrop } from "./components/UploadForm/UploadField/components/useDragAndDrop";
90
90
  export { default as useGradient } from "./components/Carousel/useGradient";
91
91
  export { default as usePagination } from "./components/Pagination/usePagination";
92
+ export { default as usePreventScroll } from "./components/Modal/utils/usePreventScroll";
92
93
  export { default as utils } from "./components/Modal/utils/utils";
93
94
  export { default as ValueField } from "./components/ValueField/ValueField";
@@ -0,0 +1,7 @@
1
+ export declare function isMobileFirefox(): boolean | undefined;
2
+ export declare function testPlatform(re: RegExp): boolean | undefined;
3
+ export declare function isMac(): boolean | undefined;
4
+ export declare function isIPhone(): boolean | undefined;
5
+ export declare function isSafari(): boolean | undefined;
6
+ export declare function isIPad(): boolean | undefined;
7
+ export declare function isIOS(): boolean | undefined;
@@ -0,0 +1,30 @@
1
+ import "core-js/modules/es.regexp.exec.js";
2
+ import "core-js/modules/es.regexp.test.js";
3
+ export function isMobileFirefox() {
4
+ var _navigator = navigator,
5
+ userAgent = _navigator.userAgent;
6
+ return typeof window !== 'undefined' && (/Firefox/.test(userAgent) && /Mobile/.test(userAgent) ||
7
+ // Android Firefox
8
+ /FxiOS/.test(userAgent)) // iOS Firefox
9
+ ;
10
+ }
11
+ export function testPlatform(re) {
12
+ return typeof window !== 'undefined' && window.navigator != null ? re.test(window.navigator.platform) : undefined;
13
+ }
14
+ export function isMac() {
15
+ return testPlatform(/^Mac/);
16
+ }
17
+ export function isIPhone() {
18
+ return testPlatform(/^iPhone/);
19
+ }
20
+ export function isSafari() {
21
+ return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
22
+ }
23
+ export function isIPad() {
24
+ return testPlatform(/^iPad/) ||
25
+ // iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
26
+ isMac() && navigator.maxTouchPoints > 1;
27
+ }
28
+ export function isIOS() {
29
+ return isIPhone() || isIPad();
30
+ }
@@ -0,0 +1,19 @@
1
+ export declare function isNumber(value: unknown): value is number;
2
+ export declare function isInteger(v: unknown): boolean;
3
+ export declare function isArray<T>(value: unknown): value is Array<T>;
4
+ export declare function isEmptyArray(value: unknown): boolean;
5
+ type AnyFunction = (...args: unknown[]) => unknown;
6
+ export declare function isFunction<T extends AnyFunction = AnyFunction>(value: unknown): value is T;
7
+ export declare function isDefined(value: unknown): boolean;
8
+ export declare function isUndefined(value: unknown): value is undefined;
9
+ export declare function isObject(value: unknown): value is Record<string, unknown>;
10
+ export declare function isEmptyObject(value: unknown): boolean;
11
+ export declare function isNull(value: unknown): value is null;
12
+ export declare function isString(value: unknown): value is string;
13
+ export declare function isCssVar(value: string): boolean;
14
+ export declare function isEmpty(value: unknown): boolean;
15
+ export declare const isBoolean: (value: unknown) => value is boolean;
16
+ type Falsy = false | 0 | '' | null | undefined | bigint;
17
+ export declare const isTruthy: <T>(value: T) => value is Exclude<T, Falsy>;
18
+ export declare const isHTMLElement: (value: unknown) => value is HTMLElement;
19
+ export {};