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

Fix Library routing on mobile #51558

Merged
merged 1 commit into from
Jun 16, 2023
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
27 changes: 19 additions & 8 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export default function Layout() {

const hubRef = useRef();
const { params } = useLocation();
const isListPage = getIsListPage( params );
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isListPage = getIsListPage( params, isMobileViewport );
const isEditorPage = ! isListPage;
const { hasFixedToolbar, canvasMode, previousShortcut, nextShortcut } =
useSelect( ( select ) => {
Expand All @@ -91,7 +92,6 @@ export default function Layout() {
next: nextShortcut,
} );
const disableMotion = useReducedMotion();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const showSidebar =
( isMobileViewport && ! isListPage ) ||
( ! isMobileViewport && ( canvasMode === 'view' || ! isEditorPage ) );
Expand Down Expand Up @@ -171,20 +171,31 @@ export default function Layout() {

<div className="edit-site-layout__content">
<AnimatePresence initial={ false }>
{ showSidebar && (
{
<motion.div
initial={ {
opacity: 0,
} }
animate={ {
opacity: 1,
} }
animate={
showSidebar
Copy link
Member Author

Choose a reason for hiding this comment

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

Instead of completely avoiding rendering the sidebar, we simply hide it here, so that some routing syncing logic in the sidebar can still apply in some senerios.

? { opacity: 1, display: 'block' }
: {
opacity: 0,
transitionEnd: {
display: 'none',
},
}
}
exit={ {
opacity: 0,
} }
transition={ {
type: 'tween',
duration: ANIMATION_DURATION,
duration:
// Disable transition in mobile to emulate a full page transition.
disableMotion || isMobileViewport
? 0
: ANIMATION_DURATION,
ease: 'easeOut',
} }
className="edit-site-layout__sidebar"
Expand All @@ -195,7 +206,7 @@ export default function Layout() {
<Sidebar />
</NavigableRegion>
</motion.div>
) }
}
</AnimatePresence>

<SavePanel />
Expand Down
48 changes: 39 additions & 9 deletions packages/edit-site/src/components/page-library/patterns-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@ import {
__experimentalHeading as Heading,
__experimentalText as Text,
__experimentalVStack as VStack,
Flex,
FlexBlock,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { symbol } from '@wordpress/icons';
import { __, isRTL } from '@wordpress/i18n';
import { symbol, chevronLeft, chevronRight } from '@wordpress/icons';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useViewportMatch } from '@wordpress/compose';

/**
* Internal dependencies
*/
import Grid from './grid';
import NoPatterns from './no-patterns';
import usePatterns from './use-patterns';
import SidebarButton from '../sidebar-button';
import useDebouncedInput from '../../utils/use-debounced-input';
import { unlock } from '../../lock-unlock';

const { useLocation, useHistory } = unlock( routerPrivateApis );

export default function PatternsList( { categoryId, label, type } ) {
const location = useLocation();
const history = useHistory();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const [ filterValue, setFilterValue, delayedFilterValue ] =
useDebouncedInput( '' );

Expand All @@ -34,13 +45,32 @@ export default function PatternsList( { categoryId, label, type } ) {

return (
<VStack spacing={ 6 }>
<SearchControl
className="edit-site-library__search"
onChange={ ( value ) => setFilterValue( value ) }
placeholder={ __( 'Search patterns' ) }
value={ filterValue }
__nextHasNoMarginBottom
/>
<Flex>
{ isMobileViewport && (
<SidebarButton
icon={ isRTL() ? chevronRight : chevronLeft }
label={ __( 'Back' ) }
onClick={ () => {
// Go back in history if we came from the library page.
// Otherwise push a stack onto the history.
if ( location.state?.backPath === '/library' ) {
history.back();
} else {
history.push( { path: '/library' } );
}
Comment on lines +54 to +60
Copy link
Member Author

Choose a reason for hiding this comment

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

The reason we have to do this instead of leveraging NavigatorToParentButton is because this tree isn't wrapped in the NavigatorProvider context, nor is it under the /library subtree of the NavigatorScreen, so navigator.goBack() won't work here.

I couldn't figure out a way to do this with the Navigator approach so I fallback to the regular browser's history approach. The way it works is that we can attach a state when pushing a stack onto the history. In this case the state is set when clicking category links in the library sidebar. If the current location has the state then we can just call history.back() and go back to the library page. Otherwise, when there's no such state (like a reload) then we just push a new entry to the history instead.

} }
/>
) }
<FlexBlock>
<SearchControl
className="edit-site-library__search"
onChange={ ( value ) => setFilterValue( value ) }
placeholder={ __( 'Search patterns' ) }
value={ filterValue }
__nextHasNoMarginBottom
/>
</FlexBlock>
</Flex>
{ isResolving && __( 'Loading' ) }
{ ! isResolving && !! syncedPatterns.length && (
<>
Expand Down
6 changes: 5 additions & 1 deletion packages/edit-site/src/components/page-library/style.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
.edit-site-library {
background: rgba(0, 0, 0, 0.05);
margin: 0;
margin: $header-height 0 0;
.components-text {
color: $gray-600;
}

.components-heading {
color: $white;
}

@include break-medium {
margin: 0;
}
}

.edit-site-library__grid {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ export default function CategoryItem( {
label,
type,
} ) {
const linkInfo = useLink( {
path: '/library',
categoryType: type,
categoryId: id,
} );
const linkInfo = useLink(
{
path: '/library',
categoryType: type,
categoryId: id,
},
{
// Keep record for where we came from in a state so we can
// use browser's back button to go back to the library.
// See the implementation of the back button in patterns-list.
backPath: '/library',
}
);

if ( ! count ) {
return;
Expand Down
20 changes: 16 additions & 4 deletions packages/edit-site/src/utils/get-is-list-page.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
/**
* Returns if the params match the list page route.
*
* @param {Object} params The url params.
* @param {string} params.path The current path.
* @param {Object} params The url params.
* @param {string} params.path The current path.
* @param {string} [params.categoryType] The current category type.
* @param {string} [params.categoryId] The current category id.
* @param {boolean} isMobileViewport Is mobile viewport.
*
* @return {boolean} Is list page or not.
*/
export default function getIsListPage( { path } ) {
return path === '/wp_template/all' || path === '/library';
export default function getIsListPage(
{ path, categoryType, categoryId },
isMobileViewport
) {
return (
path === '/wp_template/all' ||
( path === '/library' &&
// Don't treat "/library" without categoryType and categoryId as a list page
// in mobile because the sidebar covers the whole page.
( ! isMobileViewport || ( !! categoryType && !! categoryId ) ) )
);
}