Repository navigation
Navigation: Surface blocks and variations in Link UI search results #78345
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
Changes from all commits
982f15c
735a16c
f6c6606
e3ab929
25717ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,15 @@ import { | |
| VisuallyHidden, | ||
| __experimentalVStack as VStack, | ||
| } from '@wordpress/components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { LinkControl, useBlockEditingMode } from '@wordpress/block-editor'; | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { | ||
| LinkControl, | ||
| useBlockEditingMode, | ||
| store as blockEditorStore, | ||
| BlockIcon, | ||
| } from '@wordpress/block-editor'; | ||
| import { createBlock, store as blocksStore } from '@wordpress/blocks'; | ||
| import { useDispatch, useSelect } from '@wordpress/data'; | ||
| import { | ||
| useMemo, | ||
| useState, | ||
|
|
@@ -70,8 +77,28 @@ export function getSuggestionsQuery( type, kind ) { | |
| } | ||
| } | ||
|
|
||
| const EXCLUDED = new Set( [ | ||
| 'core/navigation-link', | ||
| 'core/navigation-submenu', | ||
| ] ); | ||
|
|
||
| function getVariationBadgeLabel( variation ) { | ||
| if ( variation.attributes?.kind === 'post-type' ) { | ||
| /* translators: %s: Post type name (e.g. "Post", "Page", "CPT", "Taxonomy"). */ | ||
| return sprintf( __( '%s link' ), variation.attributes?.type ); | ||
| } | ||
| if ( variation.attributes?.kind === 'taxonomy' ) { | ||
| return __( 'taxonomy' ); | ||
| } | ||
| return __( 'Link' ); | ||
| } | ||
| const matchesQuery = ( query, title = '', keywords = [] ) => | ||
| title.toLowerCase().includes( query ) || | ||
| keywords.some( ( kw ) => kw.toLowerCase().includes( query ) ); | ||
|
|
||
| function UnforwardedLinkUI( props, ref ) { | ||
| const { label, url, opensInNewTab, type, kind, id } = props.link; | ||
| const { onBlockInsert, onClose } = props; | ||
|
|
||
| const { entityRecord, hasBinding, isEntityAvailable } = props.entity || {}; | ||
|
|
||
|
|
@@ -97,11 +124,13 @@ function UnforwardedLinkUI( props, ref ) { | |
| const [ initialSearchValue, setInitialSearchValue ] = useState( '' ); | ||
| // Tracks the live search input between renders without causing re-renders. | ||
| const searchInputValueRef = useRef( '' ); | ||
| const [ searchInputValue, setSearchInputValue ] = useState( '' ); | ||
| // Call this instead of setting searchInputValueRef.current and | ||
| // setInitialSearchValue separately, to keep both in sync. | ||
| const updateSearchValue = ( value ) => { | ||
| searchInputValueRef.current = value; | ||
| setInitialSearchValue( value ); | ||
| setSearchInputValue( value ); | ||
| }; | ||
| const linkControlWrapperRef = useRef(); | ||
| const addPageButtonRef = useRef(); | ||
|
|
@@ -175,6 +204,106 @@ function UnforwardedLinkUI( props, ref ) { | |
| }, [ shouldFocusPane ] ); | ||
|
|
||
| const blockEditingMode = useBlockEditingMode(); | ||
| const { | ||
| rootClientId, | ||
| insertionIndex, | ||
| allBlockTypes, | ||
| canInsertBlockType, | ||
| navigationLinkVariations, | ||
| } = useSelect( | ||
| ( select ) => { | ||
| const { | ||
| getBlockRootClientId, | ||
| getBlockIndex, | ||
| canInsertBlockType: _canInsert, | ||
| } = select( blockEditorStore ); | ||
| const { getBlockVariations } = select( blocksStore ); | ||
| const root = getBlockRootClientId( props.clientId ); | ||
|
|
||
| return { | ||
| rootClientId: root, | ||
| insertionIndex: getBlockIndex( props.clientId ) + 1, | ||
| allBlockTypes: root | ||
| ? select( blocksStore ).getBlockTypes() | ||
| : [], | ||
| canInsertBlockType: _canInsert, | ||
| navigationLinkVariations: root | ||
| ? getBlockVariations( 'core/navigation-link', 'inserter' ) | ||
| : [], | ||
| }; | ||
| }, | ||
| [ props.clientId ] | ||
| ); | ||
|
|
||
| const { insertBlock } = useDispatch( blockEditorStore ); | ||
|
|
||
| // Filter insertable blocks by both capabilities AND the live search input in one pass. | ||
| // Empty query > no results (avoids flooding the UI before the user types). | ||
| const matchingItems = useMemo( () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the preferred architectural approach here? Is there a different pattern you'd recommend here? |
||
| const query = searchInputValue.trim().toLowerCase(); | ||
|
|
||
| if ( ! query || ! rootClientId || ( type && url ) ) { | ||
| return []; | ||
| } | ||
|
|
||
| const variations = ( navigationLinkVariations ?? [] ) | ||
| .filter( | ||
| ( v ) => | ||
| ( v.title ?? '' ).toLowerCase() !== 'page link' && | ||
| matchesQuery( query, v.title, v.keywords ) | ||
| ) | ||
| .map( ( v ) => ( { | ||
| key: `${ v.name }-${ v.title }`, | ||
| icon: v.icon ?? 'admin-links', | ||
| /* translators: %s: Variation title (e.g., "Category", "Posts") */ | ||
| label: sprintf( __( 'Add %s' ), v.title ), | ||
| badge: getVariationBadgeLabel( v ), | ||
| onInsert() { | ||
| const block = createBlock( | ||
| 'core/navigation-link', | ||
| v.attributes ?? {} | ||
| ); | ||
| insertBlock( block, insertionIndex, rootClientId ); | ||
| onBlockInsert?.( block ); | ||
| onClose?.(); | ||
| }, | ||
| } ) ); | ||
|
|
||
| const blocks = allBlockTypes | ||
| .filter( | ||
| ( bt ) => | ||
| ! EXCLUDED.has( bt.name ) && | ||
| canInsertBlockType( bt.name, rootClientId ) && | ||
| matchesQuery( query, bt.title, bt.keywords ) | ||
| ) | ||
| .map( ( bt ) => ( { | ||
| key: bt.name, | ||
| icon: bt.icon, | ||
| /* translators: %s: Block title (e.g., "Site Logo") */ | ||
| label: sprintf( __( 'Add %s Block' ), bt.title ), | ||
| badge: __( 'Block' ), | ||
| onInsert() { | ||
| const block = createBlock( bt.name ); | ||
| insertBlock( block, insertionIndex, rootClientId ); | ||
| onBlockInsert?.( block ); | ||
| onClose?.(); | ||
| }, | ||
| } ) ); | ||
|
|
||
| return [ ...variations, ...blocks ]; | ||
| }, [ | ||
| searchInputValue, | ||
| navigationLinkVariations, | ||
| allBlockTypes, | ||
| canInsertBlockType, | ||
| rootClientId, | ||
| insertionIndex, | ||
| insertBlock, | ||
| onBlockInsert, | ||
| onClose, | ||
| type, | ||
| url, | ||
| ] ); | ||
|
|
||
| return ( | ||
| <Popover | ||
|
|
@@ -214,6 +343,7 @@ function UnforwardedLinkUI( props, ref ) { | |
| // Observe the input value so we can pass the value to the page creator | ||
| // and restore it on back button click | ||
| searchInputValueRef.current = value; | ||
| setSearchInputValue( value ); | ||
| } } | ||
| inputValue={ initialSearchValue } | ||
| onRemove={ props.onRemove } | ||
|
|
@@ -243,6 +373,7 @@ function UnforwardedLinkUI( props, ref ) { | |
| canAddBlock={ | ||
| blockEditingMode === 'default' | ||
| } | ||
| matchingItems={ matchingItems } | ||
| /> | ||
| ); | ||
| } } | ||
|
|
@@ -292,16 +423,36 @@ const LinkUITools = ( { | |
| setAddingPage, | ||
| canAddPage, | ||
| canAddBlock, | ||
| matchingItems = [], | ||
| } ) => { | ||
| const blockInserterAriaRole = 'listbox'; | ||
|
|
||
| // Don't render anything if neither button should be shown | ||
| if ( ! canAddPage && ! canAddBlock ) { | ||
| if ( ! canAddPage && ! canAddBlock && matchingItems.length === 0 ) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <VStack spacing={ 0 } className="link-ui-tools"> | ||
| { canAddPage && | ||
| canAddBlock && | ||
| matchingItems.map( ( item ) => ( | ||
| <Button | ||
| __next40pxDefaultSize | ||
| key={ item.key } | ||
| icon={ <BlockIcon icon={ item.icon } /> } | ||
| onClick={ ( e ) => { | ||
| e.preventDefault(); | ||
| item.onInsert(); | ||
| } } | ||
| aria-haspopup={ blockInserterAriaRole } | ||
| > | ||
| { item.label } | ||
| <span className="link-ui-suggestion-item__badge"> | ||
| { item.badge } | ||
| </span> | ||
| </Button> | ||
| ) ) } | ||
| { canAddPage && ( | ||
| <Button | ||
| __next40pxDefaultSize | ||
|
|
||
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.
Why do we need to add
searchInputValueto state instead of relying on the ref?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.
We have to use state here because updating a Ref doesn't trigger a re-render. If we only update the ref, the matching items array will never recalculate and the dropdown UI won't visually update as the user types. The state trigers the render cycle needed for real time filtering. Happy to be corrected here & open to a more optimized approach.