Skip to content

Support more placement options in TooltipTypes.d.ts #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 59 additions & 14 deletions docs/docs/examples/place.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,82 @@ export const TooltipAnchor = ({ children, id, ...rest }) => {
)
}

The `place` prop and the `data-tooltip-place` attribute accept the following values: `'top' | 'right' | 'bottom' | 'left'`.
The `place` prop and the `data-tooltip-place` attribute accept the following values: `'top' | 'top-start' | 'top-end' | 'right' | 'right-start' | 'right-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end'`.

### Using `data-tooltip-place` attribute

```jsx
import { Tooltip } from 'react-tooltip';
const PLACES = ['top', 'top-start', 'top-end', 'right', 'right-start', 'right-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end']

<a id="my-tooltip-anchor">◕‿‿◕</a>
<Tooltip anchorSelect="#my-tooltip-anchor" content="Hello world from the top!" place="top" />
<Tooltip anchorSelect="#my-tooltip-anchor" content="Hello world from the right!" place="right" />
<Tooltip anchorSelect="#my-tooltip-anchor" content="Hello world from the bottom!" place="bottom" />
<Tooltip anchorSelect="#my-tooltip-anchor" content="Hello world from the left!" place="left" />
{PLACES.map(place => (
<Tooltip
key={place}
anchorSelect="#my-tooltip-anchor"
content={`Hello world from the ${place}!`}
place={place}
/>
))}
```

<TooltipAnchor id="my-tooltip-anchor">◕‿‿◕</TooltipAnchor>
<Tooltip anchorSelect="#my-tooltip-anchor" content="Hello world from the top!" place="top" />
<Tooltip anchorSelect="#my-tooltip-anchor" content="Hello world from the right!" place="right" />
<Tooltip anchorSelect="#my-tooltip-anchor" content="Hello world from the bottom!" place="bottom" />
<Tooltip anchorSelect="#my-tooltip-anchor" content="Hello world from the left!" place="left" />
export const PlacementExampleAttributes = () => {
const PLACES1 = ['top', 'right', 'bottom', 'left'];
const PLACES2 = ['top-start', 'right-start', 'bottom-start', 'left-start'];
const PLACES3 = ['top-end', 'right-end', 'bottom-end', 'left-end'];

return (
<>
<div style={{display: 'flex', gap: '16px'}}>
<TooltipAnchor id="my-tooltip-anchor1">◕‿‿◕</TooltipAnchor>
<TooltipAnchor id="my-tooltip-anchor2">◕‿‿◕</TooltipAnchor>
<TooltipAnchor id="my-tooltip-anchor3">◕‿‿◕</TooltipAnchor>
</div>
<div>
{PLACES1.map(place => (
<Tooltip
key={place}
anchorSelect="#my-tooltip-anchor1"
content={`Hello world from the ${place}!`}
place={place}
/>
))}
{PLACES2.map(place => (
<Tooltip
key={place}
anchorSelect="#my-tooltip-anchor2"
content={`Hello world from the ${place}!`}
place={place}
/>
))}
{PLACES3.map(place => (
<Tooltip
key={place}
anchorSelect="#my-tooltip-anchor3"
content={`Hello world from the ${place}!`}
place={place}
/>
))}
</div>
</>
)
}

<PlacementExampleAttributes />



### Using `place` prop

```jsx
import { Tooltip } from 'react-tooltip';

const PLACES = ['top', 'right', 'bottom', 'left']
const PLACES = ['top', 'top-start', 'top-end', 'right', 'right-start', 'right-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end']
const [place, setPlace] = useState(0)

<a
data-tooltip-id="my-tooltip"
onClick={() => setPlace(p => (p + 1) % 4)}
onClick={() => setPlace(p => (p + 1) % 12)}
>
◕‿‿◕
</a>
Expand All @@ -76,14 +121,14 @@ const [place, setPlace] = useState(0)
```

