Skip to content

Commit

Permalink
Fix Library routing in mobile (#51558)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 authored Jun 16, 2023
1 parent 70745bd commit e08f66f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 27 deletions.
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
? { 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' } );
}
} }
/>
) }
<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 ) ) )
);
}

0 comments on commit e08f66f

Please sign in to comment.