@octanejs/radix 0.1.28 → 0.1.29

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 (2) hide show
  1. package/package.json +5 -4
  2. package/src/use-previous.ts +18 -16
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@octanejs/radix",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
+ "sideEffects": false,
6
7
  "engines": {
7
8
  "node": ">=22.22.2"
8
9
  },
@@ -38,10 +39,10 @@
38
39
  },
39
40
  "dependencies": {
40
41
  "aria-hidden": "^1.2.6",
41
- "@octanejs/floating-ui": "0.1.28"
42
+ "@octanejs/floating-ui": "0.1.29"
42
43
  },
43
44
  "peerDependencies": {
44
- "octane": "0.1.29"
45
+ "octane": "0.1.30"
45
46
  },
46
47
  "devDependencies": {
47
48
  "@tsrx/react": "^0.2.56",
@@ -50,7 +51,7 @@
50
51
  "react": "^19.2.7",
51
52
  "react-dom": "^19.2.7",
52
53
  "vitest": "^4.1.10",
53
- "octane": "0.1.29"
54
+ "octane": "0.1.30"
54
55
  },
55
56
  "scripts": {
56
57
  "test": "vitest run"
@@ -1,28 +1,30 @@
1
1
  // Ported from @radix-ui/react-use-previous (source:
2
2
  // .radix-primitives/packages/react/use-previous/src/use-previous.tsx). Returns the
3
3
  // value from the previous render in which `value` differed from the current one.
4
- import { useMemo, useRef } from 'octane';
4
+ import { type LinkedStatePrevious, useLinkedState } from 'octane';
5
5
 
6
6
  import { S, splitSlot, subSlot } from './internal';
7
7
 
8
+ function previousCommittedValue<Value>(
9
+ current: Value,
10
+ previous: LinkedStatePrevious<Value, Value> | undefined,
11
+ ): Value {
12
+ return previous === undefined ? current : previous.source;
13
+ }
14
+
15
+ const PREVIOUS_VALUE_OPTIONS = {
16
+ sourceEqual: <Value>(previous: Value, next: Value) => previous === next,
17
+ };
18
+
8
19
  export function usePrevious<T>(...args: any[]): T {
9
20
  const [user, slotArg] = splitSlot(args);
10
21
  const slot = slotArg ?? S('usePrevious');
11
22
  const value = user[0] as T;
12
- const ref = useRef({ value, previous: value }, subSlot(slot, 'ref'));
13
-
14
- // We compare values before making an update to ensure that
15
- // a change has been made. This ensures the previous value is
16
- // persisted correctly between renders.
17
- return useMemo(
18
- () => {
19
- if (ref.current.value !== value) {
20
- ref.current.previous = ref.current.value;
21
- ref.current.value = value;
22
- }
23
- return ref.current.previous;
24
- },
25
- [value],
26
- subSlot(slot, 'm'),
23
+ const [previous] = useLinkedState<T, T>(
24
+ value,
25
+ previousCommittedValue,
26
+ PREVIOUS_VALUE_OPTIONS,
27
+ subSlot(slot, 'previous'),
27
28
  );
29
+ return previous;
28
30
  }