Skip to content

Commit 2598c32

Browse files
committed
Add Tooltip component
**Changes:** * Adds a basic tooltip (single-direction)
1 parent d9a809c commit 2598c32

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

src/components/tooltip/Readme.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
### Purpose
2+
Give the user a tooltip that appears beneath an element being hovered over.
3+
4+
### Examples
5+
#### Standard
6+
```jsx
7+
<React.Fragment>
8+
<Tooltip message="Count of created orders in the last 30 days.">
9+
<h3>30 Orders</h3>
10+
</Tooltip>
11+
</React.Fragment>
12+
```
13+
14+
#### With custom props
15+
```jsx
16+
<React.Fragment>
17+
<Tooltip
18+
message="Count of created orders in the last 30 days."
19+
className={'example__tooltip__order-count'}
20+
>
21+
<h3>30 Orders</h3>
22+
</Tooltip>
23+
</React.Fragment>
24+
```

src/components/tooltip/Tooltip.css

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.stx-tooltip-wrapper {
2+
display: inline-flex;
3+
position: relative;
4+
flex-direction: column;
5+
justify-content: center;
6+
align-items: center;
7+
}
8+
9+
.stx-tooltip {
10+
position: absolute;
11+
z-index: 9999999;
12+
top: 100%;
13+
margin-top: 1rem;
14+
left: 50%;
15+
transform: translateX(-50%);
16+
padding: 10px;
17+
border-radius: 6px;
18+
background: rgba(0, 0, 0, 0.7);
19+
color: white;
20+
text-align: center;
21+
display: none;
22+
min-width: 120px;
23+
max-width: 160px;
24+
white-space: normal;
25+
font-size: 12px;
26+
line-height: 16px;
27+
}
28+
29+
.stx-tooltip::before {
30+
content: " ";
31+
left: 50%;
32+
bottom: 100%;
33+
transform: translateX(-50%);
34+
height: 0;
35+
width: 0;
36+
position: absolute;
37+
pointer-events: none;
38+
border: 8px solid transparent;
39+
border-bottom-color: rgba(0, 0, 0, 0.7);
40+
}
41+
42+
.stx-tooltip-wrapper:hover .stx-tooltip {
43+
display: inline-block;
44+
}

src/components/tooltip/Tooltip.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import cns from 'classnames';
4+
5+
import './Tooltip.css';
6+
7+
const Tooltip = ({ children, message, className, ...otherProps }) => (
8+
<div className={cns(className, 'stx-tooltip-wrapper')} {...otherProps}>
9+
{children}
10+
<div className='stx-tooltip'>
11+
{message}
12+
</div>
13+
</div>
14+
);
15+
16+
Tooltip.defaultProps = {
17+
className: null,
18+
};
19+
20+
Tooltip.propTypes = {
21+
message: PropTypes.string.isRequired,
22+
children: PropTypes.node.isRequired,
23+
className: PropTypes.string,
24+
};
25+
26+
export default Tooltip;
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* global describe, test, expect */
2+
import React from 'react';
3+
import { shallow } from 'enzyme';
4+
import toJson from 'enzyme-to-json';
5+
import Tooltip from './Tooltip';
6+
7+
describe('Tooltip', () => {
8+
test('Tooltip renders with available and extra props', () => {
9+
const component = shallow(
10+
<Tooltip
11+
className='custom-class-name'
12+
message='Count of created orders in the last 30 days.'
13+
tabIndex={0}
14+
>
15+
<h3>30 Orders</h3>
16+
</Tooltip>
17+
);
18+
19+
expect(toJson(component)).toMatchSnapshot();
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Tooltip Tooltip renders with available and extra props 1`] = `
4+
<div
5+
className="custom-class-name stx-tooltip-wrapper"
6+
tabIndex={0}
7+
>
8+
<h3>
9+
30 Orders
10+
</h3>
11+
<div
12+
className="stx-tooltip"
13+
>
14+
Count of created orders in the last 30 days.
15+
</div>
16+
</div>
17+
`;

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export { default as LoadingSpinner } from './components/loadingspinner/LoadingSp
2020
export { default as ProgressBar } from './components/progressbar/ProgressBar';
2121
export { default as Message } from './components/message/Message';
2222
export { default as Pill } from './components/pill/Pill';
23+
export { default as Tooltip } from './components/tooltip/Tooltip';
2324

2425
// Product
2526
export { default as Details } from './components/details/Details';

0 commit comments

Comments
 (0)