|
| 1 | +import React, { forwardRef, memo, ReactElement } from "react"; |
| 2 | +import type { Equals } from "tsafe"; |
| 3 | +import { assert } from "tsafe/assert"; |
| 4 | +import { symToStr } from "tsafe/symToStr"; |
| 5 | +import { useAnalyticsId } from "./tools/useAnalyticsId"; |
| 6 | + |
| 7 | +export type TooltipProps = TooltipProps.WithClickAction | TooltipProps.WithHoverAction; |
| 8 | + |
| 9 | +export namespace TooltipProps { |
| 10 | + export type Common = { |
| 11 | + description: string; |
| 12 | + id?: string; |
| 13 | + className?: string; |
| 14 | + }; |
| 15 | + |
| 16 | + export type WithClickAction = Common & { |
| 17 | + kind: "click"; |
| 18 | + children?: ReactElement | string; |
| 19 | + }; |
| 20 | + |
| 21 | + export type WithHoverAction = Common & { |
| 22 | + kind?: "hover"; |
| 23 | + children: ReactElement | string; |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-tooltip> */ |
| 28 | +export const Tooltip = memo( |
| 29 | + forwardRef<HTMLSpanElement, TooltipProps>((props, ref) => { |
| 30 | + const { id: id_prop, className, description, kind, children, ...rest } = props; |
| 31 | + assert<Equals<keyof typeof rest, never>>(); |
| 32 | + |
| 33 | + const id = useAnalyticsId({ |
| 34 | + "defaultIdPrefix": "fr-tooltip", |
| 35 | + "explicitlyProvidedId": id_prop |
| 36 | + }); |
| 37 | + |
| 38 | + const displayChildren = ( |
| 39 | + children: ReactElement | string | undefined, |
| 40 | + id: string |
| 41 | + ): ReactElement => { |
| 42 | + if (children === undefined) return <></>; |
| 43 | + return typeof children === "string" ? ( |
| 44 | + <span aria-describedby={id} id={`tooltip-owner-${id}`}> |
| 45 | + {children} |
| 46 | + </span> |
| 47 | + ) : ( |
| 48 | + children && |
| 49 | + React.cloneElement(children, { |
| 50 | + "aria-describedby": id, |
| 51 | + "id": `tooltip-owner-${id}` |
| 52 | + }) |
| 53 | + ); |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + <> |
| 58 | + {props.kind === "click" ? ( |
| 59 | + <span ref={ref}> |
| 60 | + {children === undefined ? ( |
| 61 | + <button |
| 62 | + className="fr-btn--tooltip fr-btn" |
| 63 | + aria-describedby={id} |
| 64 | + id={`tooltip-owner-${id}`} |
| 65 | + > |
| 66 | + Information contextuelle |
| 67 | + </button> |
| 68 | + ) : ( |
| 69 | + displayChildren(children, id) |
| 70 | + )} |
| 71 | + <span |
| 72 | + className={`fr-tooltip fr-placement ${props.className}`} |
| 73 | + id={id} |
| 74 | + role="tooltip" |
| 75 | + aria-hidden="true" |
| 76 | + > |
| 77 | + {props.description} |
| 78 | + </span> |
| 79 | + </span> |
| 80 | + ) : ( |
| 81 | + <span ref={ref}> |
| 82 | + {displayChildren(children, id)} |
| 83 | + <span |
| 84 | + className={`fr-tooltip fr-placement ${props.className}`} |
| 85 | + id={id} |
| 86 | + role="tooltip" |
| 87 | + aria-hidden="true" |
| 88 | + > |
| 89 | + {props.description} |
| 90 | + </span> |
| 91 | + </span> |
| 92 | + )} |
| 93 | + </> |
| 94 | + ); |
| 95 | + }) |
| 96 | +); |
| 97 | + |
| 98 | +Tooltip.displayName = symToStr({ Tooltip }); |
| 99 | + |
| 100 | +export default Tooltip; |
0 commit comments