@fibery/react 1.2.0 → 1.3.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/react",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "private": false,
5
5
  "license": "UNLICENSED",
6
6
  "files": [
@@ -10,9 +10,12 @@
10
10
  "react": "^18.2.0"
11
11
  },
12
12
  "dependencies": {
13
- "@fibery/helpers": "1.2.0"
13
+ "@fibery/helpers": "1.2.0",
14
+ "@tanstack/react-query": "5.12.2"
14
15
  },
15
16
  "devDependencies": {
17
+ "@fibery/babel-preset": "7.4.0",
18
+ "@fibery/eslint-config": "8.5.1",
16
19
  "@testing-library/react": "13.4.0",
17
20
  "@testing-library/react-hooks": "7.0.2",
18
21
  "@types/jest": "27.0.1",
@@ -23,9 +26,7 @@
23
26
  "react": "18.2.0",
24
27
  "react-dom": "18.2.0",
25
28
  "react-test-renderer": "18.2.0",
26
- "typescript": "5.1.6",
27
- "@fibery/babel-preset": "7.3.0",
28
- "@fibery/eslint-config": "8.4.0"
29
+ "typescript": "5.1.6"
29
30
  },
30
31
  "jest": {
31
32
  "coveragePathIgnorePatterns": [
@@ -0,0 +1,13 @@
1
+ export function composeEventHandlers<E>(
2
+ originalEventHandler?: (event: E) => void,
3
+ ourEventHandler?: (event: E) => void,
4
+ {checkForDefaultPrevented = true} = {}
5
+ ) {
6
+ return function handleEvent(event: E) {
7
+ originalEventHandler?.(event);
8
+
9
+ if (checkForDefaultPrevented === false || !(event as unknown as Event).defaultPrevented) {
10
+ ourEventHandler?.(event);
11
+ }
12
+ };
13
+ }
@@ -12,8 +12,8 @@ const hasChildren = (node: ReactNode): node is ReactElement => isValidElement(no
12
12
 
13
13
  /**
14
14
  *
15
- * Debug util to get text content of react node. Might be useful for logging
16
- * based on https://github.com/fernandopasik/react-children-utilities/blob/main/src/lib/onlyText.ts
15
+ * Debug util to get text content of react node. Might be useful for logging.
16
+ * Based on https://github.com/fernandopasik/react-children-utilities/blob/main/src/lib/onlyText.ts
17
17
  */
18
18
  export const reactNodeToString = (node: ReactNode | ReactNode[]) => {
19
19
  if (!(node instanceof Array) && !isValidElement(node)) {
@@ -0,0 +1,6 @@
1
+ import {QueryClient} from "@tanstack/react-query";
2
+ import {createContext} from "./create-context";
3
+
4
+ const [Provider, useContext] = createContext<QueryClient | null>("SharedQueryClientProvider", null);
5
+
6
+ export {Provider as SharedQueryClientProvider, useContext as useSharedQueryClient};
@@ -1,4 +1,4 @@
1
- import {useCallback, useInsertionEffect, useRef} from "react";
1
+ import {useCallback, useLayoutEffect, useRef} from "react";
2
2
 
3
3
  /**
4
4
  * Hook to wrap callback in ref
@@ -10,7 +10,7 @@ import {useCallback, useInsertionEffect, useRef} from "react";
10
10
  export const useCallbackRef = <T extends (...args: any[]) => any>(callback?: T): T => {
11
11
  const callbackRef = useRef(callback);
12
12
 
13
- useInsertionEffect(() => {
13
+ useLayoutEffect(() => {
14
14
  callbackRef.current = callback;
15
15
  });
16
16
 
@@ -0,0 +1,79 @@
1
+ import {useCallbackRef} from "./use-callback-ref";
2
+ import {useEffect, useRef} from "react";
3
+
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
+ /**
10
+ * Listens for `pointerdown` outside a react subtree. We use `pointerdown` rather than `pointerup`
11
+ * to mimic layer dismissing behaviour present in OS.
12
+ * Returns props to pass to the node we want to check for outside events.
13
+ */
14
+ export function usePointerDownOutside(onPointerDownOutside?: (event: PointerEvent) => void) {
15
+ const handlePointerDownOutside = useCallbackRef(onPointerDownOutside);
16
+ const isPointerInsideReactTreeRef = useRef(false);
17
+ const handleClickRef = useRef(() => {});
18
+
19
+ useEffect(() => {
20
+ const handlePointerDown = (event: PointerEvent) => {
21
+ if (event.target && !isPointerInsideReactTreeRef.current) {
22
+ const handlePointerDown = () => {
23
+ handlePointerDownOutside(event);
24
+ };
25
+
26
+ /**
27
+ * On touch devices, we need to wait for a click event because browsers implement
28
+ * a ~350ms delay between the time the user stops touching the display and when the
29
+ * browser executes events. We need to ensure we don't reactivate pointer-events within
30
+ * this timeframe otherwise the browser may execute events that should have been prevented.
31
+ *
32
+ * Additionally, this also lets us deal automatically with cancellations when a click event
33
+ * isn't raised because the page was considered scrolled/drag-scrolled, long-pressed, etc.
34
+ *
35
+ * This is why we also continuously remove the previous listener, because we cannot be
36
+ * certain that it was raised, and therefore cleaned-up.
37
+ */
38
+ if (event.pointerType === "touch") {
39
+ document.removeEventListener("click", handleClickRef.current);
40
+ handleClickRef.current = handlePointerDown;
41
+ document.addEventListener("click", handleClickRef.current, {once: true});
42
+ } else {
43
+ handlePointerDown();
44
+ }
45
+ } else {
46
+ // We need to remove the event listener in case the outside click has been canceled.
47
+ // See: https://github.com/radix-ui/primitives/issues/2171
48
+ document.removeEventListener("click", handleClickRef.current);
49
+ }
50
+ isPointerInsideReactTreeRef.current = false;
51
+ };
52
+ /**
53
+ * if this hook executes in a component that mounts via a `pointerdown` event, the event
54
+ * would bubble up to the document and trigger a `pointerDownOutside` event. We avoid
55
+ * this by delaying the event listener registration on the document.
56
+ * This is not React specific, but rather how the DOM works, ie:
57
+ * ```
58
+ * button.addEventListener('pointerdown', () => {
59
+ * console.log('I will log');
60
+ * document.addEventListener('pointerdown', () => {
61
+ * console.log('I will also log');
62
+ * })
63
+ * });
64
+ */
65
+ const timerId = window.setTimeout(() => {
66
+ document.addEventListener("pointerdown", handlePointerDown);
67
+ }, 0);
68
+ return () => {
69
+ window.clearTimeout(timerId);
70
+ document.removeEventListener("pointerdown", handlePointerDown);
71
+ document.removeEventListener("click", handleClickRef.current);
72
+ };
73
+ }, [handlePointerDownOutside]);
74
+
75
+ return {
76
+ // ensures we check React component tree (not just DOM tree)
77
+ onPointerDownCapture: () => (isPointerInsideReactTreeRef.current = true),
78
+ };
79
+ }