|
| 1 | +import React, { forwardRef, memo, type CSSProperties } from "react"; |
| 2 | +import { symToStr } from "tsafe/symToStr"; |
| 3 | +import { createComponentI18nApi } from "./i18n"; |
| 4 | +import { fr } from "./fr"; |
| 5 | +import { assert, type Equals } from "tsafe/assert"; |
| 6 | +import { cx } from "./tools/cx"; |
| 7 | +import { useAnalyticsId } from "./tools/useAnalyticsId"; |
| 8 | +import "./assets/proconnect-btn.css"; |
| 9 | + |
| 10 | +export type ProConnectButtonProps = |
| 11 | + | ProConnectButtonProps.WithUrl |
| 12 | + | ProConnectButtonProps.WithOnClick; |
| 13 | + |
| 14 | +export namespace ProConnectButtonProps { |
| 15 | + type Common = { |
| 16 | + id?: string; |
| 17 | + className?: string; |
| 18 | + /** Default: false */ |
| 19 | + classes?: Partial<Record<"root" | "login" | "brand", string>>; |
| 20 | + style?: CSSProperties; |
| 21 | + }; |
| 22 | + export type WithUrl = Common & { |
| 23 | + url: string; |
| 24 | + onClick?: never; |
| 25 | + }; |
| 26 | + export type WithOnClick = Common & { |
| 27 | + url?: never; |
| 28 | + onClick: React.MouseEventHandler<HTMLButtonElement>; |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-proconnectbutton> */ |
| 33 | +export const ProConnectButton = memo( |
| 34 | + forwardRef<HTMLDivElement, ProConnectButtonProps>((props, ref) => { |
| 35 | + const { classes = {}, className, url: href, style, onClick, id: id_props, ...rest } = props; |
| 36 | + |
| 37 | + assert<Equals<keyof typeof rest, never>>(); |
| 38 | + |
| 39 | + const id = useAnalyticsId({ |
| 40 | + "defaultIdPrefix": "fr-proconnect-button", |
| 41 | + "explicitlyProvidedId": id_props |
| 42 | + }); |
| 43 | + |
| 44 | + const { t } = useTranslation(); |
| 45 | + |
| 46 | + const Inner = onClick !== undefined ? "button" : "a"; |
| 47 | + const innerProps = (onClick !== undefined ? { onClick } : { href }) as any; |
| 48 | + |
| 49 | + return ( |
| 50 | + <div |
| 51 | + id={id} |
| 52 | + className={cx(fr.cx("fr-connect-group"), classes.root, className)} |
| 53 | + style={style} |
| 54 | + ref={ref} |
| 55 | + > |
| 56 | + <Inner className={fr.cx("fr-btn", "fr-connect")} {...innerProps}> |
| 57 | + <span className={cx(fr.cx("fr-connect__login"), classes.login)}> |
| 58 | + S’identifier avec |
| 59 | + </span> |
| 60 | + <span className={cx(fr.cx("fr-connect__brand"), classes.brand)}> |
| 61 | + ProConnect |
| 62 | + </span> |
| 63 | + </Inner> |
| 64 | + <p> |
| 65 | + <a |
| 66 | + href={"https://proconnect.gouv.fr/"} |
| 67 | + target="_blank" |
| 68 | + rel="noopener" |
| 69 | + title={`${t("what is service")} - ${t("new window")}`} |
| 70 | + > |
| 71 | + {t("what is service")} |
| 72 | + </a> |
| 73 | + </p> |
| 74 | + </div> |
| 75 | + ); |
| 76 | + }) |
| 77 | +); |
| 78 | + |
| 79 | +ProConnectButton.displayName = symToStr({ ProConnectButton }); |
| 80 | + |
| 81 | +export default ProConnectButton; |
| 82 | + |
| 83 | +const { useTranslation, addProConnectButtonTranslations } = createComponentI18nApi({ |
| 84 | + "componentName": symToStr({ ProConnectButton }), |
| 85 | + "frMessages": { |
| 86 | + /* spell-checker: disable */ |
| 87 | + "what is service": "Qu’est-ce que ProConnect ?", |
| 88 | + "new window": "nouvelle fenêtre" |
| 89 | + /* spell-checker: enable */ |
| 90 | + } |
| 91 | +}); |
| 92 | + |
| 93 | +addProConnectButtonTranslations({ |
| 94 | + "lang": "en", |
| 95 | + "messages": { |
| 96 | + "what is service": "What's ProConnect?", |
| 97 | + "new window": "new window" |
| 98 | + } |
| 99 | +}); |
| 100 | + |
| 101 | +export { addProConnectButtonTranslations }; |
0 commit comments