@@ -6,8 +6,12 @@ sidebar_position: 1
6
6
7
7
How to customize tooltip styles in ReactTooltip styles.
8
8
9
+ Now tooltip arrow inherit his background color from tooltip (his parent).
10
+
9
11
import { Tooltip } from ' react-tooltip'
10
12
import ' react-tooltip/dist/react-tooltip.css'
13
+ import CodeBlock from ' @theme/CodeBlock'
14
+ import TooltipStyles from ' !!raw-loader!../../../src/components/Tooltip/styles.module.css'
11
15
12
16
export const TooltipAnchor = ({ children , id , ... rest }) => {
13
17
return (
@@ -33,31 +37,55 @@ export const TooltipAnchor = ({ children, id, ...rest }) => {
33
37
)
34
38
}
35
39
36
- ### Basic explanation
40
+ ### Basic explanation - Inline Styling
41
+
42
+ You can add styles into the tooltip as inline styling.
43
+
44
+ ``` jsx
45
+ import { Tooltip } from ' react-tooltip'
46
+ import ' react-tooltip/dist/react-tooltip.css'
47
+
48
+ < a id= " custom-inline-styles" data- tooltip- content= " hello world!" >
49
+ ◕‿‿◕
50
+ < / a>
51
+ < Tooltip
52
+ anchorId= " custom-inline-styles"
53
+ styles= {{ backgroundColor: " rgb(0, 255, 30)" , color: " #222" }}
54
+ / >
55
+ ```
56
+
57
+ <div
58
+ style = { { display: ' flex' , columnGap: ' 16px' , justifyContent: ' center' }}
59
+ >
60
+ <TooltipAnchor id = " custom-inline-styles" data-tooltip-content = " hello world!" >
61
+ ◕‿‿◕
62
+ </TooltipAnchor >
63
+ <Tooltip anchorId = " custom-inline-styles" styles = { { backgroundColor: " rgb(0, 255, 30)" , color: " #222" }} />
64
+ </div >
65
+
66
+
67
+ ### Basic explanation - Classes
37
68
38
69
You don't need ` !important ` anymore, you just need to know at least a bit about CSS Specificity ([ MDN Docs] ( https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity ) | [ W3c Docs] ( https://www.w3schools.com/css/css_specificity.asp ) ).
39
70
Using CSS Specificity you can add a class with more specificity than the current used in tooltip and you can override or add new rules to the component style.
40
71
72
+
41
73
``` jsx
42
74
import { Tooltip } from ' react-tooltip'
43
75
import ' react-tooltip/dist/react-tooltip.css'
44
76
45
77
< style>
46
- .example - container . example {
78
+ .example {
47
79
color: #222 ;
48
80
background- color: rgb (0 , 247 , 255 );
49
81
}
50
-
51
- .example - container .example .example - arrow {
52
- background- color: rgb (0 , 247 , 255 );
53
- }
54
82
< / style>
55
83
56
84
< div className= " example-container" >
57
85
< a id= " custom-styles" data- tooltip- content= " hello world!" >
58
86
◕‿‿◕
59
87
< / a>
60
- < Tooltip anchorId= " custom-styles" className= " example" classNameArrow = " example-arrow " / >
88
+ < Tooltip anchorId= " custom-styles" className= " example" / >
61
89
< / div>
62
90
```
63
91
@@ -68,74 +96,14 @@ import 'react-tooltip/dist/react-tooltip.css'
68
96
<TooltipAnchor id = " custom-styles" data-tooltip-content = " hello world!" >
69
97
◕‿‿◕
70
98
</TooltipAnchor >
71
- <Tooltip anchorId = " custom-styles" className = " example" classNameArrow = " example-arrow " />
99
+ <Tooltip anchorId = " custom-styles" className = " example" />
72
100
</div >
73
101
74
102
#### Explanation
75
103
76
104
In this example, we are adding an extra level to the CSS classes, the following styles are the default one of Tooltip component when we write this docs:
77
105
78
- ``` css
79
- .tooltip {
80
- visibility : hidden ;
81
- width : max-content ;
82
- position : absolute ;
83
- top : 0 ;
84
- left : 0 ;
85
- padding : 8px 16px ;
86
- border-radius : 3px ;
87
- font-size : 90% ;
88
- pointer-events : none ;
89
- opacity : 0 ;
90
- transition : opacity 0.3s ease-out ;
91
- will-change : opacity, visibility;
92
- }
93
-
94
- .arrow {
95
- position : absolute ;
96
- background : var (--rt-color-dark );
97
- width : 8px ;
98
- height : 8px ;
99
- transform : rotate (45deg );
100
- }
101
-
102
- /* * Types variant **/
103
- .dark ,
104
- .dark .arrow {
105
- background : var (--rt-color-dark );
106
- color : var (--rt-color-white );
107
- }
108
-
109
- .light ,
110
- .light .arrow {
111
- background-color : var (--rt-color-white );
112
- color : var (--rt-color-dark );
113
- }
114
-
115
- .success ,
116
- .success .arrow {
117
- background-color : var (--rt-color-success );
118
- color : var (--rt-color-white );
119
- }
120
-
121
- .warning ,
122
- .warning .arrow {
123
- background-color : var (--rt-color-warning );
124
- color : var (--rt-color-white );
125
- }
126
-
127
- .error ,
128
- .error .arrow {
129
- background-color : var (--rt-color-error );
130
- color : var (--rt-color-white );
131
- }
132
-
133
- .info ,
134
- .info .arrow {
135
- background-color : var (--rt-color-info );
136
- color : var (--rt-color-white );
137
- }
138
- ```
106
+ <CodeBlock language = " css" >{ TooltipStyles } </CodeBlock >
139
107
140
108
So, if we only add new classes like the below, this will not work because this is the same level of specificity than the default dark variant as example, you can compare:
141
109
@@ -145,20 +113,9 @@ So, if we only add new classes like the below, this will not work because this i
145
113
background-color : rgb (0 , 247 , 255 );
146
114
}
147
115
116
+ /* * add next line only if you want to have tooltip arrow with a different background color from tooltip **/
148
117
.example .example-arrow {
149
- background-color : rgb (0 , 247 , 255 );
150
- }
151
- ```
152
-
153
- ``` css
154
- .tooltip {
155
- ...;
156
- }
157
-
158
- .dark ,
159
- .dark .arrow {
160
- background : var (--rt-color-dark );
161
- color : var (--rt-color-white );
118
+ background-color : rgb (255 , 0 , 0 );
162
119
}
163
120
```
164
121
@@ -170,8 +127,9 @@ So, to make this work as expected, we need to add a new more level like this one
170
127
background-color : rgb (0 , 247 , 255 );
171
128
}
172
129
130
+ /* * add next line only if you want to have tooltip arrow with a different background color from tooltip **/
173
131
.some-class-or-rule .example .example-arrow {
174
- background-color : rgb (0 , 247 , 255 );
132
+ background-color : rgb (255 , 0 , 0 );
175
133
}
176
134
```
177
135
@@ -192,17 +150,13 @@ import 'react-tooltip/dist/react-tooltip.css'
192
150
color: #222 ;
193
151
background- color: rgb (255 , 153 , 0 );
194
152
}
195
-
196
- .example - container .example - orange .example - arrow {
197
- background- color: rgb (255 , 153 , 0 );
198
- }
199
153
< / style>
200
154
201
155
< div className= " example-container" >
202
156
< a id= " custom-styles-orange" data- tooltip- content= " hello world!" >
203
157
◕‿‿◕
204
158
< / a>
205
- < Tooltip anchorId= " custom-styles-orange" className= " example-orange" classNameArrow = " example-arrow " / >
159
+ < Tooltip anchorId= " custom-styles-orange" className= " example-orange" / >
206
160
< / div>
207
161
```
208
162
@@ -216,7 +170,6 @@ import 'react-tooltip/dist/react-tooltip.css'
216
170
<Tooltip
217
171
anchorId = " custom-styles-orange"
218
172
className = " example-orange"
219
- classNameArrow = " example-arrow"
220
173
/>
221
174
</div >
222
175
@@ -231,17 +184,13 @@ import 'react-tooltip/dist/react-tooltip.css'
231
184
color: #fff;
232
185
background- color: rgb (255 , 0 , 255 );
233
186
}
234
-
235
- .example - container .example - pink .example - arrow {
236
- background- color: rgb (255 , 0 , 255 );
237
- }
238
187
< / style>
239
188
240
189
< div className= " example-container" >
241
190
< a id= " custom-styles-pink" data- tooltip- content= " hello world!" >
242
191
◕‿‿◕
243
192
< / a>
244
- < Tooltip anchorId= " custom-styles-pink" className= " example-pink" classNameArrow = " example-arrow " / >
193
+ < Tooltip anchorId= " custom-styles-pink" className= " example-pink" / >
245
194
< / div>
246
195
```
247
196
@@ -252,7 +201,42 @@ import 'react-tooltip/dist/react-tooltip.css'
252
201
<TooltipAnchor id = " custom-styles-pink" data-tooltip-content = " hello world!" >
253
202
◕‿‿◕
254
203
</TooltipAnchor >
255
- <Tooltip anchorId = " custom-styles-pink" className = " example-pink" classNameArrow = " example-arrow" />
204
+ <Tooltip anchorId = " custom-styles-pink" className = " example-pink" />
205
+ </div >
206
+
207
+ #### Tooltip Arrow with a different color from Tooltip
208
+
209
+ ``` jsx
210
+ import { Tooltip } from ' react-tooltip'
211
+ import ' react-tooltip/dist/react-tooltip.css'
212
+
213
+ < style>
214
+ .example - container .example - diff- arrow {
215
+ color: #fff;
216
+ background- color: rgb (55 , 55 , 55 );
217
+ }
218
+
219
+ .example - container .example - diff- arrow .example - arrow {
220
+ background- color: rgb (222 , 34 , 72 );
221
+ }
222
+ < / style>
223
+
224
+ < div className= " example-container" >
225
+ < a id= " custom-styles-diff" data- tooltip- content= " hello world!" >
226
+ ◕‿‿◕
227
+ < / a>
228
+ < Tooltip anchorId= " custom-styles-diff" className= " example-diff-arrow" classNameArrow= " example-arrow" / >
229
+ < / div>
230
+ ```
231
+
232
+ <div
233
+ className = " example-container"
234
+ style = { { display: ' flex' , columnGap: ' 16px' , justifyContent: ' center' }}
235
+ >
236
+ <TooltipAnchor id = " custom-styles-diff" data-tooltip-content = " hello world!" >
237
+ ◕‿‿◕
238
+ </TooltipAnchor >
239
+ <Tooltip anchorId = " custom-styles-diff" className = " example-diff-arrow" classNameArrow = " example-arrow" />
256
240
</div >
257
241
258
242
### More examples - Radius
0 commit comments