From 2c1c8028338a92e800c62e9e004e4d26c7888824 Mon Sep 17 00:00:00 2001 From: Lilian Saget-Lethias Date: Thu, 1 Jun 2023 02:59:55 +0200 Subject: [PATCH] feat: add onclick to connect buttons and refacto agentconnect --- src/AgentConnectButton.tsx | 58 ++++++++++--------------- src/Button.tsx | 2 +- src/FranceConnectButton.tsx | 37 +++++++++++----- src/MonCompteProButton.tsx | 36 +++++++++++---- src/assets/agentconnect.css | 26 ++++++++++- stories/AgentConnectButton.stories.tsx | 6 ++- stories/FranceConnectButton.stories.tsx | 6 ++- stories/MonComptePro.stories.tsx | 6 ++- 8 files changed, 116 insertions(+), 61 deletions(-) diff --git a/src/AgentConnectButton.tsx b/src/AgentConnectButton.tsx index 98decc20a..4489341b6 100644 --- a/src/AgentConnectButton.tsx +++ b/src/AgentConnectButton.tsx @@ -1,57 +1,45 @@ -"use client"; -import React, { forwardRef, memo, useState, type CSSProperties } from "react"; +import React, { forwardRef, memo, type CSSProperties } from "react"; import { symToStr } from "tsafe/symToStr"; import { createComponentI18nApi } from "./i18n"; import { fr } from "./fr"; import { assert, type Equals } from "tsafe/assert"; -import agentconnectBtnPrincipalSvgUrl from "./assets/agentconnect-btn-principal.svg"; -import agentconnectBtnPrincipalHoverSvgUrl from "./assets/agentconnect-btn-principal-hover.svg"; -import agentconnectBtnAlternatifSvgUrl from "./assets/agentconnect-btn-alternatif.svg"; -import agentconnectBtnAlternatifHoverSvgUrl from "./assets/agentconnect-btn-alternatif-hover.svg"; import "./assets/agentconnect.css"; -import { useIsDark } from "./useIsDark"; -import { getAssetUrl } from "./tools/getAssetUrl"; import { cx } from "./tools/cx"; -export type AgentConnectButtonProps = { - className?: string; - url: string; - style?: CSSProperties; -}; +export type AgentConnectButtonProps = AgentConnectButtonProps.Common & + (AgentConnectButtonProps.WithUrl | AgentConnectButtonProps.WithOnClick); + +export namespace AgentConnectButtonProps { + export type Common = { + className?: string; + style?: CSSProperties; + }; + export type WithUrl = { + url: string; + onClick?: never; + }; + export type WithOnClick = { + url?: never; + onClick: React.MouseEventHandler; + }; +} /** @see */ export const AgentConnectButton = memo( forwardRef((props, ref) => { - const { className, url, style, ...rest } = props; + const { className, url: href, style, onClick, ...rest } = props; assert>(); const { t } = useTranslation(); - const [isMouseHover, setIsMouseHover] = useState(false); - - const { isDark } = useIsDark(); + const Inner = onClick !== undefined ? "button" : "a"; + const innerProps = (onClick !== undefined ? { onClick } : { href }) as any; return (
- setIsMouseHover(true)} - onMouseLeave={() => setIsMouseHover(false)} - > - - + +

>; - style?: CSSProperties; -}; +export type FranceConnectButtonProps = FranceConnectButtonProps.Common & + (FranceConnectButtonProps.WithUrl | FranceConnectButtonProps.WithOnClick); + +export namespace FranceConnectButtonProps { + export type Common = { + className?: string; + /** Default: false */ + plus?: boolean; + classes?: Partial>; + style?: CSSProperties; + }; + export type WithUrl = { + url: string; + onClick?: never; + }; + export type WithOnClick = { + url?: never; + onClick: React.MouseEventHandler; + }; +} /** @see */ export const FranceConnectButton = memo( forwardRef((props, ref) => { - const { classes = {}, className, url, plus = false, style, ...rest } = props; + const { classes = {}, className, url: href, plus = false, style, onClick, ...rest } = props; assert>(); const { t } = useTranslation(); + const Inner = onClick !== undefined ? "button" : "a"; + const innerProps = (onClick !== undefined ? { onClick } : { href }) as any; + return (

- + S’identifier avec FranceConnect{plus ? "+" : ""} - +

>; - style?: CSSProperties; -}; +export type FranceConnectButtonProps = FranceConnectButtonProps.Common & + (FranceConnectButtonProps.WithUrl | FranceConnectButtonProps.WithOnClick); + +export namespace FranceConnectButtonProps { + export type Common = { + className?: string; + classes?: Partial>; + style?: CSSProperties; + }; + export type WithUrl = { + url: string; + onClick?: never; + }; + export type WithOnClick = { + url?: never; + onClick: React.MouseEventHandler; + }; +} /** @see */ export const MonCompteProButton = memo( forwardRef((props, ref) => { - const { classes = {}, className, url, style, ...rest } = props; + const { classes = {}, className, url: href, style, onClick, ...rest } = props; assert>(); const { t } = useTranslation(); + const Inner = onClick !== undefined ? "button" : "a"; + const innerProps = (onClick !== undefined ? { onClick } : { href }) as any; + return (