Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
react-native-collapsible
Advanced tools
Animated collapsible component for React Native using the Animated API. Good for accordions, toggles etc
The react-native-collapsible package provides components for creating collapsible views in React Native applications. It allows developers to easily create sections of content that can be expanded or collapsed, improving the user experience by managing the visibility of large amounts of content.
Collapsible Component
The Collapsible component allows you to create a section of content that can be expanded or collapsed. The 'collapsed' prop controls the visibility of the content.
import Collapsible from 'react-native-collapsible';
import { View, Text, Button } from 'react-native';
import React, { useState } from 'react';
const Example = () => {
const [collapsed, setCollapsed] = useState(true);
return (
<View>
<Button title="Toggle" onPress={() => setCollapsed(!collapsed)} />
<Collapsible collapsed={collapsed}>
<Text>This is collapsible content</Text>
</Collapsible>
</View>
);
};
export default Example;
Accordion Component
The Accordion component allows you to create a list of sections that can be expanded or collapsed individually. It manages the state of which sections are currently expanded.
import Accordion from 'react-native-collapsible/Accordion';
import { View, Text } from 'react-native';
import React, { useState } from 'react';
const SECTIONS = [
{ title: 'First', content: 'Lorem ipsum...' },
{ title: 'Second', content: 'Lorem ipsum...' }
];
const Example = () => {
const [activeSections, setActiveSections] = useState([]);
const renderHeader = (section) => (
<View><Text>{section.title}</Text></View>
);
const renderContent = (section) => (
<View><Text>{section.content}</Text></View>
);
return (
<Accordion
sections={SECTIONS}
activeSections={activeSections}
renderHeader={renderHeader}
renderContent={renderContent}
onChange={setActiveSections}
/>
);
};
export default Example;
react-native-collapsible-view offers a straightforward API for creating collapsible views. It is simpler and more lightweight compared to react-native-collapsible, making it a good choice for projects that require basic collapsible functionality without additional features.
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).
npm install --save react-native-collapsible
import Collapsible from 'react-native-collapsible';
() => (
<Collapsible collapsed={isCollapsed}>
<SomeCollapsedView />
</Collapsible>
);
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 | () => {} |
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}
/>
);
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). |
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}
/>
);
}
}
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):
Interested in contributing to this repo? Have a look at our Contributing Guide
Joel Arvidsson Author |
MIT License. © Joel Arvidsson and contributors 2015-2021
FAQs
Animated collapsible component for React Native using the Animated API. Good for accordions, toggles etc
The npm package react-native-collapsible receives a total of 73,984 weekly downloads. As such, react-native-collapsible popularity was classified as popular.
We found that react-native-collapsible demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.