JSPM

  • Created
  • Published
  • Downloads 25490
  • Score
    100M100P100Q154606F
  • License MIT

Collapse library based on CSS transition for React

Package Exports

  • @kunukn/react-collapse

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@kunukn/react-collapse) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-collapse

Collapse component with CSS transition for elements with variable and dynamic height.

npm version npm downloads

react-collapse

logo

Demo

CSS required

⚠️ ️You need to add style (transition) in your own stylesheet to add animation. Here is an example.

<style>
  .collapse-css-transition {
    transition: height 280ms cubic-bezier(0.4, 0, 0.2, 1);
  }
</style>

Alternatively you can add it using the transition prop.

Installation for React 16.8+

(UMD minified 3.8kb, gzipped 1.7kb)

npm i @kunukn/react-collapse
# or
yarn add @kunukn/react-collapse

Installation for React 16.3+

(UMD minified 5.8kb, gzipped 2.1kb)

npm i @kunukn/react-collapse@^1
# or
yarn add @kunukn/react-collapse@^1
import Collapse from "@kunukn/react-collapse";
// or with require syntax
const Collapse = require("@kunukn/react-collapse");

<Collapse isOpen={true || false}>
  <div>Your content</div>
</Collapse>;

Properties

Prop Type Default
isOpen boolean false
children node | function
render function
className string collapse-css-transition
transition string
elementType string div
collapseHeight string 0px
onChange function
onInit function

isOpen : boolean

Expands or collapses content.

children : node | function

Render the children.

<Collapse isOpen={true || false}>
  <p>Paragraph of text</p>
  <p>Another paragraph is also OK</p>
  <p>Images and any other content are ok too</p>
  <img src="cutecat.gif" />
</Collapse>

Render content using the render-props pattern.

<Collapse
  isOpen={ true || false }
>
  {collapseState => (
    <div className="using-collapse-state-to-add-css-class " + collapseState>
      <p>I know the collapse state: {collapseState}</p>
    </div>
  )}
</Collapse>

render : function

Render content using the render-props pattern.

<Collapse
  isOpen={ true || false }
  render={collapseState => (
    <div className="using-collapse-state-to-add-css-class " + collapseState>
      <p>I know the collapse state: {collapseState}</p>
    </div>
  )}
/>

There are four possible collapse states: collapsed, collapsing, expanded, expanding.

className : string

You can specify a custom className. The default value is collapse-css-transition. Remember to add CSS transition if a className is provided.

transition : string

You can also specify a CSS transition inline by using the transition prop.

<Collapse transition="height 290ms cubic-bezier(.4, 0, .2, 1)">
  <p>Paragraph of text</p>
</Collapse>

elementType : string

You can specify the HTML element type for the collapse component. By default the element type is a div element.

<Collapse elementType="article">
  <p>Paragraph of text inside an article element</p>
</Collapse>

collapseHeight : string

You can specify the collapse height in CSS unit to partially show some content.

<Collapse collapseHeight="40px">
  <p>A long paragraph of text inside an article element</p>
</Collapse>

onChange : function

Callback function for when the transition changes.

let onChange = ({ state, style }) => {
  /*
    state: the collapseState
    style: an object containing style applied to the component.
  */
};

<Collapse onChange={onChange} isOpen={true || false}>
  <p>A long paragraph of text inside an article element</p>
</Collapse>;

onInit : function

Similar to onChange but only invoked on DOM mounted.

let onInit = ({ state, style, node }) => {
  /* 
    node: the HTML element for the component.
  */
};

<Collapse onInit={onInit} isOpen={true || false}>
  <p>A long paragraph of text inside an article element</p>
</Collapse>;

Custom props

Collapse applies custom props such as aria- and data- attributes to the component's rendered DOM element. For example this can be used to set the aria-hidden attribute:

<Collapse aria-hidden={isOpenState ? "false" : "true"} isOpen={isOpenState}>
  <p>Paragraph of text</p>
</Collapse>

Or you could specify a specific transitionDuration.

<Collapse style={{ transitionDuration: "270ms" }} isOpen={isOpenState}>
  <p>Paragraph of text</p>
</Collapse>

Development and testing

To run development

npm start or yarn start

git clone [repo]
cd [repo]
npm i
npm start
open http://localhost:6007
npm test

or with yarn

git clone [repo]
cd [repo]
yarn
yarn start
open http://localhost:6007
yarn test
  • To develop and play around: yarn start
  • To build the bundle: yarn build
  • To validate the bundle: yarn validate

To run example covering all features, use npm run storybook or yarn storybook.

CDN

https://unpkg.com/@kunukn/react-collapse/

<link
  rel="stylesheet"
  href="https://unpkg.com/@kunukn/react-collapse/dist/Collapse.umd.css"
/>
<script src="https://unpkg.com/@kunukn/react-collapse/dist/Collapse.umd.js"></script>

<script>
  var Collapse = window.Collapse;
</script>

Supported browsers

IE11 + Modern browsers

Supported React versions

  • React version 16.3+ : use Collapse version 1
  • React version 16.8+ : use Collapse version 2+

Used React 16.3 life-cycles

  • render (uses the style states to invoke CSS transition)
  • componentDidMount (initial expanded or collapsed state)
  • getDerivedStateFromProps (detect if isOpen props has changed and apply a new collapse state)
  • componentDidUpdate (update style states from the four possible collapse states)

Used React 16.8 hooks

  • useState
  • useEffect
  • useLayoutEffect
  • useRef
  • useCallback

Design goals

  • let the browser handle the animation using CSS height transition
  • minimal in file size
  • minimalistic API - only have a Collapse component which updates on isOpen props
  • flexible - provide your own markup, styling and easing
  • interruptible - can be reversed during movement
  • inert - when collapsed you should tab over the collapsed component
  • availability - from cdn or npm install
  • Collapsible on height only

This was created with heavy inspiration from

https://github.com/SparebankenVest/react-css-collapse 🎆