Skip to content

Commit 374c279

Browse files
feat: Tooltip component (#121)
* Added Tooltip Component * Revert "Added Tooltip Component" This reverts commit b2cd3ee. * Added tooltip * Update Loki * Loki * Changed proptype * Removed useless mixin * Reworked props, added arrowless variation * Memoize some internal values * Move Tooltip stories to Misc Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat>
1 parent fea05bc commit 374c279

12 files changed

+193
-0
lines changed
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import PropTypes from 'prop-types';
2+
import React, { useMemo } from 'react';
3+
4+
import { Box } from '../Box';
5+
6+
export function Tooltip({
7+
arrowPosition,
8+
...props
9+
}) {
10+
const [direction, position] = useMemo(() => {
11+
const [dir, pos] = arrowPosition ? arrowPosition.split('-') : [false, false];
12+
13+
if (!dir || dir === 'left' || dir === 'right') {
14+
return [String(dir), false];
15+
}
16+
17+
return [String(dir), pos ? String(pos) : 'center'];
18+
}, [arrowPosition]);
19+
20+
return <Box
21+
is='div'
22+
componentClassName='rcx-tooltip'
23+
mod-dir={direction}
24+
mod-pos={position}
25+
{...props}
26+
/>;
27+
}
28+
29+
Tooltip.propTypes = {
30+
position: PropTypes.oneOf(['up', 'up-left', 'up-right', 'down', 'down-left', 'down-right', 'left', 'right']),
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { action } from '@storybook/addon-actions';
2+
import { Meta, Preview, Props, Story } from '@storybook/addon-docs/blocks';
3+
4+
import { Box, Margins, Tooltip } from '../..';
5+
6+
<Meta title='Misc|Tooltips' parameters={{ jest: ['Misc/spec'] }} />
7+
8+
# Tooltip
9+
10+
A message which appears when a cursor is positioned over an icon, image, hyperlink, or other element in a graphical user
11+
interface.
12+
13+
<Preview>
14+
<Story name='Default'>
15+
<Tooltip>A example tooltip</Tooltip>
16+
</Story>
17+
</Preview>
18+
19+
<Props of={Tooltip} />
20+
21+
## Arrow positioning
22+
23+
<Preview>
24+
<Story name='Arrow positioning'>
25+
<Margins inline='neg-x8'>
26+
<Box>
27+
<Margins all='x8'>
28+
<Tooltip children='Tooltip' arrowPosition='up-left' />
29+
<Tooltip children='Tooltip' arrowPosition='up' />
30+
<Tooltip children='Tooltip' arrowPosition='up-right' />
31+
</Margins>
32+
</Box>
33+
<Box>
34+
<Margins all='x8'>
35+
<Tooltip children='Tooltip' arrowPosition='left' />
36+
<Tooltip children='Tooltip' arrowPosition={null} />
37+
<Tooltip children='Tooltip' arrowPosition='right' />
38+
</Margins>
39+
</Box>
40+
<Box>
41+
<Margins all='x8'>
42+
<Tooltip children='Tooltip' arrowPosition='down-left' />
43+
<Tooltip children='Tooltip' arrowPosition='down' />
44+
<Tooltip children='Tooltip' arrowPosition='down-right' />
45+
</Margins>
46+
</Box>
47+
</Margins>
48+
</Story>
49+
</Preview>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
@mixin triangle-direction ($direction) {
2+
&::after {
3+
position: absolute;
4+
5+
box-sizing: border-box;
6+
7+
content: ' ';
8+
9+
border-width: calc(#{ $borders-width-x4 } + #{ $borders-width-x1 });
10+
border-color: transparent transparent $tooltip-background-color $tooltip-background-color;
11+
border-radius: 0 0 0 calc(#{ $borders-radius-x2 } + #{ $borders-radius-x2} / 2);
12+
13+
@if $direction == 'up' {
14+
top: calc(-1 * (#{$spaces-x4} ));
15+
16+
transform: rotate(135deg);
17+
}
18+
@if $direction == 'down' {
19+
bottom: calc(-1 * (#{$spaces-x4} ));
20+
21+
transform: rotate(-45deg);
22+
}
23+
@if $direction == 'left' {
24+
top: $tooltip-arrow-position-center;
25+
left: calc(-1 * (#{$spaces-x4} ));
26+
27+
margin-block-start: calc(-1 * #{ $spaces-x4 });
28+
29+
transform: rotate(45deg);
30+
}
31+
@if $direction == 'right' {
32+
top: $tooltip-arrow-position-center;
33+
right: calc(-1 * (#{$spaces-x4} ));
34+
35+
margin-block-start: calc(-1 * #{ $spaces-x4 });
36+
37+
transform: rotate(-135deg);
38+
}
39+
}
40+
}
41+
42+
.rcx-tooltip {
43+
position: relative;
44+
45+
display: inline-block;
46+
47+
max-width: $spaces-x240;
48+
49+
padding-block: $spaces-x8;
50+
padding-inline: $spaces-x12;
51+
52+
word-break: break-all;
53+
54+
color: $tooltip-text-color;
55+
56+
border-radius: $borders-radius-x4;
57+
58+
background-color: $tooltip-background-color;
59+
60+
@include use-text-style(p2);
61+
62+
&--dir-up {
63+
@include triangle-direction('up');
64+
}
65+
66+
&--dir-down {
67+
@include triangle-direction('down');
68+
}
69+
70+
&--dir-left {
71+
@include triangle-direction('left');
72+
}
73+
74+
&--dir-right {
75+
@include triangle-direction('right');
76+
}
77+
78+
&--pos {
79+
&-center {
80+
&::after {
81+
left: 50%;
82+
83+
margin-inline-start: calc(-1 * #{ $spaces-x4 });
84+
}
85+
}
86+
87+
&-left {
88+
&::after {
89+
left: $tooltip-arrow-margin;
90+
91+
margin: 0;
92+
}
93+
}
94+
95+
&-right {
96+
&::after {
97+
right: $tooltip-arrow-margin;
98+
left: initial;
99+
100+
margin: 0;
101+
}
102+
}
103+
}
104+
}

packages/fuselage/src/components/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ export * from './Chip';
3636
export * from './AutoComplete';
3737
export * from './Options';
3838
export * from './Select';
39+
export * from './Tooltip';
3940
export * from './Modal';
4041
export * from './Throbber';

packages/fuselage/src/components/index.scss

+1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
@import './AutoComplete/styles.scss';
2929
@import './Options/styles.scss';
3030
@import './Select/styles.scss';
31+
@import './Tooltip/styles.scss';
3132
@import './Modal/styles.scss';
3233
@import './Throbber/styles.scss';

packages/fuselage/src/styles/variables/all.scss

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
@import './text-colors.scss';
88
@import './text-styles.scss';
99
@import './transitions.scss';
10+
@import './tooltip.scss';
1011
@import './tabs-colors.scss';

packages/fuselage/src/styles/variables/borders.scss

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ $borders-width-x4: theme('borders-width-x4', to-rem(4));
55

66
$borders-radius-none: 0;
77
$borders-radius-x2: theme('borders-radius-x2', to-rem(2));
8+
$borders-radius-x4: theme('borders-radius-x4', to-rem(4));
89
$borders-radius-full: 9999px;
910

1011
$borders: (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$tooltip-arrow-margin: $spaces-x8;
2+
$tooltip-arrow-position-center: 50%;
3+
4+
$tooltip-background-color: theme('tooltip-background-color', $colors-n900);
5+
$tooltip-text-color: theme('tooltip-text-color', $colors-white);

0 commit comments

Comments
 (0)