forked from dahliacreative/react-collapsable
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add react-dom as peer dependency and dev dependency
- Use boolean flag instead of storing children in state. - Remove spread props and rename the name to react-collapsable
- Loading branch information
Showing
4 changed files
with
4,408 additions
and
4,208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.