@fibery/react 1.6.0 → 2.0.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.
- package/lib/compose-event-handlers.d.ts +6 -0
- package/lib/compose-event-handlers.js +9 -0
- package/lib/create-context.d.ts +6 -0
- package/lib/create-context.js +18 -0
- package/lib/get-ref-value.d.ts +6 -0
- package/lib/get-ref-value.js +5 -0
- package/lib/has-non-empty-child.d.ts +5 -0
- package/lib/has-non-empty-child.js +6 -0
- package/lib/prevent-default.d.ts +6 -0
- package/lib/prevent-default.js +6 -0
- package/lib/react-node-to-string.d.ts +10 -0
- package/lib/react-node-to-string.js +24 -0
- package/lib/shared-query-client-provider.d.ts +5 -0
- package/lib/shared-query-client-provider.js +5 -0
- package/lib/stop-propagation.d.ts +4 -0
- package/lib/stop-propagation.js +6 -0
- package/lib/use-callback-ref.d.ts +10 -0
- package/lib/use-callback-ref.js +17 -0
- package/lib/use-composed-refs.d.ts +10 -0
- package/lib/use-composed-refs.js +20 -0
- package/lib/use-controllable-state/index.d.ts +2 -0
- package/lib/use-controllable-state/index.js +2 -0
- package/lib/use-controllable-state/use-controllable-state.d.ts +17 -0
- package/lib/use-controllable-state/use-controllable-state.js +35 -0
- package/lib/use-drag.d.ts +25 -0
- package/lib/use-drag.js +78 -0
- package/lib/use-focus-outside.d.ts +13 -0
- package/lib/use-focus-outside.js +29 -0
- package/lib/use-is-mounted.d.ts +4 -0
- package/lib/use-is-mounted.js +15 -0
- package/lib/use-pointer-down-outside.d.ts +15 -0
- package/lib/use-pointer-down-outside.js +68 -0
- package/lib/use-previous.d.ts +4 -0
- package/lib/use-previous.js +11 -0
- package/lib/use-resize-observer.d.ts +14 -0
- package/lib/use-resize-observer.js +36 -0
- package/lib/use-restore-focus.d.ts +10 -0
- package/lib/use-restore-focus.js +26 -0
- package/lib/use-size.d.ts +14 -0
- package/lib/use-size.js +37 -0
- package/{src/wrap.tsx → lib/wrap.d.ts} +5 -20
- package/lib/wrap.js +28 -0
- package/package.json +17 -34
- package/src/compose-event-handlers.ts +0 -13
- package/src/create-context.ts +0 -21
- package/src/get-ref-value.ts +0 -5
- package/src/has-non-empty-child.ts +0 -5
- package/src/prevent-default.ts +0 -3
- package/src/react-node-to-string.ts +0 -37
- package/src/shared-query-client-provider.tsx +0 -7
- package/src/stop-propagation.ts +0 -3
- package/src/use-callback-ref.ts +0 -18
- package/src/use-composed-refs.ts +0 -27
- package/src/use-controllable-state/index.ts +0 -1
- package/src/use-controllable-state/use-controllable-state.test.ts +0 -46
- package/src/use-controllable-state/use-controllable-state.ts +0 -61
- package/src/use-drag.ts +0 -96
- package/src/use-focus-outside.ts +0 -34
- package/src/use-is-mounted.ts +0 -15
- package/src/use-pointer-down-outside.tsx +0 -80
- package/src/use-previous.ts +0 -9
- package/src/use-resize-observer.ts +0 -50
- package/src/use-restore-focus.ts +0 -31
- package/src/use-size.ts +0 -53
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
//#region src/compose-event-handlers.d.ts
|
|
2
|
+
declare function composeEventHandlers<E>(originalEventHandler?: (event: E) => void, ourEventHandler?: (event: E) => void, { checkForDefaultPrevented }?: {
|
|
3
|
+
checkForDefaultPrevented?: boolean | undefined;
|
|
4
|
+
}): (event: E) => void;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { composeEventHandlers };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/compose-event-handlers.ts
|
|
2
|
+
function composeEventHandlers(originalEventHandler, ourEventHandler, { checkForDefaultPrevented = true } = {}) {
|
|
3
|
+
return function handleEvent(event) {
|
|
4
|
+
originalEventHandler?.(event);
|
|
5
|
+
if (checkForDefaultPrevented === false || !event.defaultPrevented) ourEventHandler?.(event);
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { composeEventHandlers };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as React$1 from "react";
|
|
2
|
+
import { Context } from "react";
|
|
3
|
+
//#region src/create-context.d.ts
|
|
4
|
+
declare const createContext: <CtxType>(displayName: string, defaultCtx?: CtxType) => [React$1.Provider<CtxType>, () => CtxType, Context<CtxType>];
|
|
5
|
+
//#endregion
|
|
6
|
+
export { createContext };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React$1 from "react";
|
|
2
|
+
//#region src/create-context.ts
|
|
3
|
+
const createContext = (displayName, defaultCtx) => {
|
|
4
|
+
const Context = React$1.createContext(defaultCtx);
|
|
5
|
+
Context.displayName = displayName;
|
|
6
|
+
function useContext() {
|
|
7
|
+
const ctx = React$1.useContext(Context);
|
|
8
|
+
if (ctx === void 0) throw new Error(`${displayName} consumer must be used within ${displayName}.Provider`);
|
|
9
|
+
return ctx;
|
|
10
|
+
}
|
|
11
|
+
return [
|
|
12
|
+
Context.Provider,
|
|
13
|
+
useContext,
|
|
14
|
+
Context
|
|
15
|
+
];
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { createContext };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { RefObject } from "react";
|
|
2
|
+
//#region src/get-ref-value.d.ts
|
|
3
|
+
/** Returns current value if refOrValue is RefObject otherwise returns itself */
|
|
4
|
+
declare const getRefValue: <Value>(refOrValue: RefObject<Value> | Value | null) => Value | null;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { getRefValue };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
//#region src/get-ref-value.ts
|
|
2
|
+
/** Returns current value if refOrValue is RefObject otherwise returns itself */
|
|
3
|
+
const getRefValue = (refOrValue) => refOrValue && typeof refOrValue === "object" && "current" in refOrValue ? refOrValue.current : refOrValue;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { getRefValue };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Children } from "react";
|
|
2
|
+
//#region src/has-non-empty-child.ts
|
|
3
|
+
/** React helper which checks whether children has at least one truthy child */
|
|
4
|
+
const hasNonEmptyChild = (children) => Children.toArray(children).some((value) => Boolean(value));
|
|
5
|
+
//#endregion
|
|
6
|
+
export { hasNonEmptyChild };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
//#region src/react-node-to-string.d.ts
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* Debug util to get text content of react node. Might be useful for logging.
|
|
6
|
+
* Based on https://github.com/fernandopasik/react-children-utilities/blob/main/src/lib/onlyText.ts
|
|
7
|
+
*/
|
|
8
|
+
declare const reactNodeToString: (node: ReactNode | ReactNode[]) => string;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { reactNodeToString };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Children, isValidElement } from "react";
|
|
2
|
+
//#region src/react-node-to-string.ts
|
|
3
|
+
const nodeToString = (child) => {
|
|
4
|
+
if (typeof child === "string" || typeof child === "number") return child.toString();
|
|
5
|
+
return "";
|
|
6
|
+
};
|
|
7
|
+
const hasChildren = (node) => isValidElement(node) && Boolean(node.props.children);
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* Debug util to get text content of react node. Might be useful for logging.
|
|
11
|
+
* Based on https://github.com/fernandopasik/react-children-utilities/blob/main/src/lib/onlyText.ts
|
|
12
|
+
*/
|
|
13
|
+
const reactNodeToString = (node) => {
|
|
14
|
+
if (!(node instanceof Array) && !isValidElement(node)) return nodeToString(node);
|
|
15
|
+
return Children.toArray(node).reduce((text, child) => {
|
|
16
|
+
let newText = "";
|
|
17
|
+
if (hasChildren(child)) newText = reactNodeToString(child.props.children);
|
|
18
|
+
else if (isValidElement(child) && !hasChildren(child)) newText = "";
|
|
19
|
+
else newText = nodeToString(child);
|
|
20
|
+
return text.concat(newText);
|
|
21
|
+
}, "");
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
24
|
+
export { reactNodeToString };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
2
|
+
//#region src/shared-query-client-provider.d.ts
|
|
3
|
+
declare const Provider: import("react").Provider<QueryClient | null>, useContext: () => QueryClient | null;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { Provider as SharedQueryClientProvider, useContext as useSharedQueryClient };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { createContext } from "./create-context.js";
|
|
2
|
+
//#region src/shared-query-client-provider.tsx
|
|
3
|
+
const [Provider, useContext] = createContext("SharedQueryClientProvider", null);
|
|
4
|
+
//#endregion
|
|
5
|
+
export { Provider as SharedQueryClientProvider, useContext as useSharedQueryClient };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/use-callback-ref.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Hook to wrap callback in ref
|
|
4
|
+
* so it doesn't trigger re-renders and remains up-to-date
|
|
5
|
+
*
|
|
6
|
+
* Could be replaced with https://github.com/reactjs/rfcs/pull/220 in future
|
|
7
|
+
*/
|
|
8
|
+
declare const useCallbackRef: <T extends (...args: any[]) => any>(callback?: T) => T;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { useCallbackRef };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useRef } from "react";
|
|
2
|
+
//#region src/use-callback-ref.ts
|
|
3
|
+
/**
|
|
4
|
+
* Hook to wrap callback in ref
|
|
5
|
+
* so it doesn't trigger re-renders and remains up-to-date
|
|
6
|
+
*
|
|
7
|
+
* Could be replaced with https://github.com/reactjs/rfcs/pull/220 in future
|
|
8
|
+
*/
|
|
9
|
+
const useCallbackRef = (callback) => {
|
|
10
|
+
const callbackRef = useRef(callback);
|
|
11
|
+
useLayoutEffect(() => {
|
|
12
|
+
callbackRef.current = callback;
|
|
13
|
+
});
|
|
14
|
+
return useCallback(((...args) => callbackRef.current?.(...args)), []);
|
|
15
|
+
};
|
|
16
|
+
//#endregion
|
|
17
|
+
export { useCallbackRef };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
//#region src/use-composed-refs.d.ts
|
|
3
|
+
declare const composeRefs: <T>(...refs: Array<React.Ref<T> | undefined>) => (node: T) => void;
|
|
4
|
+
/**
|
|
5
|
+
* Hook to compose multiple refs
|
|
6
|
+
* Accepts callback refs and RefObject(s)
|
|
7
|
+
*/
|
|
8
|
+
declare const useComposedRefs: <T>(...refs: Array<React.Ref<T> | undefined>) => React.RefCallback<T>;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { composeRefs, useComposedRefs };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
//#region src/use-composed-refs.ts
|
|
3
|
+
const composeRefs = (...refs) => {
|
|
4
|
+
return function setRefs(node) {
|
|
5
|
+
refs.forEach((ref) => {
|
|
6
|
+
if (!ref) return;
|
|
7
|
+
if (typeof ref === "function") ref(node);
|
|
8
|
+
else ref.current = node;
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Hook to compose multiple refs
|
|
14
|
+
* Accepts callback refs and RefObject(s)
|
|
15
|
+
*/
|
|
16
|
+
const useComposedRefs = (...refs) => {
|
|
17
|
+
return useCallback(composeRefs(...refs), refs);
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { composeRefs, useComposedRefs };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from "react";
|
|
2
|
+
//#region src/use-controllable-state/use-controllable-state.d.ts
|
|
3
|
+
type UseControllableStateProps<TState> = {
|
|
4
|
+
/** value to be used in controlled mode */
|
|
5
|
+
value?: TState;
|
|
6
|
+
/** initial value to be used in uncontrolled mode */
|
|
7
|
+
defaultValue?: TState;
|
|
8
|
+
/** callback for value changes */
|
|
9
|
+
onChange?: (state: TState) => unknown;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* React hook to support both controlled/uncontrolled modes
|
|
13
|
+
* @see https://reactjs.org/docs/uncontrolled-components.html
|
|
14
|
+
*/
|
|
15
|
+
declare const useControllableState: <TState>({ value, defaultValue, onChange }: UseControllableStateProps<TState>) => [TState, Dispatch<SetStateAction<TState>>];
|
|
16
|
+
//#endregion
|
|
17
|
+
export { useControllableState };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useCallbackRef } from "../use-callback-ref.js";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
//#region src/use-controllable-state/use-controllable-state.ts
|
|
4
|
+
/**
|
|
5
|
+
* React hook to support both controlled/uncontrolled modes
|
|
6
|
+
* @see https://reactjs.org/docs/uncontrolled-components.html
|
|
7
|
+
*/
|
|
8
|
+
const useControllableState = ({ value, defaultValue, onChange }) => {
|
|
9
|
+
const [uncontrolledState, setUncontrolledState] = useState(defaultValue);
|
|
10
|
+
const onChangeState = useCallbackRef(onChange);
|
|
11
|
+
const isControlled = value !== void 0;
|
|
12
|
+
const state = isControlled ? value : uncontrolledState;
|
|
13
|
+
const prevUncontrolledStateRef = useRef(uncontrolledState);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (prevUncontrolledStateRef.current !== uncontrolledState) {
|
|
16
|
+
onChangeState(uncontrolledState);
|
|
17
|
+
prevUncontrolledStateRef.current = uncontrolledState;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return [state, useCallback((nextValue) => {
|
|
21
|
+
if (isControlled) {
|
|
22
|
+
const nextState = nextValue instanceof Function ? nextValue(value) : nextValue;
|
|
23
|
+
if (value === nextState) return;
|
|
24
|
+
onChangeState(nextState);
|
|
25
|
+
} else setUncontrolledState((prevValue) => {
|
|
26
|
+
return nextValue instanceof Function ? nextValue(prevValue) : nextValue;
|
|
27
|
+
});
|
|
28
|
+
}, [
|
|
29
|
+
isControlled,
|
|
30
|
+
onChangeState,
|
|
31
|
+
value
|
|
32
|
+
])];
|
|
33
|
+
};
|
|
34
|
+
//#endregion
|
|
35
|
+
export { useControllableState };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/use-drag.d.ts
|
|
2
|
+
type Point = {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* very simple hook for simple isolated drag-like operations.
|
|
8
|
+
* e.g. dragging border to resize, when you dont need drop targets, auto-scroll, etc.
|
|
9
|
+
*
|
|
10
|
+
* For complex operations you most likely want to use atlaskit/pragmatic-drag-and-drop
|
|
11
|
+
*/
|
|
12
|
+
declare const useDrag: ({ threshold, onDragStart, onDrag, onDragEnd }: {
|
|
13
|
+
/** Minimum movement before drag starts */
|
|
14
|
+
threshold?: number;
|
|
15
|
+
onDragStart?: () => void;
|
|
16
|
+
onDrag?: (translation: Point) => void;
|
|
17
|
+
onDragEnd?: (translation: Point) => void;
|
|
18
|
+
}) => {
|
|
19
|
+
ref: import("react").RefObject<HTMLDivElement>;
|
|
20
|
+
onPointerDown: (e: React.PointerEvent) => void;
|
|
21
|
+
onPointerMove: (e: React.PointerEvent) => void;
|
|
22
|
+
onPointerUp: (e: React.PointerEvent) => void;
|
|
23
|
+
};
|
|
24
|
+
//#endregion
|
|
25
|
+
export { useDrag };
|
package/lib/use-drag.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { useRef } from "react";
|
|
2
|
+
//#region src/use-drag.ts
|
|
3
|
+
/**
|
|
4
|
+
* very simple hook for simple isolated drag-like operations.
|
|
5
|
+
* e.g. dragging border to resize, when you dont need drop targets, auto-scroll, etc.
|
|
6
|
+
*
|
|
7
|
+
* For complex operations you most likely want to use atlaskit/pragmatic-drag-and-drop
|
|
8
|
+
*/
|
|
9
|
+
const useDrag = ({ threshold = 0, onDragStart, onDrag, onDragEnd }) => {
|
|
10
|
+
const ref = useRef(null);
|
|
11
|
+
const state = useRef("idle");
|
|
12
|
+
const lastPosition = useRef({
|
|
13
|
+
x: 0,
|
|
14
|
+
y: 0
|
|
15
|
+
});
|
|
16
|
+
const translation = useRef({
|
|
17
|
+
x: 0,
|
|
18
|
+
y: 0
|
|
19
|
+
});
|
|
20
|
+
function onClick(e) {
|
|
21
|
+
if (state.current === "drag-end") {
|
|
22
|
+
e.preventDefault();
|
|
23
|
+
e.stopPropagation();
|
|
24
|
+
state.current = "idle";
|
|
25
|
+
ref.current?.removeEventListener("click", onClick);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function onPointerDown(e) {
|
|
29
|
+
lastPosition.current = {
|
|
30
|
+
x: e.clientX,
|
|
31
|
+
y: e.clientY
|
|
32
|
+
};
|
|
33
|
+
translation.current = {
|
|
34
|
+
x: 0,
|
|
35
|
+
y: 0
|
|
36
|
+
};
|
|
37
|
+
state.current = "press";
|
|
38
|
+
ref.current?.setPointerCapture(e.pointerId);
|
|
39
|
+
ref.current?.addEventListener("click", onClick);
|
|
40
|
+
}
|
|
41
|
+
function onPointerMove(e) {
|
|
42
|
+
if (state.current === "press") {
|
|
43
|
+
const dx = e.clientX - lastPosition.current.x;
|
|
44
|
+
const dy = e.clientY - lastPosition.current.y;
|
|
45
|
+
if (Math.sqrt(dx * dx + dy * dy) >= threshold) {
|
|
46
|
+
state.current = "drag";
|
|
47
|
+
onDragStart?.();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (state.current !== "drag") return;
|
|
51
|
+
const currentPosition = {
|
|
52
|
+
x: e.clientX,
|
|
53
|
+
y: e.clientY
|
|
54
|
+
};
|
|
55
|
+
const dx = currentPosition.x - lastPosition.current.x;
|
|
56
|
+
const dy = currentPosition.y - lastPosition.current.y;
|
|
57
|
+
lastPosition.current = currentPosition;
|
|
58
|
+
translation.current = {
|
|
59
|
+
x: translation.current.x + dx,
|
|
60
|
+
y: translation.current.y + dy
|
|
61
|
+
};
|
|
62
|
+
onDrag?.(translation.current);
|
|
63
|
+
}
|
|
64
|
+
function onPointerUp(e) {
|
|
65
|
+
if (state.current === "drag") state.current = "drag-end";
|
|
66
|
+
else state.current = "idle";
|
|
67
|
+
onDragEnd?.(translation.current);
|
|
68
|
+
ref.current?.releasePointerCapture(e.pointerId);
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
ref,
|
|
72
|
+
onPointerDown,
|
|
73
|
+
onPointerMove,
|
|
74
|
+
onPointerUp
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
//#endregion
|
|
78
|
+
export { useDrag };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/use-focus-outside.d.ts
|
|
2
|
+
type OnFocusOutside = (event: FocusEvent) => void;
|
|
3
|
+
/**
|
|
4
|
+
* Hook to react on focus moving outside of react tree including portals
|
|
5
|
+
*
|
|
6
|
+
* https://github.com/facebook/react/issues/19637#issuecomment-850412650
|
|
7
|
+
*/
|
|
8
|
+
declare const useFocusOutside: (onFocusOutside?: OnFocusOutside) => {
|
|
9
|
+
onFocusCapture: () => void;
|
|
10
|
+
onBlurCapture: () => void;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { useFocusOutside };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useCallbackRef } from "./use-callback-ref.js";
|
|
2
|
+
import { useLayoutEffect, useRef } from "react";
|
|
3
|
+
//#region src/use-focus-outside.ts
|
|
4
|
+
/**
|
|
5
|
+
* Hook to react on focus moving outside of react tree including portals
|
|
6
|
+
*
|
|
7
|
+
* https://github.com/facebook/react/issues/19637#issuecomment-850412650
|
|
8
|
+
*/
|
|
9
|
+
const useFocusOutside = (onFocusOutside) => {
|
|
10
|
+
const handleFocusOutside = useCallbackRef(onFocusOutside);
|
|
11
|
+
const isFocusInsideReactTreeRef = useRef(false);
|
|
12
|
+
useLayoutEffect(() => {
|
|
13
|
+
const handleFocus = (event) => {
|
|
14
|
+
if (!isFocusInsideReactTreeRef.current) handleFocusOutside(event);
|
|
15
|
+
};
|
|
16
|
+
document.addEventListener("focusin", handleFocus);
|
|
17
|
+
return () => document.removeEventListener("focusin", handleFocus);
|
|
18
|
+
}, [handleFocusOutside]);
|
|
19
|
+
return {
|
|
20
|
+
onFocusCapture: () => {
|
|
21
|
+
isFocusInsideReactTreeRef.current = true;
|
|
22
|
+
},
|
|
23
|
+
onBlurCapture: () => {
|
|
24
|
+
isFocusInsideReactTreeRef.current = false;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
//#endregion
|
|
29
|
+
export { useFocusOutside };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useRef } from "react";
|
|
2
|
+
//#region src/use-is-mounted.ts
|
|
3
|
+
function useIsMounted() {
|
|
4
|
+
const mountedRef = useRef(false);
|
|
5
|
+
const isMounted = useCallback(() => mountedRef.current, []);
|
|
6
|
+
useLayoutEffect(() => {
|
|
7
|
+
mountedRef.current = true;
|
|
8
|
+
return () => {
|
|
9
|
+
mountedRef.current = false;
|
|
10
|
+
};
|
|
11
|
+
}, []);
|
|
12
|
+
return isMounted;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { useIsMounted };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/use-pointer-down-outside.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Inspired by radix
|
|
4
|
+
* https://github.com/radix-ui/primitives/blob/c31c97274ff357aea99afe6c01c1c8c58b6356e0/packages/react/dismissable-layer/src/DismissableLayer.tsx#L223
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Listens for `pointerdown` outside a react subtree. We use `pointerdown` rather than `pointerup`
|
|
8
|
+
* to mimic layer dismissing behaviour present in OS.
|
|
9
|
+
* Returns props to pass to the node we want to check for outside events.
|
|
10
|
+
*/
|
|
11
|
+
declare function usePointerDownOutside(onPointerDownOutside?: (event: PointerEvent) => void): {
|
|
12
|
+
onPointerDownCapture: () => boolean;
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { usePointerDownOutside };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useCallbackRef } from "./use-callback-ref.js";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
//#region src/use-pointer-down-outside.tsx
|
|
4
|
+
/**
|
|
5
|
+
* Inspired by radix
|
|
6
|
+
* https://github.com/radix-ui/primitives/blob/c31c97274ff357aea99afe6c01c1c8c58b6356e0/packages/react/dismissable-layer/src/DismissableLayer.tsx#L223
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Listens for `pointerdown` outside a react subtree. We use `pointerdown` rather than `pointerup`
|
|
10
|
+
* to mimic layer dismissing behaviour present in OS.
|
|
11
|
+
* Returns props to pass to the node we want to check for outside events.
|
|
12
|
+
*/
|
|
13
|
+
function usePointerDownOutside(onPointerDownOutside) {
|
|
14
|
+
const handlePointerDownOutside = useCallbackRef(onPointerDownOutside);
|
|
15
|
+
const isPointerInsideReactTreeRef = useRef(false);
|
|
16
|
+
const handleClickRef = useRef(() => {});
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const handlePointerDown = (event) => {
|
|
19
|
+
if (event.target && !isPointerInsideReactTreeRef.current) {
|
|
20
|
+
const handlePointerDown = () => {
|
|
21
|
+
handlePointerDownOutside(event);
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* On touch devices, we need to wait for a click event because browsers implement
|
|
25
|
+
* a ~350ms delay between the time the user stops touching the display and when the
|
|
26
|
+
* browser executes events. We need to ensure we don't reactivate pointer-events within
|
|
27
|
+
* this timeframe otherwise the browser may execute events that should have been prevented.
|
|
28
|
+
*
|
|
29
|
+
* Additionally, this also lets us deal automatically with cancellations when a click event
|
|
30
|
+
* isn't raised because the page was considered scrolled/drag-scrolled, long-pressed, etc.
|
|
31
|
+
*
|
|
32
|
+
* This is why we also continuously remove the previous listener, because we cannot be
|
|
33
|
+
* certain that it was raised, and therefore cleaned-up.
|
|
34
|
+
*/
|
|
35
|
+
if (event.pointerType === "touch") {
|
|
36
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
37
|
+
handleClickRef.current = handlePointerDown;
|
|
38
|
+
document.addEventListener("click", handleClickRef.current, { once: true });
|
|
39
|
+
} else handlePointerDown();
|
|
40
|
+
} else document.removeEventListener("click", handleClickRef.current);
|
|
41
|
+
isPointerInsideReactTreeRef.current = false;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* if this hook executes in a component that mounts via a `pointerdown` event, the event
|
|
45
|
+
* would bubble up to the document and trigger a `pointerDownOutside` event. We avoid
|
|
46
|
+
* this by delaying the event listener registration on the document.
|
|
47
|
+
* This is not React specific, but rather how the DOM works, ie:
|
|
48
|
+
* ```
|
|
49
|
+
* button.addEventListener('pointerdown', () => {
|
|
50
|
+
* console.log('I will log');
|
|
51
|
+
* document.addEventListener('pointerdown', () => {
|
|
52
|
+
* console.log('I will also log');
|
|
53
|
+
* })
|
|
54
|
+
* });
|
|
55
|
+
*/
|
|
56
|
+
const timerId = window.setTimeout(() => {
|
|
57
|
+
document.addEventListener("pointerdown", handlePointerDown);
|
|
58
|
+
}, 0);
|
|
59
|
+
return () => {
|
|
60
|
+
window.clearTimeout(timerId);
|
|
61
|
+
document.removeEventListener("pointerdown", handlePointerDown);
|
|
62
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
63
|
+
};
|
|
64
|
+
}, [handlePointerDownOutside]);
|
|
65
|
+
return { onPointerDownCapture: () => isPointerInsideReactTreeRef.current = true };
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { usePointerDownOutside };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
//#region src/use-previous.ts
|
|
3
|
+
function usePrevious(value) {
|
|
4
|
+
const ref = useRef(null);
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
ref.current = value;
|
|
7
|
+
}, [value]);
|
|
8
|
+
return ref.current;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { usePrevious };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RefObject } from "react";
|
|
2
|
+
import { ResizeListenerCallback } from "@fibery/helpers/utils/resize-listener";
|
|
3
|
+
//#region src/use-resize-observer.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* React hook to observe provided element with ResizeObserver
|
|
6
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
|
|
7
|
+
*/
|
|
8
|
+
declare const useResizeObserver: <TargetElement extends HTMLElement>(
|
|
9
|
+
/** React ref created by `useRef()` or an HTML element */
|
|
10
|
+
target: RefObject<TargetElement> | TargetElement | null,
|
|
11
|
+
/**Invoked with a single `ResizeObserverEntry` any time the `target` resizes */
|
|
12
|
+
callback: ResizeListenerCallback) => ResizeObserver;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { useResizeObserver };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { getRefValue } from "./get-ref-value.js";
|
|
2
|
+
import { useCallbackRef } from "./use-callback-ref.js";
|
|
3
|
+
import { useLayoutEffect } from "react";
|
|
4
|
+
import { getResizeListener } from "@fibery/helpers/utils/resize-listener";
|
|
5
|
+
import { flushSync } from "react-dom";
|
|
6
|
+
//#region src/use-resize-observer.ts
|
|
7
|
+
/**
|
|
8
|
+
* React hook to observe provided element with ResizeObserver
|
|
9
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
|
|
10
|
+
*/
|
|
11
|
+
const useResizeObserver = (target, callback) => {
|
|
12
|
+
const resizeListener = getResizeListener();
|
|
13
|
+
const onResize = useCallbackRef(callback);
|
|
14
|
+
useLayoutEffect(() => {
|
|
15
|
+
let active = true;
|
|
16
|
+
const element = getRefValue(target);
|
|
17
|
+
if (!element) return () => {};
|
|
18
|
+
const observerCallback = (entry, observer) => {
|
|
19
|
+
if (active) flushSync(() => {
|
|
20
|
+
onResize(entry, observer);
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
resizeListener.subscribe(element, observerCallback);
|
|
24
|
+
return () => {
|
|
25
|
+
active = false;
|
|
26
|
+
resizeListener.unsubscribe(element, observerCallback);
|
|
27
|
+
};
|
|
28
|
+
}, [
|
|
29
|
+
onResize,
|
|
30
|
+
resizeListener,
|
|
31
|
+
target
|
|
32
|
+
]);
|
|
33
|
+
return resizeListener.observer;
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
export { useResizeObserver };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/use-restore-focus.d.ts
|
|
2
|
+
declare const useRestoreFocus: <T extends HTMLElement>() => {
|
|
3
|
+
inputRef: import("react").RefObject<T>;
|
|
4
|
+
selectorOpen: boolean;
|
|
5
|
+
onOpen: () => void;
|
|
6
|
+
onClose: () => void;
|
|
7
|
+
onOpenChange: (value: boolean) => void;
|
|
8
|
+
};
|
|
9
|
+
//#endregion
|
|
10
|
+
export { useRestoreFocus };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from "react";
|
|
2
|
+
//#region src/use-restore-focus.ts
|
|
3
|
+
const useRestoreFocus = () => {
|
|
4
|
+
const inputRef = useRef(null);
|
|
5
|
+
const restoreFocus = useCallback(() => {
|
|
6
|
+
if (inputRef.current) setTimeout(() => inputRef.current?.focus(), 10);
|
|
7
|
+
}, []);
|
|
8
|
+
const [selectorOpen, setSelectorOpen] = useState(false);
|
|
9
|
+
const onOpen = useCallback(() => setSelectorOpen(true), []);
|
|
10
|
+
const onClose = useCallback(() => {
|
|
11
|
+
setSelectorOpen(false);
|
|
12
|
+
restoreFocus();
|
|
13
|
+
}, [restoreFocus]);
|
|
14
|
+
return {
|
|
15
|
+
inputRef,
|
|
16
|
+
selectorOpen,
|
|
17
|
+
onOpen,
|
|
18
|
+
onClose,
|
|
19
|
+
onOpenChange: useCallback((value) => {
|
|
20
|
+
if (value) onOpen();
|
|
21
|
+
else onClose();
|
|
22
|
+
}, [onClose, onOpen])
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { useRestoreFocus };
|