@tamagui/compose-refs 1.0.1-beta.59 → 1.0.1-beta.60

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": "@tamagui/compose-refs",
3
- "version": "1.0.1-beta.59",
3
+ "version": "1.0.1-beta.60",
4
4
  "sideEffects": false,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
@@ -8,6 +8,7 @@
8
8
  "module": "dist/esm",
9
9
  "module:jsx": "dist/jsx",
10
10
  "files": [
11
+ "src",
11
12
  "types",
12
13
  "dist"
13
14
  ],
@@ -19,7 +20,7 @@
19
20
  "react": "*"
20
21
  },
21
22
  "devDependencies": {
22
- "@tamagui/build": "^1.0.1-beta.59",
23
+ "@tamagui/build": "^1.0.1-beta.60",
23
24
  "react": "*"
24
25
  },
25
26
  "publishConfig": {
@@ -0,0 +1,37 @@
1
+ // from radix
2
+ // https://raw.githubusercontent.com/radix-ui/primitives/main/packages/react/compose-refs/src/composeRefs.tsx
3
+
4
+ import * as React from 'react'
5
+
6
+ type PossibleRef<T> = React.Ref<T> | undefined
7
+
8
+ /**
9
+ * Set a given ref to a given value
10
+ * This utility takes care of different types of refs: callback refs and RefObject(s)
11
+ */
12
+ function setRef<T>(ref: PossibleRef<T>, value: T) {
13
+ if (typeof ref === 'function') {
14
+ ref(value)
15
+ } else if (ref !== null && ref !== undefined) {
16
+ ;(ref as React.MutableRefObject<T>).current = value
17
+ }
18
+ }
19
+
20
+ /**
21
+ * A utility to compose multiple refs together
22
+ * Accepts callback refs and RefObject(s)
23
+ */
24
+ function composeRefs<T>(...refs: PossibleRef<T>[]) {
25
+ return (node: T) => refs.forEach((ref) => setRef(ref, node))
26
+ }
27
+
28
+ /**
29
+ * A custom hook that composes multiple refs
30
+ * Accepts callback refs and RefObject(s)
31
+ */
32
+ function useComposedRefs<T>(...refs: PossibleRef<T>[]) {
33
+ // eslint-disable-next-line react-hooks/exhaustive-deps
34
+ return React.useCallback(composeRefs(...refs), refs)
35
+ }
36
+
37
+ export { composeRefs, useComposedRefs }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './compose-refs'