Skip to content

Commit 15c713c

Browse files
committed
Reposition AMP Preview button once block editor is fully loaded
1 parent 9f3e1c8 commit 15c713c

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

assets/src/block-editor/plugins/wrapped-amp-preview-button.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,42 @@ import AmpPreviewButton from '../components/amp-preview-button';
1414
*/
1515
function WrappedAmpPreviewButton() {
1616
const root = useRef( null );
17-
const postPublishButton = useRef( null );
17+
const referenceNode = useRef( null );
1818

1919
const isViewable = useSelect(
2020
( select ) => select( 'core' ).getPostType( select( 'core/editor' ).getEditedPostAttribute( 'type' ) )?.viewable,
2121
[],
2222
);
2323

2424
useEffect( () => {
25-
if ( ! root.current && ! postPublishButton.current ) {
26-
postPublishButton.current = document.querySelector( '.editor-post-publish-button' );
25+
if ( ! root.current && ! referenceNode.current ) {
26+
// At first, we try finding the post preview button that is visible only on small screens.
27+
// If found, we will use its next sibling so that `insertBefore` gets us to the exact location
28+
// we are looking for.
29+
referenceNode.current = document.querySelector( '.editor-post-preview' )?.nextSibling;
2730

28-
// Insert the AMP preview button immediately after the post preview button.
29-
if ( postPublishButton.current ) {
31+
// Since the mobile post preview button is rendered with a delay, we are using the post publish/update
32+
// button as a fallback. Because it is rendered early, our AMP preview button will be visible immediately.
33+
if ( ! referenceNode.current ) {
34+
referenceNode.current = document.querySelector( '.editor-post-publish-button' );
35+
}
36+
37+
if ( referenceNode.current ) {
3038
root.current = document.createElement( 'div' );
3139
root.current.className = 'amp-wrapper-post-preview';
32-
postPublishButton.current.parentNode.insertBefore( root.current, postPublishButton.current );
40+
referenceNode.current.parentNode.insertBefore( root.current, referenceNode.current );
3341
}
3442
}
3543

3644
return () => {
37-
if ( postPublishButton.current && root.current ) {
38-
postPublishButton.current.parentNode.removeChild( root.current );
45+
if ( referenceNode.current && root.current ) {
46+
referenceNode.current.parentNode.removeChild( root.current );
3947
root.current = null;
40-
postPublishButton.current = null;
48+
referenceNode.current = null;
4149
}
4250
};
43-
}, [] );
51+
// We use `isViewable` as a dependency in order to reposition the preview button once the block editor is fully loaded.
52+
}, [ isViewable ] );
4453

4554
// It is unlikely that AMP would be enabled for a non-viewable post type. This is why the Preview button will
4655
// always be displayed initially (when `isViewable` is undefined), preventing horizontal layout shift.

0 commit comments

Comments
 (0)