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

Sidebar: Add a shared component for the inserter and list view #62343

Merged
merged 17 commits into from
Jul 2, 2024
4 changes: 4 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,10 @@ _Related_

- <https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#registerStore>

### TabbedSidebar
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be a public component?


Undocumented declaration.

### ToolSelector

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export { default as __experimentalPublishDateTimePicker } from './publish-date-t
export { default as __experimentalInspectorPopoverHeader } from './inspector-popover-header';
export { default as BlockPopover } from './block-popover';
export { useBlockEditingMode } from './block-editing-mode';
export { default as TabbedSidebar } from './tabbed-sidebar';
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a helpful refactor to work towards unified sidebars, so I don't want to block this progress. That said, I think it will be important to not combine the content of the sidebar with the interaction of the sidebar wrapper. I see the eventual goal as having a shared sidebar component (or another name that does not reference the visual position of the component) with a close button. The content within it can be whatever it needs to be.

This will be possible if we move to a header title for each sidebar, as proposed in #61553


/*
* State Related Components
Expand Down
51 changes: 39 additions & 12 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import { PatternCategoryPreviewPanel } from './block-patterns-tab/pattern-catego
import { MediaTab, MediaCategoryPanel } from './media-tab';
import InserterSearchResults from './search-results';
import useInsertionPoint from './hooks/use-insertion-point';
import InserterTabs from './tabs';
import { store as blockEditorStore } from '../../store';
import TabbedSidebar from '../tabbed-sidebar';

const NOOP = () => {};
function InserterMenu(
Expand Down Expand Up @@ -315,21 +315,48 @@ function InserterMenu(
ref={ ref }
>
<div className="block-editor-inserter__main-area">
<InserterTabs
<TabbedSidebar
ref={ tabsRef }
onSelect={ handleSetSelectedTab }
onClose={ onClose }
selectedTab={ selectedTab }
>
{ inserterSearch }
{ selectedTab === 'blocks' &&
! delayedFilterValue &&
blocksTab }
{ selectedTab === 'patterns' &&
! delayedFilterValue &&
patternsTab }
{ selectedTab === 'media' && mediaTab }
</InserterTabs>
tabs={ [
{
name: 'blocks',
title: __( 'Blocks' ),
panel: (
<>
{ inserterSearch }
{ selectedTab === 'blocks' &&
! delayedFilterValue &&
blocksTab }
</>
),
},
{
name: 'patterns',
title: __( 'Patterns' ),
panel: (
<>
{ inserterSearch }
{ selectedTab === 'patterns' &&
! delayedFilterValue &&
patternsTab }
</>
),
},
{
name: 'media',
title: __( 'Media' ),
panel: (
<>
{ inserterSearch }
{ mediaTab }
</>
),
},
] }
/>
</div>
{ showInserterHelpPanel && hoveredItem && (
<Popover
Expand Down
49 changes: 0 additions & 49 deletions packages/block-editor/src/components/inserter/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@ $block-inserter-tabs-height: 44px;
}

.block-editor-inserter__main-area {
position: relative;
display: flex;
flex-direction: column;
height: 100%;
gap: $grid-unit-20;

&.show-as-tabs {
gap: 0;
}

@include break-medium {
width: $block-inserter-width;
}
}

.block-editor-inserter__popover.is-quick {
Expand Down Expand Up @@ -109,48 +102,6 @@ $block-inserter-tabs-height: 44px;
padding: $grid-unit-20 $grid-unit-20 0 $grid-unit-20;
}

.block-editor-inserter__tabs {
flex-grow: 1;
display: flex;
flex-direction: column;
overflow: hidden;

.block-editor-inserter__tablist-and-close-button {
border-bottom: $border-width solid $gray-300;
padding-right: $grid-unit-15;
display: flex;
justify-content: space-between;
}

.block-editor-inserter__close-button {
/* stylelint-disable-next-line property-disallowed-list -- This should be refactored to avoid order if possible */
order: 1;
align-self: center;
}

.block-editor-inserter__tablist {
width: 100%;
margin-bottom: -$border-width;

button[role="tab"] {
flex-grow: 1;
&[id$="reusable"] {
flex-grow: inherit;
// These are to align the `reusable` icon with the search icon.
padding-left: $grid-unit-20;
padding-right: $grid-unit-20;
}
}
}

.block-editor-inserter__tabpanel {
display: flex;
flex-grow: 1;
flex-direction: column;
overflow-y: auto;
}
}

