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

Make SiteHub available in mobile viewports #63060

Closed
wants to merge 6 commits 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
22 changes: 16 additions & 6 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ export default function Layout( { route } ) {
} }
className="edit-site-layout__sidebar"
>
<SiteHub
ref={ toggleRef }
isTransparent={
isResizableFrameOversized
}
/>
<SidebarContent routeKey={ routeKey }>
<SiteHub
Copy link
Contributor

@andrewserong andrewserong Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it looks like moving the SiteHub to be a child of SidebarContent means that the side bar content's focusing behaviour now matches against the buttons within the site hub, which causes one of the e2e tests to fail ('Can use keyboard to navigate the site editor').

From testing manually it seems that what's happening is that if you navigate to the Pages button from the root and hit Enter, when you're taken to the pages screen, the W icon is focused instead of the back button. I.e. here's where focus now lands:

image

Here's the function that handles the focusing:

function focusSidebarElement( el, direction, focusSelector ) {

I'm wondering if we need to either have a way for that focusing to skip the site hub and just look at the sidebar area, or if we need a way to give the site hub access to the navigation context without the other wrapping logic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a commit in 4401f60 that hopefully addresses the focus issue 🤞. It's a little hacky but let's see if it gets the test passing.

ref={ toggleRef }
isTransparent={
isResizableFrameOversized
}
/>
{ areas.sidebar }
</SidebarContent>
<SaveHub />
Expand All @@ -174,6 +174,16 @@ export default function Layout( { route } ) {

{ isMobileViewport && areas.mobile && (
<div className="edit-site-layout__mobile">
{ canvasMode !== 'edit' && (
<SidebarContent routeKey={ routeKey }>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsnajdr if you have any thoughts here.

Copy link
Contributor

@andrewserong andrewserong Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another side effect of wrapping in SidebarContent is that the site hub receives extra padding, so there's a jump horizontally when we dig into the lower levels of the hierarchy:

2024-07-04.12.04.19.mp4

Edit: After re-checking out this branch I can't reproduce the above 🤔

<SiteHub
ref={ toggleRef }
isTransparent={
isResizableFrameOversized
}
/>
</SidebarContent>
) }
{ areas.mobile }
</div>
) }
Expand Down
7 changes: 6 additions & 1 deletion packages/edit-site/src/components/sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ function focusSidebarElement( el, direction, focusSelector ) {
elementToFocus = el.querySelector( focusSelector );
}
if ( direction !== null && ! elementToFocus ) {
const [ firstTabbable ] = focus.tabbable.find( el );
// When looking for the first tabbable element, we need to skip the
// site hub if present, where we do not want to place focus.
const siblingOfSiteHub = el.querySelector(
'.edit-site-site-hub'
)?.nextElementSibling;
const [ firstTabbable ] = focus.tabbable.find( siblingOfSiteHub || el );
elementToFocus = firstTabbable ?? el;
}
elementToFocus?.focus();
Expand Down
56 changes: 43 additions & 13 deletions packages/edit-site/src/components/site-hub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,35 @@ import clsx from 'clsx';
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { Button, __experimentalHStack as HStack } from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { memo, forwardRef } from '@wordpress/element';
import { memo, forwardRef, useContext } from '@wordpress/element';
import { search } from '@wordpress/icons';
import { store as commandsStore } from '@wordpress/commands';
import { displayShortcut } from '@wordpress/keycodes';
import { filterURLForDisplay } from '@wordpress/url';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import SiteIcon from '../site-icon';
import { unlock } from '../../lock-unlock';
const { useHistory } = unlock( routerPrivateApis );
import { SidebarNavigationContext } from '../sidebar';

const SiteHub = memo(
forwardRef( ( { isTransparent }, ref ) => {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const history = useHistory();
const { navigate } = useContext( SidebarNavigationContext );
const isRoot =
Copy link
Member Author

@oandregal oandregal Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to detect we are in the root?

This relies on the search being empty, which it always is at the root level, as far as I've tested. If there's some pollution (a postType=X remanent in the URL, for example) this wouldn't work (but also: we should clean that).

The sidebar uses a explicit isRoot property. Given this is a temporary thing for 6.6 and will be improved by the plans/direction at #60847 I didn't want to modify the public API of SiteHub.

history.location.pathname === '/wp-admin/site-editor.php' &&
history.location.search === '';

const { dashboardLink, homeUrl, siteTitle } = useSelect( ( select ) => {
const { getSettings } = unlock( select( editSiteStore ) );

Expand Down Expand Up @@ -57,18 +68,37 @@ const SiteHub = memo(
}
) }
>
<Button
ref={ ref }
href={ dashboardLink }
label={ __( 'Go to the Dashboard' ) }
className="edit-site-layout__view-mode-toggle"
style={ {
transform: 'scale(0.5)',
borderRadius: 4,
} }
>
<SiteIcon className="edit-site-layout__view-mode-toggle-icon" />
</Button>
{ ( isRoot || ! isMobileViewport ) && (
<Button
ref={ ref }
href={ dashboardLink }
label={ __( 'Go to the Dashboard' ) }
className="edit-site-layout__view-mode-toggle"
style={ {
transform: 'scale(0.5)',
borderRadius: 4,
} }
>
<SiteIcon className="edit-site-layout__view-mode-toggle-icon" />
</Button>
) }
{ isMobileViewport && (
<Button
ref={ ref }
label={ __( 'Go to Site Editor' ) }
className="edit-site-layout__view-mode-toggle"
style={ {
transform: 'scale(0.5)',
borderRadius: 4,
} }
onClick={ () => {
history.push( {} );
navigate( 'back' );
} }
>
<SiteIcon className="edit-site-layout__view-mode-toggle-icon" />
</Button>
) }
</div>

<HStack>
Expand Down
Loading