Collapsible
nativeui/collapsibleFreeComponent
A collapsible 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/collapsible.jsonSource
1import * as React from "react";2import {3 View,4 Pressable,5 LayoutAnimation,6 Platform,7 UIManager,8} from "react-native";9import { cn } from "@/lib/utils";10import { Feather } from "@expo/vector-icons";1112// Enable layout animation for Android13if (Platform.OS === "android") {14 if (UIManager.setLayoutAnimationEnabledExperimental) {15 UIManager.setLayoutAnimationEnabledExperimental(true);16 }17}1819interface CollapsibleContextValue {20 open: boolean;21 toggle: () => void;22}2324const CollapsibleContext = React.createContext<CollapsibleContextValue | null>(25 null26);2728interface CollapsibleProps {29 children: React.ReactNode;30 open?: boolean;31 onOpenChange?: (open: boolean) => void;32 defaultOpen?: boolean;33 className?: string;34 disabled?: boolean;35}3637const Collapsible = React.forwardRef<View, CollapsibleProps>(38 (39 {40 children,41 className,42 open,43 onOpenChange,44 defaultOpen = false,45 disabled = false,46 ...props47 },48 ref49 ) => {50 const [isOpen, setIsOpen] = React.useState(51 open !== undefined ? open : defaultOpen52 );5354 const isControlled = open !== undefined;55 const currentOpen = isControlled ? open : isOpen;5657 const toggle = React.useCallback(() => {58 if (!disabled) {59 LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);6061 if (!isControlled) {62 setIsOpen(!currentOpen);63 }6465 if (onOpenChange) {66 onOpenChange(!currentOpen);67 }68 }69 }, [currentOpen, isControlled, onOpenChange, disabled]);7071 React.useEffect(() => {72 if (isControlled) {73 setIsOpen(open || false);74 }75 }, [open, isControlled]);7677 return (78 <CollapsibleContext.Provider value={{ open: currentOpen, toggle }}>79 <View80 ref={ref}81 className={cn("overflow-hidden", disabled && "opacity-50", className)}82 {...props}83 >84 {children}85 </View>86 </CollapsibleContext.Provider>87 );88 }89);9091Collapsible.displayName = "Collapsible";9293interface CollapsibleTriggerProps {94 children: React.ReactNode;95 className?: string;96 asChild?: boolean;97 icon?: boolean;98}99100const CollapsibleTrigger = React.forwardRef<View, CollapsibleTriggerProps>(101 ({ children, className, asChild, icon = true, ...props }, ref) => {102 const context = React.useContext(CollapsibleContext);103104 if (!context) {105 throw new Error("CollapsibleTrigger must be used within a Collapsible");106 }107108// … 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 |
