Skip to content

Commit 1741761

Browse files
fatton139gabrieljablonski
authored andcommitted
feat: support data-tooltip-class-name on anchor
1 parent 56a7ba1 commit 1741761

File tree

6 files changed

+56
-1
lines changed

6 files changed

+56
-1
lines changed

docs/docs/options.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { Tooltip } from 'react-tooltip';
6969
| data-tooltip-delay-hide | number | false | | any `number` | The delay (in ms) before hiding the tooltip |
7070
| data-tooltip-float | boolean | false | `false` | `true` `false` | Tooltip will follow the mouse position when it moves inside the anchor element (same as V4's `effect="float"`) |
7171
| data-tooltip-hidden | boolean | false | `false` | `true` `false` | Tooltip will not be shown |
72+
| data-tooltip-class-name | string | false | | | Classnames for the tooltip container |
7273

7374
### Props
7475

src/components/Tooltip/TooltipTypes.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type DataAttribute =
3737
| 'delay-hide'
3838
| 'float'
3939
| 'hidden'
40+
| 'class-name'
4041

4142
/**
4243
* @description floating-ui middleware

src/components/TooltipController/TooltipController.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
import { useTooltip } from 'components/TooltipProvider'
1515
import { TooltipContent } from 'components/TooltipContent'
1616
import cssSupports from 'utils/css-supports'
17+
import classNames from 'classnames'
1718
import type { ITooltipController } from './TooltipControllerTypes'
1819

1920
const TooltipController = React.forwardRef<TooltipRefProps, ITooltipController>(
@@ -75,6 +76,7 @@ const TooltipController = React.forwardRef<TooltipRefProps, ITooltipController>(
7576
const [tooltipWrapper, setTooltipWrapper] = useState<WrapperType>(wrapper)
7677
const [tooltipEvents, setTooltipEvents] = useState(events)
7778
const [tooltipPositionStrategy, setTooltipPositionStrategy] = useState(positionStrategy)
79+
const [tooltipClassName, setTooltipClassName] = useState<string | null>(null)
7880
const [activeAnchor, setActiveAnchor] = useState<HTMLElement | null>(null)
7981
const styleInjectionRef = useRef(disableStyleInjection)
8082
/**
@@ -135,6 +137,9 @@ const TooltipController = React.forwardRef<TooltipRefProps, ITooltipController>(
135137
hidden: (value) => {
136138
setTooltipHidden(value === null ? hidden : value === 'true')
137139
},
140+
'class-name': (value) => {
141+
setTooltipClassName(value)
142+
},
138143
}
139144
// reset unset data attributes to default values
140145
// without this, data attributes from the last active anchor will still be used
@@ -321,7 +326,7 @@ const TooltipController = React.forwardRef<TooltipRefProps, ITooltipController>(
321326
id,
322327
anchorId,
323328
anchorSelect,
324-
className,
329+
className: classNames(className, tooltipClassName),
325330
classNameArrow,
326331
content: renderedContent,
327332
contentWrapperRef,

src/components/TooltipController/TooltipControllerTypes.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,6 @@ declare module 'react' {
114114
'data-tooltip-delay-hide'?: number
115115
'data-tooltip-float'?: boolean
116116
'data-tooltip-hidden'?: boolean
117+
'data-tooltip-class-name'?: string
117118
}
118119
}

src/test/__snapshots__/tooltip-attributes.spec.js.snap

+23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@ exports[`tooltip attributes basic tooltip 1`] = `
2222
</div>
2323
`;
2424

25+
exports[`tooltip attributes tooltip with class name 1`] = `
26+
<div>
27+
<span
28+
data-tooltip-class-name="tooltip-class-name"
29+
data-tooltip-content="Hello World!"
30+
id="example-class-name-attr"
31+
>
32+
Lorem Ipsum
33+
</span>
34+
<div
35+
class="react-tooltip tooltip-class-name react-tooltip__place-top react-tooltip__show"
36+
role="tooltip"
37+
style="left: 5px; top: -10px;"
38+
>
39+
Hello World!
40+
<div
41+
class="react-tooltip-arrow"
42+
style="left: -1px; bottom: -4px;"
43+
/>
44+
</div>
45+
</div>
46+
`;
47+
2548
exports[`tooltip attributes tooltip with place 1`] = `
2649
<div>
2750
<span

src/test/tooltip-attributes.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,28 @@ describe('tooltip attributes', () => {
7575
expect(tooltip).toBeInTheDocument()
7676
expect(container).toMatchSnapshot()
7777
})
78+
79+
test('tooltip with class name', async () => {
80+
const { container } = render(
81+
<TooltipAttrs
82+
id="example-class-name-attr"
83+
data-tooltip-content="Hello World!"
84+
data-tooltip-class-name="tooltip-class-name"
85+
/>,
86+
)
87+
const anchorElement = screen.getByText('Lorem Ipsum')
88+
89+
await userEvent.hover(anchorElement)
90+
91+
let tooltip = null
92+
93+
await waitFor(() => {
94+
tooltip = screen.getByRole('tooltip')
95+
expect(tooltip).toHaveClass('tooltip-class-name')
96+
})
97+
98+
expect(anchorElement).toHaveAttribute('data-tooltip-class-name')
99+
expect(tooltip).toBeInTheDocument()
100+
expect(container).toMatchSnapshot()
101+
})
78102
})

0 commit comments

Comments
 (0)