@yakad/ui 1.3.164 → 1.3.165

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 (30) hide show
  1. package/dist/ActiveOnVisible/ActiveOnVisible.js +2 -1
  2. package/dist/Dropdown/Dropdown.d.ts +2 -1
  3. package/dist/Dropdown/Dropdown.js +17 -3
  4. package/dist/Popup/Popup.d.ts +2 -1
  5. package/dist/Popup/Popup.js +18 -4
  6. package/dist/RenderByScroll/RenderByScroll.js +2 -1
  7. package/dist/WithOverlay/WithOverlay.js +12 -23
  8. package/dist/index.d.ts +0 -10
  9. package/dist/index.js +0 -10
  10. package/package.json +2 -1
  11. package/dist/interactionHooks/useOnDoubleTap.d.ts +0 -2
  12. package/dist/interactionHooks/useOnDoubleTap.js +0 -23
  13. package/dist/interactionHooks/useOnHoverChange.d.ts +0 -2
  14. package/dist/interactionHooks/useOnHoverChange.js +0 -23
  15. package/dist/interactionHooks/useOnOutsideClick.d.ts +0 -2
  16. package/dist/interactionHooks/useOnOutsideClick.js +0 -21
  17. package/dist/interactionHooks/useOnPinch.d.ts +0 -2
  18. package/dist/interactionHooks/useOnPinch.js +0 -35
  19. package/dist/interactionHooks/useOnResizeChange.d.ts +0 -2
  20. package/dist/interactionHooks/useOnResizeChange.js +0 -23
  21. package/dist/interactionHooks/useOnRightClick.d.ts +0 -2
  22. package/dist/interactionHooks/useOnRightClick.js +0 -19
  23. package/dist/interactionHooks/useOnRotate.d.ts +0 -2
  24. package/dist/interactionHooks/useOnRotate.js +0 -36
  25. package/dist/interactionHooks/useOnScroll.d.ts +0 -2
  26. package/dist/interactionHooks/useOnScroll.js +0 -29
  27. package/dist/interactionHooks/useOnSwipe.d.ts +0 -2
  28. package/dist/interactionHooks/useOnSwipe.js +0 -53
  29. package/dist/interactionHooks/useOnVisibilityChange.d.ts +0 -2
  30. package/dist/interactionHooks/useOnVisibilityChange.js +0 -21
@@ -12,7 +12,8 @@ var __rest = (this && this.__rest) || function (s, e) {
12
12
  };
13
13
  import { jsx as _jsx } from "react/jsx-runtime";
14
14
  import { useImperativeHandle, useRef, useState } from "react";
15
- import { Stack, useOnVisibilityChange } from "..";
15
+ import { Stack } from "..";
16
+ import { useOnVisibilityChange } from "@yakad/use-interactions";
16
17
  export function ActiveOnVisible(_a) {
17
18
  var { onVisibilityChange, style, children, ref } = _a, restProps = __rest(_a, ["onVisibilityChange", "style", "children", "ref"]);
18
19
  const localRef = useRef(null);
@@ -1,7 +1,8 @@
1
1
  /// <reference types="react" />
2
2
  import { CardProps, OverlayProps } from "..";
3
3
  export interface DropdownProps extends CardProps, OverlayProps {
4
+ teleport?: boolean;
4
5
  ref?: React.Ref<HTMLDivElement>;
5
6
  }
6
7
  export declare function Dropdown({ fullWidthOnParentDemand, // Def in Card is True
7
- blur, level, triggerref, onClose, className, style, children, ref, ...restProps }: DropdownProps): import("react/jsx-runtime").JSX.Element;
8
+ blur, level, triggerref, onClose, className, style, children, teleport, ref, ...restProps }: DropdownProps): import("react/jsx-runtime").JSX.Element;
@@ -13,11 +13,13 @@ var __rest = (this && this.__rest) || function (s, e) {
13
13
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
14
14
  import { useRef, useState, useEffect, useImperativeHandle } from "react";
15
15
  import classNames from "classnames";
16
- import { Card, useOnOutsideClick } from "..";
16
+ import { Card } from "..";
17
17
  import styles from "./Dropdown.module.css";
18
+ import { createPortal } from "react-dom";
19
+ import { useOnOutsideClick } from "@yakad/use-interactions";
18
20
  export function Dropdown(_a) {
19
21
  var { fullWidthOnParentDemand = false, // Def in Card is True
20
- blur, level = "high", triggerref, onClose, className, style, children, ref } = _a, restProps = __rest(_a, ["fullWidthOnParentDemand", "blur", "level", "triggerref", "onClose", "className", "style", "children", "ref"]);
22
+ blur, level = "high", triggerref, onClose, className, style, children, teleport = false, ref } = _a, restProps = __rest(_a, ["fullWidthOnParentDemand", "blur", "level", "triggerref", "onClose", "className", "style", "children", "teleport", "ref"]);
21
23
  const fallbackTriggerRef = useRef(null);
22
24
  const dropdownRef = useRef(null);
23
25
  const placementTargetRef = triggerref || fallbackTriggerRef;
@@ -33,6 +35,17 @@ export function Dropdown(_a) {
33
35
  });
