@vitus-labs/attrs 2.7.1 → 2.7.2

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 (3) hide show
  1. package/README.md +16 -0
  2. package/lib/index.js +21 -10
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -1,3 +1,19 @@
1
+ <!-- LOGO:BEGIN -->
2
+ <div align="center">
3
+ <a href="https://github.com/vitus-labs/ui-system">
4
+ <picture>
5
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/vitus-labs/ui-system/main/.github/assets/vitus-labs-mark-dark.svg">
6
+ <img alt="vitus·labs" src="https://raw.githubusercontent.com/vitus-labs/ui-system/main/.github/assets/vitus-labs-mark-light.svg" height="64">
7
+ </picture>
8
+ </a>
9
+ &nbsp;&nbsp;&nbsp;&nbsp;
10
+ <picture>
11
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/vitus-labs/ui-system/main/packages/attrs/assets/logo-dark.svg">
12
+ <img alt="@vitus-labs/attrs" src="https://raw.githubusercontent.com/vitus-labs/ui-system/main/packages/attrs/assets/logo-light.svg" height="64">
13
+ </picture>
14
+ </div>
15
+ <!-- LOGO:END -->
16
+
1
17
  # @vitus-labs/attrs
2
18
 
3
19
  Immutable, chainable default-props factory for React components.
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { compose, hoistNonReactStatics, isEmpty, omit, pick, useStableValue } from "@vitus-labs/core";
2
- import { memo, useImperativeHandle, useMemo, useRef } from "react";
2
+ import { memo, useCallback, useMemo } from "react";
3
3
  import { jsx } from "react/jsx-runtime";
4
4
 
5
5
  //#region src/utils/attrs.ts
@@ -66,22 +66,33 @@ const createAttrsHOC = ({ attrs, priorityAttrs }) => {
66
66
 
67
67
  //#endregion
68
68
  //#region src/hooks/useRef.ts
69
+ const assignRef = (target, node) => {
70
+ if (!target) return;
71
+ if (typeof target === "function") target(node);
72
+ else target.current = node;
73
+ };
69
74
  /**
70
- * Unifies two ref sources into a single internal ref.
75
+ * Unifies two ref sources into a single merged callback ref.
71
76
  *
72
77
  * The attrs HOC chain creates two refs that both need to reach the same DOM node:
73
78
  * - `$attrsRef`: the consumer's original ref, forwarded through attrsHoc
74
79
  * - `ref`: a ref from any intermediate HOC added via `.compose()`
75
80
  *
76
- * This hook creates one internal ref and wires both forwarded refs to it
77
- * via `useImperativeHandle`, so `consumer.ref.current === hoc.ref.current`.
81
+ * A CALLBACK ref (not `useImperativeHandle`) is essential here: React
82
+ * re-invokes it on every attach/detach, so both forwarded refs always track
83
+ * the live node — including across host remounts (e.g. a `tag` change from
84
+ * `div` to `button`). The previous `useImperativeHandle(ref, () =>
85
+ * internalRef.current, [])` implementation snapshotted `.current` once at
86
+ * mount, leaving consumers holding the detached old node after a remount.
87
+ *
88
+ * Deps on the forwarded refs: if a consumer passes a new ref identity,
89
+ * React detaches the old callback (null) and attaches the new one (node),
90
+ * keeping both ref generations correct.
78
91
  */
79
- const useAttrsStyleRef = ({ $attrsRef, ref }) => {
80
- const internalRef = useRef(null);
81
- useImperativeHandle($attrsRef, () => internalRef.current, []);
82
- useImperativeHandle(ref, () => internalRef.current, []);
83
- return internalRef;
84
- };
92
+ const useAttrsStyleRef = ({ $attrsRef, ref }) => useCallback((node) => {
93
+ assignRef($attrsRef, node);
94
+ assignRef(ref, node);
95
+ }, [$attrsRef, ref]);
85
96
 
86
97
  //#endregion
87
98
  //#region src/utils/chaining.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitus-labs/attrs",
3
- "version": "2.7.1",
3
+ "version": "2.7.2",
4
4
  "license": "MIT",
5
5
  "author": "Vit Bokisch <vit@bokisch.cz>",
6
6
  "maintainers": [
@@ -59,7 +59,7 @@
59
59
  "typecheck": "tsc --noEmit"
60
60
  },
61
61
  "peerDependencies": {
62
- "@vitus-labs/core": "^2.7.1",
62
+ "@vitus-labs/core": "^2.7.2",
63
63
  "react": ">= 19"
64
64
  },
65
65
  "devDependencies": {
@@ -71,6 +71,6 @@
71
71
  "@vitus-labs/tools-storybook": "2.5.0",
72
72
  "@vitus-labs/tools-typescript": "2.5.0",
73
73
  "jsdom": "^29.1.1",
74
- "tinybench": "^6.0.1"
74
+ "tinybench": "^6.0.2"
75
75
  }
76
76
  }