Skip to content

Commit

Permalink
Add menu-item-home class to Navigation for front page (#37301)
Browse files Browse the repository at this point in the history
* Add menu-item-home class to Navigation for the front page

Closes #29423
  • Loading branch information
georgeh authored and Mamaduka committed Feb 16, 2022
1 parent 52d147e commit 4544361
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { ItemSubmenuIcon } from '../navigation-link/icons';
const MAX_PAGE_COUNT = 100;

export default function PageListEdit( { context, clientId } ) {
const { pagesByParentId, totalPages } = usePagesByParentId();
const { pagesByParentId, totalPages } = usePageData();

const isNavigationChild = 'showSubmenuIcon' in context;
const allowConvertToLinks =
Expand Down Expand Up @@ -94,7 +94,14 @@ export default function PageListEdit( { context, clientId } ) {
);
}

function usePagesByParentId() {
function useFrontPageId() {
return useSelect( ( select ) => {
const site = select( coreStore ).getEntityRecord( 'root', 'site' );
return site?.show_on_front === 'page' && site?.page_on_front;
}, [] );
}

function usePageData() {
const { pages } = useSelect( ( select ) => {
const { getEntityRecords } = select( coreStore );

Expand Down Expand Up @@ -137,6 +144,7 @@ const PageItems = memo( function PageItems( {
depth = 0,
} ) {
const pages = pagesByParentId.get( parentId );
const frontPageId = useFrontPageId();

if ( ! pages?.length ) {
return [];
Expand All @@ -155,6 +163,7 @@ const PageItems = memo( function PageItems( {
'open-on-hover-click':
! context.openSubmenusOnClick &&
context.showSubmenuIcon,
'menu-item-home': page.id === frontPageId,
} ) }
>
{ hasChildren && context.openSubmenusOnClick ? (
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/page-list/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ function block_core_page_list_render_nested_page_list( $open_submenus_on_click,
}
}

$front_page_id = (int) get_option( 'page_on_front' );
if ( (int) $page['page_id'] === $front_page_id ) {
$css_class .= ' menu-item-home';
}

$title = wp_kses( $page['title'], wp_kses_allowed_html( 'post' ) );
$aria_label = sprintf(
/* translators: Accessibility text. %s: Parent page title. */
Expand Down

0 comments on commit 4544361

Please sign in to comment.