-
Notifications
You must be signed in to change notification settings - Fork 6
Fix issue with preview width when window is resized #109
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 |
---|---|---|
|
@@ -7,6 +7,9 @@ import loadable from '@loadable/component'; | |
// Globals | ||
import { patternManager } from '../../globals'; | ||
|
||
// Hooks | ||
import useWindowResize from '../../hooks/useWindowResize'; | ||
|
||
// Components | ||
const PatternPreview: PatternPreviewType = loadable( | ||
async () => import( '../PatternPreview' ) | ||
|
@@ -26,6 +29,10 @@ type Props = { | |
|
||
/** Render the patterns in a grid, or a message if no patterns are found. */ | ||
export default function PatternGrid( { themePatterns }: Props ) { | ||
// If the window is resized, trigger a fresh render of the grid. | ||
// Helps ensure PatternPreview iFrames are the right size. | ||
useWindowResize(); | ||
Comment on lines
+32
to
+34
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.
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. Really good idea to keep re-rendering to this low scope |
||
|
||
return ( | ||
<> | ||
{ ! Object.entries( themePatterns ?? {} ).length ? ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useLayoutEffect, useState } from '@wordpress/element'; | ||
|
||
/** Rerender the calling component when the window is resized. */ | ||
export default function useWindowResize() { | ||
const [ , setWindowSize ] = useState( [ 0, 0 ] ); | ||
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. We could also add a 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. That's a nice trick to force a re-render. The fact that there's a no |
||
|
||
useLayoutEffect( () => { | ||
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. Nice use of |
||
function updateSizeAndRerender() { | ||
setWindowSize( [ window.innerWidth, window.innerHeight ] ); | ||
} | ||
|
||
window.addEventListener( 'resize', updateSizeAndRerender ); | ||
return () => | ||
window.removeEventListener( 'resize', updateSizeAndRerender ); | ||
}, [] ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Derived state needs to be used for
previewContainerSize
for this idea to work.With it, a fresh
getBoundingClientRect()
is called each time the component is rendered.Otherwise,
useEffect
is not called again after initial render since thepreviewContainer
dependency does not update, making it harder to callsetPreviewContainerSize
with the new bounding client rect.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea to use derived state