11import 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
37type HTMLAnchorProps = DetailedHTMLProps <
48 AnchorHTMLAttributes < HTMLAnchorElement > ,
@@ -16,7 +20,30 @@ export type RegisteredLinkProps = RegisterLink extends {
1620
1721let 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
2148export 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