@adamjanicki/ui 1.2.4 → 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.
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- type Props = {
2
+ type LayerProps = {
3
3
  /**
4
4
  * Callback that fires when the user clicks outside the layer
5
5
  */
@@ -22,10 +22,13 @@ type Props = {
22
22
  * @default false
23
23
  */
24
24
  disableEscape?: boolean;
25
+ };
26
+ type AnimatedLayerProps = LayerProps & {
25
27
  /**
26
- * [Optional] Config for visibility
28
+ * [Optional] Config for visibility, including styles and class names
29
+ * Set the `transition` property on the `style` prop to animate the layer
27
30
  */
28
- visibility?: {
31
+ visibility: {
29
32
  visible: boolean;
30
33
  invisibleStyle?: React.CSSProperties;
31
34
  visibleStyle?: React.CSSProperties;
@@ -33,5 +36,6 @@ type Props = {
33
36
  visibleClassName?: string;
34
37
  };
35
38
  };
36
- declare const Layer: ({ onClose, children, style, className, disableEscape, visibility, }: Props) => JSX.Element | null;
39
+ declare const Layer: (props: LayerProps) => JSX.Element;
40
+ export declare const AnimatedLayer: ({ visibility, className, style, ...props }: AnimatedLayerProps) => JSX.Element;
37
41
  export default Layer;
@@ -9,19 +9,23 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
+ var __rest = (this && this.__rest) || function (s, e) {
13
+ var t = {};
14
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
15
+ t[p] = s[p];
16
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
17
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
18
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
19
+ t[p[i]] = s[p[i]];
20
+ }
21
+ return t;
22
+ };
12
23
  import { jsx as _jsx } from "react/jsx-runtime";
13
24
  import React, { useEffect } from "react";
14
25
  import { useFocusTrap, useScrollLock } from "../../hooks";
15
26
  import { classNames } from "../../utils/util";
16
- var Layer = function (_a) {
17
- var onClose = _a.onClose, children = _a.children, _b = _a.style, style = _b === void 0 ? {} : _b, className = _a.className, _c = _a.disableEscape, disableEscape = _c === void 0 ? false : _c, _d = _a.visibility, visibility = _d === void 0 ? { visible: true } : _d;
18
- var visible = visibility.visible, _e = visibility.invisibleStyle, invisibleStyle = _e === void 0 ? {
19
- zIndex: -1,
20
- opacity: 0,
21
- pointerEvents: "none",
22
- userSelect: "none",
23
- } : _e, _f = visibility.visibleStyle, visibleStyle = _f === void 0 ? { opacity: 1 } : _f, invisibleClassName = visibility.invisibleClassName, visibleClassName = visibility.visibleClassName;
24
- var _g = useScrollLock(), lock = _g.lock, unlock = _g.unlock;
27
+ var BaseLayer = function (_a) {
28
+ var onClose = _a.onClose, children = _a.children, style = _a.style, className = _a.className, _b = _a.disableEscape, disableEscape = _b === void 0 ? false : _b;
25
29
  var focusRef = useFocusTrap(true);
26
30
  useEffect(function () {
27
31
  var handleEscape = function (event) {
@@ -36,11 +40,7 @@ var Layer = function (_a) {
36
40
  document.removeEventListener("keydown", handleEscape);
37
41
  };
38
42
  }, [onClose, disableEscape]);
39
- useEffect(function () {
40
- lock();
41
- return unlock;
42
- }, [lock, unlock]);
43
- return (_jsx("div", { className: classNames("ajui-layer-backdrop", className, visible ? visibleClassName : invisibleClassName), style: __assign(__assign({}, style), (visible ? visibleStyle : invisibleStyle)), onClick: onClose, children: React.cloneElement(children, {
43
+ return (_jsx("div", { className: classNames("ajui-layer-backdrop", className), style: style, onClick: onClose, children: React.cloneElement(children, {
44
44
  ref: focusRef,
45
45
  onClick: function (e) {
46
46
  var _a, _b;
@@ -49,4 +49,23 @@ var Layer = function (_a) {
49
49
  },
50
50
  }) }));
51
51
  };
52
+ var Layer = function (props) {
53
+ // Lock and unlock on mount and unmount
54
+ useScrollLock();
55
+ return _jsx(BaseLayer, __assign({}, props));
56
+ };
57
+ var defaultInvisibleStyle = {
58
+ visibility: "hidden",
59
+ opacity: 0,
60
+ };
61
+ var defaultVisibleStyle = { opacity: 1 };
62
+ export var AnimatedLayer = function (_a) {
63
+ var visibility = _a.visibility, className = _a.className, _b = _a.style, style = _b === void 0 ? {} : _b, props = __rest(_a, ["visibility", "className", "style"]);
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
+ var mergedStyle = __assign(__assign({}, style), (visible ? visibleStyle : invisibleStyle));
68
+ var mergedClassName = classNames(className, visible ? visibleClassName : invisibleClassName);
69
+ return (_jsx(BaseLayer, __assign({}, props, { style: mergedStyle, className: mergedClassName })));
70
+ };
52
71
  export default Layer;
@@ -1,2 +1,3 @@
1
- import Layer from "./Layer";
1
+ import Layer, { AnimatedLayer } from "./Layer";
2
+ export { AnimatedLayer };
2
3
  export default Layer;
@@ -1,2 +1,3 @@
1
- import Layer from "./Layer";
1
+ import Layer, { AnimatedLayer } from "./Layer";
2
+ export { AnimatedLayer };
2
3
  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/index.d.ts CHANGED
@@ -9,6 +9,7 @@ export { default as Badge } from "./components/Badge";
9
9
  export { default as Banner } from "./components/Banner";
10
10
  export { default as Select } from "./components/Select";
11
11
  export { default as ClickOutside } from "./components/ClickOutside";
12
+ export * from "./components/Layer";
12
13
  export { default as Layer } from "./components/Layer";
13
14
  export { default as Spinner } from "./components/Spinner";
14
15
  export * from "./hooks";
package/index.js CHANGED
@@ -10,6 +10,7 @@ export { default as Badge } from "./components/Badge";
10
10
  export { default as Banner } from "./components/Banner";
11
11
  export { default as Select } from "./components/Select";
12
12
  export { default as ClickOutside } from "./components/ClickOutside";
13
+ export * from "./components/Layer";
13
14
  export { default as Layer } from "./components/Layer";
14
15
  export { default as Spinner } from "./components/Spinner";
15
16
  // Hooks
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamjanicki/ui",
3
- "version": "1.2.4",
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",