1- import React , { forwardRef , memo , ReactElement } from "react" ;
1+ import React , { forwardRef , memo } from "react" ;
2+ import type { ReactNode , CSSProperties } from "react" ;
23import type { Equals } from "tsafe" ;
34import { assert } from "tsafe/assert" ;
45import { symToStr } from "tsafe/symToStr" ;
56import { useAnalyticsId } from "./tools/useAnalyticsId" ;
67import { createComponentI18nApi } from "./i18n" ;
8+ import { fr } from "./fr" ;
9+ import { cx } from "./tools/cx" ;
710
811export type TooltipProps = TooltipProps . WithClickAction | TooltipProps . WithHoverAction ;
912
1013export namespace TooltipProps {
1114 export type Common = {
12- description : string ;
15+ title : ReactNode ;
1316 id ?: string ;
1417 className ?: string ;
18+ style ?: CSSProperties ;
1519 } ;
1620
1721 export type WithClickAction = Common & {
1822 kind : "click" ;
19- children ?: ReactElement | string ;
23+ children ?: undefined ;
2024 } ;
2125
2226 export type WithHoverAction = Common & {
2327 kind ?: "hover" ;
24- children : ReactElement | string ;
28+ children : ReactNode ;
2529 } ;
2630}
2731
2832/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-tooltip> */
2933export const Tooltip = memo (
3034 forwardRef < HTMLSpanElement , TooltipProps > ( ( props , ref ) => {
31- const { id : id_prop , className, description , kind, children, ...rest } = props ;
35+ const { id : id_prop , className, title , kind, style , children, ...rest } = props ;
3236 assert < Equals < keyof typeof rest , never > > ( ) ;
3337
3438 const { t } = useTranslation ( ) ;
@@ -38,61 +42,35 @@ export const Tooltip = memo(
3842 "explicitlyProvidedId" : id_prop
3943 } ) ;
4044
41- const displayChildren = (
42- children : ReactElement | string | undefined ,
43- id : string
44- ) : ReactElement => {
45- if ( children === undefined ) return < > </ > ;
46- return typeof children === "string" ? (
47- < span aria-describedby = { id } id = { `tooltip-owner-${ id } ` } >
48- { children }
49- </ span >
50- ) : (
51- children &&
52- React . cloneElement ( children , {
53- "aria-describedby" : id ,
54- "id" : `tooltip-owner-${ id } `
55- } )
56- ) ;
57- } ;
45+ const TooltipSpan = ( ) => (
46+ < span
47+ className = { cx ( fr . cx ( "fr-tooltip" , "fr-placement" ) , className ) }
48+ id = { id }
49+ ref = { ref }
50+ style = { style }
51+ role = "tooltip"
52+ aria-hidden = "true"
53+ >
54+ { title }
55+ </ span >
56+ ) ;
5857
5958 return (
6059 < >
61- { props . kind === "click" ? (
62- < span ref = { ref } >
63- { children === undefined ? (
64- < button
65- className = "fr-btn--tooltip fr-btn"
66- aria-describedby = { id }
67- id = { `tooltip-owner-${ id } ` }
68- >
69- { t ( "tooltip-button-text" ) }
70- </ button >
71- ) : (
72- displayChildren ( children , id )
73- ) }
74- < span
75- className = { `fr-tooltip fr-placement ${ props . className } ` }
76- id = { id }
77- role = "tooltip"
78- aria-hidden = "true"
79- >
80- { props . description }
81- </ span >
82- </ span >
83- ) : (
84- < span ref = { ref } >
85- { displayChildren ( children , id ) }
86- < span
87- className = { `fr-tooltip fr-placement ${ props . className } ` }
88- id = { id }
89- role = "tooltip"
90- aria-hidden = "true"
91- >
92- { props . description }
93- </ span >
60+ { ( kind === "click" && (
61+ < button
62+ className = { fr . cx ( "fr-btn--tooltip" , "fr-btn" ) }
63+ aria-describedby = { id }
64+ id = { `tooltip-owner-${ id } ` }
65+ >
66+ { t ( "tooltip-button-text" ) }
67+ </ button >
68+ ) ) || (
69+ < span aria-describedby = { id } id = { `tooltip-owner-${ id } ` } >
70+ { children }
9471 </ span >
9572 ) }
73+ < TooltipSpan />
9674 </ >
9775 ) ;
9876 } )
0 commit comments