1.6.1 • Published 1 year ago

react-native-collapsible v1.6.1

Weekly downloads
52,722
License
MIT
Repository
github
Last release
1 year 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
enablePointerEventsEnable pointer events on collapsed viewfalse
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
renderChildrenCollapsedRender children in collapsible even if not visible.true
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
    activeSections={[0]}
    sections={['Section 1', 'Section 2', 'Section 3']}
    renderSectionTitle={this._renderSectionTitle}
    renderHeader={this._renderHeader}
    renderContent={this._renderContent}
    onChange={this._updateSections}
  />
);

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
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.
activeSectionsControl which indices in the sections array are currently open. 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.
sectionContainerStyleOptional styling for the section container.
containerStyleOptional styling for the Accordion container.
renderAsFlatListOptional 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

License

MIT License. © Joel Arvidsson and contributors 2015-2021

rn-aht-jitsi-meetsensesocialantd-mobile-rn-responsivesezzle-appiusify-module-case_management@doggoapp/doggo-mobile-components-ts@proximus/react-native-design-systemddt-react-nativejitsi-meet-rnreact-native-jitsi-meet-lib@infinitebrahmanuniverse/nolb-react-native-co@everything-registry/sub-chunk-2574@plasmatictechnologies/alana-react-native-ui-componentsreact-native-collapsible-containerreact-native-collapsible-editstepup_jitsi_clubreact-native-rndemoreact-native-optimusreact-native-schedulereact-native-schedule1react-native-pannel-libreact-native-sandboxreact-native-cometchat-calling-componentreact-native-commentsreact-native-common-accordionreact-native-4tek-component-kitreact-native-ant-design-zyreact-native-akameet-sdkreact-native-proximus-design-systemreact-native-yoai-uikitcp-componentrun-pipelinertl-antd-mobilernschedulernschedule-practicernschedule-practice1rn-commonrn-dqgbuirn-helperrn-vayvayreact-native-simple-accordionreact-native-easy-pagedcs-rn-ui-atomicreact-native-pixfactory-accordionreact-native-prixareact-native-template-estado-cearareact-native-union-betasmcinsurancebutce-asistanimcabin-mobile-rncascnacommoncomponentscascnacreatecontract@beisen/bsapp-mobile-ui@beisen/mole-components-web@carshair/app-components@swdenglian/antd-react-nativepcmli.umbrella.react-native@brightlayer-ui/react-native-components@ant-design/react-nativeprixa-telemedicine-rn-sdkpreact-h5-ui@codedreams/react-elements@devopro/react-native-common-componentsartwork-mobile-nativeartwork-react-nativesubwallet-react-native-ui@fruits-chain/react-native@eliav2/react-native-collapsible-view@fbcmobile/ui@ma-shop/components@platformbuilders/react-native-ui@proximus/myproximus-design-system@pxblue/react-native-components@johnnylc/rn-custom-ui-components@merryjs/mini@ninjavid/rn-lib@nobellcq/antd-rn@mpvite/react-native-expandable-text@molejs/mole-components-rn@molejs/mole-components-web@nice-fungal/antd-mobile@sishuguojixuefu/antd-mobile-rn@react-sextant/react-native-test@mdfe/antd-mobile@mloureiro/react-native-expandable-text@lighthouseapps/ui@vivintsolar-oss/native-vs-collapsible@zalastax/nolb-react-native-conative-solid@widergy/utilitygo-smart-bill-mobileantd-mobile-carengoantd-mobile-cyqantd-mobile_chantd-mobile-rnantd-mobile-rn-kxxantd-mobile-rn-mrhuangjser-forkedantd-mobile-ruiantdmnemo-antd-mobilenemo-mobile
1.6.1

1 year ago

1.6.0

3 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.14.0

6 years ago

0.13.0

6 years ago

0.12.0

6 years ago

0.11.3

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.11.0

6 years ago

0.10.0

6 years ago

0.9.0

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.0

8 years ago

0.6.0

8 years ago

0.5.6

8 years ago

0.5.5

8 years ago

0.5.4

8 years ago

0.5.3

8 years ago

0.5.2

8 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago