Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export namespace Components {
*/
"expand": 'compact' | 'inset';
"getAccordions": () => Promise<HTMLIonAccordionElement[]>;
/**
* 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.
*/
Expand Down Expand Up @@ -3900,6 +3904,7 @@ declare global {
interface HTMLIonAccordionGroupElementEventMap {
"ionChange": AccordionGroupChangeEventDetail;
"ionValueChange": AccordionGroupChangeEventDetail;
"ionHeadingLevelChange": void;
}
interface HTMLIonAccordionGroupElement extends Components.IonAccordionGroup, HTMLStencilElement {
addEventListener<K extends keyof HTMLIonAccordionGroupElementEventMap>(type: K, listener: (this: HTMLIonAccordionGroupElement, ev: IonAccordionGroupCustomEvent<HTMLIonAccordionGroupElementEventMap[K]>) => any, options?: boolean | AddEventListenerOptions): void;
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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<AccordionGroupChangeEventDetail>) => 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>) => void;
/**
* Emitted when the value property has changed. This is used to ensure that ion-accordion can respond to any value property changes.
*/
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 51 additions & 0 deletions core/src/components/accordion-group/accordion-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand All @@ -73,6 +84,16 @@ export class AccordionGroup implements ComponentInterface {
*/
@Event() ionValueChange!: EventEmitter<AccordionGroupChangeEventDetail>;

/**
* 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<void>;

@Watch('value')
valueChanged() {
const { value, multiple } = this;
Expand Down Expand Up @@ -172,13 +193,43 @@ 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();
}
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
Expand Down
27 changes: 27 additions & 0 deletions core/src/components/accordion-group/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 10 additions & 0 deletions core/src/components/accordion-group/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
};
33 changes: 32 additions & 1 deletion core/src/components/accordion/accordion.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 (
Expand All @@ -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)}
>
Expand Down
Loading
Loading