34
36
  const [isPlacedAbove, setIsPlacedAbove] = useState(false);
35
37
  const [isPlaced, setIsPlaced] = useState(false);
38
+ useEffect(() => {
39
+ document.body.style.overflow = "hidden";
40
+ return () => {
41
+ document.body.style.overflow = "initial";
42
+ };
43
+ }, []);
44
+ const doTeleport = () => {
45
+ const portalRoot = document.getElementsByClassName("portalRoot")[0] || document;
46
+ const t = portalRoot && createPortal(dropdown(), portalRoot);
47
+ return t;
48
+ };
36
49
  useEffect(() => {
37
50
  if ((placementTargetRef === null || placementTargetRef === void 0 ? void 0 : placementTargetRef.current) && dropdownRef.current) {
38
51
  const placementTarget = placementTargetRef.current.getBoundingClientRect();
@@ -75,11 +88,12 @@ export function Dropdown(_a) {
75
88
  useOnOutsideClick(() => {
76
89
  onClose === null || onClose === void 0 ? void 0 : onClose();
77
90
  }, dropdownRef);
78
- return (_jsxs(_Fragment, { children: [!triggerref && _jsx("div", { ref: fallbackTriggerRef }), _jsx(Card, Object.assign({ ref: dropdownRef, fullWidthOnParentDemand: fullWidthOnParentDemand, blur: blur, level: level }, restProps, { className: classNames(styles.dropdownCard, { [styles.isPlacedAbove]: isPlacedAbove }, className), style: Object.assign({ top: dropdownPosition.top, left: dropdownPosition.left, visibility: isPlaced ? "visible" : "hidden" }, style), children: children })), _jsx("span", { className: classNames(styles.arrow, {
91
+ const dropdown = () => (_jsxs(_Fragment, { children: [!triggerref && _jsx("div", { ref: fallbackTriggerRef }), _jsx(Card, Object.assign({ ref: dropdownRef, fullWidthOnParentDemand: fullWidthOnParentDemand, blur: blur, level: level }, restProps, { className: classNames(styles.dropdownCard, { [styles.isPlacedAbove]: isPlacedAbove }, className), style: Object.assign({ top: dropdownPosition.top, left: dropdownPosition.left, visibility: isPlaced ? "visible" : "hidden" }, style), children: children })), _jsx("span", { className: classNames(styles.arrow, {
79
92
  [styles.isPlacedAbove]: isPlacedAbove,
80
93
  }, { [styles.blur]: blur }), style: {
81
94
  top: arrowPosition.top,
82
95
  left: arrowPosition.left,
83
96
  visibility: isPlaced ? "visible" : "hidden",
84
97
  } })] }));
98
+ return teleport ? doTeleport() : dropdown();
85
99
  }
@@ -2,6 +2,7 @@
2
2
  import { CardProps, OverlayProps } from "..";
3
3
  export interface PopupProps extends CardProps, OverlayProps {
4
4
  heading?: string;
5
+ teleport?: boolean;
5
6
  ref?: React.Ref<HTMLDivElement>;
6
7
  }
7
- export declare function Popup({ align, onClose, heading, className, children, ref, ...restProps }: PopupProps): import("react/jsx-runtime").JSX.Element;
8
+ export declare function Popup({ align, onClose, heading, className, children, teleport, ref, ...restProps }: PopupProps): import("react/jsx-runtime").JSX.Element;
@@ -11,16 +11,30 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  return t;
12
12
  };
13
13
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
14
- import { useImperativeHandle } from "react";
14
+ import { useEffect, useImperativeHandle } from "react";
15
15
  import classNames from "classnames";
16
16
  import { Symbol } from "@yakad/symbols";
17
- import { Button, Card, Row, Screen, Spacer, Stack, Text, useOnOutsideClick, } from "..";
17
+ import { Button, Card, Row, Screen, Spacer, Stack, Text, } from "..";
18
18
  import styles from "./Popup.module.css";
19
+ import { createPortal } from "react-dom";
20
+ import { useOnOutsideClick } from "@yakad/use-interactions";
19
21
  export function Popup(_a) {
20
- var { align, onClose, heading, className, children, ref } = _a, restProps = __rest(_a, ["align", "onClose", "heading", "className", "children", "ref"]);
22
+ var { align, onClose, heading, className, children, teleport = false, ref } = _a, restProps = __rest(_a, ["align", "onClose", "heading", "className", "children", "teleport", "ref"]);
21
23
  const localRef = useOnOutsideClick(() => {
22
24
  onClose === null || onClose === void 0 ? void 0 : onClose();
23
25
  });
26
+ const doTeleport = () => {
27
+ const portalRoot = document.getElementsByClassName("portalRoot")[0] || document;
28
+ const t = portalRoot && createPortal(popup(), portalRoot);
29
+ return t;
30
+ };
31
+ useEffect(() => {
32
+ document.body.style.overflow = "hidden";
33
+ return () => {
34
+ document.body.style.overflow = "initial";
35
+ };
36
+ }, []);
24
37
  useImperativeHandle(ref, () => localRef.current);
25
- return (_jsx(Screen, { align: "center", className: styles.popupscreen, children: _jsxs(Card, Object.assign({ ref: localRef }, restProps, { className: classNames(styles.popup, className), children: [_jsxs(Row, { style: { marginBottom: "2rem" }, children: [_jsx(Text, { variant: "heading4", children: heading }), _jsx(Spacer, {}), _jsx(Button, { title: "Close", icon: _jsx(Symbol, { icon: "close" }), onClick: onClose })] }), _jsx(Stack, { align: align, style: { width: "100%", overflowY: "auto" }, children: children })] })) }));
38
+ const popup = () => (_jsx(Screen, { align: "center", className: styles.popupscreen, children: _jsxs(Card, Object.assign({ ref: localRef }, restProps, { className: classNames(styles.popup, className), children: [_jsxs(Row, { style: { marginBottom: "2rem" }, children: [_jsx(Text, { variant: "heading4", children: heading }), _jsx(Spacer, {}), _jsx(Button, { title: "Close", icon: _jsx(Symbol, { icon: "close" }), onClick: onClose })] }), _jsx(Stack, { align: align, style: { width: "100%", overflowY: "auto" }, children: children })] })) }));
39
+ return teleport ? doTeleport() : popup();
26
40
  }
@@ -12,7 +12,8 @@ var __rest = (this && this.__rest) || function (s, e) {
12
12
  };
13
13
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
14
14
  import React, { useEffect, useRef, useState } from "react";
15
- import { ActiveOnVisible, Stack, useOnVisibilityChange } from "..";
15
+ import { ActiveOnVisible, Stack } from "..";
16
+ import { useOnVisibilityChange } from "@yakad/use-interactions";
16
17
  export function RenderByScroll(_a) {
17
18
  var { scrollMarginTop = 8, // Sticky Header Height
18
19
  extraRender = 5, jumpToIndex = 0, stopNewRenders, newChildRendered, style, children } = _a, restProps = __rest(_a, ["scrollMarginTop", "extraRender", "jumpToIndex", "stopNewRenders", "newChildRendered", "style", "children"]);
@@ -11,9 +11,9 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  return t;
12
12
  };
13
13
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
14
- import { useState, cloneElement, useEffect, useRef, useImperativeHandle, } from "react";
15
- import { createPortal } from "react-dom";
16
- import { Stack, useOnRightClick, } from "..";
14
+ import { useState, useRef, useImperativeHandle, cloneElement, } from "react";
15
+ import { Stack, } from "..";
16
+ import { useOnRightClick } from "@yakad/use-interactions";
17
17
  export function WithOverlay(_a) {
18
18
  var { trigger = "onClick", overlay, children, onClick, ref } = _a, restProps = __rest(_a, ["trigger", "overlay", "children", "onClick", "ref"]);
19
19
  // Get Triggers ref and send to Overlay
@@ -28,24 +28,13 @@ export function WithOverlay(_a) {
28
28
  };
29
29
  // Let the parent access our DOM node
30
30
  useImperativeHandle(ref, () => localRef.current);
31
- // Disable Body Scroll on showOverlay
32
- useEffect(() => {
33
- document.body.style.overflow = showOverlay ? "hidden" : "initial";
34
- return () => {
35
- document.body.style.overflow = "initial";
36
- };
37
- }, [showOverlay]);
38
- // Teleport Overlay to portalRoot or body
39
- const portalRoot = document.getElementsByClassName("portalRoot")[0] || document;
40
- const teleport = overlay &&
41
- portalRoot &&
42
- createPortal(cloneElement(overlay, {
43
- triggerref: localRef,
44
- onClose: () => {
45
- var _a, _b;
46
- (_b = (_a = overlay.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
47
- setShowOverlay(false);
48
- },
49
- }), portalRoot);
50
- return (_jsxs(_Fragment, { children: [_jsx(Stack, Object.assign({ ref: localRef }, restProps, { onClick: onClickHandler, children: children })), showOverlay && teleport] }));
31
+ const clonedOverlay = cloneElement(overlay, {
32
+ triggerref: localRef,
33
+ onClose: () => {
34
+ var _a, _b;
35
+ (_b = overlay === null || overlay === void 0 ? void 0 : (_a = overlay.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
36
+ setShowOverlay(false);
37
+ },
38
+ });
39
+ return (_jsxs(_Fragment, { children: [_jsx(Stack, Object.assign({ ref: localRef }, restProps, { onClick: onClickHandler, children: children })), showOverlay && clonedOverlay] }));
51
40
  }
package/dist/index.d.ts CHANGED
@@ -34,13 +34,3 @@ export * from "./ActiveOnVisible/ActiveOnVisible";
34
34
  export * from "./Dropdown/Dropdown";
35
35
  export * from "./Popup/Popup";
36
36
  export * from "./Avatar/Avatar";
37
- export * from "./interactionHooks/useOnRightClick";
38
- export * from "./interactionHooks/useOnOutsideClick";
39
- export * from "./interactionHooks/useOnDoubleTap";
40
- export * from "./interactionHooks/useOnSwipe";
41
- export * from "./interactionHooks/useOnPinch";
42
- export * from "./interactionHooks/useOnRotate";
43
- export * from "./interactionHooks/useOnResizeChange";
44
- export * from "./interactionHooks/useOnVisibilityChange";
45
- export * from "./interactionHooks/useOnHoverChange";
46
- export * from "./interactionHooks/useOnScroll";
package/dist/index.js CHANGED
@@ -40,13 +40,3 @@ export * from "./ActiveOnVisible/ActiveOnVisible"; // Stack Based
40
40
  export * from "./Dropdown/Dropdown"; // Card Based
41
41
  export * from "./Popup/Popup"; // Card Based
42
42
  export * from "./Avatar/Avatar";
43
- export * from "./interactionHooks/useOnRightClick";
44
- export * from "./interactionHooks/useOnOutsideClick";
45
- export * from "./interactionHooks/useOnDoubleTap";
46
- export * from "./interactionHooks/useOnSwipe";
47
- export * from "./interactionHooks/useOnPinch";
48
- export * from "./interactionHooks/useOnRotate";
49
- export * from "./interactionHooks/useOnResizeChange";
50
- export * from "./interactionHooks/useOnVisibilityChange";
51
- export * from "./interactionHooks/useOnHoverChange";
52
- export * from "./interactionHooks/useOnScroll";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yakad/ui",
3
- "version": "1.3.164",
3
+ "version": "1.3.165",
4
4
  "private": false,
5
5
  "author": "Natiq",
6
6
  "description": "UI components for NextJS/React",
@@ -34,6 +34,7 @@
34
34
  "dependencies": {
35
35
  "@yakad/lib": "1.0.23",
36
36
  "@yakad/symbols": "1.0.29",
37
+ "@yakad/use-interactions": "1.0.0",
37
38
  "classnames": "2.5.1"
38
39
  },
39
40
  "peerDependencies": {
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnDoubleTap<T extends HTMLElement = HTMLElement>(callback: (e: TouchEvent) => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,23 +0,0 @@
1
- import { useCallback, useEffect, useRef, useState } from "react";
2
- export function useOnDoubleTap(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const [lastTap, setLastTap] = useState(0);
6
- const handleDoubleTap = useCallback((e) => {
7
- const now = Date.now();
8
- if (now - lastTap < 300) {
9
- callback(e);
10
- }
11
- setLastTap(now);
12
- }, [callback, lastTap]);
13
- useEffect(() => {
14
- const element = targetRef.current;
15
- if (!element)
16
- return;
17
- element.addEventListener("touchend", handleDoubleTap);
18
- return () => {
19
- element.removeEventListener("touchend", handleDoubleTap);
20
- };
21
- }, [handleDoubleTap, targetRef]);
22
- return targetRef;
23
- }
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnHoverChange<T extends HTMLElement = HTMLElement>(callback: (e: MouseEvent, hover: boolean) => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,23 +0,0 @@
1
- import { useCallback, useEffect, useRef } from "react";
2
- export function useOnHoverChange(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const handleMouseEnter = useCallback((e) => {
6
- window.matchMedia("(pointer: fine)").matches && callback(e, true);
7
- }, [callback]);
8
- const handleMouseLeave = useCallback((e) => {
9
- window.matchMedia("(pointer: fine)").matches && callback(e, false);
10
- }, [callback]);
11
- useEffect(() => {
12
- const element = targetRef.current;
13
- if (!element)
14
- return;
15
- element.addEventListener("mouseenter", handleMouseEnter);
16
- element.addEventListener("mouseleave", handleMouseLeave);
17
- return () => {
18
- element.removeEventListener("mouseenter", handleMouseEnter);
19
- element.removeEventListener("mouseleave", handleMouseLeave);
20
- };
21
- }, [handleMouseEnter, handleMouseLeave, targetRef]);
22
- return targetRef;
23
- }
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnOutsideClick<T extends HTMLElement = HTMLElement>(callback: (e: MouseEvent) => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,21 +0,0 @@
1
- import { useCallback, useEffect, useRef } from "react";
2
- export function useOnOutsideClick(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const handleClick = useCallback((e) => {
6
- e.preventDefault();
7
- const element = targetRef.current;
8
- if (!element)
9
- return;
10
- if (!element.contains(e.target)) {
11
- callback(e);
12
- }
13
- }, [callback, targetRef]);
14
- useEffect(() => {
15
- document.addEventListener("click", handleClick);
16
- return () => {
17
- document.removeEventListener("click", handleClick);
18
- };
19
- }, [handleClick]);
20
- return targetRef;
21
- }
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnPinch<T extends HTMLElement = HTMLElement>(callback: (e: TouchEvent, action: "in" | "out") => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,35 +0,0 @@
1
- import { useCallback, useEffect, useRef, useState } from "react";
2
- export function useOnPinch(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const [startDistance, setStartDistance] = useState(0);
6
- const getDistance = (t1, t2) => Math.hypot(t2.clientX - t1.clientX, t2.clientY - t1.clientY);
7
- const handleStart = useCallback((e) => {
8
- if (e.touches.length === 2) {
9
- const [t1, t2] = e.touches;
10
- setStartDistance(getDistance(t1, t2));
11
- }
12
- }, []);
13
- const handleMove = useCallback((e) => {
14
- if (e.touches.length === 2) {
15
- const [t1, t2] = e.touches;
16
- const dist = getDistance(t1, t2);
17
- if (dist < startDistance - 20)
18
- callback(e, "in");
19
- if (dist > startDistance + 20)
20
- callback(e, "out");
21
- }
22
- }, [callback, startDistance]);
23
- useEffect(() => {
24
- const element = targetRef.current;
25
- if (!element)
26
- return;
27
- element.addEventListener("touchstart", handleStart);
28
- element.addEventListener("touchmove", handleMove);
29
- return () => {
30
- element.removeEventListener("touchstart", handleStart);
31
- element.removeEventListener("touchmove", handleMove);
32
- };
33
- }, [handleMove, handleStart, targetRef]);
34
- return targetRef;
35
- }
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnResizeChange<T extends HTMLElement = HTMLElement>(callback: (e: ResizeObserverEntry) => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,23 +0,0 @@
1
- import { useCallback, useEffect, useRef } from "react";
2
- export function useOnResizeChange(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const handleResize = useCallback((e) => {
6
- callback(e);
7
- }, [callback]);
8
- useEffect(() => {
9
- const element = targetRef.current;
10
- if (!element)
11
- return;
12
- const resizeObserver = new ResizeObserver((entries) => {
13
- for (const entry of entries) {
14
- handleResize(entry);
15
- }
16
- });
17
- resizeObserver.observe(element);
18
- return () => {
19
- resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
20
- };
21
- }, [handleResize, targetRef]);
22
- return targetRef;
23
- }
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnRightClick<T extends HTMLElement = HTMLElement>(callback: (e: MouseEvent) => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,19 +0,0 @@
1
- import { useCallback, useEffect, useRef } from "react";
2
- export function useOnRightClick(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const handleContextMenu = useCallback((e) => {
6
- e.preventDefault();
7
- callback(e);
8
- }, [callback]);
9
- useEffect(() => {
10
- const element = targetRef.current;
11
- if (!element)
12
- return;
13
- element.addEventListener("contextmenu", handleContextMenu);
14
- return () => {
15
- element.removeEventListener("contextmenu", handleContextMenu);
16
- };
17
- }, [handleContextMenu, targetRef]);
18
- return targetRef;
19
- }
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnRotate<T extends HTMLElement = HTMLElement>(callback: (e: TouchEvent, deltaAngle: number) => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,36 +0,0 @@
1
- import { useCallback, useEffect, useRef, useState } from "react";
2
- export function useOnRotate(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const [startAngle, setStartAngle] = useState(0);
6
- const getAngle = (t1, t2) => Math.atan2(t2.clientY - t1.clientY, t2.clientX - t1.clientX) *
7
- (180 / Math.PI);
8
- const handleStart = useCallback((e) => {
9
- if (e.touches.length === 2) {
10
- const [t1, t2] = e.touches;
11
- setStartAngle(getAngle(t1, t2));
12
- }
13
- }, []);
14
- const handleMove = useCallback((e) => {
15
- if (e.touches.length === 2) {
16
- const [t1, t2] = e.touches;
17
- const angle = getAngle(t1, t2);
18
- const deltaAngle = angle - startAngle;
19
- if (Math.abs(deltaAngle) > 15) {
20
- callback(e, deltaAngle);
21
- }
22
- }
23
- }, [callback, startAngle]);
24
- useEffect(() => {
25
- const element = targetRef.current;
26
- if (!element)
27
- return;
28
- element.addEventListener("touchstart", handleStart);
29
- element.addEventListener("touchmove", handleMove);
30
- return () => {
31
- element.removeEventListener("touchstart", handleStart);
32
- element.removeEventListener("touchmove", handleMove);
33
- };
34
- }, [handleMove, handleStart, targetRef]);
35
- return targetRef;
36
- }
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnScroll<T extends HTMLElement = HTMLElement>(callback: (scrolling: boolean) => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,29 +0,0 @@
1
- import { useCallback, useEffect, useRef } from "react";
2
- export function useOnScroll(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const scrollTimeoutRef = useRef(undefined);
6
- const scrollingRef = useRef(false);
7
- const handleScroll = useCallback(() => {
8
- if (!scrollingRef.current) {
9
- scrollingRef.current = true;
10
- callback(true);
11
- }
12
- clearTimeout(scrollTimeoutRef.current);
13
- scrollTimeoutRef.current = setTimeout(() => {
14
- scrollingRef.current = false;
15
- callback(false);
16
- }, 150);
17
- }, [callback]);
18
- useEffect(() => {
19
- const element = targetRef.current;
20
- if (!element)
21
- return;
22
- element.addEventListener("scroll", handleScroll);
23
- return () => {
24
- element.removeEventListener("scroll", handleScroll);
25
- clearTimeout(scrollTimeoutRef.current);
26
- };
27
- }, [handleScroll, targetRef]);
28
- return targetRef;
29
- }
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnSwipe<T extends HTMLElement = HTMLElement>(callback: (e: TouchEvent, direction: "left" | "right" | "up" | "down") => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,53 +0,0 @@
1
- import { useCallback, useEffect, useRef, useState } from "react";
2
- export function useOnSwipe(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const [swipeCoordinates, setSwipeCoordinates] = useState({
6
- startX: 0,
7
- startY: 0,
8
- endX: 0,
9
- endY: 0,
10
- });
11
- const touchStart = useCallback((e) => {
12
- const t = e.touches[0];
13
- setSwipeCoordinates(prev => (Object.assign(Object.assign({}, prev), { startX: t.clientX, startY: t.clientY, endX: t.clientX, endY: t.clientY })));
14
- }, []);
15
- const touchMove = useCallback((e) => {
16
- const t = e.touches[0];
17
- setSwipeCoordinates(prev => (Object.assign(Object.assign({}, prev), { endX: t.clientX, endY: t.clientY })));
18
- }, []);
19
- const touchEnd = useCallback((e) => {
20
- // Could do unpacking of object but its not necessary
21
- const dx = swipeCoordinates.endX - swipeCoordinates.startX;
22
- const dy = swipeCoordinates.endY - swipeCoordinates.startY;
23
- const absDx = Math.abs(dx);
24
- const absDy = Math.abs(dy);
25
- const threshold = 30;
26
- if (absDx > absDy) {
27
- if (dx > threshold)
28
- callback(e, "right");
29
- if (dx < -threshold)
30
- callback(e, "left");
31
- }
32
- else {
33
- if (dy > threshold)
34
- callback(e, "down");
35
- if (dy < -threshold)
36
- callback(e, "up");
37
- }
38
- }, [callback, swipeCoordinates.endX, swipeCoordinates.startX, swipeCoordinates.endY, swipeCoordinates.startY]);
39
- useEffect(() => {
40
- const element = targetRef.current;
41
- if (!element)
42
- return;
43
- element.addEventListener("touchstart", touchStart, { passive: true });
44
- element.addEventListener("touchmove", touchMove, { passive: true });
45
- element.addEventListener("touchend", touchEnd, { passive: true });
46
- return () => {
47
- element.removeEventListener("touchstart", touchStart);
48
- element.removeEventListener("touchmove", touchMove);
49
- element.removeEventListener("touchend", touchEnd);
50
- };
51
- }, [touchStart, touchMove, touchEnd, targetRef]);
52
- return targetRef;
53
- }
@@ -1,2 +0,0 @@
1
- import { RefObject } from "react";
2
- export declare function useOnVisibilityChange<T extends HTMLElement = HTMLElement>(callback: (visible: boolean) => void, ref?: RefObject<T | null> | undefined): RefObject<T | null>;
@@ -1,21 +0,0 @@
1
- import { useCallback, useEffect, useRef } from "react";
2
- export function useOnVisibilityChange(callback, ref) {
3
- const defaultRef = useRef(null);
4
- const targetRef = ref !== null && ref !== void 0 ? ref : defaultRef;
5
- const handleVisibility = useCallback((visible) => {
6
- callback(visible);
7
- }, [callback]);
8
- useEffect(() => {
9
- const element = targetRef.current;
10
- if (!element)
11
- return;
12
- const intersectionObserver = new IntersectionObserver((entries) => {
13
- entries[0].isIntersecting ? handleVisibility(true) : handleVisibility(false);
14
- });
15
- intersectionObserver.observe(element);
16
- return () => {
17
- intersectionObserver === null || intersectionObserver === void 0 ? void 0 : intersectionObserver.disconnect();
18
- };
19
- }, [handleVisibility, targetRef]);
20
- return targetRef;
21
- }