From c94f46330550973f26343f36a5beb5fdf08670b2 Mon Sep 17 00:00:00 2001 From: Jerry Jones Date: Wed, 24 Apr 2024 09:02:20 -0500 Subject: [PATCH] Convert Media Inserter to Tabs Pattern (#60970) Co-authored-by: jeryj Co-authored-by: ntsekouras Co-authored-by: youknowriad Co-authored-by: jameskoster --- .../inserter/block-patterns-tab/index.js | 75 +---- .../inserter/category-tabs/index.js | 74 +++++ .../components/inserter/media-tab/index.js | 2 +- .../inserter/media-tab/media-panel.js | 22 -- .../inserter/media-tab/media-tab.js | 122 +++----- .../src/components/inserter/menu.js | 39 ++- .../src/components/inserter/style.scss | 260 ++++++------------ test/e2e/specs/editor/blocks/image.spec.js | 2 +- .../editor/various/inserting-blocks.spec.js | 16 +- 9 files changed, 243 insertions(+), 369 deletions(-) create mode 100644 packages/block-editor/src/components/inserter/category-tabs/index.js diff --git a/packages/block-editor/src/components/inserter/block-patterns-tab/index.js b/packages/block-editor/src/components/inserter/block-patterns-tab/index.js index 34993fc6db87fa..d2f302683ed2da 100644 --- a/packages/block-editor/src/components/inserter/block-patterns-tab/index.js +++ b/packages/block-editor/src/components/inserter/block-patterns-tab/index.js @@ -2,15 +2,9 @@ * WordPress dependencies */ import { useState } from '@wordpress/element'; -import { __, isRTL } from '@wordpress/i18n'; import { useViewportMatch } from '@wordpress/compose'; -import { - __experimentalHStack as HStack, - FlexBlock, - Button, - privateApis as componentsPrivateApis, -} from '@wordpress/components'; -import { Icon, chevronRight, chevronLeft } from '@wordpress/icons'; +import { Button } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; /** * Internal dependencies @@ -19,9 +13,7 @@ import PatternsExplorerModal from '../block-patterns-explorer'; import MobileTabNavigation from '../mobile-tab-navigation'; import { PatternCategoryPreviews } from './pattern-category-previews'; import { usePatternCategories } from './use-pattern-categories'; -import { unlock } from '../../../lock-unlock'; - -const { Tabs } = unlock( componentsPrivateApis ); +import CategoryTabs from '../category-tabs'; function BlockPatternsTab( { onSelectCategory, @@ -40,60 +32,13 @@ function BlockPatternsTab( { <> { ! isMobile && (
- { - // Pass the full category object - onSelectCategory( - categories.find( - ( category ) => category.name === categoryId - ) - ); - } } + - - { categories.map( ( category ) => ( - - - - { category.label } - - - - - ) ) } - - { categories.map( ( category ) => ( - - { children } - - ) ) } - + { children } + - ) } - /> - -
- - + { __( 'Open Media Library' ) } + + ) } + /> + ) } { isMobile && ( - + { ( category ) => ( ( <> @@ -183,8 +192,10 @@ function InserterMenu( ), [ destinationRootClientId, + onHoverPattern, onInsertPattern, onClickPatternCategory, + patternFilter, selectedPatternCategory, showPatternPanel, ] @@ -197,13 +208,22 @@ function InserterMenu( selectedCategory={ selectedMediaCategory } onSelectCategory={ setSelectedMediaCategory } onInsert={ onInsert } - /> + > + { showMediaPanel && ( + + ) } + ), [ destinationRootClientId, onInsert, selectedMediaCategory, setSelectedMediaCategory, + showMediaPanel, ] ); @@ -224,10 +244,6 @@ function InserterMenu( } ) ); const showAsTabs = ! delayedFilterValue && ( showPatterns || showMedia ); - const showMediaPanel = - selectedTab === 'media' && - ! delayedFilterValue && - selectedMediaCategory; // When the pattern panel is showing, we want to use zoom out mode useZoomOut( showPatternPanel ); @@ -243,7 +259,7 @@ function InserterMenu( return (
) }
- { showMediaPanel && ( - - ) } { showInserterHelpPanel && hoveredItem && ( button { - display: block; - } + .block-editor-inserter__media-list__item-preview-options > button { + display: block; } + } - .block-editor-inserter__media-list__item-preview-options { - position: absolute; - right: $grid-unit-10; - top: $grid-unit-10; + .block-editor-inserter__media-list__item-preview-options { + position: absolute; + right: $grid-unit-10; + top: $grid-unit-10; - > button { - background: $white; - border-radius: $radius-block-ui; - display: none; + > button { + background: $white; + border-radius: $radius-block-ui; + display: none; - // These styles are important so as focus isn't lost - // when the button is focused and we hover away. - &.is-opened, - &:focus { - display: block; - } + // These styles are important so as focus isn't lost + // when the button is focused and we hover away. + &.is-opened, + &:focus { + display: block; + } - &:hover { - box-shadow: inset 0 0 0 2px $white, 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color); + &:hover { + box-shadow: inset 0 0 0 2px $white, 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color); - // Windows High Contrast mode will show this outline, but not the box-shadow. - outline: 2px solid transparent; - } + // Windows High Contrast mode will show this outline, but not the box-shadow. + outline: 2px solid transparent; } } } +} - .block-editor-inserter__media-list__item { - height: 100%; +.block-editor-inserter__media-list__item { + height: 100%; - .block-editor-inserter__media-list__item-preview { - display: flex; - align-items: center; - overflow: hidden; - border-radius: 2px; + .block-editor-inserter__media-list__item-preview { + display: flex; + align-items: center; + overflow: hidden; + border-radius: 2px; - > * { - margin: 0 auto; - max-width: 100%; - } + > * { + margin: 0 auto; + max-width: 100%; + } - .block-editor-inserter__media-list__item-preview-spinner { - display: flex; - height: 100%; - width: 100%; - position: absolute; - justify-content: center; - background: rgba($white, 0.7); - align-items: center; - pointer-events: none; - } + .block-editor-inserter__media-list__item-preview-spinner { + display: flex; + height: 100%; + width: 100%; + position: absolute; + justify-content: center; + background: rgba($white, 0.7); + align-items: center; + pointer-events: none; } + } - &:focus .block-editor-inserter__media-list__item-preview { - box-shadow: inset 0 0 0 2px $white, 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color); + &:focus .block-editor-inserter__media-list__item-preview { + box-shadow: inset 0 0 0 2px $white, 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color); - // Windows High Contrast mode will show this outline, but not the box-shadow. - outline: 2px solid transparent; - } + // Windows High Contrast mode will show this outline, but not the box-shadow. + outline: 2px solid transparent; } } @@ -790,8 +714,4 @@ $block-inserter-tabs-height: 44px; height: 100%; } } - - .block-editor-inserter__media-dialog { - position: static; - } } diff --git a/test/e2e/specs/editor/blocks/image.spec.js b/test/e2e/specs/editor/blocks/image.spec.js index 940e4b0d47b034..24fff3e579f682 100644 --- a/test/e2e/specs/editor/blocks/image.spec.js +++ b/test/e2e/specs/editor/blocks/image.spec.js @@ -448,7 +448,7 @@ test.describe( 'Image', () => { await blockLibrary .getByRole( 'tabpanel', { name: 'Media' } ) - .getByRole( 'button', { name: 'Openverse' } ) + .getByRole( 'tab', { name: 'Openverse' } ) .click(); } diff --git a/test/e2e/specs/editor/various/inserting-blocks.spec.js b/test/e2e/specs/editor/various/inserting-blocks.spec.js index b3da80c12ab099..6e19fd31a71b05 100644 --- a/test/e2e/specs/editor/various/inserting-blocks.spec.js +++ b/test/e2e/specs/editor/various/inserting-blocks.spec.js @@ -724,18 +724,10 @@ test.describe( 'insert media from inserter', () => { } ) => { await admin.createNewPost(); - await page.click( - 'role=region[name="Editor top bar"i] >> role=button[name="Toggle block inserter"i]' - ); - await page.click( - 'role=region[name="Block Library"i] >> role=tab[name="Media"i]' - ); - await page.click( - '[aria-label="Media categories"i] >> role=button[name="Images"i]' - ); - await page.click( - `role=listbox[name="Media List"i] >> role=option[name="${ uploadedMedia.title.raw }"]` - ); + await page.getByLabel( 'Toggle block inserter' ).click(); + await page.getByRole( 'tab', { name: 'Media' } ).click(); + await page.getByRole( 'tab', { name: 'Images' } ).click(); + await page.getByLabel( uploadedMedia.title.raw ).click(); await expect.poll( editor.getEditedPostContent ).toBe( `
${ uploadedMedia.alt_text }