|
1 | | -"use client"; |
2 | | -import React, { forwardRef, memo, useState, type CSSProperties } from "react"; |
| 1 | +import React, { forwardRef, memo, type CSSProperties } from "react"; |
3 | 2 | import { symToStr } from "tsafe/symToStr"; |
4 | 3 | import { createComponentI18nApi } from "./i18n"; |
5 | 4 | import { fr } from "./fr"; |
6 | 5 | import { assert, type Equals } from "tsafe/assert"; |
7 | | -import agentconnectBtnPrincipalSvgUrl from "./assets/agentconnect-btn-principal.svg"; |
8 | | -import agentconnectBtnPrincipalHoverSvgUrl from "./assets/agentconnect-btn-principal-hover.svg"; |
9 | | -import agentconnectBtnAlternatifSvgUrl from "./assets/agentconnect-btn-alternatif.svg"; |
10 | | -import agentconnectBtnAlternatifHoverSvgUrl from "./assets/agentconnect-btn-alternatif-hover.svg"; |
11 | 6 | import "./assets/agentconnect.css"; |
12 | | -import { useIsDark } from "./useIsDark"; |
13 | | -import { getAssetUrl } from "./tools/getAssetUrl"; |
14 | 7 | import { cx } from "./tools/cx"; |
15 | 8 |
|
16 | | -export type AgentConnectButtonProps = { |
17 | | - className?: string; |
18 | | - url: string; |
19 | | - style?: CSSProperties; |
20 | | -}; |
| 9 | +export type AgentConnectButtonProps = AgentConnectButtonProps.Common & |
| 10 | + (AgentConnectButtonProps.WithUrl | AgentConnectButtonProps.WithOnClick); |
| 11 | + |
| 12 | +export namespace AgentConnectButtonProps { |
| 13 | + export type Common = { |
| 14 | + className?: string; |
| 15 | + style?: CSSProperties; |
| 16 | + }; |
| 17 | + export type WithUrl = { |
| 18 | + url: string; |
| 19 | + onClick?: never; |
| 20 | + }; |
| 21 | + export type WithOnClick = { |
| 22 | + url?: never; |
| 23 | + onClick: React.MouseEventHandler<HTMLButtonElement>; |
| 24 | + }; |
| 25 | +} |
21 | 26 |
|
22 | 27 | /** @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-franceconnectbutton> */ |
23 | 28 | export const AgentConnectButton = memo( |
24 | 29 | forwardRef<HTMLDivElement, AgentConnectButtonProps>((props, ref) => { |
25 | | - const { className, url, style, ...rest } = props; |
| 30 | + const { className, url: href, style, onClick, ...rest } = props; |
26 | 31 |
|
27 | 32 | assert<Equals<keyof typeof rest, never>>(); |
28 | 33 |
|
29 | 34 | const { t } = useTranslation(); |
30 | 35 |
|
31 | | - const [isMouseHover, setIsMouseHover] = useState(false); |
32 | | - |
33 | | - const { isDark } = useIsDark(); |
| 36 | + const Inner = onClick !== undefined ? "button" : "a"; |
| 37 | + const innerProps = (onClick !== undefined ? { onClick } : { href }) as any; |
34 | 38 |
|
35 | 39 | return ( |
36 | 40 | <div className={className} style={style} ref={ref}> |
37 | | - <a |
38 | | - className="agentconnect-button__link" |
39 | | - href={url} |
40 | | - onMouseEnter={() => setIsMouseHover(true)} |
41 | | - onMouseLeave={() => setIsMouseHover(false)} |
42 | | - > |
43 | | - <img |
44 | | - src={getAssetUrl( |
45 | | - isDark |
46 | | - ? isMouseHover |
47 | | - ? agentconnectBtnAlternatifHoverSvgUrl |
48 | | - : agentconnectBtnAlternatifSvgUrl |
49 | | - : isMouseHover |
50 | | - ? agentconnectBtnPrincipalHoverSvgUrl |
51 | | - : agentconnectBtnPrincipalSvgUrl |
52 | | - )} |
53 | | - /> |
54 | | - </a> |
| 41 | + <span className="agentconnect-button__preload-hover" /> |
| 42 | + <Inner className="agentconnect-button__link" {...innerProps} /> |
55 | 43 | <p> |
56 | 44 | <a |
57 | 45 | className={cx( |
|
0 commit comments