Skip to content

Commit b9d7940

Browse files
authored
refactor: support clearing tooltip ariaTarget by setting null (#10039)
1 parent 7794173 commit b9d7940

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

packages/rich-text-editor/src/vaadin-rich-text-editor-mixin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,9 @@ export const RichTextEditorMixin = (superClass) =>
412412
// Set up tooltip to show when hovering or focusing toolbar buttons
413413
this._tooltip = document.createElement('vaadin-tooltip');
414414
this._tooltip.slot = 'tooltip';
415-
// Create dummy aria target, as toolbar buttons already have aria-label, and also cannot be linked with the
416-
// tooltip being in the light DOM
417-
this._tooltip.ariaTarget = document.createElement('div');
415+
// Set ariaTarget to null, as toolbar buttons already have aria-label,
416+
// and also cannot be linked with the tooltip being in the light DOM
417+
this._tooltip.ariaTarget = null;
418418
this.append(this._tooltip);
419419

420420
const buttons = this.shadowRoot.querySelectorAll('[part~="toolbar-button"]');

packages/tooltip/src/vaadin-tooltip-mixin.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export declare class TooltipMixinClass {
2222
* attribute. Supports array of multiple elements.
2323
* When not set, defaults to `target`.
2424
*/
25-
ariaTarget: HTMLElement | HTMLElement[] | undefined;
25+
ariaTarget: HTMLElement | HTMLElement[] | null | undefined;
2626

2727
/**
2828
* Object with properties passed to `generator` and

packages/tooltip/src/vaadin-tooltip-mixin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ export const TooltipMixin = (superClass) =>
675675
__computeAriaTarget(ariaTarget, target) {
676676
const isElementNode = (el) => el && el.nodeType === Node.ELEMENT_NODE;
677677
const isAriaTargetSet = Array.isArray(ariaTarget) ? ariaTarget.some(isElementNode) : ariaTarget;
678-
return isAriaTargetSet ? ariaTarget : target;
678+
return ariaTarget === null || isAriaTargetSet ? ariaTarget : target;
679679
}
680680

681681
/** @private */

packages/tooltip/test/tooltip.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,30 @@ describe('vaadin-tooltip', () => {
218218
expect(ariaTarget.getAttribute('aria-describedby')).to.equal(contentNode.id);
219219
});
220220

221-
it('should remove aria-describedby when the ariaTarget is cleared', async () => {
221+
it('should remove aria-describedby and set it on the target when ariaTarget is set to undefined', async () => {
222222
tooltip.target = target;
223223
tooltip.ariaTarget = ariaTarget;
224224
await nextUpdate(tooltip);
225225

226-
tooltip.ariaTarget = null;
226+
tooltip.ariaTarget = undefined;
227227
await nextUpdate(tooltip);
228228

229229
expect(ariaTarget.hasAttribute('aria-describedby')).to.be.false;
230230
expect(target.getAttribute('aria-describedby')).to.equal(contentNode.id);
231231
});
232232

233+
it('should remove aria-describedby and not set it on the target when ariaTarget is set to null', async () => {
234+
tooltip.target = target;
235+
tooltip.ariaTarget = ariaTarget;
236+
await nextUpdate(tooltip);
237+
238+
tooltip.ariaTarget = null;
239+
await nextUpdate(tooltip);
240+
241+
expect(ariaTarget.hasAttribute('aria-describedby')).to.be.false;
242+
expect(target.hasAttribute('aria-describedby')).to.be.false;
243+
});
244+
233245
it('should set aria-describedby when providing multiple elements', async () => {
234246
const ariaTarget2 = document.createElement('button');
235247
target.appendChild(ariaTarget2);

packages/tooltip/test/typings/tooltip.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ assertType<ThemePropertyMixinClass>(tooltip);
1313

1414
// Properties
1515
assertType<string | undefined>(tooltip.for);
16-
assertType<HTMLElement | HTMLElement[] | undefined>(tooltip.ariaTarget);
16+
assertType<HTMLElement | HTMLElement[] | null | undefined>(tooltip.ariaTarget);
1717
assertType<HTMLElement | undefined>(tooltip.target);
1818
assertType<string | null | undefined>(tooltip.text);
1919
assertType<Record<string, unknown>>(tooltip.context);

0 commit comments

Comments
 (0)