Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessibility in accordion panel #537

Merged
merged 5 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions packages/widgets/src/accordionlayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ export namespace AccordionLayout {
* @returns A element representing the section title.
*/
createSectionTitle(title: Title<Widget>): HTMLElement;

/**
* Ensures the existence of the widget ID.
* It is useful to link the widget node to the 'aria-controls' attribute of the title.
*
* @param widget - The widget concerned.
*/
addWidgetId(widget: Widget): void;
}
}

Expand Down
15 changes: 14 additions & 1 deletion packages/widgets/src/accordionpanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class AccordionPanel extends SplitPanel {
* If the widget is already contained in the panel, it will be moved.
*/
addWidget(widget: Widget): void {
this.renderer.addWidgetId(widget);
super.addWidget(widget);
widget.title.changed.connect(this._onTitleChanged, this);
}
Expand Down Expand Up @@ -401,7 +402,6 @@ export namespace AccordionPanel {
*/
createSectionTitle(data: Title<Widget>): HTMLElement {
const handle = document.createElement('h3');
handle.setAttribute('role', 'tab');
handle.setAttribute('tabindex', '0');
handle.id = this.createTitleKey(data);
handle.className = this.titleClassName;
Expand Down Expand Up @@ -440,9 +440,22 @@ export namespace AccordionPanel {
return key;
}

/**
* Ensures the existence of the widget ID.
* It is useful to link the widget node to the 'aria-controls' attribute of the title.
*
* @param widget - The widget concerned.
*/
addWidgetId(widget: Widget): void {
if (!widget.id) {
widget.id = `accordion-item-${this._uuid}-${this._widgetID++}`;
}
}

private static _nInstance = 0;
private readonly _uuid: number;
private _titleID = 0;
private _widgetID = 0;
private _titleKeys = new WeakMap<Title<Widget>, string>();
}

Expand Down
3 changes: 2 additions & 1 deletion packages/widgets/tests/src/accordionlayout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { expect } from 'chai';
const renderer: AccordionLayout.IRenderer = {
titleClassName: '.lm-AccordionTitle',
createHandle: () => document.createElement('div'),
createSectionTitle: (title: Title<Widget>) => document.createElement('h3')
createSectionTitle: (title: Title<Widget>) => document.createElement('h3'),
addWidgetId: (widget: Widget) => (widget.id = 'accordion-test-id')
};

class LogAccordionLayout extends AccordionLayout {
Expand Down
3 changes: 2 additions & 1 deletion packages/widgets/tests/src/accordionpanel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const bubbles = true;
const renderer: AccordionPanel.IRenderer = {
titleClassName: '.lm-AccordionTitle',
createHandle: () => document.createElement('div'),
createSectionTitle: (title: Title<Widget>) => document.createElement('h3')
createSectionTitle: (title: Title<Widget>) => document.createElement('h3'),
addWidgetId: (widget: Widget) => (widget.id = 'accordion-test-id')
};

class LogAccordionPanel extends AccordionPanel {
Expand Down