Skip to content

Commit f46a55e

Browse files
committed
fix: replace depth with pathnames
This gives more control to the use of usePageRouter in nested layouts where sub-pages do not have a pathname in common.
1 parent 2811ceb commit f46a55e

File tree

7 files changed

+17
-30
lines changed

7 files changed

+17
-30
lines changed

www/shared/react/next-router/use-page-key.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@ import { useRouter } from 'next/router'; // eslint-disable-line no-restricted-im
66
// You may use `depth` to specify the depth of the URL you are interested in.
77
// As an example, a depth of 1 for /foo/bar, will return /foo.
88

9-
const usePageKey = (depth = null) => {
9+
const usePageKey = () => {
1010
const { asPath } = useRouter();
1111

1212
const pageKey = useMemo(() => {
1313
// Remove query string.
14-
let pageKey = asPath.replace(/\?.+/, '');
15-
16-
// Handle depth.
17-
if (depth != null) {
18-
pageKey = pageKey.split('/').slice(0, depth + 1).join('/');
19-
}
14+
const pageKey = asPath.replace(/\?.+/, '');
2015

2116
return pageKey;
22-
}, [asPath, depth]);
17+
}, [asPath]);
2318

2419
return pageKey;
2520
};

www/shared/react/next-router/use-page-key.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,3 @@ it('should return the correct page key', () => {
1717

1818
screen.getByText('/blog/foo');
1919
});
20-
21-
it('should return the correct page key with depth', () => {
22-
Router.pathname = '/blog/[name]';
23-
Router.asPath = '/blog/foo?baz=1';
24-
25-
render(<MyComponent depth={ 1 } />, { wrapper: undefined });
26-
27-
screen.getByText('/blog');
28-
});

www/shared/react/next-router/use-page-router.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import usePageKey from './use-page-key';
66
// returns the correct router even during page transitions. During the transition,
77
// the old page will still be mounted when animating out, but the router has already changed.
88
// This hook returns the same router your page started with.
9+
// Additionally, you may specify an `pathnames` array to allow the returned router to change only for these pages.
910

10-
const usePageRouter = (depth) => {
11-
const pageKey = usePageKey(depth);
11+
const usePageRouter = (pathnames) => {
1212
const router = useRouter();
13+
const pageKey = usePageKey();
1314

14-
const routerRef = useRef(router);
15+
const routerRef = useRef(router.pathname);
1516
const pageKeyRef = useRef(pageKey);
1617

17-
if (pageKeyRef.current === pageKey) {
18+
if (pageKeyRef.current === pageKey || pathnames?.includes(router.pathname)) {
1819
pageKeyRef.current = pageKey;
1920
routerRef.current = router;
2021
}

www/shared/react/next-router/use-page-router.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ it('should return the same router it started with', () => {
6060
});
6161
});
6262

63-
it('should respect depth', () => {
63+
it('should respect pathnames', () => {
6464
let router;
6565

6666
const MyComponent = () => {
67-
router = usePageRouter(1);
67+
router = usePageRouter(['/blog/[name]']);
6868

6969
return null;
7070
};

www/shared/react/next-router/with-page-router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import React, { forwardRef } from 'react';
22
import hoistNonReactStatics from 'hoist-non-react-statics';
33
import usePageRouter from './use-page-router';
44

5-
const withPageRouter = (depth) => (WrappedComponent) => {
5+
const withPageRouter = (pathnames) => (WrappedComponent) => {
66
const WithPageRouter = forwardRef((props, ref) => {
7-
const router = usePageRouter(depth);
7+
const router = usePageRouter(pathnames);
88

99
return (
1010
<WrappedComponent ref={ ref } pageRouter={ router } { ...props } />

www/shared/react/next-router/with-page-router.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ it('should inject pageRouter prop', () => {
3131
render(<MyComponent />, { wrapper: undefined });
3232
});
3333

34-
it('should respect depth', () => {
35-
const MyComponent = withPageRouter(1)(({ pageRouter }) => pageRouter.asPath);
34+
it('should respect pathnames', () => {
35+
const MyComponent = withPageRouter(['/blog/[name]'])(({ pageRouter }) => pageRouter.asPath);
3636

3737
const { rerender } = render(<MyComponent />, { wrapper: undefined });
3838

www/shared/styles/imports/mixins/typography.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@
265265
font-size: 1.4$(unit);
266266
line-height: 1.45;
267267

268-
@media (--gte-xl) {
269-
font-size: 1.6$(unit);
270-
line-height: 1.5;
268+
@media (--gte-sm) {
269+
font-size: 1.8$(unit);
270+
line-height: 1.33;
271271
}
272272
}
273273

0 commit comments

Comments
 (0)