fix(packages): only remove aria-hidden when an accessibility prop is set (#5347)

* Adjust accessibility behaviour for title elements

* Adjust angular package as well

* Fix types

* fix(packages/angular): use fixture.whenStable instead of triggering change detection manually

detectChanges() explicitly forces a check, so it can mask missing automatic change detection

* test(packages/angular): add test for non-signal bound aria attribute

* fix(angular): preserve explicit aria-hidden and support input bindings

---------

Co-authored-by: Karsa <contact@karsa.org>
This commit is contained in:
Eric Fennis
2026-12-01 12:56:09 +01:00
committed by GitHub
co-authored by Karsa
parent 475eff7532
commit 8cd4f293f2
31 changed files with 570 additions and 53 deletions
@@ -1,5 +1,6 @@
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
inputBinding,
signal,
@@ -39,6 +40,57 @@ export class LucideCircleCheck extends LucideIconBase {
})
class ClassHostComponent {}
@Component({
template: `<svg lucideCircleCheck aria-label="Done"></svg>`,
imports: [LucideCircleCheck],
})
class AriaLabelHostComponent {}
@Component({
template: `<svg lucideCircleCheck role="img" aria-labelledby="done-title"></svg>`,
imports: [LucideCircleCheck],
})
class RoleHostComponent {}
@Component({
template: `<svg lucideCircleCheck [attr.aria-label]="label()"></svg>`,
imports: [LucideCircleCheck],
})
class BoundAriaLabelHostComponent {
readonly label = signal<string | null>(null);
}
@Component({
template: `<svg lucideCircleCheck [attr.aria-description]="description"></svg>`,
imports: [LucideCircleCheck],
})
class BoundPlainAriaDescriptionHostComponent {
description: string | null = null;
}
@Component({
template: `<svg lucideCircleCheck aria-hidden="true" aria-label="Done" [title]="title()"></svg>`,
imports: [LucideCircleCheck],
})
class ExplicitlyHiddenHostComponent {
readonly title = signal<string | null>(null);
}
@Component({
template: `<svg lucideCircleCheck aria-hidden="false"></svg>`,
imports: [LucideCircleCheck],
})
class ExplicitlyExposedHostComponent {}
@Component({
template: `<svg lucideCircleCheck [aria-hidden]="hidden()" [attr.aria-label]="label()"></svg>`,
imports: [LucideCircleCheck],
})
class BoundAriaHiddenHostComponent {
readonly hidden = signal<boolean | string | null | undefined>(null);
readonly label = signal<string | null>('Done');
}
describe('LucideIconBase', () => {
let component: LucideCircleCheck;
let fixture: ComponentFixture<LucideCircleCheck>;
@@ -222,6 +274,85 @@ describe('LucideIconBase', () => {
});
});
describe('accessibility attributes', () => {
const renderSvg = <T>(host: new () => T) => {
const hostFixture = TestBed.createComponent(host);
hostFixture.detectChanges();
return { hostFixture, svg: hostFixture.nativeElement.querySelector('svg') as SVGElement };
};
it('should not set aria-hidden when an aria attribute is present', () => {
const { svg } = renderSvg(AriaLabelHostComponent);
expect(svg.getAttribute('aria-label')).toBe('Done');
expect(svg.getAttribute('aria-hidden')).toBe('false');
});
it('should not set aria-hidden when a role attribute is present', () => {
const { svg } = renderSvg(RoleHostComponent);
expect(svg.getAttribute('aria-hidden')).toBe('false');
});
it('should preserve explicit aria-hidden even when an accessible label or title is present', async () => {
const { hostFixture, svg } = renderSvg(ExplicitlyHiddenHostComponent);
expect(svg.getAttribute('aria-label')).toBe('Done');
expect(svg.getAttribute('aria-hidden')).toBe('true');
hostFixture.componentInstance.title.set('Done');
await hostFixture.whenStable();
expect(svg.getAttribute('aria-hidden')).toBe('true');
hostFixture.componentInstance.title.set(null);
await hostFixture.whenStable();
expect(svg.getAttribute('aria-hidden')).toBe('true');
});
it('should preserve explicit aria-hidden="false" without an accessible label', () => {
const { svg } = renderSvg(ExplicitlyExposedHostComponent);
expect(svg.getAttribute('aria-hidden')).toBe('false');
});
it('should follow an aria-hidden input binding and restore automatic behavior when unset', async () => {
const { hostFixture, svg } = renderSvg(BoundAriaHiddenHostComponent);
expect(svg.getAttribute('aria-hidden')).toBe('false');
for (const hidden of [true, false, 'true', 'false', undefined, null]) {
hostFixture.componentInstance.hidden.set(hidden);
await hostFixture.whenStable();
expect(svg.getAttribute('aria-hidden')).toBe(String(hidden ?? false));
}
hostFixture.componentInstance.label.set(null);
await hostFixture.whenStable();
expect(svg.getAttribute('aria-hidden')).toBe('true');
hostFixture.componentInstance.hidden.set(false);
await hostFixture.whenStable();
expect(svg.getAttribute('aria-hidden')).toBe('false');
});
it('should follow a bound aria attribute', async () => {
const { hostFixture, svg } = renderSvg(BoundAriaLabelHostComponent);
expect(svg.getAttribute('aria-hidden')).toBe('true');
hostFixture.componentInstance.label.set('Done');
await hostFixture.whenStable();
expect(svg.getAttribute('aria-hidden')).toBe('false');
hostFixture.componentInstance.label.set(null);
await hostFixture.whenStable();
expect(svg.getAttribute('aria-hidden')).toBe('true');
});
it('should follow a non-signal aria binding when the host is marked for check', async () => {
const { hostFixture, svg } = renderSvg(BoundPlainAriaDescriptionHostComponent);
const changeDetectorRef = hostFixture.debugElement.injector.get(ChangeDetectorRef);
expect(svg.getAttribute('aria-hidden')).toBe('true');
hostFixture.componentInstance.description = 'Done';
changeDetectorRef.markForCheck();
await hostFixture.whenStable();
expect(svg.getAttribute('aria-description')).toBe('Done');
expect(svg.getAttribute('aria-hidden')).toBe('false');
hostFixture.componentInstance.description = null;
changeDetectorRef.markForCheck();
await hostFixture.whenStable();
expect(svg.getAttribute('aria-description')).toBeNull();
expect(svg.getAttribute('aria-hidden')).toBe('true');
});
});
describe('LUCIDE_CONFIG', () => {
beforeEach(async () => {
TestBed.resetTestingModule();
+27 -6
View File
@@ -1,9 +1,10 @@
import { Component, computed, inject, input, Signal } from '@angular/core';
import { Component, computed, ElementRef, inject, input, Signal } from '@angular/core';
import { LUCIDE_CONFIG } from './lucide-config';
import { LucideIconData, LucideIconNode, Nullable } from './types';
import defaultAttributes from './default-attributes';
import { lucideIconTemplate } from './lucide-icon-template';
import buildLucideIconNode from './utils/buildLucideIconNode';
import { hasA11yProp } from './utils/hasA11yProp';
/**
* @internal
@@ -25,10 +26,13 @@ import buildLucideIconNode from './utils/buildLucideIconNode';
export abstract class LucideIconBase {
protected abstract readonly icon: Signal<Nullable<LucideIconData>>;
protected readonly iconConfig = inject(LUCIDE_CONFIG);
private readonly elementRef = inject<ElementRef<SVGElement>>(ElementRef);
/**
* An optional accessible label for the icon.
* - If provided, it will add the title as an [`<svg:title>` element](https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/title).
* - If not provided, the component will add an `aria-hidden="true"` attribute automatically.
* - If not provided, and the element has no `aria-*`, `role` or `title` attribute, the component will add an `aria-hidden="true"` attribute automatically.
* - An explicit `aria-hidden` attribute takes precedence over this automatic behavior.
*
* @remarks
* Please refer to our [Accessibility guide](https://lucide.dev/guide/advanced/accessibility) regarding this matter.
@@ -38,6 +42,10 @@ export abstract class LucideIconBase {
* - If your icon is functional (e.g. used in place of a label), feel free to use this property.
*/
readonly title = input<Nullable<string>>();
/**
* Overrides automatic accessibility visibility. Null or undefined restores automatic behavior.
*/
readonly ariaHidden = input<Nullable<boolean | string>>(undefined, { alias: 'aria-hidden' });
/**
* Optional CSS classes for the icon.
*/
@@ -100,7 +108,6 @@ export abstract class LucideIconBase {
absoluteStrokeWidth: this.absoluteStrokeWidth(),
nonScalingStroke: this.nonScalingStroke(),
className: this.class() ?? undefined,
hasA11yProp: this.title() != null,
})
: null;
});
@@ -148,9 +155,23 @@ export abstract class LucideIconBase {
/**
* @internal
*/
protected readonly computedAriaHidden = computed(
() => this.computedAttributes()?.['aria-hidden'] === 'true',
);
protected computedAriaHidden(): boolean | string {
return this.ariaHidden() ?? (this.title() == null && !this.hasA11yAttribute());
}
/**
* Whether the host element has an `aria-*`, `role` or `title` attribute.
* Read from the DOM on every host binding check, since attributes set by the parent are not signals.
* `aria-hidden` is skipped because this component sets it itself.
*
* @internal
*/
private hasA11yAttribute(): boolean {
const names = this.elementRef.nativeElement
.getAttributeNames()
.filter((name) => name !== 'aria-hidden');
return hasA11yProp(Object.fromEntries(names.map((name) => [name, true])));
}
/**
* @internal