|
| 1 | +import React, { forwardRef, memo } from "react"; |
| 2 | +import type { ReactNode, CSSProperties } from "react"; |
| 3 | +import type { Equals } from "tsafe"; |
| 4 | +import { assert } from "tsafe/assert"; |
| 5 | +import { symToStr } from "tsafe/symToStr"; |
| 6 | +import { useAnalyticsId } from "./tools/useAnalyticsId"; |
| 7 | +import { createComponentI18nApi } from "./i18n"; |
| 8 | +import { fr } from "./fr"; |
| 9 | +import { cx } from "./tools/cx"; |
| 10 | + |
| 11 | +export type TooltipProps = TooltipProps.WithClickAction | TooltipProps.WithHoverAction; |
| 12 | + |
| 13 | +export namespace TooltipProps { |
| 14 | + export type Common = { |
| 15 | + title: ReactNode; |
| 16 | + id?: string; |
| 17 | + className?: string; |
| 18 | + style?: CSSProperties; |
| 19 | + }; |
| 20 | + |
| 21 | + export type WithClickAction = Common & { |
| 22 | + kind: "click"; |
| 23 | + children?: undefined; |
| 24 | + }; |
| 25 | + |
| 26 | + export type WithHoverAction = Common & { |
| 27 | + kind?: "hover"; |
| 28 | + children?: ReactNode; |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-tooltip> */ |
| 33 | +export const Tooltip = memo( |
| 34 | + forwardRef<HTMLSpanElement, TooltipProps>((props, ref) => { |
| 35 | + const { id: id_prop, className, title, kind, style, children, ...rest } = props; |
| 36 | + assert<Equals<keyof typeof rest, never>>(); |
| 37 | + |
| 38 | + const { t } = useTranslation(); |
| 39 | + |
| 40 | + const id = useAnalyticsId({ |
| 41 | + "defaultIdPrefix": "fr-tooltip", |
| 42 | + "explicitlyProvidedId": id_prop |
| 43 | + }); |
| 44 | + |
| 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 | + ); |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 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 | + ) : typeof children === "undefined" ? ( |
| 69 | + // mimic default tooltip style |
| 70 | + <i |
| 71 | + className={fr.cx("fr-icon--sm", "fr-icon-question-line")} |
| 72 | + style={{ color: fr.colors.decisions.text.actionHigh.blueFrance.default }} |
| 73 | + aria-describedby={id} |
| 74 | + id={`tooltip-owner-${id}`} |
| 75 | + ></i> |
| 76 | + ) : ( |
| 77 | + <span aria-describedby={id} id={`tooltip-owner-${id}`}> |
| 78 | + {children} |
| 79 | + </span> |
| 80 | + )} |
| 81 | + <TooltipSpan /> |
| 82 | + </> |
| 83 | + ); |
| 84 | + }) |
| 85 | +); |
| 86 | + |
| 87 | +Tooltip.displayName = symToStr({ Tooltip }); |
| 88 | + |
| 89 | +const { useTranslation, addTooltipTranslations } = createComponentI18nApi({ |
| 90 | + "componentName": symToStr({ Tooltip }), |
| 91 | + "frMessages": { |
| 92 | + "tooltip-button-text": "Information contextuelle" |
| 93 | + } |
| 94 | +}); |
| 95 | + |
| 96 | +addTooltipTranslations({ |
| 97 | + "lang": "en", |
| 98 | + "messages": { |
| 99 | + "tooltip-button-text": "Contextual information" |
| 100 | + } |
| 101 | +}); |
| 102 | + |
| 103 | +export { addTooltipTranslations }; |
| 104 | + |
| 105 | +export default Tooltip; |
0 commit comments