All Projects → kunukn → react-collapse

kunukn / react-collapse

Licence: MIT license
Component-wrapper for collapse animation with CSS for elements with variable and dynamic height

Programming Languages

javascript
184084 projects - #8 most used programming language
SCSS
7915 projects
typescript
32286 projects

Projects that are alternatives of or similar to react-collapse

React Sanfona
Accessible react accordion component
Stars: ✭ 248 (+73.43%)
Mutual labels:  accordion, collapsible
BeefUp
Just a jQuery accordion plugin
Stars: ✭ 41 (-71.33%)
Mutual labels:  accordion, collapsible
React Native Collapsible
Animated collapsible component for React Native, good for accordions, toggles etc
Stars: ✭ 2,178 (+1423.08%)
Mutual labels:  accordion, collapsible
Azexpandableiconlistview
An expandable/collapsible view component written in Swift.
Stars: ✭ 284 (+98.6%)
Mutual labels:  accordion, collapsible
react-native-panel
A Customizable React Native Panel for Android and iOS
Stars: ✭ 35 (-75.52%)
Mutual labels:  accordion, collapsible
React Collapsible
React component to wrap content in Collapsible element with trigger to open and close.
Stars: ✭ 402 (+181.12%)
Mutual labels:  accordion, collapsible
Handorgel
Accessible W3C conform accordion written in ES6.
Stars: ✭ 239 (+67.13%)
Mutual labels:  accordion
qaccordion
An Accordion Widget for the Qt application framework
Stars: ✭ 38 (-73.43%)
Mutual labels:  accordion
You Dont Need Javascript
CSS is powerful, you can do a lot of things without JS.
Stars: ✭ 16,514 (+11448.25%)
Mutual labels:  accordion
Emaccordiontableviewcontroller
Accordion effect for UITableView
Stars: ✭ 158 (+10.49%)
Mutual labels:  accordion
bookmarks
Bookmarks that I ❤️
Stars: ✭ 151 (+5.59%)
Mutual labels:  codepen
codesandbox-nuxt
Starter template for CodeSandBox.io
Stars: ✭ 40 (-72.03%)
Mutual labels:  codesandbox
codepen-electron
Mac client for codepen.io
Stars: ✭ 28 (-80.42%)
Mutual labels:  codepen
gatsby-remark-embedded-codesandbox
A Gatsby Remark plugin for embedding Codesandbox given a folder of files
Stars: ✭ 31 (-78.32%)
Mutual labels:  codesandbox
Liteaccordion
A lightweight horizontal accordion plugin for jQuery.
Stars: ✭ 234 (+63.64%)
Mutual labels:  accordion
code-run
一个代码在线编辑预览工具,类似codepen、jsbin、jsfiddle等。
Stars: ✭ 325 (+127.27%)
Mutual labels:  codepen
codebook
Open-source learning tool that allows to users to follow textbook contents while practicing HTML, CSS and JS using a live code editor.
Stars: ✭ 20 (-86.01%)
Mutual labels:  codepen
vue-uix
Vue components based on the JUI components available in Vue.js
Stars: ✭ 15 (-89.51%)
Mutual labels:  accordion
Friendly Code Editor
Try this Friendly Code Editor. You'll love it. I made it with a lot of effort. It has some great features. I will update it adequately later. Very helpful for developers. Enjoy and share.
Stars: ✭ 20 (-86.01%)
Mutual labels:  codepen
codeframe
The fastest, easiest way to build and deploy quick static webpages
Stars: ✭ 107 (-25.17%)
Mutual labels:  codepen

react-collapse

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

npm version npm downloads gzip license

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");

const MyComponent = () => (
  <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
addState boolean false
noAnim boolean false
overflowOnExpanded boolean false

isOpen : boolean

Expands or collapses content.

children : node | function

Render the children.

const MyComponent = ({ isOpen }) => (
  <Collapse isOpen={isOpen}>
    <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.

const MyComponent = ({ isOpen }) => (
  <Collapse isOpen={isOpen}>
    {state => (
      <div className={`using-collapse-state-to-add-css-class ${state}`}>
        <p>I know the collapse state: {state}</p>
      </div>
    )}
  </Collapse>
);

render : function

Render content using the render-props pattern.

const MyComponent = ({ isOpen }) => {
  const render = state => (
    <div className={`using-collapse-state-to-add-css-class ${state}`}>
      <p>I know the collapse state: {state}</p>
    </div>
  );
  return <Collapse isOpen={isOpen} render={render} />;
};

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.

const MyComponent = ({ isOpen, duration = "290ms" }) => (
  <Collapse
    transition={`height ${duration} cubic-bezier(.4, 0, .2, 1)`}
    isOpen={isOpen}
  >
    <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.

const MyComponent = ({ isOpen }) => (
  <Collapse elementType="article" isOpen={isOpen}>
    <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.

const MyComponent = ({ isOpen }) => (
  <Collapse collapseHeight="40px" isOpen={isOpen}>
    <p>A long paragraph of text inside an article element</p>
  </Collapse>
);

onChange : function

Callback function for when the transition changes.

const MyComponent = ({ isOpen }) => {
  const onChange = ({ state, style }) => {
    /*
    state: string = the state of the collapse component.
    style: object = styles that are applied to the component.
  */
  };

  return (
    <Collapse onChange={onChange} isOpen={isOpen}>
      <p>A long paragraph of text inside an article element</p>
    </Collapse>
  );
};

onInit : function

Similar to onChange but only invoked on DOM mounted.
This is an example that starts collapsed and expands on mount.

const MyComponent = () => {
  const [isOpen, setIsOpen] = React.useState(false);

  const onInit = ({ state, style, node }) => {
    /*
       node: HTMLElement = the DOM node of the component.
    */

    setIsOpen(true);
  };

  return (
    <div>
      <button onClick={() => setIsOpen(state => !state)}> Toggle </button>
      <Collapse onInit={onInit} isOpen={isOpen}>
        <p>A long paragraph of text inside an article element</p>
      </Collapse>
    </div>
  );
};

addState : boolean

If added, then one of the class names will be appended to the component depending on the state.

--c-collapsed
--c-collapsing
--c-expanded
--c-expanding

noAnim : boolean

If added, then there will be no collapse animation. State changes between collapsed and expanded.

overflowOnExpanded : boolean

If added, then overflow: hidden style will not be added when the state is expanded.

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:

const MyComponent = ({ isOpen }) => (
  <Collapse aria-hidden={isOpen ? "false" : "true"} isOpen={isOpen}>
    <p>Paragraph of text</p>
  </Collapse>
);

Or you could specify a specific transitionDuration.

const collapseStyles = { transitionDuration: "270ms" };

const MyComponent = ({ isOpen }) => (
  <Collapse style={collapseStyles} isOpen={isOpen}>
    <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
  • useRef
  • useCallback
  • useReducer

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 🎆

A version also exists for Vue

https://github.com/kunukn/vue-height-collapsible

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].