Skip to content

Commit

Permalink
Add <ViewLink> component
Browse files Browse the repository at this point in the history
Displays a button with an external icon to open
a new window with the published post type
  • Loading branch information
mtias committed May 2, 2023
1 parent 272a57e commit b1ef576
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/edit-post/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import HeaderToolbar from './header-toolbar';
import MoreMenu from './more-menu';
import PostPublishButtonOrToggle from './post-publish-button-or-toggle';
import { default as DevicePreview } from '../device-preview';
import ViewLink from '../view-link';
import MainDashboardButton from './main-dashboard-button';
import { store as editPostStore } from '../../store';
import TemplateTitle from './template-title';
Expand Down Expand Up @@ -88,6 +89,7 @@ function Header( { setEntitiesSavedStatesCallback } ) {
showIconLabels={ showIconLabels }
/>
) }
<ViewLink />
<DevicePreview />
<PostPreviewButton
forceIsAutosaveable={ hasActiveMetaboxes }
Expand Down
37 changes: 37 additions & 0 deletions packages/edit-post/src/components/view-link/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { external } from '@wordpress/icons';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

export default function PreviewLink() {
const { permalink, isPublished, label } = useSelect( ( select ) => {
// Grab post type to retrieve the view_item label.
const postTypeSlug = select( editorStore ).getCurrentPostType();
const postType = select( coreStore ).getPostType( postTypeSlug );

return {
permalink: select( editorStore ).getPermalink(),
isPublished: select( editorStore ).isCurrentPostPublished(),
label: postType?.labels.view_item,
};
}, [] );

// Only render the view button if the post is published and has a permalink.
if ( ! isPublished || ! permalink ) {
return null;
}

return (
<Button
icon={ external }
label={ label || __( 'View post' ) }
href={ permalink }
target="_blank"
/>
);
}

0 comments on commit b1ef576

Please sign in to comment.