Skip to content
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

Recalculate on window resize and <details> toggle #17

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useEffect, useRef} from 'react'
import React, {useEffect, useRef, useState} from 'react'
import './index.css'

const myClassName = 'react-auto-height'
Expand Down Expand Up @@ -115,6 +115,27 @@ function AutoHeight({children, element = 'div', className: propClassName = '', .

useEffect(updateHeight)

// recalculate height on <details> toggle and window resize by forcing rerender
const [_, setStateToRerender] = useState()
useEffect(() => {
let isMounted = true
Copy link

Choose a reason for hiding this comment

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

is this in case the eventlistners don't get destroyed properly? Or more to handle possible race conditions?

Copy link
Owner Author

@Aprillion Aprillion Jul 6, 2023

Choose a reason for hiding this comment

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

yeah, the useless memory leak warning that was removed in React 18 facebook/react#22114 ... but the library supports React 17 too, so let's not scare ourselves when using this library in Stampy 😅

Copy link
Owner Author

Choose a reason for hiding this comment

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

and you are correct, it sets state to rerender .. moving code from https://github.com/StampyAI/stampy-ui/blob/3e3ae3ec4e7f4f1ad5a3ef70c3daf424eab92a3e/app/hooks/useRerenderOnResize.ts - but it needs to be new object every time to work, the set(empty) was a bug, so let's fix that here in the library and not in distant user code

const forceRerender = (e) => {
setTimeout(() => {
if (isMounted) setStateToRerender({})
})
}
addEventListener('toggle', forceRerender, true)
addEventListener('resize', forceRerender, true)
addEventListener('orientationchange', forceRerender, true)

return () => {
isMounted = false
removeEventListener('toggle', forceRerender)
removeEventListener('resize', forceRerender)
removeEventListener('orientationchange', forceRerender)
}
}, [])

return (
<Element ref={ref} className={`${myClassName} ${propClassName}`} {...props}>
{typeof children === 'function' ? children(updateHeight) : children}
Expand Down
33 changes: 33 additions & 0 deletions stories/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,36 @@ storiesOf('AutoHeight')
</>
)
})
.add('window resize and <details> element toggle', () => {
const [isShort, setIsShort] = useState(true)
const handleClick = () => setIsShort((prev) => !prev)
const extra = isShort ? null : (
<>
<p> ... extra paragraph 1</p>
<p> ... extra paragraph 2</p>
</>
)

return (
<>
{intro}
<pre style={{whiteSpace: 'pre-wrap'}}>
<AutoHeight>
{'<AutoHeight>\n'}
{' '}Here is some content that can wrap to multiple lines, so that resizing window
width might change the height of the parent element...
{'\n <details>'}
<details>
<summary style={{background: 'yellowgreen', cursor: 'pointer'}}>
{' <summary>Click here to expand the HTML details element</summary>'}
</summary>
{' '}This is the contents of the details element. It might also wrap to multiple
lines if the window is narrow enough.
</details>
{' </details>\n'}
{'</AutoHeight>'}
</AutoHeight>
</pre>
</>
)
})