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

Inserter: Add close button #61421

Merged
merged 4 commits into from
May 8, 2024
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
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function InserterLibrary(
__experimentalOnPatternCategorySelection,
onSelect = noop,
shouldFocusBlock = false,
onClose,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not great to pass the props here, but I can't find a way to access this state without passing it down.

Copy link
Contributor

Choose a reason for hiding this comment

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

This will get undone when we refactor the List View and Inserter to share the same sidebar, so I think it's OK.

},
ref
) {
Expand Down Expand Up @@ -54,6 +55,7 @@ function InserterLibrary(
}
shouldFocusBlock={ shouldFocusBlock }
ref={ ref }
onClose={ onClose }
/>
);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function InserterMenu(
__experimentalFilterValue = '',
shouldFocusBlock = true,
__experimentalOnPatternCategorySelection = NOOP,
onClose,
},
ref
) {
Expand Down Expand Up @@ -301,7 +302,11 @@ function InserterMenu(
ref={ ref }
>
<div className="block-editor-inserter__main-area">
<InserterTabs ref={ tabsRef } onSelect={ handleSetSelectedTab }>
<InserterTabs
ref={ tabsRef }
onSelect={ handleSetSelectedTab }
onClose={ onClose }
>
{ inserterSearch }
{ selectedTab === 'blocks' &&
! delayedFilterValue &&
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/inserter/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ $block-inserter-tabs-height: 44px;
overflow: hidden;

.block-editor-inserter__tablist {
border-bottom: $border-width solid $gray-300;
width: 100%;

button[role="tab"] {
flex-grow: 1;
Expand Down
40 changes: 27 additions & 13 deletions packages/block-editor/src/components/inserter/tabs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/**
* WordPress dependencies
*/
import { privateApis as componentsPrivateApis } from '@wordpress/components';
import {
Button,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { forwardRef } from '@wordpress/element';
import { closeSmall } from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -29,23 +33,33 @@ const mediaTab = {
title: __( 'Media' ),
};

function InserterTabs( { onSelect, children }, ref ) {
function InserterTabs( { onSelect, children, onClose }, ref ) {
const tabs = [ blocksTab, patternsTab, mediaTab ];

return (
<div className="block-editor-inserter__tabs" ref={ ref }>
<Tabs onSelect={ onSelect }>
<Tabs.TabList className="block-editor-inserter__tablist">
{ tabs.map( ( tab ) => (
<Tabs.Tab
key={ tab.name }
tabId={ tab.name }
className="block-editor-inserter__tab"
>
{ tab.title }
</Tabs.Tab>
) ) }
</Tabs.TabList>
<div className="block-editor-inserter-sidebar__header">
<Button
className="block-editor-inserter-sidebar__close-button"
icon={ closeSmall }
label={ __( 'Close block inserter' ) }
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the label always be block inserter? Should we have a generic label for the inserter or update per selected tab?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to update it, in a different PR :)

onClick={ () => onClose() }
size="small"
/>

<Tabs.TabList className="block-editor-inserter__tablist">
{ tabs.map( ( tab ) => (
<Tabs.Tab
key={ tab.name }
tabId={ tab.name }
className="block-editor-inserter__tab"
>
{ tab.title }
</Tabs.Tab>
) ) }
</Tabs.TabList>
</div>
{ tabs.map( ( tab ) => (
<Tabs.TabPanel
key={ tab.name }
Expand Down
12 changes: 1 addition & 11 deletions packages/editor/src/components/inserter-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { Button, VisuallyHidden } from '@wordpress/components';
import { close } from '@wordpress/icons';
import {
__experimentalLibrary as Library,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useViewportMatch } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useRef } from '@wordpress/element';
import { store as preferencesStore } from '@wordpress/preferences';

Expand Down Expand Up @@ -50,18 +47,10 @@ export default function InserterSidebar( {
const { setIsInserterOpened } = useDispatch( editorStore );

const isMobileViewport = useViewportMatch( 'medium', '<' );
const TagName = ! isMobileViewport ? VisuallyHidden : 'div';
const libraryRef = useRef();

return (
<div className="editor-inserter-sidebar">
<TagName className="editor-inserter-sidebar__header">
<Button
icon={ close }
label={ __( 'Close block inserter' ) }
onClick={ () => setIsInserterOpened( false ) }
/>
</TagName>
<div className="editor-inserter-sidebar__content">
<Library
showMostUsedBlocks={ showMostUsedBlocks }
Expand All @@ -78,6 +67,7 @@ export default function InserterSidebar( {
isRightSidebarOpen ? closeGeneralSidebar : undefined
}
ref={ libraryRef }
onClose={ () => setIsInserterOpened( false ) }
/>
</div>
</div>
Expand Down
11 changes: 8 additions & 3 deletions packages/editor/src/components/inserter-sidebar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
flex-direction: column;
}

.editor-inserter-sidebar__header {
padding-top: $grid-unit-10;
.block-editor-inserter-sidebar__header {
border-bottom: $border-width solid $gray-300;
padding-right: $grid-unit-10;
display: flex;
justify-content: flex-end;
justify-content: space-between;

.block-editor-inserter-sidebar__close-button {
order: 1;
align-self: center;
}
}

.editor-inserter-sidebar__content {
Expand Down
Loading