Skip to content

Commit 8119ec0

Browse files
committed
Move from provider to setter for the Link (preparing for appdir)
1 parent e5ea6c0 commit 8119ec0

File tree

13 files changed

+48
-59
lines changed

13 files changed

+48
-59
lines changed

src/Breadcrumb.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { symToStr } from "tsafe/symToStr";
33
import { assert } from "tsafe/assert";
44
import type { Equals } from "tsafe";
55

6-
import { RegisteredLinkProps, useLink } from "./lib/routing";
6+
import { RegisteredLinkProps, getLink } from "./lib/routing";
77
import { createComponentI18nApi } from "./lib/i18n";
88
import { fr } from "./lib";
99
import { cx } from "./lib/tools/cx";
@@ -33,7 +33,7 @@ export const Breadcrumb = memo(
3333

3434
const { t } = useTranslation();
3535

36-
const { Link } = useLink();
36+
const { Link } = getLink();
3737
const breadcrumbId = useId();
3838

3939
return (

src/Button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
import { fr } from "./lib";
99
import { cx } from "./lib/tools/cx";
1010
import type { FrIconClassName, RiIconClassName } from "./lib/generatedFromCss/classNames";
11-
import { RegisteredLinkProps, useLink } from "./lib/routing";
11+
import { RegisteredLinkProps, getLink } from "./lib/routing";
1212
import { assert } from "tsafe/assert";
1313
import type { Equals } from "tsafe";
1414
import { symToStr } from "tsafe/symToStr";
@@ -84,7 +84,7 @@ export const Button = memo(
8484

8585
assert<Equals<keyof typeof rest, never>>();
8686

87-
const { Link } = useLink();
87+
const { Link } = getLink();
8888

8989
const className = cx(
9090
fr.cx(

src/Footer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { memo, forwardRef } from "react";
22
import type { ReactNode } from "react";
3-
import { useLink } from "./lib/routing";
3+
import { getLink } from "./lib/routing";
44
import type { RegisteredLinkProps } from "./lib/routing";
55
import { symToStr } from "tsafe/symToStr";
66
import { fr } from "./lib";
@@ -102,7 +102,7 @@ export const Footer = memo(
102102

103103
assert<Equals<keyof typeof rest, never>>();
104104

105-
const { Link } = useLink();
105+
const { Link } = getLink();
106106

107107
const { t } = useTranslation();
108108

src/Header/Header.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createComponentI18nApi } from "../lib/i18n";
55
import { symToStr } from "tsafe/symToStr";
66
import { cx } from "../lib/tools/cx";
77
import type { RegisteredLinkProps } from "../lib/routing";
8-
import { useLink } from "../lib/routing";
8+
import { getLink } from "../lib/routing";
99
import type { MainNavigationProps } from "./MainNavigation";
1010
import { MainNavigation } from "./MainNavigation";
1111
import { assert } from "tsafe/assert";
@@ -121,7 +121,7 @@ export const Header = memo(
121121

122122
const { t } = useTranslation();
123123

124-
const { Link } = useLink();
124+
const { Link } = getLink();
125125

126126
return (
127127
<header

src/Header/MainNavigation/MainNavigation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Equals } from "tsafe";
77
import type { RegisteredLinkProps } from "../../lib/routing";
88
import { fr } from "../../lib";
99
import { cx } from "../../lib/tools/cx";
10-
import { useLink } from "../../lib/routing";
10+
import { getLink } from "../../lib/routing";
1111
import type { MenuProps } from "./Menu";
1212
import { Menu } from "./Menu";
1313
import type { MegaMenuProps } from "./MegaMenu";
@@ -75,7 +75,7 @@ export const MainNavigation = memo(
7575

7676
const { t } = useTranslation();
7777

78-
const { Link } = useLink();
78+
const { Link } = getLink();
7979

8080
const { getMenuId } = (function useClosure() {
8181
const id = useId();

src/Header/MainNavigation/MegaMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fr } from "../../lib";
66
import { cx } from "../../lib/tools/cx";
77
import { assert } from "tsafe/assert";
88
import type { Equals } from "tsafe";
9-
import { useLink } from "../../lib/routing";
9+
import { getLink } from "../../lib/routing";
1010
import type { RegisteredLinkProps } from "../../lib/routing";
1111

1212
export type MegaMenuProps = {
@@ -45,7 +45,7 @@ export const MegaMenu = memo(
4545

4646
const { t } = useTranslation();
4747

48-
const { Link } = useLink();
48+
const { Link } = getLink();
4949

5050
return (
5151
<div

src/Header/MainNavigation/Menu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { cx } from "../../lib/tools/cx";
66
import { assert } from "tsafe/assert";
77
import type { Equals } from "tsafe";
88
import type { RegisteredLinkProps } from "../../lib/routing";
9-
import { useLink } from "../../lib/routing";
9+
import { getLink } from "../../lib/routing";
1010

1111
export type MenuProps = {
1212
classes?: Partial<Record<"root" | "list", string>>;
@@ -27,7 +27,7 @@ export const Menu = memo(
2727

2828
assert<Equals<keyof typeof rest, never>>();
2929

30-
const { Link } = useLink();
30+
const { Link } = getLink();
3131

3232
return (
3333
<div className={cx(fr.cx("fr-menu"), classes.root)} id={id} ref={ref} {...rest}>

src/lib/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export type { SpacingToken } from "./spacing";
99
import { cx } from "./cx";
1010
export type { FrCxArg } from "./cx";
1111
export { DsfrLangProvider } from "./i18n";
12-
export { createDsfrLinkProvider } from "./routing";
1312
export type { RegisterLink, RegisteredLinkProps } from "./routing";
1413

1514
export const fr = {

src/lib/routing.tsx

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createContext, useContext } from "react";
1+
import React from "react";
22
import type { ReactNode } from "react";
33
import type { DetailedHTMLProps, AnchorHTMLAttributes } from "react";
44

@@ -8,40 +8,22 @@ type HTMLAnchorProps = DetailedHTMLProps<
88
>;
99

1010
// eslint-disable-next-line @typescript-eslint/no-empty-interface
11-
export interface RegisterLink {
12-
// Link: typeof Link
13-
}
11+
export interface RegisterLink {}
1412

1513
export type RegisteredLinkProps = RegisterLink extends {
1614
Link: (props: infer LinkProps) => any;
1715
}
1816
? Omit<LinkProps, "children">
1917
: Omit<HTMLAnchorProps, "children">;
2018

21-
const context = createContext<CreateLinkProviderPrams["Link"]>(props => <a {...props} />);
22-
23-
type CreateLinkProviderPrams = {
24-
Link: (props: RegisteredLinkProps & { children: ReactNode }) => ReturnType<React.FC>;
25-
};
26-
27-
export function createDsfrLinkProvider(params: CreateLinkProviderPrams) {
28-
const { Link } = params;
29-
30-
type Props = {
31-
children: ReactNode;
32-
};
19+
let Link: (
20+
props: RegisteredLinkProps & { children: ReactNode }
21+
) => ReturnType<React.FC> = props => <a {...props} />;
3322

34-
function DsfrLinkProvider(props: Props) {
35-
const { children } = props;
36-
37-
return <context.Provider value={Link}>{children}</context.Provider>;
38-
}
39-
40-
return { DsfrLinkProvider };
23+
export function setLink(params: { Link: typeof Link }): void {
24+
Link = params.Link;
4125
}
4226

43-
export function useLink() {
44-
const Link = useContext(context);
45-
27+
export function getLink() {
4628
return { Link };
4729
}

src/lib/start.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
import type { ReactNode } from "react";
12
import { isBrowser } from "./tools/isBrowser";
23
import { assert } from "tsafe/assert";
34
import { symToStr } from "tsafe/symToStr";
45
import { startI18nLogic } from "./i18n";
56
import type { ColorScheme } from "./darkMode";
67
import { startClientSideIsDarkLogic } from "./darkMode";
8+
import type { RegisteredLinkProps } from "./routing";
9+
import { setLink } from "./routing";
710

811
export type Params = {
912
defaultColorScheme: ColorScheme | "system";
1013
/** Default: false */
1114
verbose?: boolean;
15+
Link?: (props: RegisteredLinkProps & { children: ReactNode }) => ReturnType<React.FC>;
1216
};
1317

1418
export type NextParams = {
@@ -19,7 +23,7 @@ export type NextParams = {
1923
let isStarted = false;
2024

2125
async function startReactDsfrWithOptionalNextParams(params: Params, nextParams?: NextParams) {
22-
const { defaultColorScheme, verbose = false } = params;
26+
const { defaultColorScheme, verbose = false, Link } = params;
2327

2428
assert(
2529
isBrowser,
@@ -35,6 +39,10 @@ async function startReactDsfrWithOptionalNextParams(params: Params, nextParams?:
3539

3640
isStarted = true;
3741

42+
if (Link !== undefined) {
43+
setLink({ Link });
44+
}
45+
3846
const registerEffectAction: (action: () => void) => void =
3947
nextParams === undefined ? action => action() : nextParams.registerEffectAction;
4048

0 commit comments

Comments
 (0)