|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# Anchor select |
| 6 | + |
| 7 | +Default color stylings available for the ReactTooltip component. |
| 8 | + |
| 9 | +import { Tooltip } from 'react-tooltip' |
| 10 | +import 'react-tooltip/dist/react-tooltip.css' |
| 11 | + |
| 12 | +export const TooltipAnchor = ({ children, id, ...rest }) => { |
| 13 | + return ( |
| 14 | + <span |
| 15 | + id={id} |
| 16 | + style={{ |
| 17 | + display: 'flex', |
| 18 | + justifyContent: 'center', |
| 19 | + margin: 'auto', |
| 20 | + alignItems: 'center', |
| 21 | + width: '60px', |
| 22 | + height: '60px', |
| 23 | + borderRadius: '60px', |
| 24 | + color: '#222', |
| 25 | + background: 'rgba(255, 255, 255, 1)', |
| 26 | + cursor: 'pointer', |
| 27 | + boxShadow: '3px 4px 3px rgba(0, 0, 0, 0.5)', |
| 28 | + border: '1px solid #333', |
| 29 | + }} |
| 30 | + {...rest} |
| 31 | + > |
| 32 | + {children} |
| 33 | + </span> |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +### Basic usage |
| 38 | + |
| 39 | +The `anchorSelect` prop uses a [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) to attach the tooltip to the anchor elements. The most common use for this is selecting elements with a specific id, or with a CSS class. |
| 40 | + |
| 41 | +#### Using id attribute |
| 42 | + |
| 43 | +:::info |
| 44 | + |
| 45 | +A CSS selector for a specific id begins with a `#`. Don't forget to put it before the id on your selector! |
| 46 | + |
| 47 | +::: |
| 48 | + |
| 49 | +```jsx |
| 50 | +import { Tooltip } from 'react-tooltip'; |
| 51 | +import 'react-tooltip/dist/react-tooltip.css'; |
| 52 | + |
| 53 | +<a id="my-anchor-element-id">◕‿‿◕</a> |
| 54 | +<Tooltip |
| 55 | + // Don't forget the `#`! |
| 56 | + anchorSelect="#my-anchor-element-id" |
| 57 | + content="Hello world!" |
| 58 | +/> |
| 59 | +``` |
| 60 | + |
| 61 | +<TooltipAnchor id="my-anchor-element-id">◕‿‿◕</TooltipAnchor> |
| 62 | +<Tooltip |
| 63 | + anchorSelect="#my-anchor-element-id" |
| 64 | + content="Hello world!" |
| 65 | +/> |
| 66 | + |
| 67 | +#### Using class attribute |
| 68 | + |
| 69 | +:::info |
| 70 | + |
| 71 | +A CSS selector for a specific id begins with a `.`. Don't forget to put it before the class on your selector! |
| 72 | + |
| 73 | +::: |
| 74 | + |
| 75 | +```jsx |
| 76 | +import { Tooltip } from 'react-tooltip'; |
| 77 | +import 'react-tooltip/dist/react-tooltip.css'; |
| 78 | + |
| 79 | +<a className="my-anchor-element-class">◕‿‿◕</a> |
| 80 | +<a className="my-anchor-element-class">◕‿‿◕</a> |
| 81 | +<a className="my-anchor-element-class">◕‿‿◕</a> |
| 82 | +<a className="my-anchor-element-class">◕‿‿◕</a> |
| 83 | +<Tooltip |
| 84 | + // Don't forget the `.`! |
| 85 | + anchorSelect=".my-anchor-element-class" |
| 86 | + content="Hello world!" |
| 87 | +/> |
| 88 | +``` |
| 89 | + |
| 90 | +<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> |
| 103 | +</div> |
| 104 | +<Tooltip |
| 105 | + anchorSelect=".my-anchor-element-class" |
| 106 | + content="Hello world!" |
| 107 | +/> |
| 108 | + |
| 109 | +### Complex selectors |
| 110 | + |
| 111 | +Once you've understood how it works, you can write CSS selectors as complex as you can imagine. Here are some interesting examples. |
| 112 | + |
| 113 | +#### Attribute prefix |
| 114 | + |
| 115 | +:::info |
| 116 | + |
| 117 | +`[attr^='prefix']` can be read as "any element that has an attribute `attr` in which its value starts with `prefix`". Remove the `^` for an exact match. |
| 118 | + |
| 119 | +This example uses the name attribute, but it works for any HTML attribute (id, class, ...). |
| 120 | + |
| 121 | +::: |
| 122 | + |
| 123 | +```jsx |
| 124 | +import { Tooltip } from 'react-tooltip'; |
| 125 | +import 'react-tooltip/dist/react-tooltip.css'; |
| 126 | + |
| 127 | +<a name="my-anchor-element-1">◕‿‿◕</a> |
| 128 | +<a name="my-anchor-element-2">◕‿‿◕</a> |
| 129 | +<a name="my-anchor-element-3">◕‿‿◕</a> |
| 130 | +<a name="my-anchor-element-4">◕‿‿◕</a> |
| 131 | +<Tooltip |
| 132 | + anchorSelect="[name^='my-anchor-element-']" |
| 133 | + content="Hello world!" |
| 134 | +/> |
| 135 | +``` |
| 136 | + |
| 137 | +<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> |
| 150 | +</div> |
| 151 | +<Tooltip |
| 152 | + anchorSelect="[name^='my-anchor-element-']" |
| 153 | + content="Hello world!" |
| 154 | +/> |
| 155 | + |
| 156 | +#### Child selector |
| 157 | + |
| 158 | +:::info |
| 159 | + |
| 160 | +Make sure there isn't an easier alternative before diving into complex selectors like this. |
| 161 | + |
| 162 | +Often you can just use a class selector or something else much simpler. |
| 163 | + |
| 164 | +::: |
| 165 | + |
| 166 | +```jsx |
| 167 | +import { Tooltip } from 'react-tooltip'; |
| 168 | +import 'react-tooltip/dist/react-tooltip.css'; |
| 169 | + |
| 170 | +<section id="section-anchor-select" style={{ marginTop: '100px' }}> |
| 171 | + <a>◕‿‿◕</a> |
| 172 | + <a>◕‿‿◕</a> |
| 173 | + <a>◕‿‿◕</a> |
| 174 | + <a>◕‿‿◕</a> |
| 175 | +</section> |
| 176 | +<Tooltip |
| 177 | + anchorSelect="section[id='section-anchor-select'] > a:nth-child(odd)" |
| 178 | + content="I am an odd child!" |
| 179 | +/> |
| 180 | +<Tooltip |
| 181 | + anchorSelect="section[id='section-anchor-select'] > a:nth-child(even)" |
| 182 | + content="I am an even child!" |
| 183 | +/> |
| 184 | +``` |
| 185 | + |
| 186 | +<section |
| 187 | + id="section-anchor-select" |
| 188 | + style={{ display: 'flex', gap: '5px', width: 'fit-content', margin: 'auto' }} |
| 189 | +> |
| 190 | + <TooltipAnchor>◕‿‿◕</TooltipAnchor> |
| 191 | + <TooltipAnchor>◕‿‿◕</TooltipAnchor> |
| 192 | + <TooltipAnchor>◕‿‿◕</TooltipAnchor> |
| 193 | + <TooltipAnchor>◕‿‿◕</TooltipAnchor> |
| 194 | +</section> |
| 195 | +<Tooltip |
| 196 | + anchorSelect="section[id='section-anchor-select'] > span:nth-child(odd)" |
| 197 | + content="I am an odd child!" |
| 198 | +/> |
| 199 | +<Tooltip |
| 200 | + anchorSelect="section[id='section-anchor-select'] > span:nth-child(even)" |
| 201 | + content="I am an even child!" |
| 202 | +/> |
0 commit comments