Skip to content

Commit 9f3e1c8

Browse files
committed
Use Publish button as anchor for AMP Preview button
The post Publish/Update button is rendered early on (contrary to post Preview button) and so it's safer to use it to imperatively add an AMP Preview button to the block editor markup. Also, the `viewable` property of a post type get defined late, similarly to when the post Preview button gets added. That's why we assume a post type is viewable and show the AMP Preview button initially. Only if we learn that a post type is not viewable, we hide the AMP Preview button.
1 parent 541fab6 commit 9f3e1c8

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

assets/src/block-editor/constants.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ export const TEXT_BLOCKS = [
55
'core/quote',
66
'core/subhead',
77
];
8-
9-
export const POST_PREVIEW_CLASS = 'editor-post-preview';

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,46 @@ import { useSelect } from '@wordpress/data';
77
/**
88
* Internal dependencies
99
*/
10-
import { POST_PREVIEW_CLASS } from '../constants';
1110
import AmpPreviewButton from '../components/amp-preview-button';
1211

1312
/**
1413
* A wrapper for the AMP preview button that renders it immediately after the 'Post' preview button, when present.
1514
*/
1615
function WrappedAmpPreviewButton() {
17-
const root = useRef();
18-
const postPreviewButton = useRef();
16+
const root = useRef( null );
17+
const postPublishButton = useRef( null );
1918

2019
const isViewable = useSelect(
2120
( select ) => select( 'core' ).getPostType( select( 'core/editor' ).getEditedPostAttribute( 'type' ) )?.viewable,
2221
[],
2322
);
2423

2524
useEffect( () => {
26-
if ( isViewable && ! root.current && ! postPreviewButton.current ) {
27-
postPreviewButton.current = document.querySelector( `.${ POST_PREVIEW_CLASS }` );
25+
if ( ! root.current && ! postPublishButton.current ) {
26+
postPublishButton.current = document.querySelector( '.editor-post-publish-button' );
2827

2928
// Insert the AMP preview button immediately after the post preview button.
30-
if ( postPreviewButton.current ) {
29+
if ( postPublishButton.current ) {
3130
root.current = document.createElement( 'div' );
3231
root.current.className = 'amp-wrapper-post-preview';
33-
postPreviewButton.current.parentNode.insertBefore( root.current, postPreviewButton.current.nextSibling );
32+
postPublishButton.current.parentNode.insertBefore( root.current, postPublishButton.current );
3433
}
3534
}
3635

3736
return () => {
38-
if ( postPreviewButton.current && root.current ) {
39-
postPreviewButton.current.parentNode.removeChild( root.current );
37+
if ( postPublishButton.current && root.current ) {
38+
postPublishButton.current.parentNode.removeChild( root.current );
39+
root.current = null;
40+
postPublishButton.current = null;
4041
}
4142
};
42-
}, [ isViewable ] );
43+
}, [] );
4344

44-
if ( ! isViewable || ! root.current ) {
45+
// It is unlikely that AMP would be enabled for a non-viewable post type. This is why the Preview button will
46+
// always be displayed initially (when `isViewable` is undefined), preventing horizontal layout shift.
47+
// Once the `isViewable` value is defined (which is after the initial block editor load) and it is `false`,
48+
// the Preview button will be hidden causing a minor layout shift.
49+
if ( ! root.current || isViewable === false ) {
4550
return null;
4651
}
4752

0 commit comments

Comments
 (0)