@fibery/react 1.5.1 → 1.5.3

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,50 +1,34 @@
1
1
  {
2
2
  "name": "@fibery/react",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "private": false,
5
5
  "license": "UNLICENSED",
6
6
  "files": [
7
7
  "src"
8
8
  ],
9
- "peerDependencies": {
10
- "react": "18.3.1"
11
- },
12
9
  "dependencies": {
13
10
  "@tanstack/react-query": "5.62.7",
14
- "@fibery/helpers": "2.0.0"
11
+ "@fibery/helpers": "3.0.0"
15
12
  },
16
13
  "devDependencies": {
17
14
  "@testing-library/react": "16.3.1",
18
15
  "@testing-library/react-hooks": "8.0.1",
19
- "@types/jest": "30.0.0",
20
16
  "@types/react": "18.3.12",
21
17
  "@types/react-dom": "18.3.1",
22
- "jest": "30.2.0",
23
- "jest-junit": "16.0.0",
24
18
  "react": "18.3.1",
25
19
  "react-dom": "18.3.1",
26
20
  "react-test-renderer": "18.3.1",
27
- "typescript": "5.9.3",
28
- "@fibery/babel-preset": "7.4.2",
29
- "@fibery/eslint-config": "9.0.1"
21
+ "typescript": "6.0.1-rc",
22
+ "@fibery/babel-preset": "7.4.2"
30
23
  },
31
- "jest": {
32
- "coveragePathIgnorePatterns": [
33
- "/node_modules/"
34
- ],
35
- "collectCoverageFrom": [
36
- "src/**/*.{js,jsx,ts,tsx}",
37
- "!src/**/*.d.ts"
38
- ],
39
- "coverageReporters": [
40
- "text",
41
- "cobertura"
42
- ]
24
+ "peerDependencies": {
25
+ "react": "18.3.1"
43
26
  },
44
27
  "scripts": {
45
- "lint": "eslint .",
46
- "test": "jest",
47
- "test:ci": "pnpm run test --reporters=default --reporters=jest-junit",
48
- "test:coverage": "pnpm run test --coverage --coverageDirectory=${JEST_COVERAGE_RESULT_DIR:-$(pwd)}/coverage/react --reporters=default --reporters=jest-junit"
28
+ "check": "../../scripts/check.sh",
29
+ "lint": "../../scripts/lint.sh",
30
+ "test": "../../scripts/test.sh",
31
+ "test:ci": "../../scripts/test.sh --ci",
32
+ "test:coverage": "../../scripts/test.sh --ci --coverage"
49
33
  }
50
34
  }
@@ -1,5 +1,5 @@
1
1
  import * as React from "react";
2
- import {Context} from "react";
2
+ import type {Context} from "react";
3
3
 
4
4
  export const createContext = <CtxType>(
5
5
  displayName: string,
@@ -1,4 +1,4 @@
1
- import {RefObject} from "react";
1
+ import type {RefObject} from "react";
2
2
 
3
3
  /** Returns current value if refOrValue is RefObject otherwise returns itself */
4
4
  export const getRefValue = <Value>(refOrValue: RefObject<Value> | Value | null): Value | null =>
@@ -1,4 +1,5 @@
1
- import {Children, ReactElement, ReactNode, isValidElement} from "react";
1
+ import type {ReactElement, ReactNode} from "react";
2
+ import {Children, isValidElement} from "react";
2
3
 
3
4
  const nodeToString = (child?: React.ReactNode): string => {
4
5
  if (typeof child === "string" || typeof child === "number") {
@@ -1,4 +1,5 @@
1
- import {QueryClient} from "@tanstack/react-query";
1
+ import type {QueryClient} from "@tanstack/react-query";
2
+
2
3
  import {createContext} from "./create-context";
3
4
 
4
5
  const [Provider, useContext] = createContext<QueryClient | null>("SharedQueryClientProvider", null);
@@ -1,4 +1,5 @@
1
- import React, {useCallback} from "react";
1
+ import type React from "react";
2
+ import {useCallback} from "react";
2
3
 
3
4
  export const composeRefs = <T>(...refs: Array<React.Ref<T> | undefined>) => {
4
5
  return function setRefs(node: T) {
@@ -1,10 +1,11 @@
1
1
  import {act, renderHook} from "@testing-library/react-hooks";
2
+
2
3
  import {useControllableState} from "./use-controllable-state";
3
4
 
4
5
  describe("useControllableState", () => {
5
6
  describe("uncontrolled", () => {
6
7
  it("should work for uncontrolled", () => {
7
- const onChange = jest.fn();
8
+ const onChange = vi.fn();
8
9
  const {result} = renderHook(() => useControllableState({onChange}));
9
10
  expect(result.current[0]).toBe(undefined);
10
11
 
@@ -16,7 +17,7 @@ describe("useControllableState", () => {
16
17
  expect(onChange).toHaveBeenCalledWith(true);
17
18
  });
18
19
  it("should work for uncontrolled with default value", () => {
19
- const onChange = jest.fn();
20
+ const onChange = vi.fn();
20
21
  const {result} = renderHook(() => useControllableState({defaultValue: true, onChange}));
21
22
  expect(result.current[0]).toBe(true);
22
23
 
@@ -30,7 +31,7 @@ describe("useControllableState", () => {
30
31
  });
31
32
  describe("controlled", () => {
32
33
  it("should not change internal state. It's controlled by 'value'", () => {
33
- const onChange = jest.fn();
34
+ const onChange = vi.fn();
34
35
  const {result} = renderHook(() => useControllableState({value: true, onChange}));
35
36
  expect(result.current[0]).toEqual(true);
36
37
 
@@ -1,4 +1,6 @@
1
- import {Dispatch, SetStateAction, useCallback, useState} from "react";
1
+ import type {Dispatch, SetStateAction} from "react";
2
+ import {useCallback, useEffect, useRef, useState} from "react";
3
+
2
4
  import {useCallbackRef} from "../use-callback-ref";
3
5
 
4
6
  type UseControllableStateProps<TState> = {
@@ -25,6 +27,17 @@ export const useControllableState = <TState>({
25
27
  const isControlled = value !== undefined;
26
28
  const state = isControlled ? value : uncontrolledState;
27
29
 
30
+ // Call onChange after uncontrolled state changes via useEffect to avoid calling
31
+ // setState on another component during a state updater (which React flags as
32
+ // "setState during render" warning in development mode).
33
+ const prevUncontrolledStateRef = useRef(uncontrolledState);
34
+ useEffect(() => {
35
+ if (prevUncontrolledStateRef.current !== uncontrolledState) {
36
+ onChangeState(uncontrolledState);
37
+ prevUncontrolledStateRef.current = uncontrolledState;
38
+ }
39
+ });
40
+
28
41
  const setState = useCallback<Dispatch<SetStateAction<TState>>>(
29
42
  (nextValue) => {
30
43
  if (isControlled) {
@@ -36,11 +49,8 @@ export const useControllableState = <TState>({
36
49
  onChangeState(nextState as TState);
37
50
  } else {
38
51
  setUncontrolledState((prevValue) => {
39
- // should it be in effect to be more predictable?
40
- if (prevValue !== nextValue) {
41
- onChangeState(nextValue as TState);
42
- }
43
- return nextValue as TState;
52
+ const nextState = nextValue instanceof Function ? nextValue(prevValue) : nextValue;
53
+ return nextState as TState;
44
54
  });
45
55
  }
46
56
  },
@@ -1,4 +1,5 @@
1
1
  import {useLayoutEffect, useRef} from "react";
2
+
2
3
  import {useCallbackRef} from "./use-callback-ref";
3
4
 
4
5
  type OnFocusOutside = (event: FocusEvent) => void;
@@ -1,6 +1,7 @@
1
- import {useCallbackRef} from "./use-callback-ref";
2
1
  import {useEffect, useRef} from "react";
3
2
 
3
+ import {useCallbackRef} from "./use-callback-ref";
4
+
4
5
  /**
5
6
  * Inspired by radix
6
7
  * https://github.com/radix-ui/primitives/blob/c31c97274ff357aea99afe6c01c1c8c58b6356e0/packages/react/dismissable-layer/src/DismissableLayer.tsx#L223
@@ -1,8 +1,11 @@
1
- import {RefObject, useLayoutEffect} from "react";
1
+ import type {ResizeListenerCallback} from "@fibery/helpers/utils/resize-listener";
2
+ import {getResizeListener} from "@fibery/helpers/utils/resize-listener";
3
+ import type {RefObject} from "react";
4
+ import {useLayoutEffect} from "react";
2
5
  import {flushSync} from "react-dom";
6
+
3
7
  import {getRefValue} from "./get-ref-value";
4
8
  import {useCallbackRef} from "./use-callback-ref";
5
- import {getResizeListener, ResizeListenerCallback} from "@fibery/helpers/utils/resize-listener";
6
9
 
7
10
  /**
8
11
  * React hook to observe provided element with ResizeObserver
package/src/use-size.ts CHANGED
@@ -1,4 +1,6 @@
1
- import {RefObject, useLayoutEffect, useState} from "react";
1
+ import type {RefObject} from "react";
2
+ import {useLayoutEffect, useState} from "react";
3
+
2
4
  import {getRefValue} from "./get-ref-value";
3
5
  import {useResizeObserver} from "./use-resize-observer";
4
6