diff --git a/packages/block-library/src/query/edit/index.js b/packages/block-library/src/query/edit/index.js index 9a54b5976fe4bc..60fc2ebd6170b3 100644 --- a/packages/block-library/src/query/edit/index.js +++ b/packages/block-library/src/query/edit/index.js @@ -10,7 +10,7 @@ import { store as blockEditorStore } from '@wordpress/block-editor'; */ import QueryContent from './query-content'; import QueryPlaceholder from './query-placeholder'; -import PatternSelectionModal from './pattern-selection-modal'; +import { PatternSelectionModal } from './pattern-selection'; const QueryEdit = ( props ) => { const { clientId, attributes } = props; diff --git a/packages/block-library/src/query/edit/pattern-selection-modal.js b/packages/block-library/src/query/edit/pattern-selection.js similarity index 67% rename from packages/block-library/src/query/edit/pattern-selection-modal.js rename to packages/block-library/src/query/edit/pattern-selection.js index e4cc8d0c851c23..0c5d95d15206f2 100644 --- a/packages/block-library/src/query/edit/pattern-selection-modal.js +++ b/packages/block-library/src/query/edit/pattern-selection.js @@ -21,48 +21,67 @@ import { } from '../utils'; import { searchPatterns } from '../../utils/search-patterns'; -export default function PatternSelectionModal( { +export function PatternSelectionModal( { clientId, attributes, setIsPatternSelectionModalOpen, +} ) { + return ( + setIsPatternSelectionModalOpen( false ) } + isFullScreen + > + + + ); +} + +export function useBlockPatterns( clientId, attributes ) { + const blockNameForPatterns = useBlockNameForPatterns( + clientId, + attributes + ); + return usePatterns( clientId, blockNameForPatterns ); +} + +export default function PatternSelection( { + clientId, + attributes, + showTitlesAsTooltip = false, + showSearch = true, } ) { const [ searchValue, setSearchValue ] = useState( '' ); const { replaceBlock, selectBlock } = useDispatch( blockEditorStore ); - const onBlockPatternSelect = ( pattern, blocks ) => { - const { newBlocks, queryClientIds } = getTransformedBlocksFromPattern( - blocks, - attributes - ); - replaceBlock( clientId, newBlocks ); - if ( queryClientIds[ 0 ] ) { - selectBlock( queryClientIds[ 0 ] ); - } - }; - // When we preview Query Loop blocks we should prefer the current - // block's postType, which is passed through block context. + const blockPatterns = useBlockPatterns( clientId, attributes ); + /* + * When we preview Query Loop blocks we should prefer the current + * block's postType, which is passed through block context. + */ const blockPreviewContext = useMemo( () => ( { previewPostType: attributes.query.postType, } ), [ attributes.query.postType ] ); - const blockNameForPatterns = useBlockNameForPatterns( - clientId, - attributes - ); - const blockPatterns = usePatterns( clientId, blockNameForPatterns ); const filteredBlockPatterns = useMemo( () => { return searchPatterns( blockPatterns, searchValue ); }, [ blockPatterns, searchValue ] ); + const onBlockPatternSelect = ( pattern, blocks ) => { + const { newBlocks, queryClientIds } = getTransformedBlocksFromPattern( + blocks, + attributes + ); + replaceBlock( clientId, newBlocks ); + if ( queryClientIds[ 0 ] ) { + selectBlock( queryClientIds[ 0 ] ); + } + }; return ( - setIsPatternSelectionModalOpen( false ) } - isFullScreen - > -
+
+ { showSearch && (
- - - -
- + ) } + + + +
); } diff --git a/packages/block-library/src/query/edit/query-content.js b/packages/block-library/src/query/edit/query-content.js index 17eea9337823c5..a608f20bbeb001 100644 --- a/packages/block-library/src/query/edit/query-content.js +++ b/packages/block-library/src/query/edit/query-content.js @@ -19,10 +19,10 @@ import { store as coreStore } from '@wordpress/core-data'; * Internal dependencies */ import EnhancedPaginationControl from './inspector-controls/enhanced-pagination-control'; -import QueryToolbar from './query-toolbar'; import QueryInspectorControls from './inspector-controls'; import EnhancedPaginationModal from './enhanced-pagination-modal'; import { getQueryContextFromTemplate } from '../utils'; +import QueryToolbar from './query-toolbar'; const DEFAULTS_POSTS_PER_PAGE = 3; @@ -30,10 +30,9 @@ const TEMPLATE = [ [ 'core/post-template' ] ]; export default function QueryContent( { attributes, setAttributes, - openPatternSelectionModal, - name, clientId, context, + name, } ) { const { queryId, @@ -154,6 +153,7 @@ export default function QueryContent( { /> - + { const { getActiveBlockVariation, getBlockType } = select( blocksStore ); - const { getBlockRootClientId, getPatternsByBlockTypes } = - select( blockEditorStore ); - const rootClientId = getBlockRootClientId( clientId ); return { blockType: getBlockType( name ), activeBlockVariation: getActiveBlockVariation( name, attributes ), - hasPatterns: !! getPatternsByBlockTypes( - blockNameForPatterns, - rootClientId - ).length, }; }, - [ name, blockNameForPatterns, clientId, attributes ] + [ name, attributes ] ); + const hasPatterns = !! useBlockPatterns( clientId, attributes ).length; const icon = activeBlockVariation?.icon?.src || activeBlockVariation?.icon || diff --git a/packages/block-library/src/query/edit/query-toolbar.js b/packages/block-library/src/query/edit/query-toolbar.js index cc2d62a54d529f..25e087ebe1559c 100644 --- a/packages/block-library/src/query/edit/query-toolbar.js +++ b/packages/block-library/src/query/edit/query-toolbar.js @@ -1,30 +1,51 @@ /** * WordPress dependencies */ -import { ToolbarGroup, ToolbarButton } from '@wordpress/components'; +import { + ToolbarGroup, + ToolbarButton, + Dropdown, + __experimentalDropdownContentWrapper as DropdownContentWrapper, +} from '@wordpress/components'; import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ -import { usePatterns } from '../utils'; +import PatternSelection, { useBlockPatterns } from './pattern-selection'; -export default function QueryToolbar( { - openPatternSelectionModal, - name, - clientId, -} ) { - const hasPatterns = !! usePatterns( clientId, name ).length; +export default function QueryToolbar( { clientId, attributes } ) { + const hasPatterns = useBlockPatterns( clientId, attributes ).length; + if ( ! hasPatterns ) { + return null; + } return ( - <> - { hasPatterns && ( - - - { __( 'Replace' ) } - - - ) } - + + + ( + + { __( 'Change design' ) } + + ) } + renderContent={ () => ( + + ) } + /> + + ); } diff --git a/packages/block-library/src/query/editor.scss b/packages/block-library/src/query/editor.scss index da863bff0b5da6..5bf0db81870403 100644 --- a/packages/block-library/src/query/editor.scss +++ b/packages/block-library/src/query/editor.scss @@ -45,6 +45,12 @@ } } +.block-library-query-toolspanel__design { + .block-library-query-pattern__selection-content { + margin-top: $grid-unit-10; + } +} + .wp-block-query__enhanced-pagination-modal { @include break-small() { max-width: $break-mobile; @@ -54,3 +60,27 @@ .wp-block-query__enhanced-pagination-notice { margin: 0; } + +.block-editor-block-settings-menu__popover { + &.is-expanded { + overflow-y: scroll; + } + .block-library-query-pattern__selection-content { + height: 100%; + } + .block-editor-block-patterns-list { + display: grid; + grid-template-columns: 1fr; + @include break-small() { + grid-template-columns: 1fr 1fr; + } + grid-gap: $grid-unit-15; + min-width: $break-zoomed-in; + @include break-small() { + min-width: $break-mobile; + } + } + .block-editor-block-patterns-list__list-item { + margin-bottom: 0; + } +}