react-native-collapsible
Animated collapsible component for React Native, good for accordions, toggles etc
Top Related Projects
React Native's Animated library reimplemented
An enhanced, animated, customizable Modal for React Native.
A React Native ListView component with rows that swipe open and closed
Swiper/carousel component for React Native featuring previews, multiple layouts, parallax images, performant handling of huge numbers of items, and more. Compatible with Android & iOS.
A cross-platform Tab View component for React Native
UI Components Library for React Native
Quick Overview
React Native Collapsible is a lightweight and customizable component for creating collapsible/expandable views in React Native applications. It provides smooth animations and supports both controlled and uncontrolled modes, making it easy to implement accordions, expandable lists, and other collapsible UI elements.
Pros
- Easy to integrate and use with minimal setup
- Supports both controlled and uncontrolled modes for flexibility
- Customizable animations and styling options
- Lightweight with no external dependencies
Cons
- Limited built-in layout options (mainly vertical collapsing)
- May require additional configuration for complex use cases
- Documentation could be more comprehensive
- Some reported issues with TypeScript definitions
Code Examples
Basic usage:
import Collapsible from 'react-native-collapsible';
const MyComponent = () => {
const [collapsed, setCollapsed] = useState(true);
return (
<View>
<TouchableOpacity onPress={() => setCollapsed(!collapsed)}>
<Text>Toggle</Text>
</TouchableOpacity>
<Collapsible collapsed={collapsed}>
<Text>This content is collapsible</Text>
</Collapsible>
</View>
);
};
Customizing animation:
<Collapsible
collapsed={collapsed}
align="center"
duration={1000}
easing="bounce"
>
<Text>Custom animated content</Text>
</Collapsible>
Creating an accordion:
import Accordion from 'react-native-collapsible/Accordion';
const SECTIONS = [
{ title: 'Section 1', content: 'Content for section 1' },
{ title: 'Section 2', content: 'Content for section 2' },
];
const MyAccordion = () => {
const [activeSections, setActiveSections] = useState([]);
const renderHeader = (section, _, isActive) => (
<Text>{section.title}</Text>
);
const renderContent = (section) => (
<Text>{section.content}</Text>
);
return (
<Accordion
sections={SECTIONS}
activeSections={activeSections}
renderHeader={renderHeader}
renderContent={renderContent}
onChange={setActiveSections}
/>
);
};
Getting Started
-
Install the package:
npm install react-native-collapsible
-
Import and use the component in your React Native app:
import Collapsible from 'react-native-collapsible'; const MyComponent = () => ( <Collapsible collapsed={true}> <Text>This content will be collapsible</Text> </Collapsible> );
-
Customize the component as needed using props like
align
,duration
, andeasing
.
Competitor Comparisons
React Native's Animated library reimplemented
Pros of react-native-reanimated
- More powerful and flexible animation system
- Better performance for complex animations
- Supports gesture-based animations
Cons of react-native-reanimated
- Steeper learning curve
- Requires more setup and configuration
- Larger package size
Code Comparison
react-native-collapsible:
<Collapsible collapsed={isCollapsed}>
<View>
<Text>Collapsible content</Text>
</View>
</Collapsible>
react-native-reanimated:
const animatedHeight = useAnimatedStyle(() => {
return {
height: withTiming(isCollapsed ? 0 : 100),
};
});
return (
<Animated.View style={[styles.container, animatedHeight]}>
<Text>Collapsible content</Text>
</Animated.View>
);
react-native-collapsible provides a simpler API for basic collapsible components, while react-native-reanimated offers more control and flexibility for complex animations. react-native-reanimated requires more code and setup but allows for more customization and smoother animations. The choice between the two depends on the project's specific animation requirements and the developer's familiarity with animation concepts.
An enhanced, animated, customizable Modal for React Native.
Pros of react-native-modal
- More versatile, can be used for various modal types beyond collapsible content
- Offers a wider range of customization options and animations
- Actively maintained with frequent updates and bug fixes
Cons of react-native-modal
- Larger package size due to additional features
- May have a steeper learning curve for simple collapsible implementations
- Potentially higher performance overhead for basic use cases
Code Comparison
react-native-modal:
<Modal isVisible={isModalVisible} onBackdropPress={toggleModal}>
<View style={styles.modalContent}>
<Text>Modal Content</Text>
<Button title="Close" onPress={toggleModal} />
</View>
</Modal>
react-native-collapsible:
<Collapsible collapsed={isCollapsed}>
<View style={styles.content}>
<Text>Collapsible Content</Text>
</View>
</Collapsible>
react-native-modal provides a more comprehensive modal solution with built-in visibility control and backdrop press handling. react-native-collapsible focuses specifically on collapsible content with a simpler API. The choice between the two depends on the specific requirements of your project, with react-native-modal offering more flexibility for various modal types, while react-native-collapsible provides a lightweight solution for collapsible sections.
A React Native ListView component with rows that swipe open and closed
Pros of react-native-swipe-list-view
- Provides swipeable list functionality, allowing for more interactive and feature-rich lists
- Offers customizable swipe actions, including multiple actions per row
- Supports both iOS and Android platforms with native animations
Cons of react-native-swipe-list-view
- More complex implementation compared to simple collapsible components
- May have a steeper learning curve for developers new to swipeable interfaces
- Potentially higher performance overhead due to additional gesture handling
Code Comparison
react-native-swipe-list-view:
<SwipeListView
data={listData}
renderItem={({ item }) => (
<View>
<Text>{item.text}</Text>
</View>
)}
renderHiddenItem={({ item }) => (
<View>
<TouchableOpacity onPress={() => deleteRow(item.key)}>
<Text>Delete</Text>
</TouchableOpacity>
</View>
)}
rightOpenValue={-75}
/>
react-native-collapsible:
<Collapsible collapsed={isCollapsed}>
<View>
<Text>This content is collapsible</Text>
</View>
</Collapsible>
The code comparison shows that react-native-swipe-list-view requires more setup and configuration for its swipeable functionality, while react-native-collapsible offers a simpler implementation for collapsible content. The swipe-list-view example demonstrates the need for separate rendering of visible and hidden items, as well as configuration for swipe actions. In contrast, the collapsible example only requires wrapping the content in a Collapsible component with a collapsed prop.
Swiper/carousel component for React Native featuring previews, multiple layouts, parallax images, performant handling of huge numbers of items, and more. Compatible with Android & iOS.
Pros of react-native-snap-carousel
- More feature-rich, offering advanced carousel functionality
- Highly customizable with various animation options
- Better suited for image galleries and complex content display
Cons of react-native-snap-carousel
- Larger package size due to more features
- Steeper learning curve for basic implementations
- May be overkill for simple collapsible content needs
Code Comparison
react-native-snap-carousel:
<Carousel
data={items}
renderItem={({item}) => <MySlide item={item} />}
sliderWidth={sliderWidth}
itemWidth={itemWidth}
loop={true}
autoplay={true}
/>
react-native-collapsible:
<Collapsible collapsed={isCollapsed}>
<View>
<Text>Collapsible content here</Text>
</View>
</Collapsible>
react-native-snap-carousel is more suitable for creating interactive, swipeable carousels with various content types. It offers advanced features like autoplay, looping, and custom animations. However, it may be excessive for simple collapsible sections.
react-native-collapsible is focused on creating expandable/collapsible components. It's lighter and easier to implement for basic accordion-style interfaces but lacks the advanced carousel features of react-native-snap-carousel.
Choose react-native-snap-carousel for rich, interactive content displays, and react-native-collapsible for simpler expandable/collapsible sections in your React Native app.
A cross-platform Tab View component for React Native
Pros of react-native-tab-view
- More comprehensive tab navigation solution
- Supports both top and bottom tab layouts
- Offers smooth animations and gestures for tab switching
Cons of react-native-tab-view
- Larger package size due to more features
- Steeper learning curve for complex implementations
- May require additional configuration for custom designs
Code Comparison
react-native-tab-view:
import { TabView, SceneMap } from 'react-native-tab-view';
const renderScene = SceneMap({
first: FirstRoute,
second: SecondRoute,
});
<TabView
navigationState={{ index, routes }}
renderScene={renderScene}
onIndexChange={setIndex}
/>
react-native-collapsible:
import Collapsible from 'react-native-collapsible';
<Collapsible collapsed={isCollapsed}>
<View>
<Text>This content is collapsible</Text>
</View>
</Collapsible>
While react-native-tab-view provides a complete tab navigation solution with various layout options and animations, react-native-collapsible focuses on creating collapsible content sections. The choice between these libraries depends on the specific requirements of your project, with react-native-tab-view being more suitable for complex tab-based navigation and react-native-collapsible for simpler collapsible content implementations.
UI Components Library for React Native
Pros of react-native-ui-lib
- Comprehensive UI component library with a wide range of pre-built components
- Highly customizable and themeable, allowing for consistent design across the app
- Active development and regular updates from the Wix team
Cons of react-native-ui-lib
- Larger package size due to the extensive component library
- Steeper learning curve for developers unfamiliar with the Wix ecosystem
- May include unnecessary components for simpler projects
Code Comparison
react-native-collapsible:
import Collapsible from 'react-native-collapsible';
<Collapsible collapsed={isCollapsed}>
<View>
<Text>Collapsible content</Text>
</View>
</Collapsible>
react-native-ui-lib:
import { View, Text, Expandable } from 'react-native-ui-lib';
<Expandable expanded={isExpanded}>
<View>
<Text>Expandable content</Text>
</View>
</Expandable>
While react-native-collapsible focuses specifically on collapsible components, react-native-ui-lib offers a more comprehensive set of UI components, including an expandable/collapsible component. The react-native-ui-lib implementation provides additional customization options and integrates seamlessly with other components from the library. However, for projects that only require collapsible functionality, react-native-collapsible may be a more lightweight and focused solution.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
react-native-collapsible
Animated collapsible component for React Native using the Animated API
Pure JavaScript, supports dynamic content heights and components that is aware of its collapsed
state (good for toggling arrows etc).
Installation
npm install --save react-native-collapsible
Collapsible Usage
import Collapsible from 'react-native-collapsible';
() => (
<Collapsible collapsed={isCollapsed}>
<SomeCollapsedView />
</Collapsible>
);
Properties
Prop | Description | Default |
---|---|---|
align | Alignment of the content when transitioning, can be top , center or bottom | top |
collapsed | Whether to show the child components or not | true |
collapsedHeight | Which height should the component collapse to | 0 |
enablePointerEvents | Enable pointer events on collapsed view | false |
duration | Duration of transition in milliseconds | 300 |
easing | Function or function name from Easing (or tween-functions if < RN 0.8). Collapsible will try to combine Easing functions for you if you name them like tween-functions . | easeOutCubic |
renderChildrenCollapsed | Render children in collapsible even if not visible. | true |
style | Optional styling for the container | |
onAnimationEnd | Callback when the toggle animation is done. Useful to avoid heavy layouting work during the animation | () => {} |
Accordion Usage
This is a convenience component for a common use case, see demo below.
import Accordion from 'react-native-collapsible/Accordion';
() => (
<Accordion
activeSections={[0]}
sections={['Section 1', 'Section 2', 'Section 3']}
renderSectionTitle={this._renderSectionTitle}
renderHeader={this._renderHeader}
renderContent={this._renderContent}
onChange={this._updateSections}
/>
);
Properties
Prop | Description |
---|---|
sections | An array of sections passed to the render methods |
renderHeader(content, index, isActive, sections) | A function that should return a renderable representing the header |
renderContent(content, index, isActive, sections) | A function that should return a renderable representing the content |
renderFooter(content, index, isActive, sections) | A function that should return a renderable representing the footer |
renderSectionTitle(content, index, isActive) | A function that should return a renderable representing the title of the section outside the touchable element |
onChange(indexes) | A function that is called when the currently active section(s) are updated. |
keyExtractor(item, index) | Used to extract a unique key for a given item at the specified index. |
activeSections | Control which indices in the sections array are currently open. If empty, closes all sections. |
underlayColor | The color of the underlay that will show through when tapping on headers. Defaults to black. |
touchableComponent | The touchable component used in the Accordion. Defaults to TouchableHighlight |
touchableProps | Properties for the touchableComponent |
disabled | Set whether the user can interact with the Accordion |
align | See Collapsible |
duration | See Collapsible |
easing | See Collapsible |
onAnimationEnd(key, index) | See Collapsible . |
expandFromBottom | Expand content from the bottom instead of the top |
expandMultiple | Allow more than one section to be expanded. Defaults to false. |
sectionContainerStyle | Optional styling for the section container. |
containerStyle | Optional styling for the Accordion container. |
renderAsFlatList | Optional rendering as FlatList (defaults to false). |
Demo
Example
Check full example in the Example
folder.
import React, { Component } from 'react';
import Accordion from 'react-native-collapsible/Accordion';
const SECTIONS = [
{
title: 'First',
content: 'Lorem ipsum...',
},
{
title: 'Second',
content: 'Lorem ipsum...',
},
];
class AccordionView extends Component {
state = {
activeSections: [],
};
_renderSectionTitle = (section) => {
return (
<View style={styles.content}>
<Text>{section.content}</Text>
</View>
);
};
_renderHeader = (section) => {
return (
<View style={styles.header}>
<Text style={styles.headerText}>{section.title}</Text>
</View>
);
};
_renderContent = (section) => {
return (
<View style={styles.content}>
<Text>{section.content}</Text>
</View>
);
};
_updateSections = (activeSections) => {
this.setState({ activeSections });
};
render() {
return (
<Accordion
sections={SECTIONS}
activeSections={this.state.activeSections}
renderSectionTitle={this._renderSectionTitle}
renderHeader={this._renderHeader}
renderContent={this._renderContent}
onChange={this._updateSections}
/>
);
}
}
Transition backgrounds
If you combine with the react-native-animatable
library you can easily transition the background color between the active and inactive state or add animations.
Lets augment the example above with:
import * as Animatable from 'react-native-animatable';
(...)
_renderHeader(section, index, isActive, sections) {
return (
<Animatable.View
duration={300}
transition="backgroundColor"
style={{ backgroundColor: (isActive ? 'rgba(255,255,255,1)' : 'rgba(245,252,255,1)') }}>
<Text style={styles.headerText}>{section.title}</Text>
</Animatable.View>
);
}
_renderContent(section, i, isActive, sections) {
return (
<Animatable.View
duration={300}
transition="backgroundColor"
style={{ backgroundColor: (isActive ? 'rgba(255,255,255,1)' : 'rgba(245,252,255,1)') }}>
<Animatable.Text
duration={300}
easing="ease-out"
animation={isActive ? 'zoomIn' : false}>
{section.content}
</Animatable.Text>
</Animatable.View>
);
}
(...)
To produce this (slowed down for visibility):
Contributing
Interested in contributing to this repo? Have a look at our Contributing Guide
Maintainers
Joel Arvidsson Author |
License
MIT License. © Joel Arvidsson and contributors 2015-2021
Top Related Projects
React Native's Animated library reimplemented
An enhanced, animated, customizable Modal for React Native.
A React Native ListView component with rows that swipe open and closed
Swiper/carousel component for React Native featuring previews, multiple layouts, parallax images, performant handling of huge numbers of items, and more. Compatible with Android & iOS.
A cross-platform Tab View component for React Native
UI Components Library for React Native
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot