Skip to content

Commit cdc1a74

Browse files
authored
Merge pull request #1014 from ReactTooltip/feat/auto-inject-css
feat: change the build to auto inject css into the page
2 parents 7de068a + 23b72d0 commit cdc1a74

23 files changed

+423
-543
lines changed

README.md

+19-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[![npm download][download-image]][download-url]
77
![minified](https://badgen.net/bundlephobia/min/react-tooltip)
88
![minified gzip](https://badgen.net/bundlephobia/minzip/react-tooltip)
9+
910
<!-- ![last commit](https://badgen.net/github/last-commit/reacttooltip/react-tooltip) -->
1011

1112
[download-image]: https://img.shields.io/npm/dm/react-tooltip.svg?style=flat-square
@@ -17,7 +18,7 @@
1718
</a>
1819
</p>
1920

20-
If you like the project, please give the project a GitHub 🌟
21+
If you like the project, please give the project a GitHub 🌟
2122

2223
## Demo
2324

@@ -55,20 +56,23 @@ yarn add react-tooltip
5556

5657
## Usage
5758

59+
> :warning: ReactTooltip will inject the default styles into the page by default on version `5.13.0` or newer.
60+
> The `react-tooltip/dist/react-tooltip.css` file is only for style reference and doesn't need to be imported manually anymore if you are already using `v5.13.0` or upper.
61+
5862
> :warning: If you were already using `react-tooltip<=5.7.5`, you'll be getting some deprecation warnings regarding the `anchorId` prop and some other features.
59-
In versions >=5.8.0, we've introduced the `data-tooltip-id` attribute, and the `anchorSelect` prop, which are our recommended methods of using the tooltip moving forward. Check [the docs](https://react-tooltip.com/docs/getting-started) for more details.
63+
> In versions >=5.8.0, we've introduced the `data-tooltip-id` attribute, and the `anchorSelect` prop, which are our recommended methods of using the tooltip moving forward. Check [the docs](https://react-tooltip.com/docs/getting-started) for more details.
6064
6165
### Using NPM package
6266

6367
1 . Import the CSS file to set default styling.
6468

65-
> :warning: You must import the CSS file or the tooltip won't show!
69+
> :warning: If you are using a version before than `v5.13.0`, you must import the CSS file or the tooltip won't show!
6670
6771
```js
6872
import 'react-tooltip/dist/react-tooltip.css'
6973
```
7074

71-
This needs to be done only once. We suggest you do it on your `src/index.js` or equivalent file.
75+
This needs to be done only once and only if you are using a version before than `5.13.0`. We suggest you do it on your `src/index.js` or equivalent file.
7276

7377
2 . Import `react-tooltip` after installation.
7478

@@ -123,7 +127,7 @@ You can import `node_modules/react-tooltip/dist/react-tooltip.[mode].js` into yo
123127

124128
mode: `esm` `cjs` `umd`
125129

126-
Don't forget to import the CSS file from `node_modules/react-tooltip/dist/react-tooltip.css` to set default styling. This needs to be done only once in your application.
130+
If you are using a version older than `v5.13.0` don't forget to import the CSS file from `node_modules/react-tooltip/dist/react-tooltip.css` to set default styling. This needs to be done only once in your application. Version `v5.13.0` or newer already inject the default styles into the page by default.
127131

128132
PS: all the files have a minified version and a non-minified version.
129133

@@ -145,7 +149,7 @@ You can use [`renderToStaticMarkup()` function](https://reactjs.org/docs/react-d
145149
```jsx
146150
import ReactDOMServer from 'react-dom/server';
147151
[...]
148-
<a
152+
<a
149153
data-tooltip-id="my-tooltip"
150154
data-tooltip-html={ReactDOMServer.renderToStaticMarkup(<div>I am <b>JSX</b> content</div>)}
151155
>
@@ -172,7 +176,7 @@ If there isn't, feel free to [submit a new issue](https://github.com/ReactToolti
172176

173177
### The tooltip is broken/not showing up
174178

175-
Make sure you've imported the default styling. You only need to do this once on your application, `App.jsx`/`App.tsx` is usually a good place to do it.
179+
Make sure you've imported the default styling. You only need to do this once on your application and only if you are using a version before `5.13.0`, `App.jsx`/`App.tsx` is usually a good place to do it.
176180

177181
```jsx
178182
import 'react-tooltip/dist/react-tooltip.css'
@@ -184,7 +188,7 @@ If `data-tooltip-content` and `data-tooltip-html` are both unset (or they have e
184188

185189
### Next.js `TypeError: f is not a function`
186190

187-
This problem seems to be caused by a bug related to the SWC bundler used by Next.js.
191+
This problem seems to be caused by a bug related to the SWC bundler used by Next.js.
188192
The best way to solve this is to upgrade to `[email protected]` or later versions.
189193

190194
Less ideally, if you're unable to upgrade, you can set `swcMinify: false` on your `next.config.js` file.
@@ -199,7 +203,7 @@ This is specially relevant when using components that are conditionally rendered
199203

200204
Always try to keep the `<Tooltip />` component rendered, so if you're having issues with a tooltip you've placed inside a component which is placed/removed from the DOM dynamically, try to move the tooltip outside of it.
201205

202-
We usually recommend placing the tooltip component directly inside the root component of your application (usually on `App.jsx`/`App.tsx`). You can also move the `import 'react-tooltip/dist/react-tooltip.css'` there.
206+
We usually recommend placing the tooltip component directly inside the root component of your application (usually on `App.jsx`/`App.tsx`).
203207

204208
#### Dynamically generated anchor elements
205209

@@ -212,18 +216,12 @@ Here's a simple example on how to improve performance when using dynamically gen
212216
```jsx
213217
// ❌ BAD
214218
<div className="items-container">
215-
{
216-
myItems.map(({ id, content, tooltip }) => (
217-
<div
218-
key={id}
219-
className="item"
220-
data-tooltip-id={`tooltip-${id}`}
221-
>
222-
{content}
223-
<Tooltip id={`tooltip-${id}`} content={tooltip} />
224-
</div>
225-
))
226-
}
219+
{myItems.map(({ id, content, tooltip }) => (
220+
<div key={id} className="item" data-tooltip-id={`tooltip-${id}`}>
221+
{content}
222+
<Tooltip id={`tooltip-${id}`} content={tooltip} />
223+
</div>
224+
))}
227225
</div>
228226
```
229227

@@ -268,7 +266,6 @@ Here's a simple example on how to improve performance when using dynamically gen
268266

269267
[wwayne](https://github.com/wwayne) (inactive) - Creator of the original React Tooltip (V1.x ~ V4.x.)
270268

271-
272269
We would gladly accept a new maintainer to help out!
273270

274271
## Contributing

docs/docs/examples/anchor-select.mdx

+13-43
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ sidebar_position: 1
77
Using the ReactTooltip anchor select prop.
88

99
import { Tooltip } from 'react-tooltip'
10-
import 'react-tooltip/dist/react-tooltip.css'
1110

1211
export const TooltipAnchor = ({ children, id, ...rest }) => {
1312
return (
@@ -48,7 +47,6 @@ A CSS selector for a specific id begins with a `#`. Don't forget to put it befor
4847

4948
```jsx
5049
import { Tooltip } from 'react-tooltip';
51-
import 'react-tooltip/dist/react-tooltip.css';
5250

5351
<a id="my-anchor-element-id">◕‿‿◕</a>
5452
<Tooltip
@@ -59,10 +57,7 @@ import 'react-tooltip/dist/react-tooltip.css';
5957
```
6058

6159
<TooltipAnchor id="my-anchor-element-id">◕‿‿◕</TooltipAnchor>
62-
<Tooltip
63-
anchorSelect="#my-anchor-element-id"
64-
content="Hello world!"
65-
/>
60+
<Tooltip anchorSelect="#my-anchor-element-id" content="Hello world!" />
6661

6762
#### Using class attribute
6863

@@ -74,7 +69,6 @@ A CSS selector for a specific id begins with a `.`. Don't forget to put it befor
7469

7570
```jsx
7671
import { Tooltip } from 'react-tooltip';
77-
import 'react-tooltip/dist/react-tooltip.css';
7872

7973
<a className="my-anchor-element-class">◕‿‿◕</a>
8074
<a className="my-anchor-element-class">◕‿‿◕</a>
@@ -88,23 +82,12 @@ import 'react-tooltip/dist/react-tooltip.css';
8882
```
8983

9084
<div style={{ display: 'flex', gap: '5px', width: 'fit-content', margin: 'auto' }}>
91-
<TooltipAnchor className="my-anchor-element-class">
92-
◕‿‿◕
93-
</TooltipAnchor>
94-
<TooltipAnchor className="my-anchor-element-class">
95-
◕‿‿◕
96-
</TooltipAnchor>
97-
<TooltipAnchor className="my-anchor-element-class">
98-
◕‿‿◕
99-
</TooltipAnchor>
100-
<TooltipAnchor className="my-anchor-element-class">
101-
◕‿‿◕
102-
</TooltipAnchor>
85+
<TooltipAnchor className="my-anchor-element-class">◕‿‿◕</TooltipAnchor>
86+
<TooltipAnchor className="my-anchor-element-class">◕‿‿◕</TooltipAnchor>
87+
<TooltipAnchor className="my-anchor-element-class">◕‿‿◕</TooltipAnchor>
88+
<TooltipAnchor className="my-anchor-element-class">◕‿‿◕</TooltipAnchor>
10389
</div>
104-
<Tooltip
105-
anchorSelect=".my-anchor-element-class"
106-
content="Hello world!"
107-
/>
90+
<Tooltip anchorSelect=".my-anchor-element-class" content="Hello world!" />
10891

10992
### Complex selectors
11093

@@ -122,7 +105,6 @@ This example uses the name attribute, but it works for any HTML attribute (id, c
122105

123106
```jsx
124107
import { Tooltip } from 'react-tooltip';
125-
import 'react-tooltip/dist/react-tooltip.css';
126108

127109
<a name="my-anchor-element-1">◕‿‿◕</a>
128110
<a name="my-anchor-element-2">◕‿‿◕</a>
@@ -135,23 +117,12 @@ import 'react-tooltip/dist/react-tooltip.css';
135117
```
136118

137119
<div style={{ display: 'flex', gap: '5px', width: 'fit-content', margin: 'auto' }}>
138-
<TooltipAnchor name="my-anchor-element-1">
139-
◕‿‿◕
140-
</TooltipAnchor>
141-
<TooltipAnchor name="my-anchor-element-2">
142-
◕‿‿◕
143-
</TooltipAnchor>
144-
<TooltipAnchor name="my-anchor-element-3">
145-
◕‿‿◕
146-
</TooltipAnchor>
147-
<TooltipAnchor name="my-anchor-element-4">
148-
◕‿‿◕
149-
</TooltipAnchor>
120+
<TooltipAnchor name="my-anchor-element-1">◕‿‿◕</TooltipAnchor>
121+
<TooltipAnchor name="my-anchor-element-2">◕‿‿◕</TooltipAnchor>
122+
<TooltipAnchor name="my-anchor-element-3">◕‿‿◕</TooltipAnchor>
123+
<TooltipAnchor name="my-anchor-element-4">◕‿‿◕</TooltipAnchor>
150124
</div>
151-
<Tooltip
152-
anchorSelect="[name^='my-anchor-element-']"
153-
content="Hello world!"
154-
/>
125+
<Tooltip anchorSelect="[name^='my-anchor-element-']" content="Hello world!" />
155126

156127
#### Child selector
157128

@@ -165,7 +136,6 @@ Often you can just use a class selector or something else much simpler.
165136

166137
```jsx
167138
import { Tooltip } from 'react-tooltip';
168-
import 'react-tooltip/dist/react-tooltip.css';
169139

170140
<section id="section-anchor-select" style={{ marginTop: '100px' }}>
171141
<a>◕‿‿◕</a>
@@ -183,7 +153,7 @@ import 'react-tooltip/dist/react-tooltip.css';
183153
/>
184154
```
185155

186-
<section
156+
<section
187157
id="section-anchor-select"
188158
style={{ display: 'flex', gap: '5px', width: 'fit-content', margin: 'auto' }}
189159
>
@@ -199,4 +169,4 @@ import 'react-tooltip/dist/react-tooltip.css';
199169
<Tooltip
200170
anchorSelect="section[id='section-anchor-select'] > span:nth-child(even)"
201171
content="I am an even child!"
202-
/>
172+
/>

docs/docs/examples/basic-examples.mdx

+22-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ sidebar_position: 0
77
Some basic examples of how to use the ReactTooltip component.
88

99
import { Tooltip } from 'react-tooltip'
10-
import 'react-tooltip/dist/react-tooltip.css'
1110

1211
export const TooltipAnchor = ({ children, id, ...rest }) => {
1312
return (
@@ -38,7 +37,6 @@ export const TooltipAnchor = ({ children, id, ...rest }) => {
3837

3938
```jsx
4039
import { Tooltip } from 'react-tooltip';
41-
import 'react-tooltip/dist/react-tooltip.css';
4240

4341
<a data-tooltip-id="my-tooltip" data-tooltip-content="Hello world!">
4442
◕‿‿◕
@@ -49,7 +47,15 @@ import 'react-tooltip/dist/react-tooltip.css';
4947
<Tooltip id="my-tooltip" />
5048
```
5149

52-
<div style={{ display: 'flex', flexDirection: 'row', width: 'fit-content', margin: 'auto', gap: '5px' }}>
50+
<div
51+
style={{
52+
display: 'flex',
53+
flexDirection: 'row',
54+
width: 'fit-content',
55+
margin: 'auto',
56+
gap: '5px',
57+
}}
58+
>
5359
<TooltipAnchor data-tooltip-id="my-tooltip" data-tooltip-content="Hello world!">
5460
◕‿‿◕
5561
</TooltipAnchor>
@@ -61,25 +67,20 @@ import 'react-tooltip/dist/react-tooltip.css';
6167

6268
### Props
6369

64-
6570
#### Using anchor element `id`
6671

6772
```jsx
6873
import { Tooltip } from 'react-tooltip';
69-
import 'react-tooltip/dist/react-tooltip.css'
7074

7175
<a id="my-anchor-element">◕‿‿◕</a>
72-
<Tooltip
76+
<Tooltip
7377
anchorSelect="#my-anchor-element"
7478
content="Hello world!"
7579
/>
7680
```
7781

7882
<TooltipAnchor id="my-anchor-element">◕‿‿◕</TooltipAnchor>
79-
<Tooltip
80-
anchorSelect="#my-anchor-element"
81-
content="Hello world!"
82-
/>
83+
<Tooltip anchorSelect="#my-anchor-element" content="Hello world!" />
8384

8485
#### Using anchor elements `className`
8586

@@ -91,21 +92,25 @@ Check the [Anchor select examples](./anchor-select.mdx) for more complex use cas
9192

9293
```jsx
9394
import { Tooltip } from 'react-tooltip';
94-
import 'react-tooltip/dist/react-tooltip.css'
9595

9696
<a className="my-anchor-element">◕‿‿◕</a>
9797
<a className="my-anchor-element">◕‿‿◕</a>
98-
<Tooltip
98+
<Tooltip
9999
anchorSelect=".my-anchor-element"
100100
content="Hello world!"
101101
/>
102102
```
103103

104-
<div style={{ display: 'flex', flexDirection: 'row', width: 'fit-content', margin: 'auto', gap: '5px' }}>
104+
<div
105+
style={{
106+
display: 'flex',
107+
flexDirection: 'row',
108+
width: 'fit-content',
109+
margin: 'auto',
110+
gap: '5px',
111+
}}
112+
>
105113
<TooltipAnchor className="my-anchor-element">◕‿‿◕</TooltipAnchor>
106114
<TooltipAnchor className="my-anchor-element">◕‿‿◕</TooltipAnchor>
107115
</div>
108-
<Tooltip
109-
anchorSelect=".my-anchor-element"
110-
content="Hello world!"
111-
/>
116+
<Tooltip anchorSelect=".my-anchor-element" content="Hello world!" />

docs/docs/examples/children.mdx

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ sidebar_position: 1
77
Using children for setting the ReactTooltip content.
88

99
import { Tooltip } from 'react-tooltip'
10-
import 'react-tooltip/dist/react-tooltip.css'
1110

1211
export const TooltipAnchor = ({ children, id, ...rest }) => {
1312
return (
@@ -41,7 +40,6 @@ This is useful for setting a placeholder for the tooltip content.
4140

4241
```jsx
4342
import { Tooltip } from 'react-tooltip';
44-
import 'react-tooltip/dist/react-tooltip.css';
4543

4644
<a data-tooltip-id="my-tooltip">◕‿‿◕</a>
4745
<Tooltip id="my-tooltip">

0 commit comments

Comments
 (0)