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

Add menu-item-home class to Navigation for front page #37301

Merged
merged 3 commits into from
Jan 6, 2022
Merged
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
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