Skip to content

Commit ea1e504

Browse files
committed
Provide Link and useLang to backend as well in Next App Router
1 parent 464d2dd commit ea1e504

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/link.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export type RegisteredLinkProps = RegisterLink extends {
1616

1717
let Link: (
1818
props: RegisteredLinkProps & { children: ReactNode }
19-
) => ReturnType<React.FC> = props => <a {...props} />;
19+
) => ReturnType<React.FC> = props=> <a {...props} />;
2020

2121
export function setLink(params: { Link: typeof Link }): void {
2222
Link = props => {

src/next-appdir/DsfrHead.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useMemo, type ReactNode } from "react";
22
import { objectKeys } from "tsafe/objectKeys";
33
import { getAssetUrl } from "../tools/getAssetUrl";
44
import AppleTouchIcon from "../dsfr/favicon/apple-touch-icon.png";
@@ -7,6 +7,8 @@ import FaviconIco from "../dsfr/favicon/favicon.ico";
77
import { getScriptToRunAsap } from "../useIsDark/scriptToRunAsap";
88
import { fontUrlByFileBasename } from "./zz_internal/fontUrlByFileBasename";
99
import { getDefaultColorSchemeServerSide } from "./zz_internal/defaultColorScheme";
10+
import { setLink, type RegisteredLinkProps } from "../link";
11+
import { setUseLang } from "../i18n";
1012
//NOTE: As of now there is no way to enforce ordering in Next Appdir
1113
//See: https://github.com/vercel/next.js/issues/16630
1214
// @import url(...) doesn't work. Using Sass and @use is our last resort.
@@ -17,15 +19,31 @@ export type DsfrHeadProps = {
1719
* Preloading of fonts is only enabled in production.
1820
*/
1921
preloadFonts?: (keyof typeof fontUrlByFileBasename)[];
22+
/** Default: <a /> */
23+
Link?: (props: RegisteredLinkProps & { children: ReactNode }) => ReturnType<React.FC>;
24+
/** Default: ()=> "fr" */
25+
useLang?: () => string;
2026
};
2127

2228
const isProduction = process.env.NODE_ENV !== "development";
2329

2430
export function DsfrHead(props: DsfrHeadProps) {
25-
const { preloadFonts = [] } = props;
31+
const { preloadFonts = [], Link, useLang } = props;
2632

2733
const defaultColorScheme = getDefaultColorSchemeServerSide();
2834

35+
useMemo(() => {
36+
if (Link !== undefined) {
37+
setLink({ Link });
38+
}
39+
},[Link]);
40+
41+
useMemo(() => {
42+
if (useLang !== undefined) {
43+
setUseLang({ useLang });
44+
}
45+
},[useLang]);
46+
2947
return (
3048
<>
3149
{isProduction &&

test/integration/next-appdir/app/layout.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Footer } from "@codegouvfr/react-dsfr/Footer";
1111
import { headerFooterDisplayItem } from "@codegouvfr/react-dsfr/Display";
1212
import { fr } from "@codegouvfr/react-dsfr";
1313
import { Navigation } from "./Navigation";
14+
import Link from "next/link";
1415

1516
declare module "@codegouvfr/react-dsfr/gdpr" {
1617
interface RegisterGdprServices {
@@ -33,6 +34,7 @@ export default function RootLayout({ children }: { children: JSX.Element; }) {
3334
<title>Next 13 AppDir Demo DSFR setup</title>
3435
<StartDsfr />
3536
<DsfrHead
37+
Link={Link}
3638
preloadFonts={[
3739
//"Marianne-Light",
3840
//"Marianne-Light_Italic",
@@ -67,9 +69,9 @@ export default function RootLayout({ children }: { children: JSX.Element; }) {
6769
<Header
6870
brandTop={<>INTITULE<br />OFFICIEL</>}
6971
serviceTitle="Nom du site / service"
70-
homeLinkProps={{
71-
"href": "/",
72-
"title": "Accueil - Nom de l’entité (ministère, secrétariat d‘état, gouvernement)"
72+
homeLinkProps={{
73+
"href": "/",
74+
"title": "Accueil - Nom de l’entité (ministère, secrétariat d‘état, gouvernement)"
7375
}}
7476
quickAccessItems={[
7577
headerFooterDisplayItem,

0 commit comments

Comments
 (0)