Skip to content

Commit ed8f5f6

Browse files
committed
refactor: optimize useMove and useControllableState
1 parent f6a87aa commit ed8f5f6

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

.changeset/nine-brooms-judge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@resolid/react-ui": patch
3+
---
4+
5+
refactor: optimize useMove and useControllableState

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"build:packages": "pnpm run --filter @resolid/* build",
1313
"build:vercel": "VERCEL=1 pnpm run --filter website... build",
1414
"dev": "pnpm run --filter website dev",
15+
"changeset": "changeset",
1516
"format": "oxfmt",
1617
"preinstall": "npx only-allow pnpm",
1718
"lint": "pnpm run -r --if-present lint",

packages/react-ui/src/hooks/use-controllable-state/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const useControllableState = <T>(
1515
): readonly [T, (value: SetStateAction<T>) => void] => {
1616
const { value, defaultValue, onChange, shouldUpdate = defaultShouldUpdate } = options;
1717

18-
const [uncontrolledState, setUncontrolledState] = useState(defaultValue as T);
18+
const [uncontrolledState, setUncontrolledState] = useState(() => defaultValue as T);
1919

2020
const controlled = value !== undefined;
2121
const currentValue = controlled ? value : uncontrolledState;

packages/react-ui/src/hooks/use-move/index.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export const useMove = <T extends HTMLElement = HTMLDivElement>(
2525
const frame = useRef(0);
2626
const mounted = useRef(false);
2727
const sliding = useRef(false);
28-
const cleanup = useRef<(() => void) | null>(null);
2928

3029
const [active, setActive] = useState(false);
3130

@@ -37,15 +36,6 @@ export const useMove = <T extends HTMLElement = HTMLDivElement>(
3736

3837
const ref = useCallback(
3938
(node: T | null) => {
40-
if (cleanup.current) {
41-
cleanup.current();
42-
cleanup.current = null;
43-
}
44-
45-
if (!node) {
46-
return;
47-
}
48-
4939
const startScrubbing = () => {
5040
if (!sliding.current && mounted.current) {
5141
sliding.current = true;
@@ -125,12 +115,14 @@ export const useMove = <T extends HTMLElement = HTMLDivElement>(
125115
document.removeEventListener("touchend", stopScrubbing);
126116
};
127117

128-
node.addEventListener("mousedown", handleMouseDown);
129-
node.addEventListener("touchstart", handleTouchStart, { passive: false });
118+
node?.addEventListener("mousedown", handleMouseDown);
119+
node?.addEventListener("touchstart", handleTouchStart, { passive: false });
130120

131-
cleanup.current = () => {
132-
node.removeEventListener("mousedown", handleMouseDown);
133-
node.removeEventListener("touchstart", handleTouchStart);
121+
return () => {
122+
if (node) {
123+
node.removeEventListener("mousedown", handleMouseDown);
124+
node.removeEventListener("touchstart", handleTouchStart);
125+
}
134126
};
135127
},
136128
[direction, onChange, onScrubEnd, onScrubStart],

0 commit comments

Comments
 (0)