Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Site Editor: refactor close button slot #22179

Merged
merged 3 commits into from
May 8, 2020
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
1 change: 1 addition & 0 deletions docs/designers-developers/developers/slotfills/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const PostStatus = ( { isOpened, onTogglePanel } ) => (

There are currently eight available SlotFills in the `edit-post` package. Please refer to the individual items below for usage and example details:

* [MainDashboardButton](/docs/designers-developers/developers/slotfills/main-dashboard-button.md)
* [PluginBlockSettingsMenuItem](/docs/designers-developers/developers/slotfills/plugin-block-settings-menu-item.md)
* [PluginDocumentSettingPanel](/docs/designers-developers/developers/slotfills/plugin-document-setting-panel.md)
* [PluginMoreMenuItem](/docs/designers-developers/developers/slotfills/plugin-more-menu-item.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# MainDashboardButton

This slot allows replacing the default main dashboard button that's used for closing
the editor in fullscreen mode.

## Example

```js
import { registerPlugin } from '@wordpress/plugins';
import { __experimentalMainDashboardButton } from '@wordpress/edit-site';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this slot is just an edit-site slot? Would we need to offer the same for the other screens. Also, if the fallback button is the same across screens, should the slot be the same across screens too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't block this PR. I think it's ready.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this slot is just an edit-site slot?

Yes.

Would we need to offer the same for the other screens. Also, if the fallback button is the same across screens, should the slot be the same across screens too?

I was planning to make a separate slot for post editor. The rationale being that someone might want to provide a different component in other screens. Or it might look the same but we might want it to behave differently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% certain we need a separate component especially if we add support to more and more screens (widgets...). Also it seems possible to enqueue the extension based on the screen or check the screen and change the behavior.

Copy link
Member Author

@vindl vindl May 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it seems possible to enqueue the extension based on the screen or check the screen and change the behavior.

That's true. Since I was planning on starting a separate PR for post editor, would it be fine if I consolidate this to use the same slot in it and merge this as is for now?

Or I could expand this PR to take care of both. 🤔I generally prefer to keep them smaller.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should merge this and see in a separate PR if we can have the same slot.


const MainDashboardButtonTest = () => (
<__experimentalMainDashboardButton>
Custom main dashboard button content
</__experimentalMainDashboardButton>
);

registerPlugin( 'main-dashboard-button-test', {
render: MainDashboardButtonTest,
} );
```

If your goal is just to replace the icon of the existing button, that can be achieved
in the following way:

```js
import { registerPlugin } from '@wordpress/plugins';
import {
__experimentalMainDashboardButton,
__experimentalFullscrenModeClose
} from '@wordpress/edit-site';
import { close } from '@wordpress/icons';


const MainDashboardButtonIconTest = () => (
<__experimentalMainDashboardButton>
<__experimentalFullscrenModeClose icon={ close } />
</__experimentalMainDashboardButton>
);

registerPlugin( 'main-dashboard-button-icon-test', {
render: MainDashboardButtonIconTest,
} );
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
*/
import { useSelect } from '@wordpress/data';
import { Button } from '@wordpress/components';
import { Path, SVG } from '@wordpress/primitives';
import { __ } from '@wordpress/i18n';
import { wordpress } from '@wordpress/icons';

const wordPressLogo = (
<SVG width="28" height="28" viewBox="0 0 128 128" version="1.1">
<Path d="M100 61.3c0-6.6-2.4-11.2-4.4-14.7-2.7-4.4-5.2-8.1-5.2-12.5 0-4.9 3.7-9.5 9-9.5h.7c-9.5-8.7-22.1-14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3-2.4-18-6.5-25.6.2 1.7.4 3.5.4 5.5z" />
</SVG>
);

function FullscreenModeClose() {
function FullscreenModeClose( { icon } ) {
const isActive = useSelect( ( select ) => {
return select( 'core/edit-site' ).isFeatureActive( 'fullscreenMode' );
}, [] );
Expand All @@ -21,10 +15,12 @@ function FullscreenModeClose() {
return null;
}

const buttonIcon = icon || wordpress;

return (
<Button
className="edit-site-fullscreen-mode-close"
icon={ wordPressLogo }
icon={ buttonIcon }
iconSize={ 36 }
href="index.php"
label={ __( 'Back' ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
background: #23282e; // WP-admin gray.
color: $white;
border-radius: 0;
height: auto;
height: $header-height;
width: $header-height;

&:hover {
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { PinnedItems } from '@wordpress/interface';
* Internal dependencies
*/
import { useEditorContext } from '../editor';
import MainDashboardButton from './main-dashboard-button';
import MoreMenu from './more-menu';
import TemplateSwitcher from '../template-switcher';
import SaveButton from '../save-button';
import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
import { CloseButton } from './main-dashboard-button';

const inserterToggleProps = { isPrimary: true };

Expand Down Expand Up @@ -64,7 +64,7 @@ export default function Header( { openEntitiesSavedStates } ) {

return (
<div className="edit-site-header">
<MainDashboardButton.Slot />
<CloseButton />
<div className="edit-site-header__toolbar">
<Inserter
position="bottom right"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
import { createSlotFill } from '@wordpress/components';
import {
__experimentalUseSlot as useSlot,
createSlotFill,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import FullscreenModeClose from '../fullscreen-mode-close';

const { Fill: MainDashboardButton, Slot } = createSlotFill(
'SiteEditorMainDashboardButton'
);

MainDashboardButton.Slot = () => (
<Slot>
{ ( fills ) => {
// Return default Close button if no fills are provided, otherwise replace it with available fills.
if ( isEmpty( fills ) ) {
return <FullscreenModeClose />;
}

return <> { fills } </>;
} }
</Slot>
);
const name = '__experimentalSiteEditorMainDashboardButton';

const { Fill, Slot } = createSlotFill( name );

const MainDashboardButton = Fill;
MainDashboardButton.Slot = Slot;
MainDashboardButton.slotName = name;

export const CloseButton = () => {
const slot = useSlot( MainDashboardButton.slotName );
const hasFills = Boolean( slot.fills && slot.fills.length );

if ( ! hasFills ) {
return <FullscreenModeClose />;
}

return <MainDashboardButton.Slot bubblesVirtually />;
};

export default MainDashboardButton;
3 changes: 2 additions & 1 deletion packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export function initialize( id, settings ) {
render( <Editor settings={ settings } />, document.getElementById( id ) );
}

export { default as MainDashboardButton } from './components/header/main-dashboard-button';
export { default as __experimentalMainDashboardButton } from './components/header/main-dashboard-button';
export { default as __experimentalFullscrenModeClose } from './components/header/fullscreen-mode-close';