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

Experiment with custom control for adding Pages in Nav block #35834

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 133 additions & 38 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import {
ToolbarButton,
Tooltip,
ToolbarGroup,
Button,
Icon,
__experimentalNavigation as Navigation,
__experimentalNavigationItem as NavigationItem,
__experimentalNavigationMenu as NavigationMenu,
} from '@wordpress/components';
import { displayShortcut, isKeyboardEvent, ENTER } from '@wordpress/keycodes';
import { __, sprintf } from '@wordpress/i18n';
Expand All @@ -38,7 +43,11 @@ import {
createInterpolateElement,
} from '@wordpress/element';
import { placeCaretAtHorizontalEdge } from '@wordpress/dom';
import { link as linkIcon, addSubmenu } from '@wordpress/icons';
import {
link as linkIcon,
addSubmenu,
page as pageIcon,
} from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';

/**
Expand Down Expand Up @@ -261,6 +270,84 @@ export const updateNavigationLinkBlockAttributes = (
} );
};

const usePagesControl = true;

function usePageEntities() {
const { pages, isResolvingPages, hasResolvedPages } = useSelect(
( select ) => {
const {
getEntityRecords,
isResolving,
hasFinishedResolution,
} = select( coreStore );

const pagesParameters = [
'postType',
'page',
{
parent: 0,
order: 'asc',
orderby: 'id',
per_page: -1,
},
];

return {
pages: getEntityRecords( ...pagesParameters ) || null,
isResolvingPages: isResolving(
'getEntityRecords',
pagesParameters
),
hasResolvedPages: hasFinishedResolution(
'getEntityRecords',
pagesParameters
),
};
},
[]
);

return {
pages,
isResolvingPages,
hasResolvedPages,
hasPages: !! ( hasResolvedPages && pages?.length ),
};
}

function PagesControl( { pages, hasPages, isResolvingPages } ) {
if ( isResolvingPages ) {
<p>Loading...</p>;
}

if ( ! hasPages ) {
return <p>Your site has no Pages!</p>;
}

const pageList = pages.map( ( page ) => {
return (
<NavigationItem key={ page.id } item={ page.slug }>
<Button
href="https://wordpress.org/"
target="_blank"
rel="noreferrer"
>
<Icon icon={ pageIcon } />
<span>{ page.title.rendered }</span>
</Button>
</NavigationItem>
);
} );

return (
<Navigation className="navigation-block-page-search">
<NavigationMenu hasSearch title="Pages">
{ pageList }
</NavigationMenu>
</Navigation>
);
}

export default function NavigationLinkEdit( {
attributes,
isSelected,
Expand Down Expand Up @@ -377,6 +464,8 @@ export default function NavigationLinkEdit( {
replaceBlock( clientId, newSubmenu );
}

const pageEntities = usePageEntities();

useEffect( () => {
// Show the LinkControl on mount if the URL is empty
// ( When adding a new menu item)
Expand Down Expand Up @@ -645,45 +734,51 @@ export default function NavigationLinkEdit( {
onClose={ () => setIsLinkOpen( false ) }
anchorRef={ listItemRef.current }
>
<LinkControl
className="wp-block-navigation-link__inline-link-input"
value={ link }
showInitialSuggestions={ true }
withCreateSuggestion={ userCanCreate }
createSuggestion={ handleCreate }
createSuggestionButtonText={ ( searchTerm ) => {
let format;
if ( type === 'post' ) {
/* translators: %s: search term. */
format = __(
'Create draft post: <mark>%s</mark>'
);
} else {
/* translators: %s: search term. */
format = __(
'Create draft page: <mark>%s</mark>'
{ usePagesControl ? (
<PagesControl { ...pageEntities } />
) : (
<LinkControl
className="wp-block-navigation-link__inline-link-input"
value={ link }
showInitialSuggestions={ true }
withCreateSuggestion={ userCanCreate }
createSuggestion={ handleCreate }
createSuggestionButtonText={ (
searchTerm
) => {
let format;
if ( type === 'post' ) {
/* translators: %s: search term. */
format = __(
'Create draft post: <mark>%s</mark>'
);
} else {
/* translators: %s: search term. */
format = __(
'Create draft page: <mark>%s</mark>'
);
}
return createInterpolateElement(
sprintf( format, searchTerm ),
{ mark: <mark /> }
);
} }
noDirectEntry={ !! type }
noURLSuggestion={ !! type }
suggestionsQuery={ getSuggestionsQuery(
type,
kind
) }
onChange={ ( updatedValue ) =>
updateNavigationLinkBlockAttributes(
updatedValue,
setAttributes,
attributes
)
}
return createInterpolateElement(
sprintf( format, searchTerm ),
{ mark: <mark /> }
);
} }
noDirectEntry={ !! type }
noURLSuggestion={ !! type }
suggestionsQuery={ getSuggestionsQuery(
type,
kind
) }
onChange={ ( updatedValue ) =>
updateNavigationLinkBlockAttributes(
updatedValue,
setAttributes,
attributes
)
}
onRemove={ removeLink }
/>
onRemove={ removeLink }
/>
) }
</Popover>
) }
</a>
Expand Down
19 changes: 19 additions & 0 deletions packages/block-library/src/navigation-link/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,22 @@
overflow-wrap: break-word;
}
}


.navigation-block-page-search .components-navigation__item {
display: flex;
flex-direction: row;

.components-button {
align-items: center;
display: flex;
flex-direction: row;
height: auto;

svg {
fill: #949494;
margin-right: 8px;
flex: 1;
}
}
}