@adamjanicki/ui 1.2.5 → 1.2.6

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.
@@ -36,6 +36,6 @@ type AnimatedLayerProps = LayerProps & {
36
36
  visibleClassName?: string;
37
37
  };
38
38
  };
39
- declare const Layer: ({ onClose, children, style, className, disableEscape, }: LayerProps) => JSX.Element;
39
+ declare const Layer: (props: LayerProps) => JSX.Element;
40
40
  export declare const AnimatedLayer: ({ visibility, className, style, ...props }: AnimatedLayerProps) => JSX.Element;
41
41
  export default Layer;
@@ -24,9 +24,8 @@ import { jsx as _jsx } from "react/jsx-runtime";
24
24
  import React, { useEffect } from "react";
25
25
  import { useFocusTrap, useScrollLock } from "../../hooks";
26
26
  import { classNames } from "../../utils/util";
27
- var Layer = function (_a) {
27
+ var BaseLayer = function (_a) {
28
28
  var onClose = _a.onClose, children = _a.children, style = _a.style, className = _a.className, _b = _a.disableEscape, disableEscape = _b === void 0 ? false : _b;
29
- var _c = useScrollLock(), lock = _c.lock, unlock = _c.unlock;
30
29
  var focusRef = useFocusTrap(true);
31
30
  useEffect(function () {
32
31
  var handleEscape = function (event) {
@@ -41,10 +40,6 @@ var Layer = function (_a) {
41
40
  document.removeEventListener("keydown", handleEscape);
42
41
  };
43
42
  }, [onClose, disableEscape]);
44
- useEffect(function () {
45
- lock();
46
- return unlock;
47
- }, [lock, unlock]);
48
43
  return (_jsx("div", { className: classNames("ajui-layer-backdrop", className), style: style, onClick: onClose, children: React.cloneElement(children, {
49
44
  ref: focusRef,
50
45
  onClick: function (e) {
@@ -54,18 +49,23 @@ var Layer = function (_a) {
54
49
  },
55
50
  }) }));
56
51
  };
52
+ var Layer = function (props) {
53
+ // Lock and unlock on mount and unmount
54
+ useScrollLock();
55
+ return _jsx(BaseLayer, __assign({}, props));
56
+ };
57
57
  var defaultInvisibleStyle = {
58
- zIndex: -1,
58
+ visibility: "hidden",
59
59
  opacity: 0,
60
- pointerEvents: "none",
61
- userSelect: "none",
62
60
  };
63
61
  var defaultVisibleStyle = { opacity: 1 };
64
62
  export var AnimatedLayer = function (_a) {
65
63
  var visibility = _a.visibility, className = _a.className, _b = _a.style, style = _b === void 0 ? {} : _b, props = __rest(_a, ["visibility", "className", "style"]);
66
64
  var visible = visibility.visible, _c = visibility.invisibleStyle, invisibleStyle = _c === void 0 ? defaultInvisibleStyle : _c, _d = visibility.visibleStyle, visibleStyle = _d === void 0 ? defaultVisibleStyle : _d, invisibleClassName = visibility.invisibleClassName, visibleClassName = visibility.visibleClassName;
65
+ // lock and unlock on visibility change
66
+ useScrollLock(visible);
67
67
  var mergedStyle = __assign(__assign({}, style), (visible ? visibleStyle : invisibleStyle));
68
68
  var mergedClassName = classNames(className, visible ? visibleClassName : invisibleClassName);
69
- return _jsx(Layer, __assign({}, props, { style: mergedStyle, className: mergedClassName }));
69
+ return (_jsx(BaseLayer, __assign({}, props, { style: mergedStyle, className: mergedClassName })));
70
70
  };
71
71
  export default Layer;
@@ -1,16 +1,6 @@
1
- type ScrollLock = {
2
- /**
3
- * Callback function to lock the scroll position
4
- */
5
- lock: () => void;
6
- /**
7
- * Callback function to unlock the scroll position
8
- */
9
- unlock: () => void;
10
- };
11
1
  /**
12
- * Hook to lock and unlock the scroll position.
13
- * @returns Object with lock and unlock functions to lock and unlock the scroll position
2
+ * Hook to lock and unlock the scroll position on enable change or mount/unmount.
3
+ * @param enable whether to lock the scroll position. Defaults to `true`. Useful for using in something that stays mounted.
14
4
  */
15
- declare const useScrollLock: () => ScrollLock;
5
+ declare const useScrollLock: (enable?: boolean) => void;
16
6
  export default useScrollLock;
@@ -1,24 +1,43 @@
1
- import { useCallback } from "react";
1
+ import { useEffect } from "react";
2
+ var touchEvents = ["touchstart", "touchmove"];
3
+ var empty = function () { };
4
+ var lockScroll = function () {
5
+ var bodyStyle = document.body.style;
6
+ var bodyOverflow = bodyStyle.overflow;
7
+ // set overflow to hidden to prevent scrolling
8
+ bodyStyle.overflow = "hidden";
9
+ // handle on mobile; prevent touch events
10
+ touchEvents.forEach(function (event) {
11
+ window.addEventListener(event, empty, { passive: false });
12
+ });
13
+ return function () {
14
+ // restore overflow
15
+ if (bodyOverflow) {
16
+ bodyStyle.overflow = bodyOverflow;
17
+ }
18
+ else {
19
+ bodyStyle.removeProperty("overflow");
20
+ // remove style attribute if empty
21
+ if (!bodyStyle.length) {
22
+ document.body.removeAttribute("style");
23
+ }
24
+ }
25
+ // remove touch event listeners
26
+ touchEvents.forEach(function (event) {
27
+ window.removeEventListener(event, empty);
28
+ });
29
+ };
30
+ };
2
31
  /**
3
- * Hook to lock and unlock the scroll position.
4
- * @returns Object with lock and unlock functions to lock and unlock the scroll position
32
+ * Hook to lock and unlock the scroll position on enable change or mount/unmount.
33
+ * @param enable whether to lock the scroll position. Defaults to `true`. Useful for using in something that stays mounted.
5
34
  */
6
- var useScrollLock = function () {
7
- var lock = useCallback(function () {
8
- var scrollPosition = window.scrollY;
9
- document.body.style.overflow = "hidden";
10
- document.body.style.position = "fixed";
11
- document.body.style.top = "-".concat(scrollPosition, "px");
12
- document.body.style.width = "100%";
13
- }, []);
14
- var unlock = useCallback(function () {
15
- var scrollPosition = parseInt(document.body.style.top || "0", 10);
16
- document.body.style.overflow = "";
17
- document.body.style.position = "";
18
- document.body.style.top = "";
19
- document.body.style.width = "";
20
- window.scrollTo(0, -scrollPosition);
21
- }, []);
22
- return { lock: lock, unlock: unlock };
35
+ var useScrollLock = function (enable) {
36
+ if (enable === void 0) { enable = true; }
37
+ useEffect(function () {
38
+ if (enable) {
39
+ return lockScroll();
40
+ }
41
+ }, [enable]);
23
42
  };
24
43
  export default useScrollLock;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamjanicki/ui",
3
- "version": "1.2.5",
3
+ "version": "1.2.6",
4
4
  "description": "Basic UI components and hooks for React in TypeScript",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",