Skip to content

Commit 6dba0a1

Browse files
feat(tooltip): ✨ add anchor prop to provide renderPropable element (#342)
1 parent 01edbbb commit 6dba0a1

11 files changed

+162
-8
lines changed

src/tooltip/Tooltip.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as React from "react";
22

3+
import { passProps } from "../utils";
4+
35
import { TooltipAnchor } from "./TooltipAnchor";
46
import { TooltipArrow } from "./TooltipArrow";
57
import { TooltipPrefix } from "./TooltipPrefix";
@@ -17,11 +19,17 @@ export const Tooltip = React.forwardRef<HTMLDivElement, TooltipProps>(
1719
prefixProps,
1820
suffixProps,
1921
} = useTooltipProps(props);
20-
const { prefix, content, suffix, withArrow } = uiProps;
22+
const { anchor, prefix, content, suffix, withArrow } = uiProps;
2123

2224
return (
2325
<>
24-
<TooltipAnchor {...anchorProps} />
26+
{anchor ? (
27+
<TooltipAnchor {...anchorProps}>
28+
{props => passProps(anchor, {}, props)}
29+
</TooltipAnchor>
30+
) : (
31+
<TooltipAnchor {...anchorProps} />
32+
)}
2533
<TooltipWrapper ref={ref} {...wrapperProps}>
2634
<>
2735
{prefix ? <TooltipPrefix {...prefixProps} /> : null}

src/tooltip/TooltipAnchor.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ import { createComponent } from "../utils";
1414
import { TooltipUIProps } from "./TooltipProps";
1515

1616
export const useTooltipAnchor = createHook<TooltipAnchorOptions>(
17-
({ state, content, withArrow, prefix, suffix, isDragging, ...props }) => {
17+
({
18+
state,
19+
anchor,
20+
content,
21+
withArrow,
22+
prefix,
23+
suffix,
24+
isDragging,
25+
...props
26+
}) => {
1827
if (!state) return props;
1928

2029
const { visible, hide, show } = state;

src/tooltip/TooltipArrow.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const useTooltipArrow = createHook<TooltipArrowOptions>(
3737
({
3838
state,
3939
size = defaultSize,
40+
anchor,
4041
content,
4142
withArrow,
4243
prefix,

src/tooltip/TooltipPrefix.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ import { createComponent, cx } from "../utils";
88
import { TooltipUIProps } from "./TooltipProps";
99

1010
export const useTooltipPrefix = createHook<TooltipPrefixOptions>(
11-
({ state, content, withArrow, prefix, suffix, isDragging, ...props }) => {
11+
({
12+
state,
13+
anchor,
14+
content,
15+
withArrow,
16+
prefix,
17+
suffix,
18+
isDragging,
19+
...props
20+
}) => {
1221
const theme = useTheme("tooltip");
1322
const className = cx(theme.prefix, props.className);
1423

src/tooltip/TooltipProps.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const componentMap = {
2424
};
2525

2626
export const useTooltipProps = ({
27+
anchor,
2728
content,
2829
prefix,
2930
suffix,
@@ -72,6 +73,7 @@ export const useTooltipProps = ({
7273
renderCallback,
7374
});
7475
const uiState = useTooltipUIState({
76+
anchor,
7577
content,
7678
prefix,
7779
suffix,

src/tooltip/TooltipSuffix.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ import { createComponent, cx } from "../utils";
88
import { TooltipUIProps } from "./TooltipProps";
99

1010
export const useTooltipSuffix = createHook<TooltipSuffixOptions>(
11-
({ state, content, withArrow, prefix, suffix, isDragging, ...props }) => {
11+
({
12+
state,
13+
anchor,
14+
content,
15+
withArrow,
16+
prefix,
17+
suffix,
18+
isDragging,
19+
...props
20+
}) => {
1221
const theme = useTheme("tooltip");
1322
const className = cx(theme.suffix, props.className);
1423

src/tooltip/TooltipUIState.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TooltipUIProps } from "./TooltipProps";
44

55
export function useTooltipUIState(props: TooltipUIStateProps): TooltipUIState {
66
const {
7+
anchor,
78
content,
89
prefix,
910
suffix,
@@ -12,6 +13,7 @@ export function useTooltipUIState(props: TooltipUIStateProps): TooltipUIState {
1213
} = props;
1314

1415
return {
16+
anchor,
1517
content,
1618
prefix,
1719
suffix,
@@ -21,6 +23,7 @@ export function useTooltipUIState(props: TooltipUIStateProps): TooltipUIState {
2123
}
2224

2325
export type TooltipUIState = {
26+
anchor: RenderProp<React.HTMLAttributes<any> & React.RefAttributes<any>>;
2427
/**
2528
* Label for the Tooltip.
2629
*/
@@ -50,6 +53,6 @@ export type TooltipUIState = {
5053
export type TooltipUIStateProps = Partial<
5154
Pick<
5255
TooltipUIState,
53-
"content" | "withArrow" | "prefix" | "suffix" | "isDragging"
56+
"anchor" | "content" | "withArrow" | "prefix" | "suffix" | "isDragging"
5457
>
5558
>;

src/tooltip/TooltipWrapper.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ import { createComponent, cx } from "../utils";
99
import { TooltipUIProps } from "./TooltipProps";
1010

1111
export const useTooltipWrapper = createHook<TooltipWrapperOptions>(
12-
({ state, content, withArrow, prefix, suffix, isDragging, ...props }) => {
12+
({
13+
state,
14+
anchor,
15+
content,
16+
withArrow,
17+
prefix,
18+
suffix,
19+
isDragging,
20+
...props
21+
}) => {
1322
if (!state) return props;
1423

1524
const theme = useTheme("tooltip");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as React from "react";
2+
3+
import { Button } from "../../button";
4+
import { Tooltip, TooltipProps } from "../../index";
5+
6+
export type TooltipAnchorProps = TooltipProps & {};
7+
8+
export const TooltipAnchor: React.FC<TooltipAnchorProps> = props => {
9+
return (
10+
<Tooltip
11+
anchor={<Button>Tooltip</Button>}
12+
content="Add your information"
13+
{...props}
14+
/>
15+
);
16+
};
17+
18+
export default TooltipAnchor;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { ComponentMeta, ComponentStoryObj } from "@storybook/react";
2+
3+
import { createControls, createPreviewTabs } from "../../../.storybook/utils";
4+
import { SlotIcon } from "../../icons";
5+
6+
import js from "./templates/TooltipAnchorJsx";
7+
import ts from "./templates/TooltipAnchorTsx";
8+
import TooltipAnchor from "./TooltipAnchor.component";
9+
10+
type Meta = ComponentMeta<typeof TooltipAnchor>;
11+
type Story = ComponentStoryObj<typeof TooltipAnchor>;
12+
13+
export default {
14+
title: "Popups/Tooltip/Anchor",
15+
component: TooltipAnchor,
16+
argTypes: createControls("tooltip", {
17+
ignore: [
18+
"wrapElement",
19+
"as",
20+
"ref",
21+
"prefix",
22+
"suffix",
23+
"content",
24+
"isDragging",
25+
"visible",
26+
"defaultVisible",
27+
"setVisible",
28+
"animated",
29+
"gutter",
30+
"timeout",
31+
"getAnchorRect",
32+
"fixed",
33+
"shift",
34+
"flip",
35+
"slide",
36+
"overlap",
37+
"sameWidth",
38+
"fitViewport",
39+
"arrowPadding",
40+
"overflowPadding",
41+
"renderCallback",
42+
"wrapperProps",
43+
"portalElement",
44+
"portalRef",
45+
"hideOnEscape",
46+
"hideOnControl",
47+
],
48+
}),
49+
parameters: {
50+
layout: "centered",
51+
options: { showPanel: true },
52+
preview: createPreviewTabs({ js, ts }),
53+
},
54+
} as Meta;
55+
56+
export const Bottom: Story = { args: { placement: "bottom" } };
57+
export const Right: Story = { args: { placement: "right" } };
58+
export const Left: Story = { args: { placement: "left" } };
59+
export const Top: Story = { args: { placement: "top" } };
60+
61+
export const ArrowBottom: Story = {
62+
args: { withArrow: true, placement: "bottom" },
63+
};
64+
export const ArrowRight: Story = {
65+
args: { withArrow: true, placement: "right" },
66+
};
67+
export const ArrowLeft: Story = {
68+
args: { withArrow: true, placement: "left" },
69+
};
70+
export const ArrowTop: Story = { args: { withArrow: true, placement: "top" } };
71+
72+
export const Prefix: Story = {
73+
args: { withArrow: true, placement: "top", prefix: <SlotIcon /> },
74+
};
75+
76+
export const Suffix: Story = {
77+
args: { withArrow: true, placement: "top", suffix: <SlotIcon /> },
78+
};
79+
export const PrefixSuffix: Story = {
80+
args: {
81+
withArrow: true,
82+
placement: "top",
83+
prefix: <SlotIcon />,
84+
suffix: <SlotIcon />,
85+
},
86+
};

0 commit comments

Comments
 (0)