Accordion Component

A flexible and animated accordion component for React Native applications that allows users to expand and collapse content sections with smooth animations.

Features

  • 📁 Collapsible Sections - Expand and collapse content with smooth animations
  • 🎨 Customizable Styling - Full control over headers, content, and container styling
  • 🔄 Multiple Modes - Single or multiple section expansion support
  • 🎪 Smooth Animations - Configurable animation types and durations
  • 🎯 Custom Rendering - Custom header and content rendering capabilities
  • Accessibility Ready - Screen reader support and proper semantics

Installation

💻Bash
npm install rn-base-component
# or
yarn add rn-base-component

Basic Usage

⚛️TSX
import React from 'react'
import { Accordion } from 'rn-base-component'

const sections = [
  {
    title: 'What is React Native?',
    content: 'React Native is a framework for building mobile applications using React.',
  },
  {
    title: 'How does it work?',
    content: 'React Native uses native components instead of web components as building blocks.',
  },
  {
    title: 'Is it production ready?',
    content: 'Yes, many popular apps like Facebook, Instagram, and Airbnb use React Native.',
  },
]

export default function App() {
  return  console.log('Section pressed:', key)} />
}

Advanced Usage

Multiple Expansion

⚛️TSX
 console.log('Toggled section:', key)} />

Custom Key Extractor

⚛️TSX
 `section-${item.id || index}`}
  expandMultiple={false}
/>

Custom Styling

⚛️TSX


const styles = StyleSheet.create({
  wrapper: {
    padding: 16,
  },
  itemContainer: {
    backgroundColor: '#f8f9fa',
    marginBottom: 8,
    borderRadius: 8,
  },
  headerContainer: {
    backgroundColor: '#007AFF',
    padding: 16,
    borderRadius: 8,
  },
  contentContainer: {
    backgroundColor: 'white',
    padding: 16,
    borderBottomLeftRadius: 8,
    borderBottomRightRadius: 8,
  },
  title: {
    color: 'white',
    fontSize: 16,
    fontWeight: 'bold',
  },
})

API Reference

AccordionProps

PropTypeDefaultDescription
sectionsSection[]RequiredArray of section data
keyExtractor(item: Section, index: number) => stringindex.toString()Function to extract unique keys
expandMultiplebooleanfalseAllow multiple sections to be expanded
wrapperStyleStyleProp<ViewStyle>undefinedStyle for the accordion wrapper
onPress(key: string) => voidundefinedCallback when section is pressed
renderHeader(item, index, expanded, key) => ReactNodeundefinedCustom header renderer
renderSectionTitle(item, index, expanded) => ReactNodeundefinedCustom title renderer
renderContent(item, index, expanded, key) => ReactNodeundefinedCustom content renderer
titlestringundefinedAccordion title
itemContainerStyleStyleProp<ViewStyle>undefinedStyle for each item container
titleStyleStyleProp<TextStyle>undefinedStyle for section titles
headerContainerStyleStyleProp<ViewStyle>undefinedStyle for header containers
contentContainerStyleStyleProp<ViewStyle>undefinedStyle for content containers
openAnimationAnimationType'easeInEaseOut'Animation type for opening
closeAnimationAnimationType'easeInEaseOut'Animation type for closing
openDurationnumber300Duration for open animation (ms)
closeDurationnumber300Duration for close animation (ms)

Common Use Cases

FAQ Section

⚛️TSX
const faqData = [
  {
    title: 'How do I reset my password?',
    content: 'You can reset your password by clicking the "Forgot Password" link on the login page and following the instructions sent to your email.',
  },
  {
    title: 'Can I change my subscription plan?',
    content: 'Yes, you can upgrade or downgrade your subscription plan at any time from your account settings.',
  },
  {
    title: 'Is my data secure?',
    content: 'We use industry-standard encryption and security measures to protect your data.',
  },
]


Settings Menu

⚛️TSX
const settingsData = [
  {
    title: 'Account Settings',
    content: 'Manage your profile, email preferences, and account security.',
    icon: 'user',
  },
  {
    title: 'Privacy & Security',
    content: 'Control who can see your information and how it\'s used.',
    icon: 'shield',
  },
  {
    title: 'Notifications',
    content: 'Choose what notifications you want to receive and how.',
    icon: 'bell',
  },
]

 (
    
      
      {item.title}
      
    
  )}
/>

Accessibility

Screen Reader Support

⚛️TSX
 (
    
      {item.title}
    
  )}
/>

Best Practices

  1. Content Organization - Group related content logically
  2. Clear Titles - Use descriptive, scannable section titles
  3. Performance - Avoid complex content in collapsed sections
  4. Visual Feedback - Provide clear indicators for expandable sections
  5. Animation Timing - Keep animations quick but noticeable
  6. Accessibility - Ensure proper accessibility labels and states

Performance

Use React.memo for sections with complex content and consider lazy loading for accordion content with heavy assets.