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
;<Collapsible collapsed=isCollapsed> <SomeCollapsedView /></Collapsible>
Properties
Prop | Description | Default |
---|---|---|
align |
Alignment of the content when transitioning, can be top , center or bottom |
top |
collapsed |
Wether to show the child components or not | true |
collapsedHeight |
Which height should the component collapse to | 0 |
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 |
Accordion Usage
This is a convenience component for a common use case, see demo below.
;<Accordion sections='Section 1' 'Section 2' 'Section 3' renderHeader=this_renderHeader renderContent=this_renderContent/>
Properties
Prop | Description |
---|---|
sections |
An array of sections passed to the render methods |
renderHeader(content, index, isActive) |
A function that should return a renderable representing the header |
renderContent(content, index, isActive) |
A function that should return a renderable representing the content |
onChange(index) |
An optional function that is called when currently active section is changed, index === false when collapsed |
initiallyActiveSection |
Set which index in the sections array is initially open. Defaults to none. |
activeSection |
Control which index in the sections array is currently open. Defaults to none. If false, closes all sections. |
underlayColor |
The color of the underlay that will show through when tapping on headers. Defaults to black. |
align |
See Collapsible |
duration |
See Collapsible |
easing |
See Collapsible |
Demo
Example
Check full example in the Example
folder.
;; const SECTIONS = title: 'First' content: 'Lorem ipsum...' title: 'Second' content: 'Lorem ipsum...' ; { return <View style=stylesheader> <Text style=stylesheaderText>sectiontitle</Text> </View> ; } { return <View style=stylescontent> <Text>sectioncontent</Text> </View> ; } render return <Accordion sections=SECTIONS renderHeader=this_renderHeader renderContent=this_renderContent /> ;
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:
; ... { return <AnimatableView style=stylesheader duration=300 transition="backgroundColor" style= backgroundColor: isActive ? 'rgba(255,255,255,1)' : 'rgba(245,252,255,1)' > <Text style=stylesheaderText>sectiontitle</Text> </AnimatableView> ; } { return <AnimatableView style=stylescontent duration=300 transition="backgroundColor" style= backgroundColor: isActive ? 'rgba(255,255,255,1)' : 'rgba(245,252,255,1)' > <AnimatableText duration=300 easing="ease-out" animation=isActive ? 'zoomIn' : false> sectioncontent </AnimatableText> </AnimatableView> ; } ...
To produce this (slowed down for visibility):
License
MIT License. © Joel Arvidsson 2015