Skip to content

Commit 837345c

Browse files
committed
Enable button to be used with linkProps
1 parent 7b7f3ef commit 837345c

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/link.tsx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import React, { type ReactNode, type DetailedHTMLProps, type AnchorHTMLAttributes } from "react";
2+
import { fr } from "./fr";
3+
import { cx } from "./tools/cx";
4+
import { assert } from "tsafe/assert";
5+
import { is } from "tsafe/is";
26

37
type HTMLAnchorProps = DetailedHTMLProps<
48
AnchorHTMLAttributes<HTMLAnchorElement>,
@@ -16,7 +20,30 @@ export type RegisteredLinkProps = RegisterLink extends {
1620

1721
let Link: (
1822
props: RegisteredLinkProps & { children: ReactNode }
19-
) => ReturnType<React.FC> = props => <a {...props} />;
23+
) => ReturnType<React.FC> = props => {
24+
const { href, ...rest } = props as { to?: string; href?: string };
25+
26+
button: {
27+
if (href !== "#" || !("onClick" in rest)) {
28+
break button;
29+
}
30+
31+
assert(
32+
is<
33+
React.DetailedHTMLProps<
34+
React.ButtonHTMLAttributes<HTMLButtonElement>,
35+
HTMLButtonElement
36+
>
37+
>(rest)
38+
);
39+
40+
return <button {...rest} className={cx(fr.cx("fr-link"), rest.className)} />;
41+
}
42+
43+
return <a href={href} {...rest} />;
44+
};
45+
46+
//<a {...props} />;
2047

2148
export function setLink(params: { Link: typeof Link }): void {
2249
Link = props => {
@@ -27,6 +54,23 @@ export function setLink(params: { Link: typeof Link }): void {
2754
(typeof to === "string" ? to : undefined) ??
2855
(typeof href === "string" ? href : undefined);
2956

57+
button: {
58+
if (target !== "#" || !("onClick" in rest)) {
59+
break button;
60+
}
61+
62+
assert(
63+
is<
64+
React.DetailedHTMLProps<
65+
React.ButtonHTMLAttributes<HTMLButtonElement>,
66+
HTMLButtonElement
67+
>
68+
>(rest)
69+
);
70+
71+
return <button {...rest} className={cx(fr.cx("fr-link"), rest.className)} />;
72+
}
73+
3074
mailto: {
3175
if (target === undefined || !target.startsWith("mailto:")) {
3276
break mailto;

0 commit comments

Comments
 (0)