export const PlacementExample = () => {
const PLACES = ['top', 'right', 'bottom', 'left']
const PLACES = ['top', 'top-start', 'top-end', 'right', 'right-start', 'right-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end'];
const [place, setPlace] = useState(0)

return (
<>
<TooltipAnchor
data-tooltip-id="my-tooltip"
onClick={() => setPlace(p => (p + 1) % 4)}
onClick={() => setPlace(p => (p + 1) % 12)}
>
◕‿‿◕
</TooltipAnchor>
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import { Tooltip } from 'react-tooltip';
| data-tooltip-id | string | false | | | The id set on the tooltip element (same as V4's `data-for`) |
| data-tooltip-content | string | false | | | Content to de displayed in tooltip (`html` is priorized over `content`) |
| data-tooltip-html | string | false | | | HTML content to de displayed in tooltip |
| data-tooltip-place | string | false | `top` | `top` `right` `bottom` `left` | Position relative to the anchor element where the tooltip will be rendered (if possible) |
| data-tooltip-place | string | false | `top` | `top` `top-start` `top-end` `right` `right-start` `right-end` `bottom` `bottom-start` `bottom-end` `left` `left-start` `left-end` | Position relative to the anchor element where the tooltip will be rendered (if possible) |
| data-tooltip-offset | number | false | `10` | any `number` | Space between the tooltip element and anchor element (arrow not included in calculation) |
| data-tooltip-variant | string | false | `dark` | `dark` `light` `success` `warning` `error` `info` | Change the tooltip style with default presets |
| data-tooltip-wrapper | string | false | `div` | `div` `span` | Element wrapper for the tooltip container, can be `div`, `span`, `p` or any valid HTML tag |
Expand Down Expand Up @@ -94,7 +94,7 @@ import { Tooltip } from 'react-tooltip';
| `content` | `string` | no | | | Content to de displayed in tooltip (`html` prop is priorized over `content`) |
| ~~`html`~~ | ~~`string`~~ | ~~no~~ | | | ~~HTML content to de displayed in tooltip~~ <br/>**DEPRECATED**<br/>Use `children` or `render` instead |
| `render` | `function` | no | | | A function which receives a ref to the currently active anchor element and returns the content for the tooltip. Check the [examples](./examples/render.mdx) |
| `place` | `string` | no | `top` | `top` `right` `bottom` `left` | Position relative to the anchor element where the tooltip will be rendered (if possible) |
| `place` | `string` | no | `top` | `top` `top-start` `top-end` `right` `right-start` `right-end` `bottom` `bottom-start` `bottom-end` `left` `left-start` `left-end` | Position relative to the anchor element where the tooltip will be rendered (if possible) |
| `offset` | `number` | no | `10` | any `number` | Space between the tooltip element and anchor element (arrow not included in calculation) |
| `id` | `string` | no | | any `string` | The tooltip id. Must be set when using `data-tooltip-id` on the anchor element |
| ~~`anchorId`~~ | ~~`string`~~ | ~~no~~ | | ~~any `string`~~ | ~~The id for the anchor element for the tooltip~~ <br/>**DEPRECATED**<br/>Use `data-tooltip-id` or `anchorSelect` instead |
Expand Down
14 changes: 13 additions & 1 deletion src/components/Tooltip/TooltipTypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import type { ElementType, ReactNode, CSSProperties, RefObject } from 'react'

export type PlacesType = 'top' | 'right' | 'bottom' | 'left'
export type PlacesType =
| 'top'
| 'top-start'
| 'top-end'
| 'right'
| 'right-start'
| 'right-end'
| 'bottom'
| 'bottom-start'
| 'bottom-end'
| 'left'
| 'left-start'
| 'left-end'

export type VariantType = 'dark' | 'light' | 'success' | 'warning' | 'error' | 'info'

Expand Down
14 changes: 13 additions & 1 deletion src/utils/compute-positions-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ export interface IComputePositions {
elementReference?: Element | HTMLElement | null
tooltipReference?: Element | HTMLElement | null
tooltipArrowReference?: Element | HTMLElement | null
place?: 'top' | 'right' | 'bottom' | 'left'
place?:
| 'top'
| 'top-start'
| 'top-end'
| 'right'
| 'right-start'
| 'right-end'
| 'bottom'
| 'bottom-start'
| 'bottom-end'
| 'left'
| 'left-start'
| 'left-end'
offset?: number
strategy?: 'absolute' | 'fixed'
middlewares?: Middleware[]
Expand Down