@sanity/ui 2.6.8-canary.0 → 2.7.0

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.
@@ -1,5 +1,4 @@
1
- import {useMemo, useSyncExternalStore} from 'react'
2
- import {useUnique} from '../../hooks/_internal'
1
+ import {useMemo, useRef, useSyncExternalStore} from 'react'
3
2
  import {PortalContext} from './portalContext'
4
3
  import {PortalContextValue} from './types'
5
4
 
@@ -44,3 +43,34 @@ export function PortalProvider(props: PortalProviderProps): React.ReactElement {
44
43
  }
45
44
 
46
45
  const emptySubscribe = () => () => {}
46
+
47
+ /**
48
+ * This is a React hook to make sure that a record identity is the same on every render. Uses strict
49
+ * equality comparison (eg by identity), and only goes one level deep.
50
+ */
51
+ function useUnique<ValueType extends Comparable = Comparable>(value: ValueType): ValueType {
52
+ const valueRef = useRef<ValueType>(value)
53
+
54
+ if (!_isEqual(valueRef.current, value)) {
55
+ valueRef.current = value
56
+ }
57
+
58
+ return valueRef.current
59
+ }
60
+
61
+ function _isEqual(objA: Comparable, objB: Comparable): boolean {
62
+ if (!objA || !objB) {
63
+ return objA === objB
64
+ }
65
+
66
+ const keysA = Object.keys(objA)
67
+ const keysB = Object.keys(objB)
68
+
69
+ if (keysA.length !== keysB.length) {
70
+ return false
71
+ }
72
+
73
+ return keysA.every((key) => objA[key] === objB[key])
74
+ }
75
+
76
+ type Comparable = Record<string | number | symbol, unknown> | undefined | null
@@ -1 +0,0 @@
1
- export * from './useUnique'
@@ -1,34 +0,0 @@
1
- import {useRef} from 'react'
2
-
3
- /**
4
- * This is a React hook to make sure that a record identity is the same on every render. Uses strict
5
- * equality comparison (eg by identity), and only goes one level deep.
6
- *
7
- * @internal
8
- */
9
- type Comparable = Record<string | number | symbol, unknown> | undefined | null
10
-
11
- export function useUnique<ValueType extends Comparable = Comparable>(value: ValueType): ValueType {
12
- const valueRef = useRef<ValueType>(value)
13
-
14
- if (!_isEqual(valueRef.current, value)) {
15
- valueRef.current = value
16
- }
17
-
18
- return valueRef.current
19
- }
20
-
21
- function _isEqual(objA: Comparable, objB: Comparable): boolean {
22
- if (!objA || !objB) {
23
- return objA === objB
24
- }
25
-
26
- const keysA = Object.keys(objA)
27
- const keysB = Object.keys(objB)
28
-
29
- if (keysA.length !== keysB.length) {
30
- return false
31
- }
32
-
33
- return keysA.every((key) => objA[key] === objB[key])
34
- }