All Projects → roginfarrer → react-collapsed

roginfarrer / react-collapsed

Licence: MIT license
A React custom-hook for creating flexible and accessible expand/collapse components.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to react-collapsed

TreeView
"TreeView - sub-cells simplified" (c). Enable subcells in UITableView with a single drop-in extension. CocoaPod:
Stars: ✭ 54 (-86.22%)
Mutual labels:  collapse, expand
Accordion-Collapse-react-native
React native Accordion/Collapse component, very good to use in toggles & show/hide content
Stars: ✭ 147 (-62.5%)
Mutual labels:  collapse, expand
AAG-Visualizer
🌆 🏙 🌃 Viz.js Graphviz - An Elegant Visualizer for And-Inverter Graph
Stars: ✭ 95 (-75.77%)
Mutual labels:  css-animations
use-scroll-direction
A simple, performant, and cross-browser hook for detecting scroll direction in your next react app.
Stars: ✭ 24 (-93.88%)
Mutual labels:  custom-hook
sidebar-menu
jQuery sidebar-menu component
Stars: ✭ 77 (-80.36%)
Mutual labels:  collapse
Auto-Collapse-Closing-Blocks
Default close blocks. Allows developers to leave an inline comment in a source file (html, ts, js, scss, xml, etc) that will automatically collapse the block starting on the next line whenever they open a file.
Stars: ✭ 18 (-95.41%)
Mutual labels:  collapse
Accordion.JS
Accordion.JS: Free jQuery Accordion plugin
Stars: ✭ 34 (-91.33%)
Mutual labels:  collapse
react-class-hooks
React Hooks implementation for Class Components. Support React >= 15.3.2
Stars: ✭ 63 (-83.93%)
Mutual labels:  custom-hook
react-collapse-pane
The splittable, draggable, collapsible panel layout library 🎉
Stars: ✭ 156 (-60.2%)
Mutual labels:  collapse
angular-app
Angular 14 ,Bootstrap 5, Node.js, Express.js, ESLint, CRUD, PWA, SSR, SEO, Universal, Lazy Loading
Stars: ✭ 389 (-0.77%)
Mutual labels:  collapse
expand-hash
Recursively expands property keys with dot-notation into objects.
Stars: ✭ 25 (-93.62%)
Mutual labels:  expand
use-lazyload-ref
🖼️ Custom hook to use lazyload easily
Stars: ✭ 58 (-85.2%)
Mutual labels:  custom-hook
hexa
Awesome Css Animation. The documentation =>
Stars: ✭ 32 (-91.84%)
Mutual labels:  css-animations
ui-components
💄 Library of UI components
Stars: ✭ 84 (-78.57%)
Mutual labels:  css-animations
Landing-Page-Animation
Landing page animation made using CSS
Stars: ✭ 45 (-88.52%)
Mutual labels:  css-animations
portfolio
Personal portfolio (2018)
Stars: ✭ 388 (-1.02%)
Mutual labels:  css-animations
useCustomHooks
📦 npm package containing a set of custom hooks for your next React project.
Stars: ✭ 12 (-96.94%)
Mutual labels:  custom-hook
flhooks
React like Hooks implementation for Flutter.
Stars: ✭ 38 (-90.31%)
Mutual labels:  custom-hook
JSONPreview
🎨 A view that previews JSON in highlighted form, it also provides the ability to format and collapse nodes.
Stars: ✭ 21 (-94.64%)
Mutual labels:  collapse
animusjs
🎆 AnimusJS is the solution for combine JS and CSS animations.
Stars: ✭ 42 (-89.29%)
Mutual labels:  css-animations

react-collapsed (useCollapse)

CI npm bundle size (version) npm version Netlify Status

A custom hook for creating accessible expand/collapse components in React. Animates the height using CSS transitions from 0 to auto.

Features

  • Handles the height of animations of your elements, auto included!
  • You control the UI - useCollapse provides the necessary props, you control the styles and the elements.
  • Accessible out of the box - no need to worry if your collapse/expand component is accessible, since this takes care of it for you!
  • No animation framework required! Simply powered by CSS animations
  • Written in TypeScript

Demo

See the demo site!

CodeSandbox demo

Installation

$ yarn add react-collapsed
# or
$ npm i react-collapsed

Usage

Simple Usage

import React from 'react'
import useCollapse from 'react-collapsed'

function Demo() {
  const { getCollapseProps, getToggleProps, isExpanded } = useCollapse()

  return (
    <div>
      <button {...getToggleProps()}>
        {isExpanded ? 'Collapse' : 'Expand'}
      </button>
      <section {...getCollapseProps()}>Collapsed content 🙈</section>
    </div>
  )
}

Control it yourself

import React, { useState } from 'react'
import useCollapse from 'react-collapsed'

function Demo() {
  const [isExpanded, setExpanded] = useState(false)
  const { getCollapseProps, getToggleProps } = useCollapse({ isExpanded })

  return (
    <div>
      <button
        {...getToggleProps({
          onClick: () => setExpanded((prevExpanded) => !prevExpanded),
        })}
      >
        {isExpanded ? 'Collapse' : 'Expand'}
      </button>
      <section {...getCollapseProps()}>Collapsed content 🙈</section>
    </div>
  )
}

API

const { getCollapseProps, getToggleProps, isExpanded, setExpanded } =
  useCollapse({
    isExpanded: boolean,
    defaultExpanded: boolean,
    expandStyles: {},
    collapseStyles: {},
    collapsedHeight: 0,
    easing: string,
    duration: number,
    onCollapseStart: func,
    onCollapseEnd: func,
    onExpandStart: func,
    onExpandEnd: func,
  })

useCollapse Config

The following are optional properties passed into useCollapse({ }):

Prop Type Default Description
isExpanded boolean undefined If true, the Collapse is expanded
defaultExpanded boolean false If true, the Collapse will be expanded when mounted
expandStyles object {} Style object applied to the collapse panel when it expands
collapseStyles object {} Style object applied to the collapse panel when it collapses
collapsedHeight number 0 The height of the content when collapsed
easing string cubic-bezier(0.4, 0, 0.2, 1) The transition timing function for the animation
duration number undefined The duration of the animation in milliseconds. By default, the duration is programmatically calculated based on the height of the collapsed element
onCollapseStart function no-op Handler called when the collapse animation begins
onCollapseEnd function no-op Handler called when the collapse animation ends
onExpandStart function no-op Handler called when the expand animation begins
onExpandEnd function no-op Handler called when the expand animation ends
hasDisabledAnimation boolean false If true, will disable the animation

What you get

Name Description
getCollapseProps Function that returns a prop object, which should be spread onto the collapse element
getToggleProps Function that returns a prop object, which should be spread onto an element that toggles the collapse panel
isExpanded Whether or not the collapse is expanded (if not controlled)
setExpanded Sets the hook's internal isExpanded state

Alternative Solutions

  • react-spring - JavaScript animation based library that can potentially have smoother animations. Requires a bit more work to create an accessible collapse component.
  • react-animate-height - Another library that uses CSS transitions to animate to any height. It provides components, not a hook.

FAQ

When I apply vertical padding to the component that gets getCollapseProps, the animation is janky and it doesn't collapse all the way. What gives?

The collapse works by manipulating the height property. If an element has vertical padding, that padding expandes the size of the element, even if it has height: 0; overflow: hidden.

To avoid this, simply move that padding from the element to an element directly nested within in.

// from
<div {...getCollapseProps({style: {padding: 20}})}
  This will do weird things
</div>

// to
<div {...getCollapseProps()}
  <div style={{padding: 20}}>
    Much better!
  </div>
</div>
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].