From f184aaf4ecf9bf22c14acfc59491f406176912d6 Mon Sep 17 00:00:00 2001 From: Brandy Smith <6577830+brandyscarney@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:32:32 -0500 Subject: [PATCH 1/4] feat(accordion-group): add headingLevel property Add a headingLevel property to ion-accordion-group so each accordion header in the group is exposed to assistive technology as a heading that contains the toggle button, following the WAI-ARIA accordion pattern. Previously there was no way to get this structure, so screen reader users could not move between accordions using heading navigation. The level applies to every accordion in the group. Nested groups set their own level and do not inherit one. Values that are not a whole number from 1 to 6 do not expose a heading and log a warning. Closes #31621 --- core/api.txt | 1 + core/src/components.d.ts | 14 + .../accordion-group/accordion-group.tsx | 50 +++ core/src/components/accordion-group/utils.ts | 10 + core/src/components/accordion/accordion.tsx | 34 +- .../test/heading-level/accordion.e2e.ts | 373 ++++++++++++++++++ .../angular/src/lazy/directives/proxies.ts | 4 +- .../directives/ion-accordion-group.ts | 4 +- packages/vue/src/proxies.ts | 7 +- 9 files changed, 490 insertions(+), 7 deletions(-) create mode 100644 core/src/components/accordion-group/utils.ts create mode 100644 core/src/components/accordion/test/heading-level/accordion.e2e.ts 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..8d1d6790275 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 that 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 that 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..d780fd46b34 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,15 @@ 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 that 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 +83,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 +192,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 +220,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.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..1ed8bdfbd36 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; @@ -138,10 +149,20 @@ export class Accordion implements ComponentInterface { @Prop() toggleIconSlot: 'start' | 'end' = 'end'; connectedCallback() { + const previousGroupEl = this.accordionGroupEl; const accordionGroupEl = (this.accordionGroupEl = this.el?.closest('ion-accordion-group')); if (accordionGroupEl) { this.updateState(); addEventListener(accordionGroupEl, 'ionValueChange', this.accordionGroupUpdateHandler); + addEventListener(accordionGroupEl, 'ionHeadingLevelChange', this.accordionGroupHeadingLevelHandler); + } + + /** + * An accordion moved into a different group, or out of a group, + * re-renders so its header uses the heading level of its new group. + */ + if (previousGroupEl !== accordionGroupEl) { + forceUpdate(this); } } @@ -149,6 +170,7 @@ export class Accordion implements ComponentInterface { const accordionGroupEl = this.accordionGroupEl; if (accordionGroupEl) { removeEventListener(accordionGroupEl, 'ionValueChange', this.accordionGroupUpdateHandler); + removeEventListener(accordionGroupEl, 'ionHeadingLevelChange', this.accordionGroupHeadingLevelHandler); } } @@ -495,6 +517,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 +550,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..aec2f5d4f90 --- /dev/null +++ b/core/src/components/accordion/test/heading-level/accordion.e2e.ts @@ -0,0 +1,373 @@ +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 }) => { + 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 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 Date: Tue, 1 Dec 2026 12:30:58 -0500 Subject: [PATCH 2/4] test(accordion-group): add spec tests for heading level validation --- .../components/accordion-group/utils.spec.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 core/src/components/accordion-group/utils.spec.ts 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); + }); +}); From aa814404ebde7adf877e28db5d612e203b5d681d Mon Sep 17 00:00:00 2001 From: Brandy Smith <6577830+brandyscarney@users.noreply.github.com> Date: Tue, 1 Dec 2026 13:20:16 -0500 Subject: [PATCH 3/4] fix(accordion): use the current heading level when reattached to a group An accordion that was removed from its group, and then added back to the same group after the group's heading level changed, kept the old level. The accordion now re-renders every time it connects, so its header always uses the current heading level of its group. Also annotates the heading level test with the issue it covers. --- core/src/components/accordion/accordion.tsx | 11 +++-- .../test/heading-level/accordion.e2e.ts | 41 ++++++++++++++++++- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/core/src/components/accordion/accordion.tsx b/core/src/components/accordion/accordion.tsx index 1ed8bdfbd36..e6a73f18c69 100644 --- a/core/src/components/accordion/accordion.tsx +++ b/core/src/components/accordion/accordion.tsx @@ -149,7 +149,6 @@ export class Accordion implements ComponentInterface { @Prop() toggleIconSlot: 'start' | 'end' = 'end'; connectedCallback() { - const previousGroupEl = this.accordionGroupEl; const accordionGroupEl = (this.accordionGroupEl = this.el?.closest('ion-accordion-group')); if (accordionGroupEl) { this.updateState(); @@ -158,12 +157,12 @@ export class Accordion implements ComponentInterface { } /** - * An accordion moved into a different group, or out of a group, - * re-renders so its header uses the heading level of its new group. + * 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. */ - if (previousGroupEl !== accordionGroupEl) { - forceUpdate(this); - } + forceUpdate(this); } disconnectedCallback() { diff --git a/core/src/components/accordion/test/heading-level/accordion.e2e.ts b/core/src/components/accordion/test/heading-level/accordion.e2e.ts index aec2f5d4f90..6d50a362c86 100644 --- a/core/src/components/accordion/test/heading-level/accordion.e2e.ts +++ b/core/src/components/accordion/test/heading-level/accordion.e2e.ts @@ -7,7 +7,12 @@ import { configs, test } from '@utils/test/playwright'; */ 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 }) => { + 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( ` @@ -335,6 +340,40 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ config, title }) => { 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( ` From 6594f56275a1fd98d28ac73c6c00d3a44c9960bf Mon Sep 17 00:00:00 2001 From: Brandy Smith <6577830+brandyscarney@users.noreply.github.com> Date: Tue, 1 Dec 2026 13:20:16 -0500 Subject: [PATCH 4/4] docs(accordion-group): clarify the heading contains the button with an ion-item --- core/src/components.d.ts | 4 ++-- core/src/components/accordion-group/accordion-group.tsx | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/components.d.ts b/core/src/components.d.ts index 8d1d6790275..dee06ade4e3 100644 --- a/core/src/components.d.ts +++ b/core/src/components.d.ts @@ -129,7 +129,7 @@ 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 that contains the button used to expand or collapse the accordion. This lets screen reader users navigate between accordions using heading navigation. + * 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; /** @@ -5252,7 +5252,7 @@ declare namespace LocalJSX { */ "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 that contains the button used to expand or collapse the accordion. This lets screen reader users navigate between accordions using heading navigation. + * 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; /** diff --git a/core/src/components/accordion-group/accordion-group.tsx b/core/src/components/accordion-group/accordion-group.tsx index d780fd46b34..63df9124171 100644 --- a/core/src/components/accordion-group/accordion-group.tsx +++ b/core/src/components/accordion-group/accordion-group.tsx @@ -62,9 +62,10 @@ export class AccordionGroup implements ComponentInterface { /** * The heading level of each accordion header in the group. * When set, each header is exposed to assistive technology as a - * heading that contains the button used to expand or collapse - * the accordion. This lets screen reader users navigate between - * accordions using heading navigation. + * 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;