Skip to content

Commit 941f524

Browse files
committed
Better link managment
1 parent 7de502d commit 941f524

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

src/Header/Header.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ export const Header = memo(
142142
);
143143
})()}
144144
</div>
145+
{/*
146+
<div class="fr-header__operator">
147+
<a href="/" title="Accueil - [À MODIFIER - texte alternatif de l’image : nom de l'opérateur ou du site serviciel] - République Française">
148+
<img class="fr-responsive-img" style="width:3.5rem;" src="../../../example/img/placeholder.9x16.png" alt="[À MODIFIER - texte alternatif de l’image]" />
149+
<!-- L’alternative de l’image (attribut alt) doit impérativement être renseignée et reprendre le texte visible dans l’image -->
150+
</a>
151+
</div>
152+
*/}
153+
145154
{(quickAccessItems.length > 0 ||
146155
renderSearchInput !== undefined) && (
147156
<div

src/lib/routing.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ import type { ReactNode } from "react";
33

44
import type { DetailedHTMLProps, AnchorHTMLAttributes } from "react";
55

6-
interface AriaEnabled {
7-
[key: `aria-${string}`]: string | boolean;
8-
}
9-
106
// eslint-disable-next-line @typescript-eslint/no-empty-interface
11-
export interface LinkProps extends AriaEnabled {
7+
export interface LinkProps extends React.AriaAttributes {
128
className?: string;
139
children?: ReactNode;
1410
}
@@ -18,7 +14,8 @@ export type HTMLAnchorProps = DetailedHTMLProps<
1814
HTMLAnchorElement
1915
>;
2016

21-
const context = createContext<CreateLinkProviderPrams["Link"]>(props => <a {...props} />);
17+
//NOTE: Here we have use as any because the module augmentation that we define in ../next applies unfortunately.
18+
const context = createContext<CreateLinkProviderPrams["Link"]>(props => <a {...(props as any)} />);
2219

2320
type CreateLinkProviderPrams = {
2421
Link: (props: LinkProps) => ReturnType<React.FC>;

src/next.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import DefaultDocument from "next/document";
3434
import { getAssetUrl } from "./lib/tools/getAssetUrl";
3535
import { setLangToUseIfProviderNotUsed } from "./lib/i18n";
3636
import { getColors } from "./lib/colors";
37+
import { createDsfrLinkProvider } from "./lib/routing";
38+
import Link from "next/link";
3739
import "./dsfr/dsfr.css";
3840
import "./dsfr/utility/icons/icons.css";
3941

@@ -50,6 +52,15 @@ const fontUrlByFileBasename = {
5052
"Spectral-ExtraBold": spectralExtraBoldWoff2Url
5153
} as const;
5254

55+
type InferLinkProps<Link> = Link extends React.ForwardRefExoticComponent<infer Props>
56+
? Props
57+
: never;
58+
59+
declare module "./lib/routing" {
60+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
61+
export interface LinkProps extends InferLinkProps<typeof Link> {}
62+
}
63+
5364
export type Params = Params.WithDarkModeCookie | Params.WithoutDarkModeCookie;
5465
export namespace Params {
5566
export type Common = StartDsfrReactParams & {
@@ -135,6 +146,8 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
135146
}
136147
}
137148

149+
const { DsfrLinkProvider } = createDsfrLinkProvider({ Link });
150+
138151
const isDarkPropKey = "dsfrIsDark";
139152

140153
function withDsfr<AppComponent extends NextComponentType<any, any, any>>(
@@ -155,7 +168,7 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
155168
}, []);
156169

157170
return (
158-
<>
171+
<DsfrLinkProvider>
159172
<Head>
160173
{process.env.NODE_ENV !== "development" &&
161174
objectKeys(fontUrlByFileBasename)
@@ -197,7 +210,7 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
197210
<App {...(props as any)} />
198211
</SsrIsDarkProvider>
199212
)}
200-
</>
213+
</DsfrLinkProvider>
201214
);
202215
}
203216

test/integration/next/pages/_app.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ import { Header } from "@codegouvfr/react-dsfr/Header";
66
import { createEmotionSsrAdvancedApproach } from "tss-react/next";
77
import { useStyles } from "tss-react/dsfr";
88
import { fr } from "@codegouvfr/react-dsfr";
9-
import type { LinkProps as NextLinkProps } from "next/link";
10-
11-
declare module "@codegouvfr/react-dsfr" {
12-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
13-
export interface LinkProps extends NextLinkProps { }
14-
15-
}
169

1710
const {
1811
withDsfr,

0 commit comments

Comments
 (0)