1
- import React , { forwardRef , memo , ReactElement } from "react" ;
1
+ import React , { forwardRef , memo } from "react" ;
2
+ import type { ReactNode , CSSProperties } from "react" ;
2
3
import type { Equals } from "tsafe" ;
3
4
import { assert } from "tsafe/assert" ;
4
5
import { symToStr } from "tsafe/symToStr" ;
5
6
import { useAnalyticsId } from "./tools/useAnalyticsId" ;
6
7
import { createComponentI18nApi } from "./i18n" ;
8
+ import { fr } from "./fr" ;
9
+ import { cx } from "./tools/cx" ;
7
10
8
11
export type TooltipProps = TooltipProps . WithClickAction | TooltipProps . WithHoverAction ;
9
12
10
13
export namespace TooltipProps {
11
14
export type Common = {
12
- description : string ;
15
+ title : ReactNode ;
13
16
id ?: string ;
14
17
className ?: string ;
18
+ style ?: CSSProperties ;
15
19
} ;
16
20
17
21
export type WithClickAction = Common & {
18
22
kind : "click" ;
19
- children ?: ReactElement | string ;
23
+ children ?: undefined ;
20
24
} ;
21
25
22
26
export type WithHoverAction = Common & {
23
27
kind ?: "hover" ;
24
- children : ReactElement | string ;
28
+ children : ReactNode ;
25
29
} ;
26
30
}
27
31
28
32
/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-tooltip> */
29
33
export const Tooltip = memo (
30
34
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 ;
32
36
assert < Equals < keyof typeof rest , never > > ( ) ;
33
37
34
38
const { t } = useTranslation ( ) ;
@@ -38,61 +42,35 @@ export const Tooltip = memo(
38
42
"explicitlyProvidedId" : id_prop
39
43
} ) ;
40
44
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
+ ) ;
58
57
59
58
return (
60
59
< >
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 }
94
71
</ span >
95
72
) }
73
+ < TooltipSpan />
96
74
</ >
97
75
) ;
98
76
} )
0 commit comments