-
Notifications
You must be signed in to change notification settings - Fork 1
Some optimisations on the component #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove this and just add it to the conditional below |
||
| // remove child | ||
| toggleChildren(false) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| if (isOpen) { | ||
| const contentHeight = content.current.scrollHeight | ||
|
|
@@ -26,6 +40,7 @@ const Collapsable = ({ | |
| ? maxAnimationDuration | ||
| : time | ||
| content.current.style.visibility = 'visible' | ||
| toggleChildren(true) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
@@ -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 ( | ||
|
|
@@ -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> | ||
| ) | ||
|
|
@@ -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 = { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.