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
+6 -2
View File
@@ -23,12 +23,16 @@ Only if an icon **conveys essential meaning on its own** should it be made acces
## Making an Icon Accessible
To expose an icon to assistive technologies, provide an accessible name by binding the `title` input of the icon component.
To expose an icon to assistive technologies, provide an accessible name by binding the `title` input of the icon component, or by setting an `aria-label` attribute on it.
This removes the `aria-hidden` attribute and makes the icon visible to screen readers.
Setting the `title` input, or any `aria-*` or `role` attribute, stops the icon from being hidden from screen readers.
```angular-html
<svg lucideHouse title="This is my house" />
<!-- or -->
<svg lucideHouse aria-label="This is my house" />
```
Choose a label that clearly describes the meaning of the icon or the action it represents in the context of your application.
+4 -4
View File
@@ -23,13 +23,13 @@ Only if an icon **conveys essential meaning on its own** should it be made acces
## Making an icon accessible
To expose an icon to assistive technologies, provide an accessible name by passing a `title` element as a child or passing the `aria-label` prop to the icon component.
To expose an icon to assistive technologies, provide an accessible name by passing the `aria-label` prop to the icon component, or by passing a `title` element as a child and referencing it with `role="img"` and `aria-labelledby`.
This removes the `aria-hidden` attribute and makes the icon visible to screen readers.
Passing an `aria-*`, `role` or `title` prop removes the `aria-hidden` attribute and makes the icon visible to screen readers. A `title` child on its own does not: without those props the icon stays hidden.
```astro
<House>
<title>This is my house</title>
<House role="img" aria-labelledby="house-title">
<title id="house-title">This is my house</title>
</House>
// or
+4 -4
View File
@@ -23,13 +23,13 @@ Only if an icon **conveys essential meaning on its own** should it be made acces
## Making an icon accessible
To expose an icon to assistive technologies, provide an accessible name by passing a `title` element as a child or passing the `aria-label` prop to the icon component.
To expose an icon to assistive technologies, provide an accessible name by passing the `aria-label` prop to the icon component, or by passing a `title` element as a child and referencing it with `role="img"` and `aria-labelledby`.
This removes the `aria-hidden` attribute and makes the icon visible to screen readers.
Passing an `aria-*`, `role` or `title` prop removes the `aria-hidden` attribute and makes the icon visible to screen readers. A `title` child on its own does not: without those props the icon stays hidden.
```tsx
<House>
<title>This is my house</title>
<House role="img" aria-labelledby="house-title">
<title id="house-title">This is my house</title>
</House>
// or
+4 -4
View File
@@ -22,13 +22,13 @@ Only if an icon **conveys essential meaning on its own** should it be made acces
## Making an icon accessible
To expose an icon to assistive technologies, provide an accessible name by passing a `title` element as a child or passing the `aria-label` prop to the icon component.
To expose an icon to assistive technologies, provide an accessible name by passing the `aria-label` prop to the icon component, or by passing a `title` element as a child and referencing it with `role="img"` and `aria-labelledby`.
This removes the `aria-hidden` attribute and makes the icon visible to screen readers.
Passing an `aria-*`, `role` or `title` prop removes the `aria-hidden` attribute and makes the icon visible to screen readers. A `title` child on its own does not: without those props the icon stays hidden.
```tsx
<House>
<title>This is my house</title>
<House role="img" aria-labelledby="house-title">
<title id="house-title">This is my house</title>
</House>
// or
+4 -4
View File
@@ -22,13 +22,13 @@ Only if an icon **conveys essential meaning on its own** should it be made acces
## Making an icon accessible
To expose an icon to assistive technologies, provide an accessible name by passing a `title` element as a child or passing the `aria-label` prop to the icon component.
To expose an icon to assistive technologies, provide an accessible name by passing the `aria-label` prop to the icon component, or by passing a `title` element as a child and referencing it with `role="img"` and `aria-labelledby`.
This removes the `aria-hidden` attribute and makes the icon visible to screen readers.
Passing an `aria-*`, `role` or `title` prop removes the `aria-hidden` attribute and makes the icon visible to screen readers. A `title` child on its own does not: without those props the icon stays hidden.
```tsx
<House>
<title>This is my house</title>
<House role="img" aria-labelledby="house-title">
<title id="house-title">This is my house</title>
</House>
// or
+4 -4
View File
@@ -23,13 +23,13 @@ Only if an icon **conveys essential meaning on its own** should it be made acces
## Making an icon accessible
To expose an icon to assistive technologies, provide an accessible name by passing a `title` element as a child or passing the `aria-label` prop to the icon component.
To expose an icon to assistive technologies, provide an accessible name by passing the `aria-label` prop to the icon component, or by passing a `title` element as a child and referencing it with `role="img"` and `aria-labelledby`.
This removes the `aria-hidden` attribute and makes the icon visible to screen readers.
Passing an `aria-*`, `role` or `title` prop removes the `aria-hidden` attribute and makes the icon visible to screen readers. A `title` child on its own does not: without those props the icon stays hidden.
```svelte
<House>
<title>This is my house</title>
<House role="img" aria-labelledby="house-title">
<title id="house-title">This is my house</title>
</House>
// or
+4 -4
View File
@@ -23,13 +23,13 @@ Only if an icon **conveys essential meaning on its own** should it be made acces
## Making an icon accessible
To expose an icon to assistive technologies, provide an accessible name by passing a `title` element as a child or passing the `aria-label` prop to the icon component.
To expose an icon to assistive technologies, provide an accessible name by passing the `aria-label` prop to the icon component, or by passing a `title` element as a child and referencing it with `role="img"` and `aria-labelledby`.
This removes the `aria-hidden` attribute and makes the icon visible to screen readers.
Passing an `aria-*`, `role` or `title` prop removes the `aria-hidden` attribute and makes the icon visible to screen readers. A `title` child on its own does not: without those props the icon stays hidden.
```vue
<House>
<title>This is my house</title>
<House role="img" aria-labelledby="house-title">
<title id="house-title">This is my house</title>
</House>
// or
+1 -1
View File
@@ -20,7 +20,7 @@
"prebuild": "pnpm clean && pnpm copy:license && pnpm copy:utils && pnpm build:icons",
"build": "pnpm prebuild && pnpm build:ng",
"copy:license": "cp ../../LICENSE ./LICENSE",
"copy:utils": "cpy ../../packages/shared/src/build/{buildLucideIconNode.ts,defaultAttributes.ts,types.ts} ../../packages/shared/src/utils/mergeClasses.ts ./src/utils/",
"copy:utils": "cpy ../../packages/shared/src/build/{buildLucideIconNode.ts,defaultAttributes.ts,types.ts} ../../packages/shared/src/utils/{hasA11yProp.ts,mergeClasses.ts} ./src/utils/",
"clean": "rm -rf dist && rm -rf ./src/icons/*.ts",
"build:icons": "build-icons --output=./src --templateSrc=./scripts/exportTemplate.mts --renderUniqueKey --iconFileExtension=.ts --exportFileName=lucide-angular.ts --useDefaultExports=0",
"build:ng": "ng build --configuration production",
@@ -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
+44
View File
@@ -56,4 +56,48 @@ describe('Icon Component Accessibility', () => {
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
});
it('should render a labelled title child and not have aria-hidden prop', async () => {
const { container } = await render(Rocket, {
props: {
role: 'img',
'aria-labelledby': 'warn-title',
},
slots: {
default: createAstroHTMLString(
'<title id="warn-title">Warning: Action cannot be undone</title>',
),
},
});
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
expect(container.firstChild).toHaveAttribute('role', 'img');
expect(container.firstChild).toHaveAttribute('aria-labelledby', 'warn-title');
expect(container.querySelector('title#warn-title')).toHaveTextContent(
'Warning: Action cannot be undone',
);
});
it('should keep aria-hidden prop when there are only children', async () => {
const { container } = await render(Rocket, {
slots: {
default: createAstroHTMLString('<title>Some title</title>'),
},
});
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
});
it('should never override aria-hidden prop', async () => {
const { container } = await render(Rocket, {
props: {
size: 48,
stroke: 'red',
absoluteStrokeWidth: true,
'aria-hidden': 'false',
},
});
expect(container.firstChild).toHaveAttribute('aria-hidden', 'false');
});
});
+1 -1
View File
@@ -70,7 +70,7 @@ const Icon = ({
absoluteStrokeWidth: absoluteStrokeWidth ?? contextAbsoluteStrokeWidth,
nonScalingStroke: nonScalingStroke ?? contextNonScalingStroke,
className: mergeClasses(contextClass, resolveClassName(classes), resolveClassName(className)),
hasA11yProp: Boolean(children) || hasA11yProp(rest),
hasA11yProp: hasA11yProp(rest),
attributes: rest,
});
+37 -2
View File
@@ -33,6 +33,19 @@ describe('Using Icon Component', () => {
});
describe('Icon Component Accessibility', () => {
it('should have aria-hidden prop when no aria prop is present', async () => {
const { container } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
absoluteStrokeWidth
/>,
);
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
});
it('should not have aria-hidden prop when aria prop is present', async () => {
const { container } = render(
<Icon
@@ -61,7 +74,29 @@ describe('Icon Component Accessibility', () => {
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
});
it('should not have aria-hidden prop when there are children that could be a <title> element', async () => {
it('should render a labelled title child and not have aria-hidden prop', async () => {
const { container } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
absoluteStrokeWidth
role="img"
aria-labelledby="warn-title"
>
<title id="warn-title">Warning: Action cannot be undone</title>
</Icon>,
);
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
expect(container.firstChild).toHaveAttribute('role', 'img');
expect(container.firstChild).toHaveAttribute('aria-labelledby', 'warn-title');
expect(container.querySelector('title#warn-title')).toHaveTextContent(
'Warning: Action cannot be undone',
);
});
it('should keep aria-hidden prop when there are only children', async () => {
const { container } = render(
<Icon
iconNode={airVent}
@@ -73,7 +108,7 @@ describe('Icon Component Accessibility', () => {
</Icon>,
);
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
});
it('should never override aria-hidden prop', async () => {
+1 -1
View File
@@ -82,7 +82,7 @@ const Icon = forwardRef<SVGSVGElement, IconComponentProps>(
className: contextClass = '',
} = useLucideContext() ?? {};
const hasAccessibleProp = Boolean(children) || hasA11yProp(rest);
const hasAccessibleProp = hasA11yProp(rest);
const [, svgAttributes, builtIconNode = []] = buildLucideIconForReact(icon, {
color: (color ?? contextColor) as string,
@@ -122,3 +122,72 @@ describe('Using Icon Component', () => {
}
});
});
describe('Icon Component Accessibility', () => {
it('should have aria-hidden prop when no aria prop is present', async () => {
const { container } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
/>,
);
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
});
it('should not have aria-hidden prop when aria prop is present', async () => {
const { container } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
aria-label="Air conditioning"
/>,
);
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
expect(container.firstChild).toHaveAttribute('aria-label', 'Air conditioning');
});
it('should not have aria-hidden prop when title prop is present', async () => {
const { container } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
title="Air conditioning"
/>,
);
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
});
it('should keep aria-hidden prop when there are only children', async () => {
const { container, getByTestId } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
>
<rect data-testid="child" />
</Icon>,
);
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
expect(getByTestId('child')).toBeInTheDocument();
});
it('should never override aria-hidden prop', async () => {
const { container } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
aria-hidden={false}
/>,
);
expect(container.firstChild).toHaveAttribute('aria-hidden', 'false');
});
});
@@ -319,6 +319,7 @@ exports[`Using lucide icon components > should work with a single child componen
stroke-linecap="round"
stroke-linejoin="round"
class="lucide lucide-grid-3x3 lucide-grid lucide-grid-3-x-3"
aria-hidden="true"
data-testid="single-child"
>
<rect fill="none"
@@ -437,6 +438,7 @@ exports[`Using lucide icon components > should work with several children compon
stroke-linecap="round"
stroke-linejoin="round"
class="lucide lucide-grid-3x3 lucide-grid lucide-grid-3-x-3"
aria-hidden="true"
data-testid="multiple-children"
>
<rect fill="none"
+1 -1
View File
@@ -64,7 +64,7 @@ const Icon = forwardRef<SVGSVGElement, IconComponentProps>(
className: contextClass = '',
} = useLucideContext() ?? {};
const hasAccessibleProp = Boolean(children) || hasA11yProp(rest);
const hasAccessibleProp = hasA11yProp(rest);
const [name, svgAttributes, builtIconNode = []] = buildLucideIconForReact(icon, {
color: color ?? contextColor,
+24 -2
View File
@@ -75,7 +75,29 @@ describe('Icon Component Accessibility', () => {
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
});
it('should not have aria-hidden prop when there are children that could be a <title> element', async () => {
it('should render a labelled title child and not have aria-hidden prop', async () => {
const { container } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
absoluteStrokeWidth
role="img"
aria-labelledby="warn-title"
>
<title id="warn-title">Warning: Action cannot be undone</title>
</Icon>,
);
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
expect(container.firstChild).toHaveAttribute('role', 'img');
expect(container.firstChild).toHaveAttribute('aria-labelledby', 'warn-title');
expect(container.querySelector('title#warn-title')).toHaveTextContent(
'Warning: Action cannot be undone',
);
});
it('should keep aria-hidden prop when there are only children', async () => {
const { container } = render(
<Icon
iconNode={airVent}
@@ -87,7 +109,7 @@ describe('Icon Component Accessibility', () => {
</Icon>,
);
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
});
it('should never override aria-hidden prop', async () => {
+16
View File
@@ -144,6 +144,22 @@ describe('Icon Component Accessibility', () => {
);
});
it('should keep aria-hidden prop when there are only children', async () => {
const { container } = render(() => (
<Icon
iconNode={airVent}
size={48}
stroke="red"
absoluteStrokeWidth
>
<title>Some title</title>
</Icon>
));
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
expect(container.querySelector('title')).toHaveTextContent('Some title');
});
it('should update reactive children', async () => {
const [showTitle, setShowTitle] = createSignal(false);
const { container } = render(() => (
+32
View File
@@ -111,6 +111,38 @@ describe('createIcons', () => {
expect(element).toHaveAttribute('aria-label', 'Volume');
});
it('should not add aria-hidden attribute when a title attribute is present', () => {
document.body.innerHTML = `<i data-lucide="volume-2" class="lucide" title="Volume"></i>`;
createIcons({ icons });
const element = document.querySelector('svg') as SVGSVGElement;
expect(element).not.toHaveAttribute('aria-hidden');
expect(element).toHaveAttribute('title', 'Volume');
});
it('should not add aria-hidden attribute when a role attribute is present', () => {
document.body.innerHTML = `<i data-lucide="volume-2" class="lucide" role="img"></i>`;
createIcons({ icons });
const element = document.querySelector('svg') as SVGSVGElement;
expect(element).not.toHaveAttribute('aria-hidden');
expect(element).toHaveAttribute('role', 'img');
});
it('should never override aria-hidden attribute', () => {
document.body.innerHTML = `<i data-lucide="volume-2" class="lucide" aria-hidden="false"></i>`;
createIcons({ icons });
const element = document.querySelector('svg') as SVGSVGElement;
expect(element).toHaveAttribute('aria-hidden', 'false');
});
it('should not replace icons inside template elements by default', () => {
document.body.innerHTML = `<template><i data-lucide="house"></i></template>`;
+16
View File
@@ -145,6 +145,22 @@ describe('Icon Component Accessibility', () => {
);
});
it('should keep aria-hidden prop when there are only children', async () => {
const { container } = render(() => (
<Icon
iconNode={airVent}
size={48}
stroke="red"
absoluteStrokeWidth
>
<title>Some title</title>
</Icon>
));
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
expect(container.querySelector('title')).toHaveTextContent('Some title');
});
it('should update reactive children', async () => {
const [showTitle, setShowTitle] = createSignal(false);
const { container } = render(() => (
+1 -3
View File
@@ -26,9 +26,7 @@
...props
}: IconProps = $props();
const hasAccessibleProp = $derived(
Boolean(children) || hasA11yProp(props as Record<string, any>),
);
const hasAccessibleProp = $derived(hasA11yProp(props as Record<string, any>));
const [, svgAttributes, builtIconNode = []] = $derived(
buildLucideIconNode(
+14 -3
View File
@@ -75,18 +75,29 @@ describe('Icon Component Accessibility', () => {
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
});
it('should not have aria-hidden prop when there are children that could be a <title> element', async () => {
it('should render a labelled title child and not have aria-hidden prop', async () => {
const { container } = render(TestSlotsTitle, {
props: {
iconNode: airVent,
size: 48,
color: 'red',
absoluteStrokeWidth: true,
'aria-label': 'Air conditioning',
role: 'img',
'aria-labelledby': 'warn-title',
},
});
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
expect(container.firstChild).toHaveAttribute('role', 'img');
expect(container.firstChild).toHaveAttribute('aria-labelledby', 'warn-title');
expect(container.querySelector('title#warn-title')).toHaveTextContent(
'Warning: Action cannot be undone',
);
});
it('should keep aria-hidden prop when there are only children', async () => {
const { container } = render(TestSlotsTitle);
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
});
it('should override aria-hidden prop', async () => {
+2 -2
View File
@@ -1,8 +1,8 @@
<script lang="ts">
import FaceSlightlySmiling from '../src/icons/face-slightly-smiling.svelte';
import type { IconProps } from '../src/types.js';
import type { LucideProps } from '../src/types.js';
const props: IconProps = $props();
const props: LucideProps = $props();
</script>
<FaceSlightlySmiling {...props}>
+3 -3
View File
@@ -1,10 +1,10 @@
<script lang="ts">
import FaceSlightlySmiling from '../src/icons/face-slightly-smiling.svelte';
import type { IconProps } from '../src/types.js';
import type { LucideProps } from '../src/types.js';
const props: IconProps = $props();
const props: LucideProps = $props();
</script>
<FaceSlightlySmiling {...props}>
<title>Air conditioning</title>
<title id="warn-title">Warning: Action cannot be undone</title>
</FaceSlightlySmiling>
@@ -190,6 +190,7 @@ exports[`Using lucide icon components > should render an component 1`] = `
exports[`Using lucide icon components > should render an icon slot 1`] = `
<div>
<svg
aria-hidden="true"
class="lucide lucide-face-slightly-smiling lucide-smile lucide-icon"
fill="none"
height="24"
+18 -1
View File
@@ -3,6 +3,7 @@ import { render, cleanup } from '@testing-library/svelte';
import { Smile, Pen, Edit2 } from '../src/lucide-svelte.js';
import { __iconData, __iconNode } from '../src/icons/face-slightly-smiling.svelte';
import TestSlots from './TestSlots.svelte';
import TestSlotsTitle from './TestSlotsTitle.svelte';
import ContextWrapper from './ContextWrapper.svelte';
describe('Using lucide icon components', () => {
@@ -127,10 +128,26 @@ describe('Icon Component Accessibility', () => {
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
});
it('should not have aria-hidden prop when there are children that could be a <title> element', async () => {
it('should keep aria-hidden prop when there are only children', async () => {
const { container } = render(TestSlots);
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
});
it('should render a labelled title child and not have aria-hidden prop', async () => {
const { container } = render(TestSlotsTitle, {
props: {
role: 'img',
'aria-labelledby': 'warn-title',
},
});
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
expect(container.firstChild).toHaveAttribute('role', 'img');
expect(container.firstChild).toHaveAttribute('aria-labelledby', 'warn-title');
expect(container.querySelector('title#warn-title')).toHaveTextContent(
'Warning: Action cannot be undone',
);
});
});
+1 -1
View File
@@ -82,7 +82,7 @@ const Icon: FunctionalComponent<LucideProps & IconProps> = (
absoluteStrokeWidth: isAbsoluteStrokeWidth,
nonScalingStroke: isNonScalingStroke,
className: contextClass,
hasA11yProp: (defaultSlot != null && defaultSlot.length > 0) || hasA11yProp(props),
hasA11yProp: hasA11yProp(props),
attributes: props,
});
+63
View File
@@ -70,3 +70,66 @@ describe('Using Icon Component', () => {
expect(container.firstChild).toHaveAttribute('height', '48');
});
});
describe('Icon Component Accessibility', () => {
it('should have aria-hidden prop when no aria prop is present', async () => {
const { container } = render(Icon, {
props: {
iconNode: airVent,
name: 'AirVent',
size: 48,
color: 'red',
absoluteStrokeWidth: true,
},
});
expect(container.firstChild).toHaveAttribute('aria-hidden', 'true');
});
it('should not have aria-hidden prop when aria prop is present', async () => {
const { container } = render(Icon, {
props: {
iconNode: airVent,
name: 'AirVent',
size: 48,
color: 'red',
absoluteStrokeWidth: true,
'aria-label': 'Air conditioning',
},
});
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
expect(container.firstChild).toHaveAttribute('aria-label', 'Air conditioning');
});
it('should not have aria-hidden prop when title prop is present', async () => {
const { container } = render(Icon, {
props: {
iconNode: airVent,
name: 'AirVent',
size: 48,
color: 'red',
absoluteStrokeWidth: true,
// @ts-expect-error
title: 'Air conditioning',
},
});
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
});
it('should never override aria-hidden prop', async () => {
const { container } = render(Icon, {
props: {
iconNode: airVent,
name: 'AirVent',
size: 48,
color: 'red',
absoluteStrokeWidth: true,
'aria-hidden': 'false',
},
});
expect(container.firstChild).toHaveAttribute('aria-hidden', 'false');
});
});
@@ -103,6 +103,7 @@ exports[`Using lucide icon components > should adjust the size, stroke color and
exports[`Using lucide icon components > should pass children to the icon slot 1`] = `
<div>
<svg
aria-hidden="true"
class="lucide lucide-face-slightly-smiling lucide-smile"
fill="none"
height="24"
+34
View File
@@ -110,6 +110,40 @@ describe('Using lucide icon components', () => {
expect(container).toMatchSnapshot();
});
it('should keep aria-hidden prop when there is only slot content', () => {
const { container } = render(Smile, {
slots: {
default: { name: 'Stub', template: '<title>Some title</title>' },
},
});
expect(container.firstElementChild).toHaveAttribute('aria-hidden', 'true');
});
it('should render a labelled title slot and not have aria-hidden prop', () => {
const { container } = render(Smile, {
attrs: {
role: 'img',
'aria-labelledby': 'warn-title',
},
slots: {
default: {
name: 'Stub',
template: '<title id="warn-title">Warning: Action cannot be undone</title>',
},
},
});
const icon = container.firstElementChild;
expect(icon).not.toHaveAttribute('aria-hidden');
expect(icon).toHaveAttribute('role', 'img');
expect(icon).toHaveAttribute('aria-labelledby', 'warn-title');
expect(container.querySelector('title#warn-title')).toHaveTextContent(
'Warning: Action cannot be undone',
);
});
it('should handle non-extensible slots objects', () => {
const Icon = createLucideIcon('test-icon', [['path', { d: 'M0 0h1', key: 'test-key' }]]);
const slots = Object.freeze({