1
1
import { Component } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
- import styled from 'styled-components' ;
4
- import placementConfig from './options' ;
5
- import {
6
- colors ,
7
- spacing ,
8
- baseFontSize as defaultBaseFontSize ,
9
- } from '../shared/theme' ;
10
-
11
- const Tip = styled . div `
12
- border-radius: 4px;
13
- font-weight: bold;
14
- opacity: ${ ( { visible } ) => ( visible ? '1' : '0' ) } ;
15
- visibility: ${ ( { visible } ) => ( visible ? 'visible' : 'hidden' ) } ;
16
- position: absolute;
17
- line-height: 0;
18
- text-align: center;
19
- transition:
20
- opacity 0.2s ease-in-out,
21
- visibility 0.2s ease-in-out;
22
- z-index: 100;
23
-
24
- ${ ( {
25
- theme : {
26
- colors : { neutral } ,
27
- spacing : { xsmall } ,
28
- baseFontSize,
29
- } ,
30
- } ) => `
31
- background-color: ${ neutral [ 700 ] } ;
32
- border-color: ${ neutral [ 700 ] } ;
33
- color: ${ neutral [ 100 ] } ;
34
- font-size: ${ baseFontSize } px;
35
- padding: ${ xsmall } px;
36
- ` }
37
-
38
- ${ ( { placement } ) => placementConfig . tipPosition [ placement ] } ;
39
-
40
- &:before {
41
- content: '';
42
- position: absolute;
43
- ${ ( { placement } ) => placementConfig . arrowPosition [ placement ] } ;
44
- }
45
- ` ;
46
-
47
- const TipText = styled . span `
48
- display: inline-block;
49
- max-width: ${ ( { multiline } ) => ( multiline ? 'unset' : '250px' ) } ;
50
- overflow: hidden;
51
- text-overflow: ellipsis;
52
- white-space: ${ ( { multiline } ) => ( multiline ? 'pre' : 'nowrap' ) } ;
53
- line-height: 20px;
54
- text-align: ${ ( { multiline } ) => ( multiline ? 'left' : 'unset' ) } ;
55
- ` ;
56
-
57
- const Wrapper = styled . div `
58
- position: relative;
59
- float: left;
60
- clear: left;
61
- width: ${ ( { multiline } ) => ( multiline ? 'max-content' : 'unset' ) } ;
62
- ` ;
3
+ import classNames from 'classnames' ;
4
+ import styles from './Tooltip.module.css' ;
63
5
64
6
class Tooltip extends Component {
65
7
constructor ( props ) {
@@ -73,37 +15,43 @@ class Tooltip extends Component {
73
15
render ( ) {
74
16
const {
75
17
children,
18
+ className,
76
19
placement,
77
20
text,
78
21
visible : visibleProp ,
79
- theme,
80
22
multiline,
81
23
...rest
82
24
} = this . props ;
83
25
const { visible : visibleState } = this . state ;
84
26
27
+ const wrapperClass = classNames (
28
+ styles [ 'tooltip-wrapper' ] ,
29
+ { [ styles [ 'tooltip-wrapper-multiline' ] ] : multiline } ,
30
+ className ,
31
+ ) ;
32
+ const tipClass = classNames ( styles . tip , styles [ `tip-${ placement } ` ] , {
33
+ [ styles [ 'tip-visible' ] ] : visibleProp || visibleState ,
34
+ } ) ;
35
+ const tipTextClass = classNames ( styles [ 'tip-text' ] , {
36
+ [ styles [ 'tip-text-multiline' ] ] : multiline ,
37
+ } ) ;
38
+
85
39
return (
86
- < Wrapper
40
+ < div
87
41
onMouseEnter = { ( ) => this . isVisible ( true ) }
88
42
onMouseLeave = { ( ) => this . isVisible ( false ) }
89
- multiline = { multiline }
43
+ className = { wrapperClass }
90
44
{ ...rest }
91
45
>
92
- < Tip
93
- placement = { placement }
94
- visible = { visibleProp || visibleState }
95
- theme = { theme }
96
- >
97
- < TipText multiline = { multiline } > { text } </ TipText >
98
- </ Tip >
46
+ < div className = { tipClass } >
47
+ < span className = { tipTextClass } > { text } </ span >
48
+ </ div >
99
49
{ children }
100
- </ Wrapper >
50
+ </ div >
101
51
) ;
102
52
}
103
53
}
104
54
105
- Tip . displayName = 'Tip' ;
106
-
107
55
Tooltip . propTypes = {
108
56
/** Content the tooltip will show */
109
57
text : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . element ] ) . isRequired ,
@@ -115,22 +63,12 @@ Tooltip.propTypes = {
115
63
PropTypes . arrayOf ( PropTypes . node ) ,
116
64
PropTypes . node ,
117
65
] ) . isRequired ,
118
- theme : PropTypes . shape ( {
119
- spacing : PropTypes . object ,
120
- colors : PropTypes . object ,
121
- baseFontSize : PropTypes . number ,
122
- } ) ,
123
66
} ;
124
67
125
68
Tooltip . defaultProps = {
126
69
placement : 'top' ,
127
70
visible : false ,
128
71
multiline : false ,
129
- theme : {
130
- spacing,
131
- colors,
132
- baseFontSize : defaultBaseFontSize ,
133
- } ,
134
72
} ;
135
73
136
74
export default Tooltip ;
0 commit comments