|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# Render |
| 6 | + |
| 7 | +Using the ReactTooltip render prop to render dynamic content based on the active anchor element. |
| 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 `render` prop can be used to render the tooltip content dynamically based on the currently active anchor element. |
| 40 | +The function signature is as follows: |
| 41 | + |
| 42 | +```ts |
| 43 | +(render: { content: string | null; activeAnchor: HTMLElement | null }) => ChildrenType |
| 44 | +``` |
| 45 | + |
| 46 | +- `content` is available for quick access to the `data-tooltip-content` attribute on the anchor element |
| 47 | +- `activeAnchor` is a ref to the anchor element currently active (or `null` if no element is active) |
| 48 | +- `ChildrenType` is essentially the same as `React.ReactNode` |
| 49 | + |
| 50 | +```jsx |
| 51 | +import { Tooltip } from 'react-tooltip'; |
| 52 | +import 'react-tooltip/dist/react-tooltip.css'; |
| 53 | + |
| 54 | +<a |
| 55 | + data-tooltip-id="my-tooltip" |
| 56 | + data-tooltip-content="1" |
| 57 | + data-some-relevant-attr="wow" |
| 58 | +> |
| 59 | + ◕‿‿◕ |
| 60 | +</a> |
| 61 | +<a |
| 62 | + data-tooltip-id="my-tooltip" |
| 63 | + data-tooltip-content="2" |
| 64 | + data-some-relevant-attr="so relevant" |
| 65 | +> |
| 66 | + ◕‿‿◕ |
| 67 | +</a> |
| 68 | +<a |
| 69 | + data-tooltip-id="my-tooltip" |
| 70 | + data-tooltip-content="3" |
| 71 | + data-some-relevant-attr="much important" |
| 72 | +> |
| 73 | + ◕‿‿◕ |
| 74 | +</a> |
| 75 | +<Tooltip |
| 76 | + id="my-tooltip" |
| 77 | + render={({ content, activeAnchor }) => ( |
| 78 | + <span> |
| 79 | + The element #{content} is currently active. |
| 80 | + <br/> |
| 81 | + Relevant attribute: {activeAnchor?.getAttribute('data-some-relevant-attr') || 'not set'} |
| 82 | + </span> |
| 83 | + )} |
| 84 | +/> |
| 85 | +``` |
| 86 | +
|
| 87 | +<div style={{ display: 'flex', gap: '5px', width: 'fit-content', margin: 'auto' }}> |
| 88 | + <TooltipAnchor |
| 89 | + data-tooltip-id="my-tooltip" |
| 90 | + data-tooltip-content="1" |
| 91 | + data-some-relevant-attr="wow" |
| 92 | + > |
| 93 | + ◕‿‿◕ |
| 94 | + </TooltipAnchor> |
| 95 | + <TooltipAnchor |
| 96 | + data-tooltip-id="my-tooltip" |
| 97 | + data-tooltip-content="2" |
| 98 | + data-some-relevant-attr="so relevant" |
| 99 | + > |
| 100 | + ◕‿‿◕ |
| 101 | + </TooltipAnchor> |
| 102 | + <TooltipAnchor |
| 103 | + data-tooltip-id="my-tooltip" |
| 104 | + data-tooltip-content="3" |
| 105 | + data-some-relevant-attr="much important" |
| 106 | + > |
| 107 | + ◕‿‿◕ |
| 108 | + </TooltipAnchor> |
| 109 | + <Tooltip |
| 110 | + id="my-tooltip" |
| 111 | + render={({ content, activeAnchor }) => ( |
| 112 | + <span> |
| 113 | + The element #{content} is currently active. |
| 114 | + <br/> |
| 115 | + Relevant attribute:{' '} |
| 116 | + { |
| 117 | + activeAnchor?.getAttribute('data-some-relevant-attr') || 'not set' |
| 118 | + } |
| 119 | + </span> |
| 120 | + )} |
| 121 | + /> |
| 122 | +</div> |
0 commit comments