• Stars
    star
    2,450
  • Rank 18,753 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Animated collapsible component for React Native, good for accordions, toggles etc

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

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

accordion-demo

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

More Repositories

1

react-native-vector-icons

Customizable Icons for React Native with support for image source and full styling.
JavaScript
17,427
star
2

react-native-animatable

Standard set of easy to use animations and declarative transitions for React Native
JavaScript
9,838
star
3

react-native-progress

Progress indicators and spinners for React Native
JavaScript
3,628
star
4

hush

🤫 Noiseless Browsing – Content Blocker for Safari
JavaScript
3,264
star
5

react-native-keychain

🔑 Keychain Access for React Native
Kotlin
3,194
star
6

react-native-lightbox

Images etc in Full Screen Lightbox Popovers for React Native
JavaScript
2,818
star
7

loki

👁 Visual Regression Testing for Storybook
JavaScript
1,778
star
8

react-native-image-progress

Progress indicator for networked images in React Native
JavaScript
1,723
star
9

angular-scroll

Scrollspy, animated scrollTo and scroll events for angular.js
JavaScript
1,482
star
10

react-native-performance

📐 Monitor and measure React Native performance
TypeScript
929
star
11

react-native-store-review

Rate on App/Play Store directly in your React Native app
Java
751
star
12

react-native-shimmer

Simple shimmering effect for any view in React Native
Java
700
star
13

react-native-esbuild

Fast bundler and dev server for react-native using esbuild
JavaScript
595
star
14

react-native-parallax

Parallax effects for React Native using Animated API
JavaScript
585
star
15

react-native-vector-image

iOS/Android native vector assets generated from SVG
JavaScript
310
star
16

react-native-pinchable

Instagram like pinch to zoom for React Native
Java
225
star
17

angular-parallax

Lightweight & performant parallax scrolling for angular.js.
JavaScript
203
star
18

angular-lazytube

Lightweight, responsive, lazy loaded YouTube videos that degrades gracefully.
JavaScript
45
star
19

diglett

Keep your JS project lean by detecting duplicate dependencies
JavaScript
44
star
20

esbuild-server

Fast, lightweight and powerful development server for esbuild
TypeScript
38
star
21

react-native-oxc

10x your react-native builds with OXC
TypeScript
28
star
22

oblador.github.io

Landing page for my GitHub projects
HTML
10
star
23

oblador

5
star
24

node-cloud-imager

Powerful yet simple way to apply filters/crop/resize images and upload to cloud providers (Amazon S3, Rackspace, Azure).
JavaScript
3
star
25

node-ups-sdk

JavaScript
3
star
26

sovra

🤏 Rust-based Test Decider for JavaScript using Oxc
Rust
2
star
27

expo-requestidlecallback-issue

TypeScript
1
star
28

react-native-android-autolinking-bug-repro

TypeScript
1
star
29

.github

1
star
30

react-native-fabric-image-unicode-issue

TypeScript
1
star