Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Fix preview width when searching or filtering patterns #115

Merged
merged 9 commits into from
Mar 15, 2023
Merged
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
6 changes: 2 additions & 4 deletions wp-modules/app/js/src/components/Patterns/PatternGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import loadable from '@loadable/component';
import { patternManager } from '../../globals';

// Hooks
import useWindowResize from '../../hooks/useWindowResize';
import useForceRerender from '../../hooks/useForceRerender';

// Components
const PatternPreview: PatternPreviewType = loadable(
Expand All @@ -29,9 +29,7 @@ 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();
useForceRerender( [ themePatterns ] );

return (
<>
Expand Down
22 changes: 22 additions & 0 deletions wp-modules/app/js/src/hooks/useForceRerender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useLayoutEffect, useState } from '@wordpress/element';

type WindowDimensions = [ number, number ];

/** Re-render the calling component when the window is resized or dependencies update. */
export default function useForceRerender< T extends unknown >(
dependencies: T[]
) {
const [ , setForceUpdate ] = useState< T[] | WindowDimensions >();

useLayoutEffect( () => {
setForceUpdate( dependencies );

function updateSizeAndRerender() {
setForceUpdate( [ window.innerWidth, window.innerHeight ] );
}

window.addEventListener( 'resize', updateSizeAndRerender );
return () =>
window.removeEventListener( 'resize', updateSizeAndRerender );
}, [ ...dependencies ] );
}
16 changes: 0 additions & 16 deletions wp-modules/app/js/src/hooks/useWindowResize.ts

This file was deleted.