.block-editor-inserter__no-tab-container {
overflow-y: auto;
flex-grow: 1;
Expand Down
76 changes: 76 additions & 0 deletions packages/block-editor/src/components/tabbed-sidebar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Tabbed Panel

The `TabbedPanel` component is used to create the secondary panels in the editor.

## Development guidelines

This acts as a wrapper for the `Tabs` component, but adding conventions that can be shared between all secondary panels, for example:

- A close button
- Tabs that fill the panel
- Custom scollbars

### Usage

Renders a block alignment toolbar with alignments options.

```jsx
import { TabbedSidebar } from '@wordpress/components';

const MyTabbedSidebar = () => (
<TabbedSidebar
tabs={ [
{
name: 'slug-1',
title: _x( 'Title 1', 'context' ),
panel: <PanelContents>,
panelRef: useRef('an-optional-ref'),
},
{
name: 'slug-2',
title: _x( 'Title 2', 'context' ),
panel: <PanelContents />,
},
] }
onClose={ onClickCloseButton }
onSelect={ onSelectTab }
defaultTabId="slug-1"
ref={ tabsRef }
/>
Comment on lines +21 to +39
Copy link
Contributor

Choose a reason for hiding this comment

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

To elaborate on the above, I think this should eventually look more like:

<StickyPanel title="Block Inserter" onClose={ onClickCloseButton }>
  <Tabs>
    ...
   </Tabs>
</StickyPanel>

Name tbd 😬 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree on this. We should try to keep things composable to avoid "YAP" syndrome.

);
```

### Props

### `defaultTabId`

- **Type:** `String`
- **Default:** `undefined`

This is passed to the `Tabs` component so it can handle the tab to select by default when it component renders.

### `onClose`

- **Type:** `Function`

The function that is called when the close button is clicked.

### `onSelect`

- **Type:** `Function`

This is passed to the `Tabs` component - it will be called when a tab has been selected. It is passed the selected tab's ID as an argument.

### `selectedTab`

- **Type:** `String`
- **Default:** `undefined`

This is passed to the `Tabs` component - it will display this tab as selected.

### `tabs`

- **Type:** `Array`
- **Default:** `undefined`

An array of tabs which will be rendered as `TabList` and `TabPanel` components.
71 changes: 71 additions & 0 deletions packages/block-editor/src/components/tabbed-sidebar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* WordPress dependencies
*/
import {
Button,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { forwardRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { closeSmall } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';

const { Tabs } = unlock( componentsPrivateApis );

function TabbedSidebar(
{ defaultTabId, onClose, onSelect, selectedTab, tabs },
ref
) {
return (
<div className="block-editor-tabbed-sidebar">
<Tabs
selectOnMove={ false }
defaultTabId={ defaultTabId }
onSelect={ onSelect }
selectedTabId={ selectedTab }
>
<div className="block-editor-tabbed-sidebar__tablist-and-close-button">
<Button
className="block-editor-tabbed-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.

This will need to be dynamic, as the list view close button tooltip also says "Close block inserter"

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

<Tabs.TabList
className="block-editor-tabbed-sidebar__tablist"
ref={ ref }
>
{ tabs.map( ( tab ) => (
<Tabs.Tab
key={ tab.name }
tabId={ tab.name }
className="block-editor-tabbed-sidebar__tab"
>
{ tab.title }
</Tabs.Tab>
) ) }
</Tabs.TabList>
</div>
{ tabs.map( ( tab ) => (
<Tabs.TabPanel
key={ tab.name }
tabId={ tab.name }
focusable={ false }
className="block-editor-tabbed-sidebar__tabpanel"
ref={ tab.panelRef }
>
{ tab.panel }
</Tabs.TabPanel>
) ) }
</Tabs>
</div>
);
}

export default forwardRef( TabbedSidebar );
53 changes: 53 additions & 0 deletions packages/block-editor/src/components/tabbed-sidebar/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.block-editor-tabbed-sidebar {
height: 100%;
display: flex;
flex-direction: column;
flex-grow: 1;
overflow: hidden;

@include break-medium() {
width: 350px;
}
}

.block-editor-tabbed-sidebar__tablist-and-close-button {
border-bottom: $border-width solid $gray-300;
display: flex;
justify-content: space-between;
padding-right: $grid-unit-15;
}


.block-editor-tabbed-sidebar__close-button {
background: $white;
/* stylelint-disable-next-line property-disallowed-list -- This should be removed when https://github.com/WordPress/gutenberg/issues/59013 is fixed. */
order: 1;
align-self: center;
}

.block-editor-tabbed-sidebar__tablist {
box-sizing: border-box;
flex-grow: 1;
margin-bottom: -$border-width;
width: 100%;
}

.block-editor-tabbed-sidebar__tab {
flex-grow: 1;
margin-bottom: -$border-width;

&[id$="reusable"] {
flex-grow: inherit;
// These are to align the `reusable` icon with the search icon.
padding-left: $grid-unit-20;
padding-right: $grid-unit-20;
}
}

.block-editor-tabbed-sidebar__tabpanel {
display: flex;
flex-grow: 1;
flex-direction: column;
overflow-y: auto;
scrollbar-gutter: auto;
}
1 change: 1 addition & 0 deletions packages/block-editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
@import "./components/rich-text/style.scss";
@import "./components/segmented-text-control/style.scss";
@import "./components/skip-to-selected-block/style.scss";
@import "./components/tabbed-sidebar/style.scss";
@import "./components/tool-selector/style.scss";
@import "./components/url-input/style.scss";
@import "./components/url-popover/style.scss";
Expand Down
Loading
Loading