Skip to content

Commit

Permalink
Block theme: Fix preview height on my patterns page
Browse files Browse the repository at this point in the history
This brings back the dynamic iframe height, but only when the preview
  • Loading branch information
ryelle committed Apr 11, 2024
1 parent f242b52 commit 7230772
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'url' => $view_url,
'previewWidth' => 1200,
'previewHeight' => 200,
'isControlled' => true,
];
$encoded_state = wp_json_encode( $init_state );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
margin: 0;
display: block;
border-radius: var(--wp--custom--wporg-pattern-preview--border--radius);
pointer-events: auto;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,33 @@ const { actions, state } = store( 'wporg/patterns/preview', {
},
updatePreviewHeight() {
const context = getContext();
context.previewHeight = 600;
const { ref } = getElement();

// If this is a "controlled" preview (has the toggles and
// drag handles), it should also have a fixed height.
if ( context.isControlled ) {
context.previewHeight = 600;
return;
}

// Need to "use" previewWidth so that `data-wp-watch` will re-run this action when it changes.
context.previewWidth; // eslint-disable-line no-unused-expressions

const iframeDoc = ref.contentDocument;
const height = iframeDoc.querySelector( '.entry-content' )?.clientHeight;
if ( height ) {
context.previewHeight = height * state.scale;
}
},
handleOnResize() {
const context = getContext();
const { ref } = getElement();

// Back up to the block container, so that this works regardless
// of which element interaction triggered it.
const container = ref.closest( '.wp-block-wporg-pattern-view-control' );
const container =
ref.closest( '.wp-block-wporg-pattern-view-control' ) ||
ref.closest( '.wp-block-wporg-pattern-preview' );
if ( container ) {
const preview = container.querySelector( '.wp-block-wporg-pattern-preview__container' );
context.pageWidth = preview?.clientWidth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'url' => $view_url,
'previewWidth' => $viewport_width,
'previewHeight' => 200,
'isControlled' => false,
];
$encoded_state = wp_json_encode( $init_state );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
iframe {
border: none;
max-width: none;
pointer-events: none;
}
}

0 comments on commit 7230772

Please sign in to comment.