From 4c3a407021395f6273de134b34b54b3ac3a145b5 Mon Sep 17 00:00:00 2001 From: celineung Date: Wed, 30 Aug 2023 11:09:17 +0200 Subject: [PATCH] fixup! add Tooltip component --- src/Tooltip.tsx | 90 +++++++++++++++++++------------------ stories/Tooltip.stories.tsx | 49 +++++++++----------- 2 files changed, 68 insertions(+), 71 deletions(-) diff --git a/src/Tooltip.tsx b/src/Tooltip.tsx index eef00329..811de4eb 100644 --- a/src/Tooltip.tsx +++ b/src/Tooltip.tsx @@ -5,73 +5,75 @@ import { useAnalyticsId } from "./tools/useAnalyticsId"; import { fr } from "./fr"; import { cx } from "./tools/cx"; -export type TooltipProps = { +export type UseTooltipParams = { id?: string; className?: string; trigger?: "click" | "hover"; - children: string; -}; +} -export const createTooltip = ( - props?: Omit -): { - id: string; +type UseTooltipReturn = { + wrapperProps: { + "aria-describedby": string; + className?: string; + }; TooltipComponent: ({ children }: { children: string }) => JSX.Element; -} => { + TooltipButtonComponent?: JSX.Element; +} + +export const useTooltip = ( + params?: UseTooltipParams +): UseTooltipReturn => { const tooltipId = useAnalyticsId({ "defaultIdPrefix": "fr-tooltip", - "explicitlyProvidedId": props?.id + "explicitlyProvidedId": params?.id }); return { - id: tooltipId, + wrapperProps: { + "aria-describedby": tooltipId, + ...(params?.trigger === "click" && {"className": cx(fr.cx("fr-btn--tooltip"), params?.className)}) + }, + TooltipButtonComponent: params?.trigger === "click" ? : undefined, TooltipComponent: ({ children }) => ( - + {children} ) }; }; +const TooltipButton = ({ tooltipId }: { tooltipId: string }) => { + const tooltipButtonId = `${tooltipId}-button`; + + return ( + + ) +} + +export type TooltipProps = { + id?: string; + className?: string; + children: string; +}; + /** @see */ const Tooltip = memo( forwardRef((props, ref) => { - const { id: id_prop, className, trigger, children, ...rest } = props; + const { id, className, children, ...rest } = props; assert>(); - const id = useAnalyticsId({ - "defaultIdPrefix": "fr-tooltip", - "explicitlyProvidedId": id_prop - }); - return ( - <> - {props.trigger === "click" ? ( - - - - - ) : ( - - )} - + ); }) ); diff --git a/stories/Tooltip.stories.tsx b/stories/Tooltip.stories.tsx index dd16a712..98ca8ce5 100644 --- a/stories/Tooltip.stories.tsx +++ b/stories/Tooltip.stories.tsx @@ -1,19 +1,24 @@ -import { createTooltip, type TooltipProps } from "../dist/Tooltip"; +import React from "react"; +import { useTooltip, type UseTooltipParams } from "../dist/Tooltip"; import { sectionName } from "./sectionName"; import { getStoryFactory } from "./getStory"; -import { assert, Equals } from "tsafe/assert"; -import React from "react"; -const Template = args => { - const tooltip = createTooltip(args); +const Template = (args: { + trigger?: "click" | "hover", + children: string, + className?: string; + id?: string; +}) => { + const tooltip = useTooltip(args); return ( <> {args.trigger != "click" && ( - + Example with hover )} + {args.trigger === "click" && tooltip.TooltipButtonComponent } ); @@ -31,17 +36,6 @@ const { meta, getStory } = getStoryFactory({ "description": "Optional : tootlip Id, which is also use as aria-describedby for hovered/clicked element" }, - "trigger": { - "control": { "type": "select" }, - "options": (() => { - const options = ["hover", "click"] as const; - - assert>(); - - return options; - })(), - "description": "Default: hover" - }, "className": { "control": { "type": "text" }, "description": "Optional" @@ -60,14 +54,14 @@ Default.parameters = { docs: { source: { code: ` -import { createTooltip } from "@codegouvfr/react-dsfr/Tooltip"; +import { useTooltip } from "@codegouvfr/react-dsfr/Tooltip"; function MyComponent() { - const tooltipOnHover = createTooltip() + const tooltipOnHover = useTooltip() return ( <> - + Example with hover Tooltip content @@ -88,14 +82,14 @@ TooltipOnHover.parameters = { docs: { source: { code: ` -import { createTooltip } from "@codegouvfr/react-dsfr/Tooltip"; +import { useTooltip } from "@codegouvfr/react-dsfr/Tooltip"; function MyComponent() { - const tooltipOnHover = createTooltip({ trigger: "hover"}) - + const tooltipOnHover = useTooltip({ trigger: "hover"}) + return ( <> - + Example with hover Tooltip content @@ -116,12 +110,13 @@ TooltipOnClick.parameters = { docs: { source: { code: ` -import { createTooltip } from "@codegouvfr/react-dsfr/Tooltip"; +import { useTooltip } from "@codegouvfr/react-dsfr/Tooltip"; function MyComponent() { - const tooltipOnClick = createTooltip({ trigger: "click"}) - + const tooltipOnClick = useTooltip({ trigger: "click"}) + return ( + {tooltipOnClick.TooltipButtonComponent} Tooltip content ) }