-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[Patterns]: Browse Patterns in a Modal #35773
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
47794da
[Patterns]: Paterns explorer in modal
ntsekouras df94a0a
add search results header info
ntsekouras 8f15805
Update packages/block-editor/src/components/inserter/block-patterns-e…
ntsekouras 675a8eb
fix lint error
ntsekouras 484a7a9
small design changes
ntsekouras 73ec327
Refactor patterns tab/explorer for performance
ntsekouras 50fb786
Moving hover and focus styles to the preview container.
shaunandrews 4341955
Specifying a gap for the Grid component.
shaunandrews b6bf5a7
Updating the button label to Explore and using the outlined style of …
shaunandrews 195085a
Visual tweaks to the inserter modal
shaunandrews d5cf852
adjust min-height in quick inserter
ntsekouras 701ca5e
remove commented line
ntsekouras 7328481
Adjusting min/max height for previews in the modal and changing colum…
shaunandrews 3a1aebc
More visual tweaks to the patterns list.
shaunandrews 9f9839f
Increasing the space between the dropdown and the button to match the…
shaunandrews 3d6c430
Using the Heading component and adding a className to the list contai…
shaunandrews f340730
Absolute positioning the explorer sidebar to allow for independent sc…
shaunandrews e0210ce
hide explore button when inserter goes full width + small css fix for…
ntsekouras d83b678
address feedback
ntsekouras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/block-editor/src/components/inserter/block-patterns-explorer/explorer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Modal } from '@wordpress/components'; | ||
import { useState } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PatternExplorerSidebar from './sidebar'; | ||
import PatternList from './patterns-list'; | ||
|
||
function PatternsExplorer( { initialCategory, patternCategories } ) { | ||
const [ filterValue, setFilterValue ] = useState( '' ); | ||
const [ selectedCategory, setSelectedCategory ] = useState( | ||
initialCategory?.name | ||
); | ||
return ( | ||
<div className="block-editor-block-patterns-explorer"> | ||
<PatternExplorerSidebar | ||
selectedCategory={ selectedCategory } | ||
patternCategories={ patternCategories } | ||
onClickCategory={ setSelectedCategory } | ||
filterValue={ filterValue } | ||
setFilterValue={ setFilterValue } | ||
/> | ||
<PatternList | ||
filterValue={ filterValue } | ||
selectedCategory={ selectedCategory } | ||
patternCategories={ patternCategories } | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
function PatternsExplorerModal( { onModalClose, ...restProps } ) { | ||
return ( | ||
<Modal | ||
title={ __( 'Patterns' ) } | ||
closeLabel={ __( 'Close' ) } | ||
onRequestClose={ onModalClose } | ||
isFullScreen | ||
> | ||
<PatternsExplorer { ...restProps } /> | ||
</Modal> | ||
); | ||
} | ||
|
||
export default PatternsExplorerModal; |
122 changes: 122 additions & 0 deletions
122
packages/block-editor/src/components/inserter/block-patterns-explorer/patterns-list.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, useEffect } from '@wordpress/element'; | ||
import { _n, sprintf } from '@wordpress/i18n'; | ||
import { useDebounce, useAsyncList } from '@wordpress/compose'; | ||
import { __experimentalHeading as Heading } from '@wordpress/components'; | ||
import { speak } from '@wordpress/a11y'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockPatternsList from '../../block-patterns-list'; | ||
import InserterNoResults from '../no-results'; | ||
import useInsertionPoint from '../hooks/use-insertion-point'; | ||
import usePatternsState from '../hooks/use-patterns-state'; | ||
import InserterListbox from '../../inserter-listbox'; | ||
import { searchItems } from '../search-items'; | ||
|
||
const INITIAL_INSERTER_RESULTS = 2; | ||
|
||
function PatternsListHeader( { filterValue, filteredBlockPatternsLength } ) { | ||
if ( ! filterValue ) { | ||
return null; | ||
} | ||
return ( | ||
<Heading | ||
level={ 2 } | ||
lineHeight={ '48px' } | ||
className="block-editor-block-patterns-explorer__search-results-count" | ||
> | ||
{ sprintf( | ||
/* translators: %d: number of patterns. %s: block pattern search query */ | ||
_n( | ||
'%1$d pattern found for "%2$s"', | ||
'%1$d patterns found for "%2$s"', | ||
filteredBlockPatternsLength | ||
), | ||
filteredBlockPatternsLength, | ||
filterValue | ||
) } | ||
</Heading> | ||
); | ||
} | ||
|
||
function PatternList( { filterValue, selectedCategory, patternCategories } ) { | ||
const debouncedSpeak = useDebounce( speak, 500 ); | ||
const [ destinationRootClientId, onInsertBlocks ] = useInsertionPoint( { | ||
shouldFocusBlock: true, | ||
} ); | ||
const [ allPatterns, , onSelectBlockPattern ] = usePatternsState( | ||
onInsertBlocks, | ||
destinationRootClientId | ||
); | ||
const registeredPatternCategories = useMemo( | ||
() => | ||
patternCategories.map( | ||
( patternCategory ) => patternCategory.name | ||
), | ||
[ patternCategories ] | ||
); | ||
|
||
const filteredBlockPatterns = useMemo( () => { | ||
if ( ! filterValue ) { | ||
return allPatterns.filter( ( pattern ) => | ||
selectedCategory === 'uncategorized' | ||
? ! pattern.categories?.length || | ||
pattern.categories.every( | ||
( category ) => | ||
! registeredPatternCategories.includes( | ||
category | ||
) | ||
) | ||
: pattern.categories?.includes( selectedCategory ) | ||
); | ||
} | ||
return searchItems( allPatterns, filterValue ); | ||
}, [ filterValue, selectedCategory, allPatterns ] ); | ||
|
||
// Announce search results on change. | ||
useEffect( () => { | ||
if ( ! filterValue ) { | ||
return; | ||
} | ||
const count = filteredBlockPatterns.length; | ||
const resultsFoundMessage = sprintf( | ||
/* translators: %d: number of results. */ | ||
_n( '%d result found.', '%d results found.', count ), | ||
count | ||
); | ||
debouncedSpeak( resultsFoundMessage ); | ||
}, [ filterValue, debouncedSpeak ] ); | ||
|
||
const currentShownPatterns = useAsyncList( filteredBlockPatterns, { | ||
step: INITIAL_INSERTER_RESULTS, | ||
} ); | ||
|
||
const hasItems = !! filteredBlockPatterns?.length; | ||
return ( | ||
<div className="block-editor-block-patterns-explorer__list"> | ||
{ hasItems && ( | ||
<PatternsListHeader | ||
filterValue={ filterValue } | ||
filteredBlockPatternsLength={ filteredBlockPatterns.length } | ||
/> | ||
) } | ||
<InserterListbox> | ||
{ ! hasItems && <InserterNoResults /> } | ||
{ hasItems && ( | ||
<BlockPatternsList | ||
shownPatterns={ currentShownPatterns } | ||
blockPatterns={ filteredBlockPatterns } | ||
onClickPattern={ onSelectBlockPattern } | ||
isDraggable={ false } | ||
/> | ||
) } | ||
</InserterListbox> | ||
</div> | ||
); | ||
} | ||
|
||
export default PatternList; |
73 changes: 73 additions & 0 deletions
73
packages/block-editor/src/components/inserter/block-patterns-explorer/sidebar.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Button, SearchControl } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
function PatternCategoriesList( { | ||
selectedCategory, | ||
patternCategories, | ||
onClickCategory, | ||
} ) { | ||
const baseClassName = 'block-editor-block-patterns-explorer__sidebar'; | ||
return ( | ||
<div className={ `${ baseClassName }__categories-list` }> | ||
{ patternCategories.map( ( { name, label } ) => { | ||
return ( | ||
<Button | ||
key={ name } | ||
label={ label } | ||
className={ `${ baseClassName }__categories-list__item` } | ||
isPressed={ selectedCategory === name } | ||
onClick={ () => { | ||
onClickCategory( name ); | ||
} } | ||
> | ||
{ label } | ||
</Button> | ||
); | ||
} ) } | ||
</div> | ||
); | ||
} | ||
|
||
function PatternsExplorerSearch( { filterValue, setFilterValue } ) { | ||
const baseClassName = 'block-editor-block-patterns-explorer__search'; | ||
return ( | ||
<div className={ baseClassName }> | ||
<SearchControl | ||
onChange={ setFilterValue } | ||
value={ filterValue } | ||
label={ __( 'Search for patterns' ) } | ||
placeholder={ __( 'Search' ) } | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
function PatternExplorerSidebar( { | ||
selectedCategory, | ||
patternCategories, | ||
onClickCategory, | ||
filterValue, | ||
setFilterValue, | ||
} ) { | ||
const baseClassName = 'block-editor-block-patterns-explorer__sidebar'; | ||
return ( | ||
<div className={ baseClassName }> | ||
<PatternsExplorerSearch | ||
filterValue={ filterValue } | ||
setFilterValue={ setFilterValue } | ||
/> | ||
{ ! filterValue && ( | ||
<PatternCategoriesList | ||
selectedCategory={ selectedCategory } | ||
patternCategories={ patternCategories } | ||
onClickCategory={ onClickCategory } | ||
/> | ||
) } | ||
</div> | ||
); | ||
} | ||
|
||
export default PatternExplorerSidebar; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor note, but what could we do instead of keeping this magic constant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect this would go away as we iterate more; Right now it helps resolve a visual imbalance when searching, but I expect the headings to change (like adding descriptions or actions) as we add them to the other parts of the browsing experience.