diff --git a/core/api.txt b/core/api.txt index ac5705a32f4..c05dedcbac5 100644 --- a/core/api.txt +++ b/core/api.txt @@ -14,6 +14,7 @@ ion-accordion-group,shadow ion-accordion-group,prop,animated,boolean,true,false,false ion-accordion-group,prop,disabled,boolean,false,false,false ion-accordion-group,prop,expand,"compact" | "inset",'compact',false,false +ion-accordion-group,prop,headingLevel,1 | 2 | 3 | 4 | 5 | 6 | undefined,undefined,false,false ion-accordion-group,prop,mode,"ios" | "md",undefined,false,false ion-accordion-group,prop,multiple,boolean | undefined,undefined,false,false ion-accordion-group,prop,readonly,boolean,false,false,false diff --git a/core/src/components.d.ts b/core/src/components.d.ts index f3221eb9260..dee06ade4e3 100644 --- a/core/src/components.d.ts +++ b/core/src/components.d.ts @@ -128,6 +128,10 @@ export namespace Components { */ "expand": 'compact' | 'inset'; "getAccordions": () => Promise; + /** + * The heading level of each accordion header in the group. When set, each header is exposed to assistive technology as a heading. When the header is an `ion-item`, the heading contains the button used to expand or collapse the accordion. This lets screen reader users navigate between accordions using heading navigation. + */ + "headingLevel"?: 1 | 2 | 3 | 4 | 5 | 6; /** * The mode determines which platform styles to use. */ @@ -3900,6 +3904,7 @@ declare global { interface HTMLIonAccordionGroupElementEventMap { "ionChange": AccordionGroupChangeEventDetail; "ionValueChange": AccordionGroupChangeEventDetail; + "ionHeadingLevelChange": void; } interface HTMLIonAccordionGroupElement extends Components.IonAccordionGroup, HTMLStencilElement { addEventListener(type: K, listener: (this: HTMLIonAccordionGroupElement, ev: IonAccordionGroupCustomEvent) => any, options?: boolean | AddEventListenerOptions): void; @@ -5246,6 +5251,10 @@ declare namespace LocalJSX { * @default 'compact' */ "expand"?: 'compact' | 'inset'; + /** + * The heading level of each accordion header in the group. When set, each header is exposed to assistive technology as a heading. When the header is an `ion-item`, the heading contains the button used to expand or collapse the accordion. This lets screen reader users navigate between accordions using heading navigation. + */ + "headingLevel"?: 1 | 2 | 3 | 4 | 5 | 6; /** * The mode determines which platform styles to use. */ @@ -5258,6 +5267,10 @@ declare namespace LocalJSX { * Emitted when the value property has changed as a result of a user action such as a click. This event will not emit when programmatically setting the `value` property. */ "onIonChange"?: (event: IonAccordionGroupCustomEvent) => void; + /** + * Emitted when the heading level property has changed. This is used to ensure that ion-accordion re-renders its header with the new heading level. It does not bubble so that accordions in an outer group do not respond to a change in a nested group. + */ + "onIonHeadingLevelChange"?: (event: IonAccordionGroupCustomEvent) => void; /** * Emitted when the value property has changed. This is used to ensure that ion-accordion can respond to any value property changes. */ @@ -9012,6 +9025,7 @@ declare namespace LocalJSX { "disabled": boolean; "readonly": boolean; "expand": 'compact' | 'inset'; + "headingLevel": 1 | 2 | 3 | 4 | 5 | 6; } interface IonActionSheetAttributes { "overlayIndex": number; diff --git a/core/src/components/accordion-group/accordion-group.tsx b/core/src/components/accordion-group/accordion-group.tsx index ba5b110c8b6..63df9124171 100644 --- a/core/src/components/accordion-group/accordion-group.tsx +++ b/core/src/components/accordion-group/accordion-group.tsx @@ -5,6 +5,7 @@ import { printIonWarning } from '@utils/logging'; import { getIonMode } from '../../global/ionic-global'; import type { AccordionGroupChangeEventDetail } from './accordion-group-interface'; +import { isValidHeadingLevel } from './utils'; /** * @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use. @@ -58,6 +59,16 @@ export class AccordionGroup implements ComponentInterface { */ @Prop() expand: 'compact' | 'inset' = 'compact'; + /** + * The heading level of each accordion header in the group. + * When set, each header is exposed to assistive technology as a + * heading. When the header is an `ion-item`, the heading contains + * the button used to expand or collapse the accordion. This lets + * screen reader users navigate between accordions using heading + * navigation. + */ + @Prop() headingLevel?: 1 | 2 | 3 | 4 | 5 | 6; + /** * Emitted when the value property has changed as a result of a user action such as a click. * @@ -73,6 +84,16 @@ export class AccordionGroup implements ComponentInterface { */ @Event() ionValueChange!: EventEmitter; + /** + * Emitted when the heading level property has changed. + * This is used to ensure that ion-accordion re-renders its + * header with the new heading level. It does not bubble so + * that accordions in an outer group do not respond to a + * change in a nested group. + * @internal + */ + @Event({ bubbles: false }) ionHeadingLevelChange!: EventEmitter; + @Watch('value') valueChanged() { const { value, multiple } = this; @@ -172,6 +193,27 @@ export class AccordionGroup implements ComponentInterface { } } + @Watch('headingLevel') + headingLevelChanged() { + const { headingLevel } = this; + + /** + * Accordions do not expose their headers as headings when the level + * is invalid. The group logs the warning so it appears once per change. + */ + if (headingLevel != null && !isValidHeadingLevel(headingLevel)) { + printIonWarning( + `[ion-accordion-group] - The heading level must be a whole number from 1 to 6. The accordion headers will not be exposed as headings. + + Heading Level Passed: ${headingLevel} +`, + this.el + ); + } + + this.ionHeadingLevelChange.emit(); + } + async componentDidLoad() { if (this.disabled) { this.disabledChanged(); @@ -179,6 +221,15 @@ export class AccordionGroup implements ComponentInterface { if (this.readonly) { this.readonlyChanged(); } + /** + * An accordion can render before this group is defined, such as + * when the components are registered individually. Notifying + * the accordions once the group has loaded ensures they pick up + * the heading level. + */ + if (this.headingLevel != null) { + this.headingLevelChanged(); + } /** * When binding values in frameworks such as Angular * it is possible for the value to be set after the Web Component diff --git a/core/src/components/accordion-group/utils.spec.ts b/core/src/components/accordion-group/utils.spec.ts new file mode 100644 index 00000000000..29904d37a58 --- /dev/null +++ b/core/src/components/accordion-group/utils.spec.ts @@ -0,0 +1,27 @@ +import { isValidHeadingLevel } from './utils'; + +describe('isValidHeadingLevel()', () => { + it('should return true for whole numbers from 1 to 6', () => { + [1, 2, 3, 4, 5, 6].forEach((level) => expect(isValidHeadingLevel(level)).toBe(true)); + }); + + it('should return false when the heading level is not set', () => { + expect(isValidHeadingLevel(undefined)).toBe(false); + expect(isValidHeadingLevel(null)).toBe(false); + }); + + it('should return false for numbers outside of 1 to 6', () => { + expect(isValidHeadingLevel(0)).toBe(false); + expect(isValidHeadingLevel(7)).toBe(false); + expect(isValidHeadingLevel(-1)).toBe(false); + }); + + it('should return false for decimals and NaN', () => { + expect(isValidHeadingLevel(2.5)).toBe(false); + /** + * A non-numeric attribute value, such as `heading-level="foo"`, + * parses to `NaN`. + */ + expect(isValidHeadingLevel(NaN)).toBe(false); + }); +}); diff --git a/core/src/components/accordion-group/utils.ts b/core/src/components/accordion-group/utils.ts new file mode 100644 index 00000000000..59c4a59a071 --- /dev/null +++ b/core/src/components/accordion-group/utils.ts @@ -0,0 +1,10 @@ +/** + * Returns `true` when the heading level is a whole number from 1 to 6. + * Removing the `heading-level` attribute or binding `null` sets the level + * to `null`, and a non-numeric attribute value parses to `NaN`. A heading + * without a valid `aria-level` is exposed as a level 2 heading, so only + * valid levels expose accordion headers as headings. + */ +export const isValidHeadingLevel = (headingLevel: number | null | undefined): headingLevel is number => { + return headingLevel != null && Number.isInteger(headingLevel) && headingLevel >= 1 && headingLevel <= 6; +}; diff --git a/core/src/components/accordion/accordion.tsx b/core/src/components/accordion/accordion.tsx index 2d4bfc452b3..e6a73f18c69 100644 --- a/core/src/components/accordion/accordion.tsx +++ b/core/src/components/accordion/accordion.tsx @@ -1,10 +1,11 @@ import type { ComponentInterface } from '@stencil/core'; -import { Component, Element, Host, Prop, State, Watch, h } from '@stencil/core'; +import { Component, Element, Host, Prop, State, Watch, forceUpdate, h } from '@stencil/core'; import { addEventListener, getElementRoot, raf, removeEventListener, transitionEndAsync } from '@utils/helpers'; import { chevronDown } from 'ionicons/icons'; import { config } from '../../global/config'; import { getIonMode } from '../../global/ionic-global'; +import { isValidHeadingLevel } from '../accordion-group/utils'; const enum AccordionState { Collapsed = 1 << 0, @@ -72,6 +73,16 @@ export class Accordion implements ComponentInterface { this.updateState(); }; + + /** + * The header reads the heading level from the group when it + * renders, so it needs to re-render when the group's heading + * level changes. + */ + private accordionGroupHeadingLevelHandler = () => { + forceUpdate(this); + }; + private contentEl: HTMLDivElement | undefined; private contentElWrapper: HTMLDivElement | undefined; private headerEl: HTMLDivElement | undefined; @@ -142,13 +153,23 @@ export class Accordion implements ComponentInterface { if (accordionGroupEl) { this.updateState(); addEventListener(accordionGroupEl, 'ionValueChange', this.accordionGroupUpdateHandler); + addEventListener(accordionGroupEl, 'ionHeadingLevelChange', this.accordionGroupHeadingLevelHandler); } + + /** + * The accordion does not receive heading level changes while it is + * disconnected. Re-rendering on every connect ensures its header uses + * the current heading level of its group, or no heading level when + * it is no longer in a group. + */ + forceUpdate(this); } disconnectedCallback() { const accordionGroupEl = this.accordionGroupEl; if (accordionGroupEl) { removeEventListener(accordionGroupEl, 'ionValueChange', this.accordionGroupUpdateHandler); + removeEventListener(accordionGroupEl, 'ionHeadingLevelChange', this.accordionGroupHeadingLevelHandler); } } @@ -495,6 +516,14 @@ export class Accordion implements ComponentInterface { const headerPart = expanded ? 'header expanded' : 'header'; const contentPart = expanded ? 'content expanded' : 'content'; + /** + * When the group sets a valid heading level, the header wrapper is + * exposed as a heading. The slotted item renders a native button + * inside of it, so the heading contains the toggle button. + */ + const groupHeadingLevel = this.accordionGroupEl?.headingLevel; + const headingLevel = isValidHeadingLevel(groupHeadingLevel) ? groupHeadingLevel : undefined; + this.setAria(expanded); return ( @@ -520,6 +549,8 @@ export class Accordion implements ComponentInterface { onClick={() => this.toggleExpanded()} id="header" part={headerPart} + role={headingLevel !== undefined ? 'heading' : undefined} + aria-level={headingLevel} aria-controls="content" ref={(headerEl) => (this.headerEl = headerEl)} > diff --git a/core/src/components/accordion/test/heading-level/accordion.e2e.ts b/core/src/components/accordion/test/heading-level/accordion.e2e.ts new file mode 100644 index 00000000000..6d50a362c86 --- /dev/null +++ b/core/src/components/accordion/test/heading-level/accordion.e2e.ts @@ -0,0 +1,412 @@ +import { expect } from '@playwright/test'; +import { configs, test } from '@utils/test/playwright'; + +/** + * This behavior does not vary across modes or directions. + * These tests query the accessibility tree, which requires a browser. + */ +configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ config, title }) => { + test.describe(title('accordion: heading level'), () => { + test('should expose the header as a heading containing the toggle button', async ({ page }, testInfo) => { + testInfo.annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31621', + }); + + await page.setContent( + ` + + + + First + +
Content
+
+ + + Second + +
Content
+
+
+ `, + config + ); + + const headings = page.getByRole('heading', { level: 3 }); + await expect(headings).toHaveCount(2); + + /** + * The heading takes its accessible name from its contents, so a + * matching name confirms the slotted item and its button render + * inside of the heading. + */ + await expect(headings.nth(0)).toHaveAccessibleName('First'); + await expect(headings.nth(1)).toHaveAccessibleName('Second'); + + await expect(page.getByRole('button', { name: 'First', expanded: false })).toBeVisible(); + await expect(page.getByRole('button', { name: 'Second', expanded: false })).toBeVisible(); + }); + + test('should toggle the accordion from the keyboard when the header is a heading', async ({ + page, + browserName, + }) => { + await page.setContent( + ` + + + + First + +
Content
+
+
+ `, + config + ); + + const tabKey = browserName === 'webkit' ? 'Alt+Tab' : 'Tab'; + const header = page.locator('ion-accordion ion-item[slot="header"]'); + + await page.keyboard.press(tabKey); + await expect(header).toBeFocused(); + + await page.keyboard.press('Enter'); + await page.waitForChanges(); + + await expect(page.getByRole('button', { name: 'First', expanded: true })).toBeVisible(); + await expect(page.getByRole('heading', { level: 3, name: 'First' })).toBeVisible(); + }); + + test('should not expose the header as a heading when the heading level is not set', async ({ page }) => { + await page.setContent( + ` + + + + First + +
Content
+
+
+ `, + config + ); + + await expect(page.getByRole('button', { name: 'First' })).toBeVisible(); + await expect(page.getByRole('heading')).toHaveCount(0); + }); + + test('should update the heading level when it changes on the group', async ({ page }) => { + await page.setContent( + ` + + + + First + +
Content
+
+
+ `, + config + ); + + const accordionGroup = page.locator('ion-accordion-group'); + + await accordionGroup.evaluate((el: HTMLIonAccordionGroupElement) => { + el.headingLevel = 2; + }); + await page.waitForChanges(); + + await expect(page.getByRole('heading', { level: 2, name: 'First' })).toBeVisible(); + + await accordionGroup.evaluate((el: HTMLIonAccordionGroupElement) => { + el.headingLevel = 4; + }); + await page.waitForChanges(); + + await expect(page.getByRole('heading', { level: 4, name: 'First' })).toBeVisible(); + await expect(page.getByRole('heading', { level: 2 })).toHaveCount(0); + + await accordionGroup.evaluate((el: HTMLIonAccordionGroupElement) => { + el.headingLevel = undefined; + }); + await page.waitForChanges(); + + await expect(page.getByRole('heading')).toHaveCount(0); + }); + + test('should not expose the header as a heading when the heading level attribute is removed', async ({ page }) => { + await page.setContent( + ` + + + + First + +
Content
+
+
+ `, + config + ); + + await expect(page.getByRole('heading', { level: 3, name: 'First' })).toBeVisible(); + + /** + * Removing the attribute sets the heading level to `null` + * rather than `undefined`. + */ + await page.locator('ion-accordion-group').evaluate((el: HTMLIonAccordionGroupElement) => { + el.removeAttribute('heading-level'); + }); + await page.waitForChanges(); + + await expect(page.getByRole('button', { name: 'First' })).toBeVisible(); + await expect(page.getByRole('heading')).toHaveCount(0); + }); + + test('should not expose the header as a heading and should warn when the heading level is invalid', async ({ + page, + }) => { + const warnings: string[] = []; + page.on('console', (msg) => { + if (msg.type() === 'warning') { + warnings.push(msg.text()); + } + }); + + await page.setContent( + ` + + + + First + +
Content
+
+
+ `, + config + ); + + await expect(page.getByRole('button', { name: 'First' })).toBeVisible(); + await expect(page.getByRole('heading')).toHaveCount(0); + + expect(warnings.some((warning) => warning.includes('The heading level must be a whole number from 1 to 6'))).toBe( + true + ); + }); + + test('should use the heading level of the closest group for nested accordions', async ({ page }) => { + await page.setContent( + ` + + + + Outer + +
+ + + + Inner + +
Content
+
+
+
+
+
+ `, + config + ); + + await expect(page.getByRole('heading', { level: 2, name: 'Outer' })).toBeVisible(); + await expect(page.getByRole('heading', { level: 3, name: 'Inner' })).toBeVisible(); + }); + + test('should update the heading levels of nested groups independently', async ({ page }) => { + await page.setContent( + ` + + + + Outer + +
+ + + + Inner + +
Content
+
+
+
+
+
+ `, + config + ); + + await page.locator('#outer').evaluate((el: HTMLIonAccordionGroupElement) => { + el.headingLevel = 4; + }); + await page.waitForChanges(); + + await expect(page.getByRole('heading', { level: 4, name: 'Outer' })).toBeVisible(); + await expect(page.getByRole('heading', { level: 3, name: 'Inner' })).toBeVisible(); + + await page.locator('#inner').evaluate((el: HTMLIonAccordionGroupElement) => { + el.headingLevel = 5; + }); + await page.waitForChanges(); + + await expect(page.getByRole('heading', { level: 4, name: 'Outer' })).toBeVisible(); + await expect(page.getByRole('heading', { level: 5, name: 'Inner' })).toBeVisible(); + }); + + test('should not expose the headers of a nested group without a heading level as headings', async ({ page }) => { + await page.setContent( + ` + + + + Outer + +
+ + + + Inner + +
Content
+
+
+
+
+
+ `, + config + ); + + await expect(page.getByRole('heading', { level: 2, name: 'Outer' })).toBeVisible(); + + /** + * A nested group does not inherit the heading level of the + * group that contains it. + */ + await expect(page.getByRole('button', { name: 'Inner' })).toBeVisible(); + await expect(page.getByRole('heading', { name: 'Inner' })).toHaveCount(0); + }); + + test('should use the heading level of the new group when an accordion is moved', async ({ page }) => { + await page.setContent( + ` + + + + First + +
Content
+
+
+ + + `, + config + ); + + await expect(page.getByRole('heading', { level: 3, name: 'First' })).toBeVisible(); + + await page.evaluate(() => { + const accordion = document.querySelector('ion-accordion')!; + document.querySelector('#no-level')!.appendChild(accordion); + }); + await page.waitForChanges(); + + await expect(page.getByRole('button', { name: 'First' })).toBeVisible(); + await expect(page.getByRole('heading')).toHaveCount(0); + + await page.evaluate(() => { + const accordion = document.querySelector('ion-accordion')!; + document.querySelector('#level-four')!.appendChild(accordion); + }); + await page.waitForChanges(); + + await expect(page.getByRole('heading', { level: 4, name: 'First' })).toBeVisible(); + }); + + test('should use the current heading level when an accordion is reattached to the same group', async ({ page }) => { + await page.setContent( + ` + + + + First + +
Content
+
+
+ `, + config + ); + + await expect(page.getByRole('heading', { level: 3, name: 'First' })).toBeVisible(); + + /** + * The heading level changes while the accordion is detached, + * so the accordion only picks it up when it is reattached. + */ + await page.evaluate(() => { + const group = document.querySelector('ion-accordion-group')!; + const accordion = group.querySelector('ion-accordion')!; + accordion.remove(); + group.headingLevel = 4; + group.appendChild(accordion); + }); + await page.waitForChanges(); + + await expect(page.getByRole('heading', { level: 4, name: 'First' })).toBeVisible(); + await expect(page.getByRole('heading', { level: 3 })).toHaveCount(0); + }); + + test('should apply the heading level to accordions added after the group loads', async ({ page }) => { + await page.setContent( + ` + + + + First + +
Content
+
+
+ `, + config + ); + + await expect(page.getByRole('heading', { level: 3, name: 'First' })).toBeVisible(); + + await page.locator('ion-accordion-group').evaluate((el: HTMLIonAccordionGroupElement) => { + el.insertAdjacentHTML( + 'beforeend', + ` + + + Second + +
Content
+
+ ` + ); + }); + await page.waitForChanges(); + + await expect(page.getByRole('heading', { level: 3, name: 'Second' })).toBeVisible(); + }); + }); +}); diff --git a/packages/angular/src/lazy/directives/proxies.ts b/packages/angular/src/lazy/directives/proxies.ts index e8b64405f1f..43b48be4bd8 100644 --- a/packages/angular/src/lazy/directives/proxies.ts +++ b/packages/angular/src/lazy/directives/proxies.ts @@ -31,14 +31,14 @@ export declare interface IonAccordion extends Components.IonAccordion {} @ProxyCmp({ - inputs: ['animated', 'disabled', 'expand', 'mode', 'multiple', 'readonly', 'value'] + inputs: ['animated', 'disabled', 'expand', 'headingLevel', 'mode', 'multiple', 'readonly', 'value'] }) @Component({ selector: 'ion-accordion-group', changeDetection: ChangeDetectionStrategy.OnPush, template: '', // eslint-disable-next-line @angular-eslint/no-inputs-metadata-property - inputs: ['animated', 'disabled', 'expand', 'mode', 'multiple', 'readonly', 'value'], + inputs: ['animated', 'disabled', 'expand', 'headingLevel', 'mode', 'multiple', 'readonly', 'value'], outputs: ['ionChange'], standalone: false }) diff --git a/packages/angular/src/standalone/directives/ion-accordion-group.ts b/packages/angular/src/standalone/directives/ion-accordion-group.ts index f082fe2cf19..21af38a2c21 100644 --- a/packages/angular/src/standalone/directives/ion-accordion-group.ts +++ b/packages/angular/src/standalone/directives/ion-accordion-group.ts @@ -10,14 +10,14 @@ import { defineCustomElement as defineIonAccordionGroup } from '@ionic/core/comp @ProxyCmp({ defineCustomElementFn: defineIonAccordionGroup, - inputs: ['animated', 'disabled', 'expand', 'mode', 'multiple', 'readonly', 'value'] + inputs: ['animated', 'disabled', 'expand', 'headingLevel', 'mode', 'multiple', 'readonly', 'value'] }) @Component({ selector: 'ion-accordion-group', changeDetection: ChangeDetectionStrategy.OnPush, template: '', // eslint-disable-next-line @angular-eslint/no-inputs-metadata-property - inputs: ['animated', 'disabled', 'expand', 'mode', 'multiple', 'readonly', 'value'], + inputs: ['animated', 'disabled', 'expand', 'headingLevel', 'mode', 'multiple', 'readonly', 'value'], outputs: ['ionChange'], }) export class IonAccordionGroup { diff --git a/packages/vue/src/proxies.ts b/packages/vue/src/proxies.ts index 6a5f7b868b1..41b2fe00bf9 100644 --- a/packages/vue/src/proxies.ts +++ b/packages/vue/src/proxies.ts @@ -102,11 +102,14 @@ export const IonAccordionGroup: StencilVueComponent