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

Site editor: fix nav error due to lack of context provider in narrow viewports #68907

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
145 changes: 86 additions & 59 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { unlock } from '../../lock-unlock';
import SaveKeyboardShortcut from '../save-keyboard-shortcut';
import { useIsSiteEditorLoading } from './hooks';
import useMovingAnimation from './animation';
import SidebarContent from '../sidebar';
import SidebarContent, { SidebarNavigationContext } from '../sidebar';
import SaveHub from '../save-hub';
import SavePanel from '../save-panel';

Expand All @@ -55,6 +55,30 @@ const { useLocation } = unlock( routerPrivateApis );

const ANIMATION_DURATION = 0.3;

// Navigation state that is updated when navigating back or forward. Helps us
// manage the animations and also focus.
function createNavState() {
let state = {
direction: null,
focusSelector: null,
};

return {
get() {
return state;
},
navigate( direction, focusSelector = null ) {
state = {
direction,
focusSelector:
direction === 'forward' && focusSelector
? focusSelector
: state.focusSelector,
};
},
};
}

function Layout() {
const { query, name: routeKey, areas, widths } = useLocation();
const { canvas = 'view' } = query;
Expand Down Expand Up @@ -90,6 +114,8 @@ function Layout() {
// Should not depend on the previous canvas mode value but the next.
}, [ canvas ] );

const [ navState ] = useState( createNavState );

return (
<>
<UnsavedChangesWarning />
Expand All @@ -108,75 +134,76 @@ function Layout() {
) }
>
<div className="edit-site-layout__content">
{ /*
<SidebarNavigationContext.Provider value={ navState }>
{ /*
The NavigableRegion must always be rendered and not use
`inert` otherwise `useNavigateRegions` will fail.
*/ }
{ ( ! isMobileViewport || ! areas.mobile ) && (
<NavigableRegion
ariaLabel={ __( 'Navigation' ) }
className="edit-site-layout__sidebar-region"
>
<AnimatePresence>
{ canvas === 'view' && (
<motion.div
initial={ { opacity: 0 } }
animate={ { opacity: 1 } }
exit={ { opacity: 0 } }
transition={ {
type: 'tween',
duration:
// Disable transition in mobile to emulate a full page transition.
disableMotion ||
isMobileViewport
? 0
: ANIMATION_DURATION,
ease: 'easeOut',
} }
className="edit-site-layout__sidebar"
>
<SiteHub
{ ( ! isMobileViewport || ! areas.mobile ) && (
<NavigableRegion
ariaLabel={ __( 'Navigation' ) }
className="edit-site-layout__sidebar-region"
>
<AnimatePresence>
{ canvas === 'view' && (
<motion.div
initial={ { opacity: 0 } }
animate={ { opacity: 1 } }
exit={ { opacity: 0 } }
transition={ {
type: 'tween',
duration:
// Disable transition in mobile to emulate a full page transition.
disableMotion ||
isMobileViewport
? 0
: ANIMATION_DURATION,
ease: 'easeOut',
} }
className="edit-site-layout__sidebar"
>
<SiteHub
ref={ toggleRef }
isTransparent={
isResizableFrameOversized
}
/>
<SidebarContent
shouldAnimate={
routeKey !== 'styles'
}
routeKey={ routeKey }
>
<ErrorBoundary>
{ areas.sidebar }
</ErrorBoundary>
</SidebarContent>
<SaveHub />
<SavePanel />
</motion.div>
) }
</AnimatePresence>
</NavigableRegion>
) }

{ isMobileViewport && areas.mobile && (
<div className="edit-site-layout__mobile">
{ canvas !== 'edit' && (
<SidebarContent routeKey={ routeKey }>
<SiteHubMobile
ref={ toggleRef }
isTransparent={
isResizableFrameOversized
}
/>
<SidebarContent
shouldAnimate={
routeKey !== 'styles'
}
routeKey={ routeKey }
>
<ErrorBoundary>
{ areas.sidebar }
</ErrorBoundary>
</SidebarContent>
<SaveHub />
<SavePanel />
</motion.div>
</SidebarContent>
) }
</AnimatePresence>
</NavigableRegion>
) }

<ErrorBoundary>{ areas.mobile }</ErrorBoundary>
</div>
) }
</SidebarNavigationContext.Provider>
<EditorSnackbars />

{ isMobileViewport && areas.mobile && (
<div className="edit-site-layout__mobile">
{ canvas !== 'edit' && (
<SidebarContent routeKey={ routeKey }>
<SiteHubMobile
ref={ toggleRef }
isTransparent={
isResizableFrameOversized
}
/>
</SidebarContent>
) }
<ErrorBoundary>{ areas.mobile }</ErrorBoundary>
</div>
) }

{ ! isMobileViewport &&
areas.content &&
canvas !== 'edit' && (
Expand Down
44 changes: 8 additions & 36 deletions packages/edit-site/src/components/sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,6 @@ function focusSidebarElement( el, direction, focusSelector ) {
elementToFocus?.focus();
}

// Navigation state that is updated when navigating back or forward. Helps us
// manage the animations and also focus.
function createNavState() {
let state = {
direction: null,
focusSelector: null,
};

return {
get() {
return state;
},
navigate( direction, focusSelector = null ) {
state = {
direction,
focusSelector:
direction === 'forward' && focusSelector
? focusSelector
: state.focusSelector,
};
},
};
}

function SidebarContentWrapper( { children, shouldAnimate } ) {
const navState = useContext( SidebarNavigationContext );
const wrapperRef = useRef();
Expand Down Expand Up @@ -92,18 +68,14 @@ export default function SidebarContent( {
shouldAnimate,
children,
} ) {
const [ navState ] = useState( createNavState );

return (
<SidebarNavigationContext.Provider value={ navState }>
<div className="edit-site-sidebar__content">
<SidebarContentWrapper
shouldAnimate={ shouldAnimate }
key={ routeKey }
>
{ children }
</SidebarContentWrapper>
</div>
</SidebarNavigationContext.Provider>
<div className="edit-site-sidebar__content">
<SidebarContentWrapper
shouldAnimate={ shouldAnimate }
key={ routeKey }
>
{ children }
</SidebarContentWrapper>
</div>
);
}
Loading