From 8cd4f293f21a4201395a00657497eb7a69d6dcad Mon Sep 17 00:00:00 2001 From: Eric Fennis Date: Tue, 1 Dec 2026 12:56:09 +0100 Subject: [PATCH] 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 --- docs/guide/angular/advanced/accessibility.md | 8 +- docs/guide/astro/advanced/accessibility.md | 8 +- docs/guide/preact/advanced/accessibility.md | 8 +- docs/guide/react/advanced/accessibility.md | 8 +- docs/guide/solid/advanced/accessibility.md | 8 +- docs/guide/svelte/advanced/accessibility.md | 8 +- docs/guide/vue/advanced/accessibility.md | 8 +- packages/angular/package.json | 2 +- packages/angular/src/lucide-icon-base.spec.ts | 131 ++++++++++++++++++ packages/angular/src/lucide-icon-base.ts | 33 ++++- packages/astro/tests/Icon.spec.ts | 44 ++++++ packages/lucide-preact/src/Icon.ts | 2 +- packages/lucide-preact/tests/Icon.spec.tsx | 39 +++++- packages/lucide-react-native/src/Icon.ts | 2 +- .../lucide-react-native/tests/Icon.spec.tsx | 69 +++++++++ .../lucide-react-native.spec.tsx.snap | 2 + packages/lucide-react/src/Icon.ts | 2 +- packages/lucide-react/tests/Icon.spec.tsx | 26 +++- packages/lucide-solid/tests/Icon.spec.tsx | 16 +++ packages/lucide/tests/lucide.spec.ts | 32 +++++ packages/solid/tests/Icon.spec.tsx | 16 +++ packages/svelte/src/Icon.svelte | 4 +- packages/svelte/tests/Icon.spec.ts | 17 ++- packages/svelte/tests/TestSlots.svelte | 4 +- packages/svelte/tests/TestSlotsTitle.svelte | 6 +- .../__snapshots__/lucide-svelte.spec.ts.snap | 1 + packages/svelte/tests/lucide-svelte.spec.ts | 19 ++- packages/vue/src/Icon.ts | 2 +- packages/vue/tests/Icon.spec.ts | 63 +++++++++ .../__snapshots__/lucide-vue.spec.ts.snap | 1 + packages/vue/tests/lucide-vue.spec.ts | 34 +++++ 31 files changed, 570 insertions(+), 53 deletions(-) diff --git a/docs/guide/angular/advanced/accessibility.md b/docs/guide/angular/advanced/accessibility.md index bd370726a..9903f995d 100644 --- a/docs/guide/angular/advanced/accessibility.md +++ b/docs/guide/angular/advanced/accessibility.md @@ -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 + + + + ``` Choose a label that clearly describes the meaning of the icon or the action it represents in the context of your application. diff --git a/docs/guide/astro/advanced/accessibility.md b/docs/guide/astro/advanced/accessibility.md index 4b13d4645..877dcfc42 100644 --- a/docs/guide/astro/advanced/accessibility.md +++ b/docs/guide/astro/advanced/accessibility.md @@ -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 - - This is my house + + This is my house // or diff --git a/docs/guide/preact/advanced/accessibility.md b/docs/guide/preact/advanced/accessibility.md index 18ee27981..7ea3889de 100644 --- a/docs/guide/preact/advanced/accessibility.md +++ b/docs/guide/preact/advanced/accessibility.md @@ -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 - - This is my house + + This is my house // or diff --git a/docs/guide/react/advanced/accessibility.md b/docs/guide/react/advanced/accessibility.md index e42436021..d12495647 100644 --- a/docs/guide/react/advanced/accessibility.md +++ b/docs/guide/react/advanced/accessibility.md @@ -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 - - This is my house + + This is my house // or diff --git a/docs/guide/solid/advanced/accessibility.md b/docs/guide/solid/advanced/accessibility.md index 39a224f55..c3ddce1ec 100644 --- a/docs/guide/solid/advanced/accessibility.md +++ b/docs/guide/solid/advanced/accessibility.md @@ -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 - - This is my house + + This is my house // or diff --git a/docs/guide/svelte/advanced/accessibility.md b/docs/guide/svelte/advanced/accessibility.md index 73747093b..d4da76671 100644 --- a/docs/guide/svelte/advanced/accessibility.md +++ b/docs/guide/svelte/advanced/accessibility.md @@ -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 - - This is my house + + This is my house // or diff --git a/docs/guide/vue/advanced/accessibility.md b/docs/guide/vue/advanced/accessibility.md index e7808bb13..c17e23c30 100644 --- a/docs/guide/vue/advanced/accessibility.md +++ b/docs/guide/vue/advanced/accessibility.md @@ -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 - - This is my house + + This is my house // or diff --git a/packages/angular/package.json b/packages/angular/package.json index 4dbfa75df..2287923e3 100644 --- a/packages/angular/package.json +++ b/packages/angular/package.json @@ -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", diff --git a/packages/angular/src/lucide-icon-base.spec.ts b/packages/angular/src/lucide-icon-base.spec.ts index f5df57065..68dc7b063 100644 --- a/packages/angular/src/lucide-icon-base.spec.ts +++ b/packages/angular/src/lucide-icon-base.spec.ts @@ -1,5 +1,6 @@ import { ChangeDetectionStrategy, + ChangeDetectorRef, Component, inputBinding, signal, @@ -39,6 +40,57 @@ export class LucideCircleCheck extends LucideIconBase { }) class ClassHostComponent {} +@Component({ + template: ``, + imports: [LucideCircleCheck], +}) +class AriaLabelHostComponent {} + +@Component({ + template: ``, + imports: [LucideCircleCheck], +}) +class RoleHostComponent {} + +@Component({ + template: ``, + imports: [LucideCircleCheck], +}) +class BoundAriaLabelHostComponent { + readonly label = signal(null); +} + +@Component({ + template: ``, + imports: [LucideCircleCheck], +}) +class BoundPlainAriaDescriptionHostComponent { + description: string | null = null; +} + +@Component({ + template: ``, + imports: [LucideCircleCheck], +}) +class ExplicitlyHiddenHostComponent { + readonly title = signal(null); +} + +@Component({ + template: ``, + imports: [LucideCircleCheck], +}) +class ExplicitlyExposedHostComponent {} + +@Component({ + template: ``, + imports: [LucideCircleCheck], +}) +class BoundAriaHiddenHostComponent { + readonly hidden = signal(null); + readonly label = signal('Done'); +} + describe('LucideIconBase', () => { let component: LucideCircleCheck; let fixture: ComponentFixture; @@ -222,6 +274,85 @@ describe('LucideIconBase', () => { }); }); + describe('accessibility attributes', () => { + const renderSvg = (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(); diff --git a/packages/angular/src/lucide-icon-base.ts b/packages/angular/src/lucide-icon-base.ts index 624307076..e31ec6e9c 100644 --- a/packages/angular/src/lucide-icon-base.ts +++ b/packages/angular/src/lucide-icon-base.ts @@ -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>; protected readonly iconConfig = inject(LUCIDE_CONFIG); + private readonly elementRef = inject>(ElementRef); + /** * An optional accessible label for the icon. * - If provided, it will add the title as an [`` 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>(); + /** + * Overrides automatic accessibility visibility. Null or undefined restores automatic behavior. + */ + readonly ariaHidden = input>(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 diff --git a/packages/astro/tests/Icon.spec.ts b/packages/astro/tests/Icon.spec.ts index af2ce9ebb..97789ff40 100644 --- a/packages/astro/tests/Icon.spec.ts +++ b/packages/astro/tests/Icon.spec.ts @@ -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( + 'Warning: Action cannot be undone', + ), + }, + }); + + 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('Some 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'); + }); }); diff --git a/packages/lucide-preact/src/Icon.ts b/packages/lucide-preact/src/Icon.ts index 989797227..c98314504 100644 --- a/packages/lucide-preact/src/Icon.ts +++ b/packages/lucide-preact/src/Icon.ts @@ -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, }); diff --git a/packages/lucide-preact/tests/Icon.spec.tsx b/packages/lucide-preact/tests/Icon.spec.tsx index a78874888..e4f5e2a7a 100644 --- a/packages/lucide-preact/tests/Icon.spec.tsx +++ b/packages/lucide-preact/tests/Icon.spec.tsx @@ -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( + , + ); + + expect(container.firstChild).toHaveAttribute('aria-hidden', 'true'); + }); + it('should not have aria-hidden prop when aria prop is present', async () => { const { container } = render( { expect(container.firstChild).not.toHaveAttribute('aria-hidden'); }); - it('should not have aria-hidden prop when there are children that could be a 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 + , + ); + + 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( { , ); - expect(container.firstChild).not.toHaveAttribute('aria-hidden'); + expect(container.firstChild).toHaveAttribute('aria-hidden', 'true'); }); it('should never override aria-hidden prop', async () => { diff --git a/packages/lucide-react-native/src/Icon.ts b/packages/lucide-react-native/src/Icon.ts index f9ac4c8f6..92b978460 100644 --- a/packages/lucide-react-native/src/Icon.ts +++ b/packages/lucide-react-native/src/Icon.ts @@ -82,7 +82,7 @@ const Icon = forwardRef( className: contextClass = '', } = useLucideContext() ?? {}; - const hasAccessibleProp = Boolean(children) || hasA11yProp(rest); + const hasAccessibleProp = hasA11yProp(rest); const [, svgAttributes, builtIconNode = []] = buildLucideIconForReact(icon, { color: (color ?? contextColor) as string, diff --git a/packages/lucide-react-native/tests/Icon.spec.tsx b/packages/lucide-react-native/tests/Icon.spec.tsx index c4baa08d1..632422e83 100644 --- a/packages/lucide-react-native/tests/Icon.spec.tsx +++ b/packages/lucide-react-native/tests/Icon.spec.tsx @@ -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( + , + ); + + expect(container.firstChild).toHaveAttribute('aria-hidden', 'true'); + }); + + it('should not have aria-hidden prop when aria prop is present', async () => { + const { container } = render( + , + ); + + 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( + , + ); + + expect(container.firstChild).not.toHaveAttribute('aria-hidden'); + }); + + it('should keep aria-hidden prop when there are only children', async () => { + const { container, getByTestId } = render( + + + , + ); + + expect(container.firstChild).toHaveAttribute('aria-hidden', 'true'); + expect(getByTestId('child')).toBeInTheDocument(); + }); + + it('should never override aria-hidden prop', async () => { + const { container } = render( + , + ); + + expect(container.firstChild).toHaveAttribute('aria-hidden', 'false'); + }); +}); diff --git a/packages/lucide-react-native/tests/__snapshots__/lucide-react-native.spec.tsx.snap b/packages/lucide-react-native/tests/__snapshots__/lucide-react-native.spec.tsx.snap index bd64bb3d3..7d0be4299 100644 --- a/packages/lucide-react-native/tests/__snapshots__/lucide-react-native.spec.tsx.snap +++ b/packages/lucide-react-native/tests/__snapshots__/lucide-react-native.spec.tsx.snap @@ -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" > 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" > ( className: contextClass = '', } = useLucideContext() ?? {}; - const hasAccessibleProp = Boolean(children) || hasA11yProp(rest); + const hasAccessibleProp = hasA11yProp(rest); const [name, svgAttributes, builtIconNode = []] = buildLucideIconForReact(icon, { color: color ?? contextColor, diff --git a/packages/lucide-react/tests/Icon.spec.tsx b/packages/lucide-react/tests/Icon.spec.tsx index 612b58e7d..3ab578c00 100644 --- a/packages/lucide-react/tests/Icon.spec.tsx +++ b/packages/lucide-react/tests/Icon.spec.tsx @@ -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 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 + , + ); + + 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( { , ); - expect(container.firstChild).not.toHaveAttribute('aria-hidden'); + expect(container.firstChild).toHaveAttribute('aria-hidden', 'true'); }); it('should never override aria-hidden prop', async () => { diff --git a/packages/lucide-solid/tests/Icon.spec.tsx b/packages/lucide-solid/tests/Icon.spec.tsx index d84125b84..9d8a788dd 100644 --- a/packages/lucide-solid/tests/Icon.spec.tsx +++ b/packages/lucide-solid/tests/Icon.spec.tsx @@ -144,6 +144,22 @@ describe('Icon Component Accessibility', () => { ); }); + it('should keep aria-hidden prop when there are only children', async () => { + const { container } = render(() => ( + + Some title + + )); + + 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(() => ( diff --git a/packages/lucide/tests/lucide.spec.ts b/packages/lucide/tests/lucide.spec.ts index 005e6ad96..79d6bc730 100644 --- a/packages/lucide/tests/lucide.spec.ts +++ b/packages/lucide/tests/lucide.spec.ts @@ -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 = ``; + + 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 = ``; + + 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 = ``; + + 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 = ``; diff --git a/packages/solid/tests/Icon.spec.tsx b/packages/solid/tests/Icon.spec.tsx index 9eaa4c36e..a8e12330f 100644 --- a/packages/solid/tests/Icon.spec.tsx +++ b/packages/solid/tests/Icon.spec.tsx @@ -145,6 +145,22 @@ describe('Icon Component Accessibility', () => { ); }); + it('should keep aria-hidden prop when there are only children', async () => { + const { container } = render(() => ( + + Some title + + )); + + 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(() => ( diff --git a/packages/svelte/src/Icon.svelte b/packages/svelte/src/Icon.svelte index 724ec0c50..707983990 100644 --- a/packages/svelte/src/Icon.svelte +++ b/packages/svelte/src/Icon.svelte @@ -26,9 +26,7 @@ ...props }: IconProps = $props(); - const hasAccessibleProp = $derived( - Boolean(children) || hasA11yProp(props as Record), - ); + const hasAccessibleProp = $derived(hasA11yProp(props as Record)); const [, svgAttributes, builtIconNode = []] = $derived( buildLucideIconNode( diff --git a/packages/svelte/tests/Icon.spec.ts b/packages/svelte/tests/Icon.spec.ts index 52d56dce6..8e8207f33 100644 --- a/packages/svelte/tests/Icon.spec.ts +++ b/packages/svelte/tests/Icon.spec.ts @@ -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 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 () => { diff --git a/packages/svelte/tests/TestSlots.svelte b/packages/svelte/tests/TestSlots.svelte index 0e5d345d2..da0e5fc3f 100644 --- a/packages/svelte/tests/TestSlots.svelte +++ b/packages/svelte/tests/TestSlots.svelte @@ -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}> diff --git a/packages/svelte/tests/TestSlotsTitle.svelte b/packages/svelte/tests/TestSlotsTitle.svelte index a04922d3e..09849f0c9 100644 --- a/packages/svelte/tests/TestSlotsTitle.svelte +++ b/packages/svelte/tests/TestSlotsTitle.svelte @@ -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 + Warning: Action cannot be undone diff --git a/packages/svelte/tests/__snapshots__/lucide-svelte.spec.ts.snap b/packages/svelte/tests/__snapshots__/lucide-svelte.spec.ts.snap index f4ac12359..6bf402bd8 100644 --- a/packages/svelte/tests/__snapshots__/lucide-svelte.spec.ts.snap +++ b/packages/svelte/tests/__snapshots__/lucide-svelte.spec.ts.snap @@ -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`] = `