Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": ["@babel/plugin-proposal-export-default-from"]
"plugins": [
"@babel/plugin-proposal-export-default-from",
"@babel/plugin-transform-runtime"
]
}
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/plugin-proposal-export-default-from": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.11.0",
"@babel/preset-env": "^7.4.5",
"@babel/preset-react": "^7.0.0",
"@emotion/babel-preset-css-prop": "^10.0.9",
Expand All @@ -45,20 +46,23 @@
"babel-plugin-emotion": "^10.0.13",
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0",
"enzyme-to-json": "^3.3.5",
"eslint": "^5.16.0",
"eslint-config-react-app": "^3.0.8",
"eslint-plugin-flowtype": "^2.50.3",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.12.4",
"jest": "^24.8.0",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"enzyme-to-json": "^3.3.5",
"prop-types": "^15.7.2"
"react-dom": "^16.8.6"
},
"peerDependencies": {
"@emotion/core": "^10.0.10",
"react": "^16.8.6"
"prop-types": "^15.7.2",
"@babel/runtime": "^7.11.2",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"jest": {
"collectCoverageFrom": [
Expand Down
30 changes: 10 additions & 20 deletions src/__snapshots__/spec.js.snap
Original file line number Diff line number Diff line change
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>
`;
30 changes: 22 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@ const Collapsable = ({
maxAnimationDuration,
speedDivider,
easing,
children
children,
keepChildrenOnClose
}) => {
const content = useRef()
const [state, setState] = useState({
height: isOpen ? 'auto' : 0,
speed: 0
})

const [keepChildren, toggleChildren] = useState(isOpen)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this just go into the state object above?


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

if (!keepChildrenOnClose) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this and just add it to the conditional below {(keepChildren || keepChildrenOnClose) && children}
Perhaps also change keepChildren to something like displayChildren or shouldDisplayChildren feels a bit clearer.

// remove child
toggleChildren(false)
}
}
}

useEffect(() => {
if (isOpen) {
const contentHeight = content.current.scrollHeight
Expand All @@ -26,6 +40,7 @@ const Collapsable = ({
? maxAnimationDuration
: time
content.current.style.visibility = 'visible'
toggleChildren(true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again if we put this in the state object you can just set this below.

setState({
...state,
height: contentHeight,
Expand All @@ -36,16 +51,13 @@ const Collapsable = ({
...state,
height: 0
})
setTimeout(() => {
content.current.style.visibility = 'hidden'
}, state.speed * speedDivider)
}
}, [
isOpen,
minAnimationDuration,
maxAnimationDuration,
speedDivider,
children
keepChildren
])

return (
Expand All @@ -54,9 +66,10 @@ const Collapsable = ({
overflow: 'hidden',
height: state.height,
transition: `height ${state.speed}s ${easing}`
}}>
}}
onTransitionEnd={handleClose}>
<div ref={content} style={{ overflow: 'auto' }}>
{children}
{keepChildren && children}
</div>
</div>
)
Expand All @@ -67,7 +80,8 @@ Collapsable.propTypes = {
minAnimationDuration: PropTypes.number,
maxAnimationDuration: PropTypes.number,
speedDivider: PropTypes.number,
easing: PropTypes.string
easing: PropTypes.string,
keepChildrenOnClose: PropTypes.bool
}

Collapsable.defaultProps = {
Expand Down
Loading