Toggle
nativeui/toggleFreeComponent
A toggle component for React Native applications.
Live preview
live · rendered by the registry
Rendered by nativeui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://nativeui.ribbi.ch/r/toggle.jsonSource
1import { cn } from "@/lib/utils";2import * as React from "react";3import { Platform, Pressable, View } from "react-native";45interface ToggleProps {6 pressed?: boolean;7 onPressedChange?: (pressed: boolean) => void;8 disabled?: boolean;9 children?: React.ReactNode;10 className?: string;11 variant?: "default" | "outline";12 size?: "default" | "sm" | "lg";13}1415const Toggle = React.forwardRef<View, ToggleProps>(16 (17 {18 pressed,19 onPressedChange,20 disabled,21 children,22 className,23 variant = "default",24 size = "default",25 ...props26 },27 ref28 ) => {29 const [isPressed, setIsPressed] = React.useState(pressed);3031 React.useEffect(() => {32 setIsPressed(pressed);33 }, [pressed]);3435 const handlePress = () => {36 if (disabled) return;37 const newValue = !isPressed;38 setIsPressed(newValue);39 onPressedChange?.(newValue);40 };4142 const getSizeStyles = () => {43 switch (size) {44 case "sm":45 return Platform.OS === "ios" ? "h-10 px-3" : "h-12 px-3";46 case "lg":47 return Platform.OS === "ios" ? "h-12 px-4" : "h-14 px-4";48 default:49 return Platform.OS === "ios" ? "h-11 px-3.5" : "h-13 px-3.5";50 }51 };5253 const getVariantStyles = () => {54 switch (variant) {55 case "outline":56 return isPressed57 ? "border border-toggle-border bg-toggle-active/20"58 : "border border-toggle-border bg-background/10";59 default:60 return isPressed61 ? "bg-toggle-active/20"62 : "bg-background/10";63 }64 };6566 return (67 <Pressable68 ref={ref}69 onPress={handlePress}70 disabled={disabled}71 className={cn(72 "flex-row items-center justify-center rounded-lg",73 getSizeStyles(),74 getVariantStyles(),75 isPressed76 ? "text-toggle-active-foreground"77 : "text-foreground",78 disabled && "opacity-50 bg-muted text-muted-foreground",79 className80 )}81 {...props}82 >83 {children}84 </Pressable>85 );86 }87);8889Toggle.displayName = "Toggle";9091export { Toggle };
What it pulls in
npm packages
Files it writes
More from nativeui
All 29 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | nativeui | Components | Free | 2 deps | |
| Alertalert | nativeui | Components | Free | 2 deps | |
| Alert Dialogalert-dialog | nativeui | Components | Free | 1 dep | |
| Avataravatar | nativeui | Components | Free | 1 dep | |
| Badgebadge | nativeui | Components | Free | 2 deps | |
| Breadcrumbbreadcrumb | nativeui | Components | Free | 2 deps | |
| Buttonbutton | nativeui | Components | Free | 2 deps | |
| Calendarcalendar | nativeui | Components | Free | 4 deps |
