UNPKG

2.81 kBTypeScriptView Raw
1import React, { forwardRef, memo, type CSSProperties } from "react";
2import { symToStr } from "tsafe/symToStr";
3import { createComponentI18nApi } from "./i18n";
4import { fr } from "./fr";
5import { assert, type Equals } from "tsafe/assert";
6import "./assets/agentconnect.css";
7import { cx } from "./tools/cx";
8
9export type AgentConnectButtonProps =
10 | AgentConnectButtonProps.WithUrl
11 | AgentConnectButtonProps.WithOnClick;
12
13export namespace AgentConnectButtonProps {
14 type Common = {
15 className?: string;
16 id?: string;
17 style?: CSSProperties;
18 };
19 export type WithUrl = Common & {
20 url: string;
21 onClick?: never;
22 };
23 export type WithOnClick = Common & {
24 url?: never;
25 onClick: React.MouseEventHandler<HTMLButtonElement>;
26 };
27}
28
29/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-franceconnectbutton> */
30export const AgentConnectButton = memo(
31 forwardRef<HTMLDivElement, AgentConnectButtonProps>((props, ref) => {
32 const { className, url: href, style, onClick, id: id_props, ...rest } = props;
33
34 assert<Equals<keyof typeof rest, never>>();
35
36 const { t } = useTranslation();
37
38 const Inner = onClick !== undefined ? "button" : "a";
39
40 return (
41 <div
42 id={id_props ?? "fr-agentconnect-button"}
43 className={className}
44 style={style}
45 ref={ref}
46 >
47 <span className="agentconnect-button__preload-hover" />
48 <Inner
49 className="agentconnect-button__link"
50 {...((onClick !== undefined ? { onClick } : { href }) as any)}
51 />
52 <p>
53 <a
54 className={cx(
55 "agentconnect-button__hint",
56 fr.cx("fr-text--sm", "fr-mt-1v")
57 )}
58 href="https://agentconnect.gouv.fr/"
59 target="_blank"
60 >
61 {t("what is AgentConnect ?")}
62 </a>
63 </p>
64 </div>
65 );
66 })
67);
68
69AgentConnectButton.displayName = symToStr({ AgentConnectButton });
70
71export default AgentConnectButton;
72
73const { useTranslation, addAgentConnectButtonTranslations } = createComponentI18nApi({
74 "componentName": symToStr({ AgentConnectButton }),
75 "frMessages": {
76 /* spell-checker: disable */
77 "what is AgentConnect ?": "Qu’est-ce que AgentConnect ?"
78 /* spell-checker: enable */
79 }
80});
81
82addAgentConnectButtonTranslations({
83 "lang": "en",
84 "messages": {
85 "what is AgentConnect ?": "What's AgentConnect ?"
86 }
87});
88
89export { addAgentConnectButtonTranslations };
90
\No newline at end of file