@radix-ui/react-compose-refs 0.0.0-20250116175529

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/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # `react-compose-refs`
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ yarn add @radix-ui/react-compose-refs
7
+ # or
8
+ $ npm install @radix-ui/react-compose-refs
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ This is an internal utility, not intended for public usage.
@@ -0,0 +1,15 @@
1
+ import * as React from 'react';
2
+
3
+ type PossibleRef<T> = React.Ref<T> | undefined;
4
+ /**
5
+ * A utility to compose multiple refs together
6
+ * Accepts callback refs and RefObject(s)
7
+ */
8
+ declare function composeRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T>;
9
+ /**
10
+ * A custom hook that composes multiple refs
11
+ * Accepts callback refs and RefObject(s)
12
+ */
13
+ declare function useComposedRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T>;
14
+
15
+ export { composeRefs, useComposedRefs };
@@ -0,0 +1,15 @@
1
+ import * as React from 'react';
2
+
3
+ type PossibleRef<T> = React.Ref<T> | undefined;
4
+ /**
5
+ * A utility to compose multiple refs together
6
+ * Accepts callback refs and RefObject(s)
7
+ */
8
+ declare function composeRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T>;
9
+ /**
10
+ * A custom hook that composes multiple refs
11
+ * Accepts callback refs and RefObject(s)
12
+ */
13
+ declare function useComposedRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T>;
14
+
15
+ export { composeRefs, useComposedRefs };
package/dist/index.js ADDED
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // packages/react/compose-refs/src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ composeRefs: () => composeRefs,
34
+ useComposedRefs: () => useComposedRefs
35
+ });
36
+ module.exports = __toCommonJS(src_exports);
37
+
38
+ // packages/react/compose-refs/src/composeRefs.tsx
39
+ var React = __toESM(require("react"));
40
+ function setRef(ref, value) {
41
+ if (typeof ref === "function") {
42
+ return ref(value);
43
+ } else if (ref !== null && ref !== void 0) {
44
+ ref.current = value;
45
+ }
46
+ }
47
+ function composeRefs(...refs) {
48
+ return (node) => {
49
+ let hasCleanup = false;
50
+ const cleanups = refs.map((ref) => {
51
+ const cleanup = setRef(ref, node);
52
+ if (!hasCleanup && typeof cleanup == "function") {
53
+ hasCleanup = true;
54
+ }
55
+ return cleanup;
56
+ });
57
+ if (hasCleanup) {
58
+ return () => {
59
+ for (let i = 0; i < cleanups.length; i++) {
60
+ const cleanup = cleanups[i];
61
+ if (typeof cleanup == "function") {
62
+ cleanup();
63
+ } else {
64
+ setRef(refs[i], null);
65
+ }
66
+ }
67
+ };
68
+ }
69
+ };
70
+ }
71
+ function useComposedRefs(...refs) {
72
+ return React.useCallback(composeRefs(...refs), refs);
73
+ }
74
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts", "../src/composeRefs.tsx"],
4
+ "sourcesContent": ["export { composeRefs, useComposedRefs } from './composeRefs';\n", "import * as React from 'react';\n\ntype PossibleRef<T> = React.Ref<T> | undefined;\n\n/**\n * Set a given ref to a given value\n * This utility takes care of different types of refs: callback refs and RefObject(s)\n */\nfunction setRef<T>(ref: PossibleRef<T>, value: T) {\n if (typeof ref === 'function') {\n return ref(value);\n } else if (ref !== null && ref !== undefined) {\n ref.current = value;\n }\n}\n\n/**\n * A utility to compose multiple refs together\n * Accepts callback refs and RefObject(s)\n */\nfunction composeRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T> {\n return (node) => {\n let hasCleanup = false;\n const cleanups = refs.map((ref) => {\n const cleanup = setRef(ref, node);\n if (!hasCleanup && typeof cleanup == 'function') {\n hasCleanup = true;\n }\n return cleanup;\n });\n\n // React <19 will log an error to the console if a callback ref returns a\n // value. We don't use ref cleanups internally so this will only happen if a\n // user's ref callback returns a value, which we only expect if they are\n // using the cleanup functionality added in React 19.\n if (hasCleanup) {\n return () => {\n for (let i = 0; i < cleanups.length; i++) {\n const cleanup = cleanups[i];\n if (typeof cleanup == 'function') {\n cleanup();\n } else {\n setRef(refs[i], null);\n }\n }\n };\n }\n };\n}\n\n/**\n * A custom hook that composes multiple refs\n * Accepts callback refs and RefObject(s)\n */\nfunction useComposedRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T> {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n return React.useCallback(composeRefs(...refs), refs);\n}\n\nexport { composeRefs, useComposedRefs };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AAQvB,SAAS,OAAU,KAAqB,OAAU;AAChD,MAAI,OAAO,QAAQ,YAAY;AAC7B,WAAO,IAAI,KAAK;AAAA,EAClB,WAAW,QAAQ,QAAQ,QAAQ,QAAW;AAC5C,QAAI,UAAU;AAAA,EAChB;AACF;AAMA,SAAS,eAAkB,MAA8C;AACvE,SAAO,CAAC,SAAS;AACf,QAAI,aAAa;AACjB,UAAM,WAAW,KAAK,IAAI,CAAC,QAAQ;AACjC,YAAM,UAAU,OAAO,KAAK,IAAI;AAChC,UAAI,CAAC,cAAc,OAAO,WAAW,YAAY;AAC/C,qBAAa;AAAA,MACf;AACA,aAAO;AAAA,IACT,CAAC;AAMD,QAAI,YAAY;AACd,aAAO,MAAM;AACX,iBAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;AACxC,gBAAM,UAAU,SAAS,CAAC;AAC1B,cAAI,OAAO,WAAW,YAAY;AAChC,oBAAQ;AAAA,UACV,OAAO;AACL,mBAAO,KAAK,CAAC,GAAG,IAAI;AAAA,UACtB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAMA,SAAS,mBAAsB,MAA8C;AAE3E,SAAa,kBAAY,YAAY,GAAG,IAAI,GAAG,IAAI;AACrD;",
6
+ "names": []
7
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,41 @@
1
+ // packages/react/compose-refs/src/composeRefs.tsx
2
+ import * as React from "react";
3
+ function setRef(ref, value) {
4
+ if (typeof ref === "function") {
5
+ return ref(value);
6
+ } else if (ref !== null && ref !== void 0) {
7
+ ref.current = value;
8
+ }
9
+ }
10
+ function composeRefs(...refs) {
11
+ return (node) => {
12
+ let hasCleanup = false;
13
+ const cleanups = refs.map((ref) => {
14
+ const cleanup = setRef(ref, node);
15
+ if (!hasCleanup && typeof cleanup == "function") {
16
+ hasCleanup = true;
17
+ }
18
+ return cleanup;
19
+ });
20
+ if (hasCleanup) {
21
+ return () => {
22
+ for (let i = 0; i < cleanups.length; i++) {
23
+ const cleanup = cleanups[i];
24
+ if (typeof cleanup == "function") {
25
+ cleanup();
26
+ } else {
27
+ setRef(refs[i], null);
28
+ }
29
+ }
30
+ };
31
+ }
32
+ };
33
+ }
34
+ function useComposedRefs(...refs) {
35
+ return React.useCallback(composeRefs(...refs), refs);
36
+ }
37
+ export {
38
+ composeRefs,
39
+ useComposedRefs
40
+ };
41
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/composeRefs.tsx"],
4
+ "sourcesContent": ["import * as React from 'react';\n\ntype PossibleRef<T> = React.Ref<T> | undefined;\n\n/**\n * Set a given ref to a given value\n * This utility takes care of different types of refs: callback refs and RefObject(s)\n */\nfunction setRef<T>(ref: PossibleRef<T>, value: T) {\n if (typeof ref === 'function') {\n return ref(value);\n } else if (ref !== null && ref !== undefined) {\n ref.current = value;\n }\n}\n\n/**\n * A utility to compose multiple refs together\n * Accepts callback refs and RefObject(s)\n */\nfunction composeRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T> {\n return (node) => {\n let hasCleanup = false;\n const cleanups = refs.map((ref) => {\n const cleanup = setRef(ref, node);\n if (!hasCleanup && typeof cleanup == 'function') {\n hasCleanup = true;\n }\n return cleanup;\n });\n\n // React <19 will log an error to the console if a callback ref returns a\n // value. We don't use ref cleanups internally so this will only happen if a\n // user's ref callback returns a value, which we only expect if they are\n // using the cleanup functionality added in React 19.\n if (hasCleanup) {\n return () => {\n for (let i = 0; i < cleanups.length; i++) {\n const cleanup = cleanups[i];\n if (typeof cleanup == 'function') {\n cleanup();\n } else {\n setRef(refs[i], null);\n }\n }\n };\n }\n };\n}\n\n/**\n * A custom hook that composes multiple refs\n * Accepts callback refs and RefObject(s)\n */\nfunction useComposedRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T> {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n return React.useCallback(composeRefs(...refs), refs);\n}\n\nexport { composeRefs, useComposedRefs };\n"],
5
+ "mappings": ";AAAA,YAAY,WAAW;AAQvB,SAAS,OAAU,KAAqB,OAAU;AAChD,MAAI,OAAO,QAAQ,YAAY;AAC7B,WAAO,IAAI,KAAK;AAAA,EAClB,WAAW,QAAQ,QAAQ,QAAQ,QAAW;AAC5C,QAAI,UAAU;AAAA,EAChB;AACF;AAMA,SAAS,eAAkB,MAA8C;AACvE,SAAO,CAAC,SAAS;AACf,QAAI,aAAa;AACjB,UAAM,WAAW,KAAK,IAAI,CAAC,QAAQ;AACjC,YAAM,UAAU,OAAO,KAAK,IAAI;AAChC,UAAI,CAAC,cAAc,OAAO,WAAW,YAAY;AAC/C,qBAAa;AAAA,MACf;AACA,aAAO;AAAA,IACT,CAAC;AAMD,QAAI,YAAY;AACd,aAAO,MAAM;AACX,iBAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;AACxC,gBAAM,UAAU,SAAS,CAAC;AAC1B,cAAI,OAAO,WAAW,YAAY;AAChC,oBAAQ;AAAA,UACV,OAAO;AACL,mBAAO,KAAK,CAAC,GAAG,IAAI;AAAA,UACtB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAMA,SAAS,mBAAsB,MAA8C;AAE3E,SAAa,kBAAY,YAAY,GAAG,IAAI,GAAG,IAAI;AACrD;",
6
+ "names": []
7
+ }
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@radix-ui/react-compose-refs",
3
+ "version": "0.0.0-20250116175529",
4
+ "license": "MIT",
5
+ "exports": {
6
+ ".": {
7
+ "import": {
8
+ "types": "./dist/index.d.mts",
9
+ "default": "./dist/index.mjs"
10
+ },
11
+ "require": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ }
15
+ }
16
+ },
17
+ "source": "./src/index.ts",
18
+ "main": "./dist/index.js",
19
+ "module": "./dist/index.mjs",
20
+ "types": "./dist/index.d.ts",
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "sideEffects": false,
26
+ "scripts": {
27
+ "clean": "rm -rf dist"
28
+ },
29
+ "peerDependencies": {
30
+ "@types/react": "*",
31
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
32
+ },
33
+ "peerDependenciesMeta": {
34
+ "@types/react": {
35
+ "optional": true
36
+ }
37
+ },
38
+ "homepage": "https://radix-ui.com/primitives",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/radix-ui/primitives.git"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/radix-ui/primitives/issues"
45
+ }
46
+ }