-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace private Radix UI hook with public useControllableState
- Loading branch information
Showing
7 changed files
with
96 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as React from "react" | ||
|
||
/** | ||
* @see https://github.com/radix-ui/primitives/blob/main/packages/react/use-callback-ref/src/useCallbackRef.tsx | ||
*/ | ||
|
||
/** | ||
* A custom hook that converts a callback to a ref to avoid triggering re-renders when passed as a | ||
* prop or avoid re-executing effects when passed as a dependency | ||
*/ | ||
function useCallbackRef<T extends (...args: never[]) => unknown>( | ||
callback: T | undefined | ||
): T { | ||
const callbackRef = React.useRef(callback) | ||
|
||
React.useEffect(() => { | ||
callbackRef.current = callback | ||
}) | ||
|
||
// https://github.com/facebook/react/issues/19240 | ||
return React.useMemo( | ||
() => ((...args) => callbackRef.current?.(...args)) as T, | ||
[] | ||
) | ||
} | ||
|
||
export { useCallbackRef } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as React from "react" | ||
|
||
import { useCallbackRef } from "@/hooks/use-callback-ref" | ||
|
||
/** | ||
* @see https://github.com/radix-ui/primitives/blob/main/packages/react/use-controllable-state/src/useControllableState.tsx | ||
*/ | ||
|
||
type UseControllableStateParams<T> = { | ||
prop?: T | undefined | ||
defaultProp?: T | undefined | ||
onChange?: (state: T) => void | ||
} | ||
|
||
type SetStateFn<T> = (prevState?: T) => T | ||
|
||
function useControllableState<T>({ | ||
prop, | ||
defaultProp, | ||
onChange = () => {}, | ||
}: UseControllableStateParams<T>) { | ||
const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ | ||
defaultProp, | ||
onChange, | ||
}) | ||
const isControlled = prop !== undefined | ||
const value = isControlled ? prop : uncontrolledProp | ||
const handleChange = useCallbackRef(onChange) | ||
|
||
const setValue: React.Dispatch<React.SetStateAction<T | undefined>> = | ||
React.useCallback( | ||
(nextValue) => { | ||
if (isControlled) { | ||
const setter = nextValue as SetStateFn<T> | ||
const value = | ||
typeof nextValue === "function" ? setter(prop) : nextValue | ||
if (value !== prop) handleChange(value as T) | ||
} else { | ||
setUncontrolledProp(nextValue) | ||
} | ||
}, | ||
[isControlled, prop, setUncontrolledProp, handleChange] | ||
) | ||
|
||
return [value, setValue] as const | ||
} | ||
|
||
function useUncontrolledState<T>({ | ||
defaultProp, | ||
onChange, | ||
}: Omit<UseControllableStateParams<T>, "prop">) { | ||
const uncontrolledState = React.useState<T | undefined>(defaultProp) | ||
const [value] = uncontrolledState | ||
const prevValueRef = React.useRef(value) | ||
const handleChange = useCallbackRef(onChange) | ||
|
||
React.useEffect(() => { | ||
if (prevValueRef.current !== value) { | ||
handleChange(value as T) | ||
prevValueRef.current = value | ||
} | ||
}, [value, prevValueRef, handleChange]) | ||
|
||
return uncontrolledState | ||
} | ||
|
||
export { useControllableState } |
This file was deleted.
Oops, something went wrong.