0.12.0 • Published 6 years ago

@lewisf/react-native-collapsible v0.12.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

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

PropDescriptionDefault
alignAlignment of the content when transitioning, can be top, center or bottomtop
collapsedWhether to show the child components or nottrue
collapsedHeightWhich height should the component collapse to0
durationDuration of transition in milliseconds300
easingFunction 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
styleOptional styling for the container
onAnimationEndCallback 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
  sections={['Section 1', 'Section 2', 'Section 3']}
  renderSectionTitle={this._renderSectionTitle}
  renderHeader={this._renderHeader}
  renderContent={this._renderContent}
/>;

Properties

PropDescription
sectionsAn 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
renderSectionTitle(content, index, isActive)A function that should return a renderable representing the title of the section outside the touchable element
onChange(index)An optional function that is called when currently active section is changed, index === false when collapsed
initiallyActiveSectionSet which index in the sections array is initially open. Defaults to none.
activeSectionsControl which indices in the sections array are currently open. Defaults to empty array. If empty, closes all sections.
underlayColorThe color of the underlay that will show through when tapping on headers. Defaults to black.
touchableComponentThe touchable component used in the Accordion. Defaults to TouchableHighlight
touchablePropsProperties for the touchableComponent
disabledSet whether the user can interact with the Accordion
alignSee Collapsible
durationSee Collapsible
easingSee Collapsible
onAnimationEnd(key, index)See Collapsible.
expandFromBottomExpand content from the bottom instead of the top
expandMultipleAllow more than one section to be expanded. Defaults to false.

Demo

demo

Example

Check full example in the Example folder.

import React, { Component } from 'react-native';
import Accordion from 'react-native-collapsible/Accordion';

const SECTIONS = [
  {
    title: 'First',
    content: 'Lorem ipsum...'
  },
  {
    title: 'Second',
    content: 'Lorem ipsum...'
  }
];

class AccordionView extends Component {
  _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>
    );
  }

  render() {
    return (
      <Accordion
        sections={SECTIONS}
        renderSectionTitle={this._renderSectionTitle}
        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:

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):

accordion-demo

Contributing

Interested in contributing to this repo? Have a look at our Contributing Guide

Maintainers

License

MIT License. © Joel Arvidsson 2015-2017