Skip to content

Commit 9f9ef21

Browse files
committed
fix(tooltip): review fixes
1 parent a921417 commit 9f9ef21

File tree

2 files changed

+44
-61
lines changed

2 files changed

+44
-61
lines changed

src/Tooltip.tsx

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
1-
import React, { forwardRef, memo, ReactElement } from "react";
1+
import React, { forwardRef, memo } from "react";
2+
import type { ReactNode, CSSProperties } from "react";
23
import type { Equals } from "tsafe";
34
import { assert } from "tsafe/assert";
45
import { symToStr } from "tsafe/symToStr";
56
import { useAnalyticsId } from "./tools/useAnalyticsId";
67
import { createComponentI18nApi } from "./i18n";
8+
import { fr } from "./fr";
9+
import { cx } from "./tools/cx";
710

811
export type TooltipProps = TooltipProps.WithClickAction | TooltipProps.WithHoverAction;
912

1013
export 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> */
2933
export 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
})

stories/Tooltip.stories.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from "react";
12
import { Tooltip, type TooltipProps } from "../dist/Tooltip";
23
import { sectionName } from "./sectionName";
34
import { getStoryFactory } from "./getStory";
@@ -30,28 +31,32 @@ const { meta, getStory } = getStoryFactory({
3031
})(),
3132
"description": "Optional."
3233
},
33-
"description": {
34+
"title": {
3435
"control": { "type": "text" }
3536
},
3637
"children": {
3738
"control": { "type": "text" }
3839
}
39-
},
40-
"disabledProps": ["lang"]
40+
}
4141
});
4242

4343
export default meta;
4444

4545
const defaultOnHoverProps: TooltipProps.WithHoverAction = {
46-
"description": "lorem ipsum",
47-
"children": "Exemple"
46+
"title": "lorem ipsum",
47+
"children": "Hover example"
4848
};
4949

5050
export const Default = getStory(defaultOnHoverProps);
5151

5252
export const TooltipOnHover = getStory(defaultOnHoverProps);
5353

54+
export const TooltipOnHoverLink = getStory({
55+
...defaultOnHoverProps,
56+
children: <a href="#">Some link</a>
57+
});
58+
5459
export const TooltipOnClick = getStory({
5560
"kind": "click",
56-
"description": "lorem ipsum"
61+
"title": "lorem ipsum"
5762
});

0 commit comments

Comments
 (0)