Skip to content

Commit

Permalink
Remove client-side transitions completely
Browse files Browse the repository at this point in the history
With nextjs, internal links must be wrapped by a <Link> component to
enable client-side transition.

dsfr framework is broken with SPA, and the menu doesn't display
on mobile after you click on a link.

The hack in _app.jsx disables client side transitions completely.
  • Loading branch information
brmzkw committed Oct 4, 2021
1 parent 687552b commit 7160caf
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 27 deletions.
18 changes: 13 additions & 5 deletions components/ButtonLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import React from 'react';
import PropTypes from 'prop-types';

const ButtonLink = React.forwardRef(
({ onClick, children, variant }, ref) => (
<button type="button" onClick={onClick} ref={ref} className={`fr-btn ${variant === "secondary" ? "fr-btn--secondary" : ""}`}>
{ children }
</button>
),
({ onClick, children, variant }, ref) => {
const classes = ['fr-btn'];

if (variant === 'secondary') {
classes.push('fr-btn--secondary');
}

return (
<button type="button" onClick={onClick} ref={ref} className={classes.join(' ')}>
{ children }
</button>
);
}
);

ButtonLink.defaultProps = {
Expand Down
10 changes: 7 additions & 3 deletions components/QuestionSection.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Link from 'next/link';

import ButtonLink from '@/components/ButtonLink';

import styles from '@/styles/components/QuestionSection.module.css';
Expand All @@ -8,9 +10,11 @@ export default function QuestionSection() {
<div className="fr-container">
<div className="fr-grid-row fr-grid-row--middle fr-grid-row--center">
<h5>Vous avez des questions ?</h5>
<a className="fr-ml-3w" href="/faq">
<ButtonLink>Consltez la FAQ</ButtonLink>
</a>
<div className="fr-ml-3w">
<Link href="/faq" passHref>
<ButtonLink>Consltez la FAQ</ButtonLink>
</Link>
</div>
</div>
</div>
</section>
Expand Down
40 changes: 26 additions & 14 deletions layouts/BaseLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ function Header() {
</p>
</div>
<div className="fr-header__operator">
<a href="/">
<img src="/images/icons/logo.svg" className="fr-responsive-img" style={{ maxWidth: '9.0625rem' }} alt="Logo le.taxi" />
</a>
<Link href="/" passHref>
<a>
<img src="/images/icons/logo.svg" className="fr-responsive-img" style={{ maxWidth: '9.0625rem' }} alt="Logo le.taxi" />
</a>
</Link>
</div>
<div className="fr-header__navbar">
<button className="fr-btn--menu fr-btn" data-fr-opened="false" aria-controls="modal-menu" aria-haspopup="menu" title="Menu" type="button">
Expand Down Expand Up @@ -60,16 +62,24 @@ function Header() {
<nav className="fr-nav" role="navigation" aria-label="Menu principal" id="header-navigation">
<ul className="fr-nav__list">
<li className="fr-nav__item">
<a href="/taxis_group" className="fr-nav__link" {...(router.asPath === "/taxis_group" ? { "aria-current": "page" } : {})}>Groupement de taxis</a>
<Link href="/taxis_group" passHref>
<a className="fr-nav__link" {...(router.asPath === "/taxis_group" ? { "aria-current": "page" } : {})}>Groupement de taxis</a>
</Link>
</li>
<li className="fr-nav__item">
<a href="/taxi" className="fr-nav__link" {...(router.asPath === "/taxi" ? { "aria-current": "page" } : {})}>Artisan taxi</a>
<Link href="/taxi" passHref>
<a className="fr-nav__link" {...(router.asPath === "/taxi" ? { "aria-current": "page" } : {})}>Artisan taxi</a>
</Link>
</li>
<li className="fr-nav__item">
<a href="/aom" className="fr-nav__link" {...(router.asPath === "/aom" ? { "aria-current": "page" } : {})}>Mobilité publique</a>
<Link href="/aom" passHref>
<a className="fr-nav__link" {...(router.asPath === "/aom" ? { "aria-current": "page" } : {})}>Mobilité publique</a>
</Link>
</li>
<li className="fr-nav__item">
<a href="/maas" className="fr-nav__link" {...(router.asPath === "/maas" ? { "aria-current": "page" } : {})}>Application de mobilité</a>
<Link href="/maas" passHref>
<a className="fr-nav__link" {...(router.asPath === "/maas" ? { "aria-current": "page" } : {})}>Application de mobilité</a>
</Link>
</li>
</ul>
</nav>
Expand All @@ -85,13 +95,15 @@ function Footer() {
<div className="fr-container">
<div className="fr-footer__body">
<div className="fr-footer__brand fr-enlarge-link">
<a href="/" title="Retour à l’accueil">
<p className="fr-logo" title="république française">
Ministère
<br />chargé des
<br />transports
</p>
</a>
<Link href="/" passHref>
<a title="Retour à l’accueil">
<p className="fr-logo" title="république française">
Ministère
<br />chargé des
<br />transports
</p>
</a>
</Link>
</div>
<div className="fr-footer__content">
<p className="fr-footer__content-desc">
Expand Down
26 changes: 25 additions & 1 deletion pages/_app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@

import React from 'react';

import { useRouter } from 'next/router';

import '@/styles/globals.css';
import '@gouvfr/dsfr/dist/css/dsfr.min.css';

function MyApp({ Component, pageProps }) {
React.useEffect(() => import('@gouvfr/dsfr/src/main'));
const router = useRouter();

React.useEffect(() => import('@gouvfr/dsfr/src/main'), []);

/*
* Disable client-side transition.
*
* Framework dsfr is broken with SPA. If you attempt to load the page and
* click on the link, it is not possible to click on the menu on mobile.
*
* As a work around, we force a full browser reload on page change.
*/
React.useEffect(() => {
const handleRouteChange = (url) => {
window.location.href = url;
};

router.events.on('routeChangeStart', handleRouteChange);
return () => {
router.events.off('routeChangeStart', handleRouteChange);
};
}, []);

return (
<>
<Component {...pageProps} />
Expand Down
17 changes: 13 additions & 4 deletions pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Image from 'next/image';
import Link from 'next/link';

import BaseLayout from '@/layouts/BaseLayout';

Expand Down Expand Up @@ -60,7 +61,9 @@ function ChallengesSection() {
<div className="fr-card fr-enlarge-link fr-pt-3w">
<div className="fr-card__body">
<h4 className="fr-card__title">
<a href="/taxi" className="fr-card__link">Artisan taxi</a>
<Link href="/taxi" passHref>
<a className="fr-card__link">Artisan taxi</a>
</Link>
</h4>
<p className="fr-card__desc">Recevez de nouvelles courses lorsque vous êtes en maraude</p>
</div>
Expand All @@ -74,7 +77,9 @@ function ChallengesSection() {
<div className="fr-card fr-enlarge-link fr-pt-3w">
<div className="fr-card__body">
<h4 className="fr-card__title">
<a href="/taxis_group" className="fr-card__link">Groupement de taxis</a>
<Link href="/taxis_group" passHref>
<a className="fr-card__link">Groupement de taxis</a>
</Link>
</h4>
<p className="fr-card__desc">Connectez vos taxis à tous les usagers</p>
</div>
Expand All @@ -88,7 +93,9 @@ function ChallengesSection() {
<div className="fr-card fr-enlarge-link fr-pt-3w">
<div className="fr-card__body">
<h4 className="fr-card__title">
<a href="/maas" className="fr-card__link">Application de mobilité</a>
<Link href="/maas" passHref>
<a className="fr-card__link">Application de mobilité</a>
</Link>
</h4>
<p className="fr-card__desc">Proposez aussi les taxis dans votre application grand public</p>
</div>
Expand All @@ -102,7 +109,9 @@ function ChallengesSection() {
<div className="fr-card fr-enlarge-link fr-pt-3w">
<div className="fr-card__body">
<h4 className="fr-card__title">
<a href="/aom" className="fr-card__link">Mobilité publique</a>
<Link href="/aom" passHref>
<a className="fr-card__link">Mobilité publique</a>
</Link>
</h4>
<p className="fr-card__desc">Intégrez le service public des taxis à votre offre locale</p>
</div>
Expand Down

0 comments on commit 7160caf

Please sign in to comment.