Toggle Group
nativeui/toggle-groupFreeComponent
A toggle group 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-group.jsonSource
1import * as React from "react";2import { View, Pressable, Platform } from "react-native";3import { cn } from "@/lib/utils";45interface ToggleGroupProps {6 type?: "single" | "multiple";7 value?: string | string[];8 onValueChange?: (value: string | string[]) => void;9 disabled?: boolean;10 children?: React.ReactNode;11 className?: string;12 variant?: "default" | "outline";13 size?: "default" | "sm" | "lg";14}1516interface ToggleGroupItemProps {17 value: string;18 disabled?: boolean;19 children?: React.ReactNode;20 className?: string;21 variant?: "default" | "outline";22 size?: "default" | "sm" | "lg";23}2425const ToggleGroupContext = React.createContext<{26 type: "single" | "multiple";27 value: string | string[];28 onValueChange?: (value: string | string[]) => void;29 disabled?: boolean;30 variant?: "default" | "outline";31 size?: "default" | "sm" | "lg";32}>({33 type: "single",34 value: "",35});3637const ToggleGroup = React.forwardRef<View, ToggleGroupProps>(38 (39 {40 type = "single",41 value,42 onValueChange,43 disabled,44 children,45 className,46 variant = "default",47 size = "default",48 ...props49 },50 ref51 ) => {52 return (53 <ToggleGroupContext.Provider54 value={{55 type,56 value: value || (type === "single" ? "" : []),57 onValueChange,58 disabled,59 variant,60 size,61 }}62 >63 <View64 ref={ref}65 className={cn(66 "flex-row items-center justify-center",67 Platform.OS === "ios" ? "gap-2" : "gap-1",68 className69 )}70 {...props}71 >72 {children}73 </View>74 </ToggleGroupContext.Provider>75 );76 }77);7879ToggleGroup.displayName = "ToggleGroup";8081const ToggleGroupItem = React.forwardRef<View, ToggleGroupItemProps>(82 ({ value, disabled, children, className, variant, size, ...props }, ref) => {83 const context = React.useContext(ToggleGroupContext);84 const isSelected =85 context.type === "single"86 ? context.value === value87 : (context.value as string[]).includes(value);8889 const handlePress = () => {90 if (disabled || context.disabled) return;9192 if (context.type === "single") {93 context.onValueChange?.(value);94 } else {95 const currentValue = context.value as string[];96 const newValue = currentValue.includes(value)97 ? currentValue.filter((v) => v !== value)98// … truncated
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 |
