Skip to content

Commit 096334a

Browse files
committed
Added missing files
1 parent 86dca51 commit 096334a

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

src/Tooltip/Tooltip.stories.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ComponentMeta, ComponentStory } from "@storybook/react";
2+
import TooltipComponent from ".";
3+
import { TooltipProps } from "./types";
4+
5+
export default {
6+
title: "User Interface/Tooltip",
7+
component: TooltipComponent,
8+
} as ComponentMeta<typeof TooltipComponent>;
9+
10+
export const Tooltip: any = (args: TooltipProps<"span">) => {
11+
return <TooltipComponent {...args} />;
12+
};
13+
14+
Tooltip.args = {
15+
children: "Tooltip",
16+
title: "Title text",
17+
};
18+
19+
export const TwoLines: any = (args: TooltipProps<"span">) => {
20+
return <TooltipComponent {...args} />;
21+
};
22+
23+
TwoLines.args = {
24+
children: "Tooltip",
25+
title: (
26+
<div>
27+
<span>Lorem ipsum dolor sit amet, consectetur.</span> <span>Ut ornare nunc tortor.</span>
28+
</div>
29+
),
30+
};
31+
32+
export const LongText: any = (args: TooltipProps<"span">) => {
33+
return <TooltipComponent {...args} />;
34+
};
35+
36+
LongText.args = {
37+
children: "Tooltip",
38+
title:
39+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ornare nunc tortor, id eleifend metus elementum in. Cras eget dui at ligula bibendum convallis et id est. Donec consequat mauris quis orci hendrerit interdum. Cras nec varius orci. Nunc mollis quam ac varius vulputate. Morbi cursus consectetur nulla, eget varius augue lacinia at. Mauris varius fermentum mauris ut dictum. In iaculis efficitur congue. Quisque enim diam, aliquam vitae nibh ut, euismod luctus est. Aenean rutrum est pulvinar ligula dapibus lacinia. Sed et lacus augue. Curabitur non ultrices mi.",
40+
};

src/Tooltip/index.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import classes from "../utils/classes";
2+
import getStringLengthOfChildren from "../utils/getStringLengthOfChildren";
3+
import { AsMap, TooltipProps } from "./types";
4+
import style from "./style/Tooltip.module.scss";
5+
import Icon from "../Icon";
6+
7+
export default function Tooltip<T extends keyof AsMap>({ as, className, title, children, ...props }: TooltipProps<T>) {
8+
const Tag: any = as || "span";
9+
10+
console.log(title);
11+
12+
const length = getStringLengthOfChildren(title);
13+
14+
console.log(length);
15+
const wrapperClasses = classes([style.tooltip, className, length > 30 && style.multiline]);
16+
17+
return (
18+
<Tag {...props} className={wrapperClasses}>
19+
{children}
20+
<span className={style.icon}>
21+
<Icon icon="info" />
22+
<span className={style.message}>{title}</span>
23+
</span>
24+
</Tag>
25+
);
26+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
$side: var(--m-xxxs);
2+
$height: calc($side * 1.732);
3+
4+
.tooltip {
5+
display: inline-flex;
6+
align-items: center;
7+
gap: var(--m-xs);
8+
9+
.icon {
10+
position: relative;
11+
12+
svg {
13+
display: block;
14+
cursor: pointer;
15+
}
16+
17+
&:hover,
18+
&:active {
19+
.message {
20+
display: block;
21+
}
22+
}
23+
}
24+
25+
.message {
26+
position: absolute;
27+
top: 100%;
28+
left: 50%;
29+
transform: translateX(-50%);
30+
background-color: var(--color-background-modal);
31+
z-index: 2;
32+
padding: var(--m-xxs) var(--m-xs);
33+
border-radius: var(--border-radius);
34+
display: none;
35+
margin-top: var(--m-xs);
36+
box-shadow: 0 0 0 1px var(--color-border), 0 5px 15px rgba(0, 0, 0, 0.2);
37+
white-space: nowrap;
38+
39+
&::after,
40+
&::before {
41+
position: absolute;
42+
top: calc($height * -1);
43+
left: 50%;
44+
border-left: $side solid transparent;
45+
border-right: $side solid transparent;
46+
border-bottom: $height solid var(--color-border);
47+
content: "";
48+
font-size: 0;
49+
line-height: 0;
50+
width: 0;
51+
transform: translateX(-50%);
52+
}
53+
54+
&::after {
55+
top: calc($height * -1 + 2px);
56+
border-bottom: $height solid var(--color-background-modal);
57+
}
58+
}
59+
60+
&.multiline .message {
61+
width: 260px;
62+
white-space: normal;
63+
}
64+
}

src/Tooltip/types/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ReactNode } from "react";
2+
3+
export type AsMap = {
4+
div: React.HTMLProps<HTMLDivElement>;
5+
span: React.HTMLProps<HTMLSpanElement>;
6+
p: React.HTMLProps<HTMLParagraphElement>;
7+
};
8+
9+
export type TooltipProps<T extends keyof AsMap = "span"> = {
10+
as?: T;
11+
title?: string | ReactNode;
12+
} & AsMap[T];

src/assets/icons/info.svg

Lines changed: 5 additions & 0 deletions
Loading
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { isValidElement } from "react";
2+
3+
export default function getStringLengthOfChildren(children: React.ReactNode): number {
4+
if (typeof children === "string") return children.length;
5+
6+
if (Array.isArray(children)) return children.reduce((sum, child) => sum + getStringLengthOfChildren(child), 0);
7+
8+
// If child is a React element, process its children recursively
9+
if (isValidElement(children)) return getStringLengthOfChildren(children.props.children);
10+
11+
// If none of the above, return 0
12+
return 0;
13+
}

0 commit comments

Comments
 (0)