Skip to content

Commit 2596fbf

Browse files
fix(tooltip): backwards compatibility (#19922)
* fix(tooltip): backwards compatibility * test(tooltip): update tests to reflect ARIA logic * chore(tooltip): clean up hasAriaLabel --------- Co-authored-by: Nikhil Tomar <63502271+2nikhiltom@users.noreply.github.com>
1 parent d2d38f1 commit 2596fbf

File tree

3 files changed

+7
-29
lines changed

3 files changed

+7
-29
lines changed

packages/react/src/components/IconButton/index.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,6 @@ IconButton.propTypes = {
336336
/**
337337
* Provide the label to be rendered inside of the Tooltip. The label will use
338338
* `aria-labelledby` and will fully describe the child node that is provided.
339-
* If the child node already has an `aria-label`, the tooltip will not apply
340-
* `aria-labelledby`. If the child node has `aria-labelledby`, that value will
341-
* be used instead. Otherwise, the tooltip will use its own ID as the label.
342339
* This means that if you have text in the child node it will not be
343340
* announced to the screen reader.
344341
* If using `badgeCount={0}`, make sure the label explains that there is a

packages/react/src/components/Tooltip/Tooltip.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ interface TooltipBaseProps {
8989
/**
9090
* Provide the label to be rendered inside of the Tooltip. The label will use
9191
* `aria-labelledby` and will fully describe the child node that is provided.
92-
* If the child already has an `aria-label`, the tooltip will not apply
93-
* `aria-labelledby`. If the child has its own `aria-labelledby`, that value
94-
* will be kept. Otherwise, the tooltip will use its own ID to label the child.
9592
* This means that if you have text in the child node, that it will not be
9693
* announced to the screen reader.
9794
*
@@ -142,28 +139,13 @@ const Tooltip: TooltipComponent = React.forwardRef(
142139
const child = React.Children.only(children);
143140

144141
const {
145-
'aria-label': ariaLabel,
146142
'aria-labelledby': ariaLabelledBy,
147143
'aria-describedby': ariaDescribedBy,
148144
} = child?.props ?? {};
149145

150146
const hasLabel = !!label;
151-
const hasAriaLabel =
152-
typeof ariaLabel === 'string' ? ariaLabel.trim() !== '' : false;
153-
154-
// An `aria-label` takes precedence over `aria-describedby`, but when it's
155-
// needed and the user doesn't specify one, the fallback `id` is used.
156-
const labelledBy = hasAriaLabel
157-
? undefined
158-
: hasLabel
159-
? (ariaLabelledBy ?? id)
160-
: undefined;
161-
162-
// If `aria-label` is present, use any provided `aria-describedby`.
163-
// If not, fallback to child's `aria-describedby` or the tooltip `id` if needed.
164-
const describedBy = hasAriaLabel
165-
? ariaDescribedBy
166-
: (ariaDescribedBy ?? (!hasLabel && !ariaLabelledBy ? id : undefined));
147+
const labelledBy = ariaLabelledBy ?? (hasLabel ? id : undefined);
148+
const describedBy = ariaDescribedBy ?? (!hasLabel ? id : undefined);
167149

168150
const triggerProps = {
169151
onFocus: () => !focusByMouse && setOpen(true),

packages/react/src/components/Tooltip/__tests__/Tooltip-test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
@@ -112,15 +112,14 @@ describe('Tooltip', () => {
112112
});
113113

114114
describe('Tooltip ARIA logic', () => {
115-
it('should not use aria-labelledby when the button already has aria-label', () => {
115+
it('should prefer aria-describedby from the button if already present', () => {
116116
render(
117-
<Tooltip defaultOpen label="Label text">
118-
<button aria-label="Aria Label">X</button>
117+
<Tooltip defaultOpen description="Some description">
118+
<button aria-describedby="desc-id">X</button>
119119
</Tooltip>
120120
);
121121
const button = screen.getByRole('button');
122-
expect(button).not.toHaveAttribute('aria-labelledby');
123-
expect(button).toHaveAttribute('aria-label', 'Aria Label');
122+
expect(button).toHaveAttribute('aria-describedby', 'desc-id');
124123
});
125124

126125
it('should keep the button’s aria-labelledby if it already has one', () => {

0 commit comments

Comments
 (0)