Skip to content

Commit 7472211

Browse files
docs: update readme with anchorSelect changes
1 parent 7e62bbb commit 7472211

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

README.md

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ yarn add react-tooltip
4646

4747
## Usage
4848

49-
### Using NPM
49+
### Using NPM package
5050

51-
1 . Import the CSS file to set default styling
51+
1 . Import the CSS file to set default styling.
5252

5353
```js
5454
import 'react-tooltip/dist/react-tooltip.css'
5555
```
5656

5757
This needs to be done only once. We suggest you do it on your `src/index.js` or equivalent file.
5858

59-
2 . Import `react-tooltip` after installation
59+
2 . Import `react-tooltip` after installation.
6060

6161
```js
6262
import { Tooltip } from 'react-tooltip'
@@ -68,20 +68,41 @@ or if you want to still use the name ReactTooltip as V4:
6868
import { Tooltip as ReactTooltip } from 'react-tooltip'
6969
```
7070

71-
3 . Add `data-tooltip-content="your placeholder"` to your element
71+
3 . Add `data-tooltip-content="<your placeholder>"` and `data-tooltip-id="<tooltip id>"` to your element.
72+
73+
> `data-tooltip-id` is the equivalent of V4's `data-for`.
7274
7375
```jsx
74-
<p id="my-element" data-tooltip-content="hello world">
75-
Tooltip
76-
</p>
76+
<a data-tooltip-id="my-tooltip" data-tooltip-content="Hello world!">
77+
◕‿‿◕
78+
</a>
79+
```
80+
81+
4 . Include the `<Tooltip />` element.
82+
83+
> Don't forget to set the id, it won't work without it!
84+
85+
```jsx
86+
<Tooltip id="my-tooltip" />
7787
```
7888

79-
4 . Include react-tooltip component
89+
#### Using multiple anchor elements
90+
91+
You can also set the `anchorSelect` tooltip prop to use the tooltip with multiple anchor elements without having to set `data-tooltip-id` on each of them.
92+
`anchorSelect` must be a valid [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).
8093

8194
```jsx
82-
<ReactTooltip anchorId="my-element" />
95+
<a className="my-anchor-element" data-tooltip-content="Hello world!">
96+
◕‿‿◕
97+
</a>
98+
<a className="my-anchor-element" data-tooltip-content="Hello to you too!">
99+
◕‿‿◕
100+
</a>
101+
<Tooltip anchorSelect=".my-anchor-element" />
83102
```
84103

104+
Check [the V5 docs](https://react-tooltip.com/docs/getting-started) for more complex use cases.
105+
85106
### Standalone
86107

87108
You can import `node_modules/react-tooltip/dist/react-tooltip.[mode].js` into your page. Please make sure that you have already imported `react` and `react-dom` into your page.
@@ -98,27 +119,26 @@ PS: all the files have a minified version and a non-minified version.
98119

99120
For all available options, please check [React Tooltip Options](https://react-tooltip.com/docs/options)
100121

101-
### Security Note
122+
### Security note
102123

103124
The `html` option allows a tooltip to directly display raw HTML. This is a security risk if any of that content is supplied by the user. Any user-supplied content must be sanitized, using a package like [sanitize-html](https://www.npmjs.com/package/sanitize-html). We chose not to include sanitization after discovering it [increased our package size](https://github.com/wwayne/react-tooltip/issues/429) too much - we don't want to penalize people who don't use the `html` option.
104125

105-
#### JSX Note
126+
#### JSX note
106127

107-
You can use React's [`renderToStaticMarkup`-function](https://reactjs.org/docs/react-dom-server.html#rendertostaticmarkup) to use JSX instead of HTML.
128+
You can use [`React.renderToStaticMarkup()` function](https://reactjs.org/docs/react-dom-server.html#rendertostaticmarkup) to use JSX instead of HTML.
108129
**Example:**
109130

110131
```jsx
111132
import ReactDOMServer from 'react-dom/server';
112133
[...]
113-
<p id="my-element" data-tooltip-html={ReactDOMServer.renderToString(<div>I am <b>JSX</b> content</div>)}>
114-
Hover me
115-
</p>
134+
<a
135+
data-tooltip-id="my-tooltip"
136+
data-tooltip-html={ReactDOMServer.renderToStaticMarkup(<div>I am <b>JSX</b> content</div>)}
137+
>
138+
◕‿‿◕
139+
</a>
116140
```
117141

118-
#### Note
119-
120-
- **id** is necessary, because `<ReactTooltip anchorId="my-element" />` finds the tooltip via this attribute
121-
122142
## Article
123143

124144
[How I insert sass into react component](https://medium.com/@wwayne_me/how-i-insert-sass-into-my-npm-react-component-b46b9811c226#.gi4hxu44a)

0 commit comments

Comments
 (0)