-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from ruru-m07/refactor-switch-component
refactor(component: switch): make Switch component self-dependent by removing @radix-ui/react-switch dependency
- Loading branch information
Showing
9 changed files
with
153 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
pnpm run --filter=./apps/www build:registry && pnpm prettier | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
echo "Running pre-commit hook..." | ||
if ! pnpm run --filter=./apps/www build:registry; then | ||
echo "Error: Failed to build registry. Commit aborted." | ||
exit 1 | ||
fi | ||
echo "Registry built successfully. Running Prettier..." | ||
if ! pnpm prettier; then | ||
echo "Error: Prettier formatting failed. Commit aborted." | ||
exit 1 | ||
fi | ||
echo "Pre-commit hook completed successfully." |
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
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
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 |
---|---|---|
@@ -1,38 +1,75 @@ | ||
import * as React from "react"; | ||
import * as SwitchPrimitives from "@radix-ui/react-switch"; | ||
"use client"; | ||
|
||
import * as React from "react"; | ||
import { cn } from "@/utils/cn"; | ||
|
||
interface SwitchProps extends React.ComponentPropsWithoutRef<"button"> { | ||
checked?: boolean; | ||
onCheckedChange?: (checked: boolean) => void; | ||
defaultChecked?: boolean; | ||
} | ||
|
||
/** | ||
* Switch component | ||
* | ||
* @param {string} className - Additional class names for the switch. | ||
* @param {React.Ref<React.ElementRef<typeof SwitchPrimitives.Root>>} ref - Forwarded ref. | ||
* @param {boolean} checked - Whether the switch is checked. | ||
* @param {(checked: boolean) => void} onCheckedChange - Callback when the switch is checked or unchecked. | ||
* @param {boolean} defaultChecked - Whether the switch is checked by default. | ||
* @param {React.Ref<HTMLButtonElement>} ref - Forwarded ref. | ||
* | ||
* @example | ||
* | ||
* ```tsx | ||
* <Switch /> | ||
* ``` | ||
*/ | ||
const Switch = React.forwardRef< | ||
React.ElementRef<typeof SwitchPrimitives.Root>, | ||
React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root> | ||
>(({ className, ...props }, ref) => ( | ||
<SwitchPrimitives.Root | ||
className={cn( | ||
"peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary bg-accent data-[state=unchecked]:bg-input", | ||
className, | ||
)} | ||
{...props} | ||
ref={ref} | ||
> | ||
<SwitchPrimitives.Thumb | ||
className={cn( | ||
"pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0", | ||
)} | ||
/> | ||
</SwitchPrimitives.Root> | ||
)); | ||
Switch.displayName = SwitchPrimitives.Root.displayName; | ||
const Switch = React.forwardRef<HTMLButtonElement, SwitchProps>( | ||
( | ||
{ className, checked, onCheckedChange, defaultChecked = false, ...props }, | ||
ref, | ||
) => { | ||
const [isCheckedInternal, setIsCheckedInternal] = | ||
React.useState(defaultChecked); | ||
|
||
const isControlled = checked !== undefined; | ||
const isCheckedValue = isControlled ? checked : isCheckedInternal; | ||
|
||
const handleClick = () => { | ||
const newChecked = !isCheckedValue; | ||
if (!isControlled) { | ||
setIsCheckedInternal(newChecked); | ||
} | ||
if (onCheckedChange) { | ||
onCheckedChange(newChecked); | ||
} | ||
}; | ||
|
||
return ( | ||
<button | ||
type="button" | ||
role="switch" | ||
aria-checked={isCheckedValue} | ||
className={cn( | ||
"peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50", | ||
isCheckedValue ? "bg-primary" : "bg-accent", | ||
className, | ||
)} | ||
onClick={handleClick} | ||
ref={ref} | ||
{...props} | ||
> | ||
<span | ||
className={cn( | ||
"pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform", | ||
isCheckedValue ? "translate-x-4" : "translate-x-0", | ||
)} | ||
/> | ||
</button> | ||
); | ||
}, | ||
); | ||
|
||
Switch.displayName = "Switch"; | ||
|
||
export { Switch }; |
Oops, something went wrong.