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

Added header toolbar slotfill #66190

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added header toolbar slotfill
  • Loading branch information
rinkalpagdar committed Oct 16, 2024
commit 2161a41acc6587507d9ad906fbfefbeaa62ae5b5
4 changes: 4 additions & 0 deletions packages/editor/src/components/document-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import clsx from 'clsx';
import PluginHeaderToolbar from '../header-toolbar-slot/header-toolbar-slot';

/**
* WordPress dependencies
Expand Down Expand Up @@ -177,6 +178,9 @@ function DocumentTools( { className, disableBlockTools = false } ) {
size="compact"
/>
) }
<div className="block-editor-header-toolbar-extender">
<PluginHeaderToolbar.Slot />
</div>
</>
) }
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* WordPress dependencies
*/
import {
createSlotFill,
Button,
ToolbarItem,
Dropdown,
} from '@wordpress/components';
import warning from '@wordpress/warning';
import { plugins as pluginsIcon } from '@wordpress/icons';
import { compose } from '@wordpress/compose';
import { withPluginContext } from '@wordpress/plugins';

const { Fill, Slot } = createSlotFill( 'PluginHeaderToolbar' );

/**
* Whether the argument is a function.
*
* @param {*} maybeFunc The argument to check.
* @return {boolean} True if the argument is a function, false otherwise.
*/
function isFunction( maybeFunc ) {
return typeof maybeFunc === 'function';
}

const PluginHeaderToolbarFill = ( {
icon = pluginsIcon,
renderContent = null,
className = 'plugin-header-toolbar-button',
contentClassName = 'plugin-header-toolbar-content',
} ) => {

if ( null === renderContent || ! isFunction( renderContent ) ) {
warning(
'PluginHeaderToolbar requires renderContent property to be specified and be a valid function.'
);
return null;
}
return (
<Fill>
{ ( { showIconLabels } ) => (
<ToolbarItem
showTooltip={ ! showIconLabels }
variant={ showIconLabels ? 'tertiary' : undefined }
as={ () => (
<Dropdown
className={ className }
contentClassName={ contentClassName }
popoverProps={ { placement: 'bottom-start' } }
renderToggle={ ( { isOpen, onToggle } ) => (
<Button
onClick={ onToggle }
aria-expanded={ isOpen }
icon={ icon }
className="components-button"
/>
) }
renderContent={ renderContent }
/>
) }
/>
) }
</Fill>
);
};

const PluginHeaderToolbar = compose(
withPluginContext( ( context, ownProps ) => {
return {
icon: ownProps.icon || context.icon,
};
} )
)( PluginHeaderToolbarFill );

PluginHeaderToolbar.Slot = Slot;

export default PluginHeaderToolbar;
1 change: 1 addition & 0 deletions packages/editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ export const VisualEditorGlobalKeyboardShortcuts = EditorKeyboardShortcuts;
* and toggling the sidebar.
*/
export const TextEditorGlobalKeyboardShortcuts = EditorKeyboardShortcuts;
export { default as PluginHeaderToolbar } from './header-toolbar-slot/header-toolbar-slot';
Loading