Skip to content

Commit 2bc3a2c

Browse files
committed
tooltip component
1 parent 0aca1db commit 2bc3a2c

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

src/components/molecules/Modal/Modal.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const meta: Meta<typeof Modal> = {
2020
</div>
2121
),
2222
],
23+
tags: ['autodocss']
2324
};
2425

2526
export default meta;

src/components/molecules/Modal/Modal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { forwardRef, ReactNode, CSSProperties } from "react";
22
import Card from "../Card/Card";
3+
import { IoClose } from "react-icons/io5";
34

45
export type ModalProps = {
56
isOpen: boolean;
@@ -38,7 +39,7 @@ const Modal = forwardRef<HTMLDivElement, ModalProps>(({
3839
onClick={onClose}
3940
className="absolute top-2 right-2 text-gray-700 hover:text-gray-900"
4041
>
41-
42+
<IoClose className="h-6 w-6" />
4243
</button>
4344
{children}
4445
</Card>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { Meta, StoryObj } from '@storybook/react';
2+
import Tooltip, { TooltipProps } from './Tooltip';
3+
import { Text } from '../../atoms/Text/Text';
4+
import Button from '../../atoms/Button/Button';
5+
6+
const meta: Meta<typeof Tooltip> = {
7+
title: 'Components/Molecules/Tooltip',
8+
component: Tooltip,
9+
parameters: {
10+
layout: 'centered',
11+
},
12+
argTypes: {
13+
position: {
14+
control: {
15+
type: 'select',
16+
options: ['top', 'right', 'bottom', 'left'],
17+
},
18+
},
19+
},
20+
decorators: [
21+
(Story) => (
22+
<div style={{ minHeight: '100vh', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
23+
<Story />
24+
</div>
25+
),
26+
],
27+
tags: ['autodocs'],
28+
};
29+
30+
export default meta;
31+
type TooltipStory = StoryObj<TooltipProps>;
32+
33+
export const Top: TooltipStory = (args: any) => (
34+
<Tooltip {...args} className='bg-green-500 text-white w-[200px] max-h-[200px]' content={
35+
<>
36+
<Text size="sm">This is some description text in the tooltip.</Text>
37+
</>
38+
}>
39+
<Button label={'Hover me'} className="bg-blue-500 hover:bg-blue-600 text-white mb-4" />
40+
</Tooltip>
41+
);
42+
43+
Top.args = {
44+
position: 'top',
45+
};
46+
47+
export const Right: TooltipStory = (args: any) => (
48+
<Tooltip {...args} content={
49+
<>
50+
<Text size="base" weight="bold" className="mb-2">Tooltip Title</Text>
51+
<Text size="sm">This is some description text in the tooltip.</Text>
52+
<Button variant="solid" size="sm" backgroundColor="#4CAF50" label="Click Me" className="mt-2" />
53+
</>
54+
}>
55+
<Button label={'Hover me'} className="bg-blue-500 hover:bg-blue-600 text-white mb-4" />
56+
</Tooltip>
57+
);
58+
59+
Right.args = {
60+
position: 'right',
61+
};
62+
63+
export const Bottom: TooltipStory = (args: any) => (
64+
<Tooltip {...args} content={
65+
<>
66+
<Text size="base" weight="bold" className="mb-2">Tooltip Title</Text>
67+
<Text size="sm">This is some description text in the tooltip.</Text>
68+
<Button variant="solid" size="sm" backgroundColor="#4CAF50" label="Click Me" className="mt-2" />
69+
</>
70+
}>
71+
<Button label={'Hover me'} className="bg-blue-500 hover:bg-blue-600 text-white mb-4" />
72+
</Tooltip>
73+
);
74+
75+
Bottom.args = {
76+
position: 'bottom',
77+
};
78+
79+
export const Left: TooltipStory = (args: any) => (
80+
<Tooltip {...args} content={
81+
<>
82+
<Text size="base" weight="bold" className="mb-2">Tooltip Title</Text>
83+
<Text size="sm">This is some description text in the tooltip.</Text>
84+
<Button variant="solid" size="sm" backgroundColor="#4CAF50" label="Click Me" className="mt-2" />
85+
</>
86+
}>
87+
<Button label={'Hover me'} className="bg-blue-500 hover:bg-blue-600 text-white mb-4" />
88+
</Tooltip>
89+
);
90+
91+
Left.args = {
92+
position: 'left',
93+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { ReactNode, useState } from "react";
2+
import { cn } from "../../../utils";
3+
4+
export type TooltipProps = {
5+
content: ReactNode;
6+
position?: 'top' | 'right' | 'bottom' | 'left';
7+
className?: string;
8+
children: ReactNode;
9+
};
10+
11+
const Tooltip = ({ content, position = 'top', className, children }: TooltipProps) => {
12+
const [isVisible, setIsVisible] = useState(false);
13+
14+
return (
15+
<div className="relative inline-block">
16+
<div
17+
onMouseEnter={() => setIsVisible(true)}
18+
onMouseLeave={() => setIsVisible(false)}
19+
className="cursor-pointer"
20+
>
21+
{children}
22+
</div>
23+
{isVisible && (
24+
<div
25+
className={cn(
26+
"absolute z-10 p-2 text-sm rounded shadow-lg transition-opacity duration-300",
27+
position === 'top' && 'bottom-full left-1/2 transform -translate-x-1/2 mb-2',
28+
position === 'right' && 'left-full top-1/2 transform -translate-y-1/2 ml-2',
29+
position === 'bottom' && 'top-full left-1/2 transform -translate-x-1/2 mt-2',
30+
position === 'left' && 'right-full top-1/2 transform -translate-y-1/2 mr-2',
31+
className
32+
)}
33+
>
34+
{content}
35+
</div>
36+
)}
37+
</div>
38+
);
39+
};
40+
41+
export default Tooltip;

0 commit comments

Comments
 (0)