Skip to content

Commit 45366f8

Browse files
committed
Enable external links when using react-router
1 parent 789f7ae commit 45366f8

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

src/link.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,19 @@ let Link: (
1919
) => ReturnType<React.FC> = props => <a {...props} />;
2020

2121
export function setLink(params: { Link: typeof Link }): void {
22-
Link = params.Link;
22+
Link = props => {
23+
external_link_react_router: {
24+
const { to, ...rest } = props as { to?: string };
25+
26+
if (to === undefined || (!to.startsWith("//") && !/^https?:\/\//.test(to))) {
27+
break external_link_react_router;
28+
}
29+
30+
return <a href={to} target="_blank" {...rest} />;
31+
}
32+
33+
return <params.Link {...props} />;
34+
};
2335
}
2436

2537
export function getLink() {

test/integration/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"dependencies": {
1616
"react": "18.2.0",
1717
"react-dom": "18.2.0",
18-
"react-router-dom": "^6.4.3",
18+
"react-router-dom": "^6.8.1",
1919
"@mui/x-date-pickers": "^5.0.9",
2020
"@mui/icons-material": "^5.10.16",
2121
"dayjs": "^1.11.6",

test/integration/vite/src/Home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Alert } from "@codegouvfr/react-dsfr/Alert";
22
import { fr } from "@codegouvfr/react-dsfr";
3-
import { useIsDark } from "@codegouvfr/react-dsfr";
3+
import { useIsDark } from "@codegouvfr/react-dsfr/useIsDark";
44

55
export function Home() {
66
const { isDark, setIsDark } = useIsDark();

test/integration/vite/src/Mui.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
2-
import { createMuiDsfrThemeProvider } from "@codegouvfr/react-dsfr/mui";
2+
import { MuiDsfrThemeProvider } from "@codegouvfr/react-dsfr/mui";
33
import { ThemeProvider, createTheme } from "@mui/material/styles";
4-
import { useIsDark } from "@codegouvfr/react-dsfr";
4+
import { useIsDark } from "@codegouvfr/react-dsfr/useIsDark";
55
import TextField from '@mui/material/TextField';
66
import Autocomplete from '@mui/material/Autocomplete';
77
import Stack from '@mui/material/Stack';
@@ -43,8 +43,6 @@ const muiDefaultLightTheme = createTheme({
4343
}
4444
});
4545

46-
const { MuiDsfrThemeProvider } = createMuiDsfrThemeProvider();
47-
4846
export function Mui() {
4947

5048
const { isDark, setIsDark } = useIsDark();

test/integration/vite/src/main.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import ReactDOM from "react-dom/client";
33
import { Home } from "./Home";
44
import { Mui } from "./Mui";
5-
import { startReactDsfr } from "@codegouvfr/react-dsfr";
5+
import { startReactDsfr } from "@codegouvfr/react-dsfr/spa";
66
import { Header } from "@codegouvfr/react-dsfr/Header";
77
import { BrowserRouter } from "react-router-dom";
88
import { Routes, Route, Link, useLocation } from "react-router-dom";
@@ -11,7 +11,7 @@ import { fr } from "@codegouvfr/react-dsfr";
1111

1212
startReactDsfr({ "defaultColorScheme": "system", Link });
1313

14-
declare module "@codegouvfr/react-dsfr" {
14+
declare module "@codegouvfr/react-dsfr/spa" {
1515
interface RegisterLink {
1616
Link: typeof Link;
1717
}
@@ -36,7 +36,7 @@ function Root() {
3636
serviceTitle="Nom du site / service"
3737
homeLinkProps={{ "to": "/", "title": "Accueil - Nom de l’entité (ministère, secrétariat d‘état, gouvernement)" }}
3838
quickAccessItems={[headerFooterDisplayItem]}
39-
navItems={[
39+
navigation={[
4040
{
4141
"text": "Home",
4242
"linkProps": {
@@ -50,6 +50,13 @@ function Root() {
5050
"to": "/mui"
5151
},
5252
"isActive": location.pathname === "/mui"
53+
},
54+
{
55+
"text": "External link",
56+
"linkProps": {
57+
"to": "https://example.fr"
58+
},
59+
"isActive": false
5360
}
5461
]}
5562
/>

test/integration/vite/yarn.lock

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
to-fast-properties "^2.0.0"
249249

250250
"@codegouvfr/react-dsfr@file:../../../dist":
251-
version "0.3.0"
251+
version "0.33.1"
252252
dependencies:
253253
tsafe "^1.4.0"
254254

@@ -371,10 +371,10 @@
371371
react-transition-group "^4.4.5"
372372
rifm "^0.12.1"
373373

374-
"@remix-run/router@1.0.5":
375-
version "1.0.5"
376-
resolved "https://registry.yarnpkg.com/@remix-run/router/-/router-1.0.5.tgz#d5c65626add4c3c185a89aa5bd38b1e42daec075"
377-
integrity sha512-my0Mycd+jruq/1lQuO5LBB6WTlL/e8DTCYWp44DfMTDcXz8DcTlgF0ISaLsGewt+ctHN+yA8xMq3q/N7uWJPug==
374+
"@remix-run/router@1.3.2":
375+
version "1.3.2"
376+
resolved "https://registry.yarnpkg.com/@remix-run/router/-/router-1.3.2.tgz#58cd2bd25df2acc16c628e1b6f6150ea6c7455bc"
377+
integrity sha512-t54ONhl/h75X94SWsHGQ4G/ZrCEguKSRQr7DrjTciJXW0YU1QhlwYeycvK5JgkzlxmvrK7wq1NB/PLtHxoiDcA==
378378

379379
"@types/prop-types@*", "@types/prop-types@^15.7.5":
380380
version "15.7.5"
@@ -798,20 +798,20 @@ react-refresh@^0.14.0:
798798
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.14.0.tgz#4e02825378a5f227079554d4284889354e5f553e"
799799
integrity sha512-wViHqhAd8OHeLS/IRMJjTSDHF3U9eWi62F/MledQGPdJGDhodXJ9PBLNGr6WWL7qlH12Mt3TyTpbS+hGXMjCzQ==
800800

801-
react-router-dom@^6.4.3:
802-
version "6.4.5"
803-
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-6.4.5.tgz#4fdb12efef4f3848c693a76afbeaed1f6ca02047"
804-
integrity sha512-a7HsgikBR0wNfroBHcZUCd9+mLRqZS8R5U1Z1mzLWxFXEkUT3vR1XXmSIVoVpxVX8Bar0nQYYYc9Yipq8dWwAA==
801+
react-router-dom@^6.8.1:
802+
version "6.8.1"
803+
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-6.8.1.tgz#7e136b67d9866f55999e9a8482c7008e3c575ac9"
804+
integrity sha512-67EXNfkQgf34P7+PSb6VlBuaacGhkKn3kpE51+P6zYSG2kiRoumXEL6e27zTa9+PGF2MNXbgIUHTVlleLbIcHQ==
805805
dependencies:
806-
"@remix-run/router" "1.0.5"
807-
react-router "6.4.5"
806+
"@remix-run/router" "1.3.2"
807+
react-router "6.8.1"
808808

809-
react-router@6.4.5:
810-
version "6.4.5"
811-
resolved "https://registry.yarnpkg.com/react-router/-/react-router-6.4.5.tgz#73f382af2c8b9a86d74e761a7c5fc3ce7cb0024d"
812-
integrity sha512-1RQJ8bM70YEumHIlNUYc6mFfUDoWa5EgPDenK/fq0bxD8DYpQUi/S6Zoft+9DBrh2xmtg92N5HMAJgGWDhKJ5Q==
809+
react-router@6.8.1:
810+
version "6.8.1"
811+
resolved "https://registry.yarnpkg.com/react-router/-/react-router-6.8.1.tgz#e362caf93958a747c649be1b47cd505cf28ca63e"
812+
integrity sha512-Jgi8BzAJQ8MkPt8ipXnR73rnD7EmZ0HFFb7jdQU24TynGW1Ooqin2KVDN9voSC+7xhqbbCd2cjGUepb6RObnyg==
813813
dependencies:
814-
"@remix-run/router" "1.0.5"
814+
"@remix-run/router" "1.3.2"
815815

816816
react-transition-group@^4.4.5:
817817
version "4.4.5"

0 commit comments

Comments
 (0)