Skip to content

Commit

Permalink
- Add react-dom as peer dependency and dev dependency
Browse files Browse the repository at this point in the history
- Use boolean flag instead of storing children in state.
- Remove spread props and rename the name to react-collapsable
  • Loading branch information
s-yadav committed Sep 17, 2020
1 parent 8339f45 commit abc4379
Show file tree
Hide file tree
Showing 4 changed files with 4,408 additions and 4,208 deletions.
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -55,12 +55,14 @@
"eslint-plugin-react": "^7.12.4",
"jest": "^24.8.0",
"prop-types": "^15.7.2",
"react": "^16.8.6"
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"peerDependencies": {
"prop-types": "^15.7.2",
"@babel/runtime": "^7.11.2",
"react": "^16.8.6"
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"jest": {
"collectCoverageFrom": [
Expand Down
30 changes: 10 additions & 20 deletions src/__snapshots__/spec.js.snap
Expand Up @@ -9,6 +9,7 @@ exports[`<Collapsable/> Closes 1`] = `
speedDivider={1000}
>
<div
onTransitionEnd={[Function]}
style={
Object {
"height": 500,
Expand Down Expand Up @@ -41,6 +42,7 @@ exports[`<Collapsable/> Opens 1`] = `
speedDivider={1000}
>
<div
onTransitionEnd={[Function]}
style={
Object {
"height": 0,
Expand All @@ -55,11 +57,7 @@ exports[`<Collapsable/> Opens 1`] = `
"overflow": "auto",
}
}
>
<h1>
My Title
</h1>
</div>
/>
</div>
</Collapsable>
`;
Expand All @@ -73,6 +71,7 @@ exports[`<Collapsable/> Renders open by default 1`] = `
speedDivider={1000}
>
<div
onTransitionEnd={[Function]}
style={
Object {
"height": 500,
Expand Down Expand Up @@ -105,6 +104,7 @@ exports[`<Collapsable/> Renders without crashing 1`] = `
speedDivider={1000}
>
<div
onTransitionEnd={[Function]}
style={
Object {
"height": 0,
Expand All @@ -119,11 +119,7 @@ exports[`<Collapsable/> Renders without crashing 1`] = `
"overflow": "auto",
}
}
>
<h1>
My Title
</h1>
</div>
/>
</div>
</Collapsable>
`;
Expand All @@ -137,6 +133,7 @@ exports[`<Collapsable/> Respects maxAnimationDuration 1`] = `
speedDivider={1000}
>
<div
onTransitionEnd={[Function]}
style={
Object {
"height": 0,
Expand All @@ -151,11 +148,7 @@ exports[`<Collapsable/> Respects maxAnimationDuration 1`] = `
"overflow": "auto",
}
}
>
<h1>
My Title
</h1>
</div>
/>
</div>
</Collapsable>
`;
Expand All @@ -169,6 +162,7 @@ exports[`<Collapsable/> Respects minAnimationDuration 1`] = `
speedDivider={1000}
>
<div
onTransitionEnd={[Function]}
style={
Object {
"height": 0,
Expand All @@ -183,11 +177,7 @@ exports[`<Collapsable/> Respects minAnimationDuration 1`] = `
"overflow": "auto",
}
}
>
<h1>
My Title
</h1>
</div>
/>
</div>
</Collapsable>
`;
172 changes: 84 additions & 88 deletions src/index.js
@@ -1,99 +1,95 @@
import React, { useRef, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import React, { useRef, useState, useEffect } from 'react'
import PropTypes from 'prop-types'

const Collapsible = ({
isOpen,
minAnimationDuration,
maxAnimationDuration,
speedDivider,
easing,
children,
keepChildrenOnClose,
...restProps
const Collapsable = ({
isOpen,
minAnimationDuration,
maxAnimationDuration,
speedDivider,
easing,
children,
keepChildrenOnClose
}) => {
const content = useRef();
const [state, setState] = useState({
height: isOpen ? 'auto' : 0,
speed: 0,
});
const content = useRef()
const [state, setState] = useState({
height: isOpen ? 'auto' : 0,
speed: 0
})

const [effectiveChildren, setChildren] = useState(children);
const [keepChildren, toggleChildren] = useState(isOpen)

const handleClose = () => {
if (!isOpen) {
content.current.style.visibility = 'hidden';
const handleClose = () => {
if (!isOpen) {
content.current.style.visibility = 'hidden'

if (!keepChildrenOnClose) {
// remove child
setChildren(null);
}
if (!keepChildrenOnClose) {
// remove child
toggleChildren(false)
}
}
}
};

useEffect(() => {
if (isOpen) {
const contentHeight = content.current.scrollHeight;
const time = contentHeight / speedDivider;
const animation =
time < minAnimationDuration
? minAnimationDuration
: time > maxAnimationDuration
? maxAnimationDuration
: time;
content.current.style.visibility = 'visible';
setChildren(children);
setState({
...state,
height: contentHeight,
speed: animation,
});
} else {
setState({
...state,
height: 0,
});
}
}, [
isOpen,
minAnimationDuration,
maxAnimationDuration,
speedDivider,
children,
effectiveChildren,
]);
useEffect(() => {
if (isOpen) {
const contentHeight = content.current.scrollHeight
const time = contentHeight / speedDivider
const animation =
time < minAnimationDuration
? minAnimationDuration
: time > maxAnimationDuration
? maxAnimationDuration
: time
content.current.style.visibility = 'visible'
toggleChildren(true)
setState({
...state,
height: contentHeight,
speed: animation
})
} else {
setState({
...state,
height: 0
})
}
}, [
isOpen,
minAnimationDuration,
maxAnimationDuration,
speedDivider,
keepChildren
])

return (
<div
{...restProps}
style={{
overflow: 'hidden',
height: state.height,
transition: `height ${state.speed}s ${easing}`,
}}
onTransitionEnd={handleClose}
>
<div ref={content} style={{ overflow: 'auto' }}>
{effectiveChildren}
</div>
</div>
);
};
return (
<div
style={{
overflow: 'hidden',
height: state.height,
transition: `height ${state.speed}s ${easing}`
}}
onTransitionEnd={handleClose}>
<div ref={content} style={{ overflow: 'auto' }}>
{keepChildren && children}
</div>
</div>
)
}

Collapsible.propTypes = {
isOpen: PropTypes.bool,
minAnimationDuration: PropTypes.number,
maxAnimationDuration: PropTypes.number,
speedDivider: PropTypes.number,
easing: PropTypes.string,
keepChildrenOnClose: PropTypes.bool,
};
Collapsable.propTypes = {
isOpen: PropTypes.bool,
minAnimationDuration: PropTypes.number,
maxAnimationDuration: PropTypes.number,
speedDivider: PropTypes.number,
easing: PropTypes.string,
keepChildrenOnClose: PropTypes.bool
}

Collapsible.defaultProps = {
isOpen: false,
minAnimationDuration: 0.3,
maxAnimationDuration: 1,
speedDivider: 1000,
easing: 'ease-in-out',
};
Collapsable.defaultProps = {
isOpen: false,
minAnimationDuration: 0.3,
maxAnimationDuration: 1,
speedDivider: 1000,
easing: 'ease-in-out'
}

export default Collapsible;
export default Collapsable

0 comments on commit abc4379

Please sign in to comment.