Accordion
nativeui/accordionFreeComponent
An accordion 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/accordion.jsonSource
1import * as React from 'react';2import { Pressable, View, Text, LayoutAnimation, Platform, UIManager } from 'react-native';3import { Feather } from '@expo/vector-icons';4import { cn } from '@/lib/utils';56// Enable layout animation for Android7if (Platform.OS === 'android') {8 if (UIManager.setLayoutAnimationEnabledExperimental) {9 UIManager.setLayoutAnimationEnabledExperimental(true);10 }11}1213interface AccordionContextValue {14 value: string[];15 onValueChange: (itemValue: string) => void;16 type: 'single' | 'multiple';17}1819const AccordionContext = React.createContext<AccordionContextValue | null>(null);2021export interface AccordionProps {22 type?: 'single' | 'multiple';23 collapsible?: boolean;24 value?: string[];25 onValueChange?: (value: string[]) => void;26 defaultValue?: string[];27 className?: string;28 children: React.ReactNode;29}3031const Accordion = ({32 type = 'single',33 collapsible = false,34 value,35 onValueChange,36 defaultValue,37 className,38 children,39}: AccordionProps) => {40 const [state, setState] = React.useState<string[]>(value || defaultValue || []);4142 const isControlled = value !== undefined;43 const accordionValue = isControlled ? value : state;4445 const handleValueChange = React.useCallback((itemValue: string) => {46 const isSelected = accordionValue.includes(itemValue);4748 let newValue: string[] = [];4950 if (type === 'single') {51 if (isSelected) {52 newValue = collapsible ? [] : [itemValue];53 } else {54 newValue = [itemValue];55 }56 } else {57 if (isSelected) {58 newValue = accordionValue.filter((v) => v !== itemValue);59 } else {60 newValue = [...accordionValue, itemValue];61 }62 }6364 if (!isControlled) {65 setState(newValue);66 }6768 onValueChange?.(newValue);69 }, [accordionValue, collapsible, isControlled, onValueChange, type]);7071 return (72 <AccordionContext.Provider value={{ value: accordionValue, onValueChange: handleValueChange, type }}>73 <View className={cn("w-full", className)}>74 {children}75 </View>76 </AccordionContext.Provider>77 );78};7980interface AccordionItemProps {81 value: string;82 className?: string;83 children: React.ReactNode;84}8586const AccordionItem = ({ value, className, children }: AccordionItemProps) => {87 const context = React.useContext(AccordionContext);8889 if (!context) {90 throw new Error('AccordionItem must be used within an Accordion');91 }9293 const isExpanded = context.value.includes(value);9495 return (96// … truncated
What it pulls in
npm packages
Files it writes
More from nativeui
All 29 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| 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 | |
| Cardcard | nativeui | Components | Free | 1 dep |
