-
-
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.
refactor(component: switch): make Switch component self-dependent by …
…removing @radix-ui/react-switch dependency Fixes #63 Refactor the `Switch` component to be self-contained and remove dependency on `@radix-ui/react-switch`. * **Switch Component**: - Remove import statement for `@radix-ui/react-switch`. - Update the `Switch` component to be self-contained with its own state management and event handling. - Update the `Switch` component's props to include `checked` and `onCheckedChange`. * **Documentation**: - Update the documentation in `apps/www/content/docs/components/switch.mdx` to reference the self-contained `Switch` component. - Add a new props table to the documentation to reflect the updated `Switch` component's props. * **Dependencies**: - Remove `@radix-ui/react-switch` from `packages/ui/package.json`. - Update `pnpm-lock.yaml` to remove references to `@radix-ui/react-switch`. * **Registry**: - Update `apps/www/public/registry/index.json` and `apps/www/public/registry/components/switch.json` to remove the dependency on `@radix-ui/react-switch`. - Update `apps/www/registry/ui.ts` to remove the dependency on `@radix-ui/react-switch`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ruru-m07/ruru-ui/issues/63?shareId=XXXX-XXXX-XXXX-XXXX).
- Loading branch information
Showing
7 changed files
with
116 additions
and
79 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
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,62 @@ | ||
import * as React from "react"; | ||
import * as SwitchPrimitives from "@radix-ui/react-switch"; | ||
|
||
import { cn } from "@/utils/cn"; | ||
|
||
interface SwitchProps extends React.ComponentPropsWithoutRef<"button"> { | ||
checked?: boolean; | ||
onCheckedChange?: (checked: boolean) => void; | ||
} | ||
|
||
/** | ||
* 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 {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, ...props }, ref) => { | ||
const [isChecked, setIsChecked] = React.useState(checked || false); | ||
|
||
const handleClick = () => { | ||
const newChecked = !isChecked; | ||
setIsChecked(newChecked); | ||
if (onCheckedChange) { | ||
onCheckedChange(newChecked); | ||
} | ||
}; | ||
|
||
return ( | ||
<button | ||
type="button" | ||
role="switch" | ||
aria-checked={isChecked} | ||
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", | ||
isChecked ? "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", | ||
isChecked ? "translate-x-4" : "translate-x-0" | ||
)} | ||
/> | ||
</button> | ||
); | ||
} | ||
); | ||
|
||
Switch.displayName = "Switch"; | ||
|
||
export { Switch }; |
Oops, something went wrong.