Gathering detailed insights and metrics for @gaearon/react-collapsed
Gathering detailed insights and metrics for @gaearon/react-collapsed
Gathering detailed insights and metrics for @gaearon/react-collapsed
Gathering detailed insights and metrics for @gaearon/react-collapsed
Headless UI for for creating flexible and accessible expand/collapse components in TS/JS.
npm install @gaearon/react-collapsed
Typescript
Module System
Min. Node Version
Node Version
NPM Version
66.5
Supply Chain
92
Quality
74.9
Maintenance
100
Vulnerability
100
License
react-collapsed@4.2.0
Updated on Dec 23, 2024
@collapsed/react@5.1.0
Updated on Dec 23, 2024
react-collapsed@4.1.2
Updated on Nov 16, 2023
react-collapsed@4.1.1
Updated on Oct 23, 2023
react-collapsed@4.1.0
Updated on Oct 16, 2023
react-collapsed@4.0.4
Updated on Jul 18, 2023
TypeScript (90.74%)
JavaScript (5.48%)
CSS (3.31%)
HTML (0.47%)
Total Downloads
29,403
Last Day
4
Last Week
51
Last Month
223
Last Year
3,114
MIT License
514 Stars
180 Commits
40 Forks
5 Watchers
17 Branches
9 Contributors
Updated on Apr 10, 2025
Minified
Minified + Gzipped
Latest Version
3.3.0-patched-react-18
Package Id
@gaearon/react-collapsed@3.3.0-patched-react-18
Unpacked Size
120.87 kB
Size
21.97 kB
File Count
26
NPM Version
8.1.2
Node Version
16.13.2
Cumulative downloads
Total Downloads
Last Day
0%
4
Compared to previous day
Last Week
-29.2%
51
Compared to previous week
Last Month
-18.9%
223
Compared to previous month
Last Year
-82%
3,114
Compared to previous year
2
41
A custom hook for creating accessible expand/collapse components in React. Animates the height using CSS transitions from 0
to auto
.
auto
included!useCollapse
provides the necessary props, you control the styles and the elements.1$ yarn add react-collapsed 2# or 3$ npm i react-collapsed
1import React from 'react' 2import useCollapse from 'react-collapsed' 3 4function Demo() { 5 const { getCollapseProps, getToggleProps, isExpanded } = useCollapse() 6 7 return ( 8 <div> 9 <button {...getToggleProps()}> 10 {isExpanded ? 'Collapse' : 'Expand'} 11 </button> 12 <section {...getCollapseProps()}>Collapsed content 🙈</section> 13 </div> 14 ) 15}
1import React, { useState } from 'react' 2import useCollapse from 'react-collapsed' 3 4function Demo() { 5 const [isExpanded, setExpanded] = useState(false) 6 const { getCollapseProps, getToggleProps } = useCollapse({ isExpanded }) 7 8 return ( 9 <div> 10 <button 11 {...getToggleProps({ 12 onClick: () => setExpanded((prevExpanded) => !prevExpanded), 13 })} 14 > 15 {isExpanded ? 'Collapse' : 'Expand'} 16 </button> 17 <section {...getCollapseProps()}>Collapsed content 🙈</section> 18 </div> 19 ) 20}
1const { getCollapseProps, getToggleProps, isExpanded, setExpanded } =
2 useCollapse({
3 isExpanded: boolean,
4 defaultExpanded: boolean,
5 expandStyles: {},
6 collapseStyles: {},
7 collapsedHeight: 0,
8 easing: string,
9 duration: number,
10 onCollapseStart: func,
11 onCollapseEnd: func,
12 onExpandStart: func,
13 onExpandEnd: func,
14 })
useCollapse
ConfigThe 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 |
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 |
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.
1// from 2<div {...getCollapseProps({style: {padding: 20}})} 3 This will do weird things 4</div> 5 6// to 7<div {...getCollapseProps()} 8 <div style={{padding: 20}}> 9 Much better! 10 </div> 11</div>
No vulnerabilities found.
No security vulnerabilities found.