Gathering detailed insights and metrics for react-collapse
Gathering detailed insights and metrics for react-collapse
Gathering detailed insights and metrics for react-collapse
Gathering detailed insights and metrics for react-collapse
Component-wrapper for collapse animation with react-motion for elements with variable (and dynamic) height
npm install react-collapse
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,129 Stars
664 Commits
113 Forks
8 Watching
4 Branches
29 Contributors
Updated on 03 Nov 2024
JavaScript (94.15%)
CSS (5.85%)
Cumulative downloads
Total Downloads
Last day
-5.1%
29,492
Compared to previous day
Last week
1.6%
150,697
Compared to previous week
Last month
17.3%
638,445
Compared to previous month
Last year
8%
6,719,011
Compared to previous year
1
30
Component-wrapper for collapse animation for elements with variable (and dynamic) height
http://nkbt.github.io/react-collapse
http://codepen.io/nkbt/pen/MarzEg
1npm install --save react-collapse
1yarn add react-collapse
1<script src="https://unpkg.com/react/umd/react.production.min.js"></script> 2<script src="https://unpkg.com/react-collapse/build/react-collapse.min.js"></script> 3(Module exposed as `ReactCollapse`)
1.ReactCollapse--collapse { 2 transition: height 500ms; 3}
IMPORTANT: Without adding a CSS transition there will be no animation and component will open/close instantly.
1import {Collapse} from 'react-collapse'; 2 3// ... 4<Collapse isOpened={true || false}> 5 <div>Random content</div> 6</Collapse>
Use Unmount
component provided as:
1import {UnmountClosed} from 'react-collapse'; 2 3// ... 4<UnmountClosed isOpened={true || false}> 5 <div>Random content</div> 6</UnmountClosed>
Example example/App/AutoUnmount.js
If you want a controlled and accessible implementation, check out this example
isOpened
: PropTypes.boolean.isRequiredExpands or collapses content.
children
: PropTypes.node.isRequiredOne or multiple children with static, variable or dynamic height.
1<Collapse isOpened={true}> 2 <p>Paragraph of text</p> 3 <p>Another paragraph is also OK</p> 4 <p>Images and any other content are ok too</p> 5 <img src="nyancat.gif" /> 6</Collapse>
theme
: PropTypes.objectOf(PropTypes.string)It is possible to set className
for extra div
elements that ReactCollapse creates.
Example:
1<Collapse theme={{collapse: 'foo', content: 'bar'}}> 2 <div>Customly animated container</div> 3</Collapse>
Default values:
1const theme = { 2 collapse: 'ReactCollapse--collapse', 3 content: 'ReactCollapse--content' 4}
Which ends up in the following markup:
1<div class="ReactCollapse--collapse"> 2 <div class="ReactCollapse--content"> 3 {children} 4 </div> 5</div>
IMPORTANT: these are not style objects, but class names!
onRest
, onWork
: PropTypes.funcCallback functions, triggered when animation has completed (onRest
) or has just started (onWork
)
Both functions are called with argument:
1const arg = { 2 isFullyOpened: true || false, // `true` only when Collapse reached final height 3 isFullyClosed: true || false, // `true` only when Collapse is fully closed and height is zero 4 isOpened: true || false, // `true` if Collapse has any non-zero height 5 containerHeight: 123, // current pixel height of Collapse container (changes until reaches `contentHeight`) 6 contentHeight: 123 // determined height of supplied Content 7}
1<Collapse onRest={console.log} onWork={console.log}> 2 <div>Container text</div> 3</Collapse>
Example example/App/Hooks.js
initialStyle
: PropTypes.shape1initialStyle: PropTypes.shape({
2 height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
3 overflow: PropTypes.string
4})
You may control initial element style, for example to force initial animation from 0 to height by using initialStyle={{height: '0px', overflow: 'hidden'}}
IMPORTANT Any updates to this prop will be ignored after first render.
Default value is determined based on initial isOpened
value:
1 initialStyle = props.isOpened 2 ? {height: 'auto', overflow: 'initial'} 3 : {height: '0px', overflow: 'hidden'};
Example: example/App/ForceInitialAnimation.js
checkTimeout
: PropTypes.numberNumber in ms
.
Collapse will check height after thins timeout to determine if animation is completed, the shorter the number - the faster onRest
will be triggered and the quicker height: auto
will be applied. The downside - more calculations.
Default value is: 50
.
IMPORTANT Collapse does not support any pass-through props, so any non-supported props will be ignored
Because we need to have control over when Collapse
component is updated and it is not possible or very hard to achieve when any random props can be passed to the component.
initially opened Collapse elements will be statically rendered with no animation. You can override this behaviour by using initialStyle
prop
due to the complexity of margins and their potentially collapsible nature, ReactCollapse
does not support (vertical) margins on their children. It might lead to the animation "jumping" to its correct height at the end of expanding. To avoid this, use padding instead of margin.
See #101 for more details
v4
to v5
v5
was another complete rewrite that happened quite a while ago, it was published as @nkbt/react-collapse
and tested in real projects for a long time and now fully ready to be used.
In the most common scenario upgrade is trivial (add CSS transition to collapse element), but if you were using a few deprecated props - there might be some extra work required.
Luckily almost every deprecated callback or prop has fully working analogue in v5
. Unfortunately not all of them could maintain full backward compatibility, so please check this migration guide below.
New Collapse does not implement animations anymore, it only determines Content
height and updates Collapse
wrapper height to match it.
Only after Collapse
height reaches Content
height (animation finished), Collapse's style is updated to have height: auto; overflow: initial
.
The implications is that you will need to update your CSS with transition:
1.ReactCollapse--collapse { 2 transition: height 500ms; 3}
IMPORTANT: Without adding a CSS transition there will be no animation and component will open/close instantly.
onRest
/onWork
callbacks (see above for full description). Though onRest
did exist previously, now it is called with arguments containing few operational params and flags.
initialStyle
you may control initial element style, for example to force initial animation from 0 to height by using initialStyle={{height: '0px', overflow: 'hidden'}}
IMPORTANT Any updates to this prop will be ignored after first render.
Default value is:
1 initialStyle = props.isOpened 2 ? {height: 'auto', overflow: 'initial'} 3 : {height: '0px', overflow: 'hidden'};
checkTimeout
number in ms
. Collapse will check height after thins timeout to determine if animation is completed, the shorter the number - the faster onRest
will be triggered and the quicker height: auto
will be applied. The downside - more calculations.
Default value is: 50
.
v5
)Pass-through props - any unknown props passed to Collapse
component will be ignored
hasNestedCollapse - no longer necessary, as v5 does not care if there are nested Collapse elements, see example/App/Nested.js
fixedHeight - no longer necessary, just set whatever height you need for content element and Collapse will work as expected, see example/App/VariableHeight.js
springConfig - as new Collapse relies on simple CSS transitions (or your own implementation of animation) and does not use react-collapse, springConfig is no longer necessary. You can control control animation with css like
1.ReactCollapse--collapse { 2 transition: height 500ms; 3}
forceInitialAnimation - you can use new prop initialStyle={{height: '0px', overflow: 'hidden'}}
instead, so when new height will be set after component is rendered - it should naturally animate.
onMeasure - please use onRest
and onWork
instead and pick contentHeight
from argument
1<Collapse 2 onRest={({contentHeight}) => console.log(contentHeight)} 3 onWork={({contentHeight}) => console.log(contentHeight)}> 4 <div>content</div> 5</Collapse>
onRender - since animations are fully controlled by external app (e.g. with CSS) we no draw each frame and do not actually re-render component anymore, so it is impossible to have onRender
callback
Currently is being developed and tested with the latest stable Node
on OSX
.
To run example covering all ReactCollapse
features, use yarn start
, which will compile example/Example.js
1git clone git@github.com:nkbt/react-collapse.git 2cd react-collapse 3yarn install 4yarn start 5 6# then 7open http://localhost:8080
1# to run ESLint check 2yarn lint 3 4# to run tests 5yarn test
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/12 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
66 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More