Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): expose the active tab and panel names to screen readers

Elian Doran351722b83281
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts
index f63a30d0a2..4e37e20f40 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts
@@ -106,6 +106,29 @@ describe("Tabs", () => {
expect(activeTitles()).toEqual(["Linux"]);
});
+ it("exposes the active tab and each panel's title to assistive technology", () => {
+ editor.setData(TWO_TABS);
+ const root = editor.editing.view.getDomRoot();
+ const titles = [...root?.querySelectorAll(".trilium-tab-title") ?? []];
+ const panels = [...root?.querySelectorAll(".trilium-tab-panel") ?? []];
+ expect(titles).toHaveLength(2);
+ expect(panels).toHaveLength(2);
+
+ expect(titles.map(title => title.getAttribute("aria-current"))).toEqual(["true", null]);
+ for (const [index, title] of titles.entries()) {
+ const panel = panels[index];
+ expect(panel.id).not.toBe("");
+ expect(title.getAttribute("aria-controls")).toBe(panel.id);
+ expect(panel.getAttribute("aria-labelledby")).toBe(title.id);
+ expect(root?.querySelector(`#${title.id}`)?.textContent).toBe(["Windows", "Linux"][index]);
+ }
+ expect(new Set([...titles, ...panels].map(element => element.id)).size).toBe(4);
+
+ const linux = tabsElement().getChild(1) as ModelElement;
+ editor.model.change(writer => writer.setSelection(linux.getChild(1) as ModelElement, 0));
+ expect(titles.map(title => title.getAttribute("aria-current"))).toEqual([null, "true"]);
+ });
+
it("adds, moves and removes the tab holding the selection", () => {
editor.setData(TWO_TABS);
const windows = tabsElement().getChild(0) as ModelElement;
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts b/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts
index 8c3f063d35..2f31f7ecfe 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts
@@ -10,6 +10,7 @@ import {
toWidgetEditable,
type ViewDocumentEnterEvent,
type ViewEditableElement,
+ uid,
Widget
} from "ckeditor5";
@@ -45,6 +46,9 @@ export default class TabsEditing extends Plugin {
/** The tab each tabs block shows, keyed by the `tabs` model element. */
private readonly activeTabs = new WeakMap<ModelElement, ModelElement>();
+ /** The editing-view ID prefix of each tab's title and panel, keyed by the `tab` model element. */
+ private readonly tabIds = new WeakMap<ModelElement, string>();
+
public init(): void {
const editor = this.editor;
editor.commands.add("tabs", new InsertTabsCommand(editor));
@@ -73,6 +77,20 @@ export default class TabsEditing extends Plugin {
return first?.is("element", ELEMENTS.tab) ? first : null;
}
+ private isActiveTab(tab: ModelElement) {
+ const tabs = tab.parent;
+ return !!tabs?.is("element", ELEMENTS.tabs) && this.getActiveTab(tabs) === tab;
+ }
+
+ private getTabId(tab: ModelElement) {
+ let id = this.tabIds.get(tab);
+ if (!id) {
+ id = `trilium-tab-${uid()}`;
+ this.tabIds.set(tab, id);
+ }
+ return id;
+ }
+
private registerSchema() {
const schema = this.editor.model.schema;
schema.register(ELEMENTS.tabs, {
@@ -150,17 +168,24 @@ export default class TabsEditing extends Plugin {
conversion.for("editingDowncast").elementToElement({
model: ELEMENTS.tab,
view: (model, { writer }) => {
- const tabs = model.parent;
- const isActive = !!tabs?.is("element", ELEMENTS.tabs) && this.getActiveTab(tabs) === model;
- const classes = isActive ? [CLASSES.tab, CLASSES.activeTab] : [CLASSES.tab];
+ const classes = this.isActiveTab(model) ? [CLASSES.tab, CLASSES.activeTab] : [CLASSES.tab];
return writer.createContainerElement("section", { class: classes.join(" ") });
}
});
conversion.for("editingDowncast").elementToElement({
model: ELEMENTS.tabTitle,
- view: (_model, { writer }) => {
+ view: (model, { writer }) => {
+ // The title keeps its textbox role so that screen readers offer to edit it; the
+ // global `aria-current` and `aria-controls` attributes carry the tab semantics.
+ const tab = model.parent as ModelElement;
+ const id = this.getTabId(tab);
const title: ViewEditableElement & PlaceholderableViewElement =
- writer.createEditableElement("div", { class: CLASSES.tabTitle });
+ writer.createEditableElement("div", {
+ class: CLASSES.tabTitle,
+ id: `${id}-title`,
+ "aria-controls": `${id}-panel`,
+ ...(this.isActiveTab(tab) ? { "aria-current": "true" } : {})
+ });
title.placeholder = t("Tab title");
enableViewPlaceholder({
view: editor.editing.view,
@@ -172,8 +197,15 @@ export default class TabsEditing extends Plugin {
});
conversion.for("editingDowncast").elementToElement({
model: ELEMENTS.tabPanel,
- view: (_model, { writer }) => {
- const panel = writer.createEditableElement("div", { class: CLASSES.tabPanel });
+ view: (model, { writer }) => {
+ // `aria-labelledby` names the panel after its title; an empty title falls back to
+ // the `aria-label`.
+ const id = this.getTabId(model.parent as ModelElement);
+ const panel = writer.createEditableElement("div", {
+ class: CLASSES.tabPanel,
+ id: `${id}-panel`,
+ "aria-labelledby": `${id}-title`
+ });
return toWidgetEditable(panel, writer, { label: t("Tab content") });
}
});
@@ -245,15 +277,18 @@ export default class TabsEditing extends Plugin {
continue;
}
const active = this.getActiveTab(tabs);
- for (const tab of tabs.getChildren()) {
- const view = editing.mapper.toViewElement(tab as ModelElement);
- if (!view) {
+ for (const tab of tabs.getChildren() as IterableIterator<ModelElement>) {
+ const view = editing.mapper.toViewElement(tab);
+ const title = editing.mapper.toViewElement(tab.getChild(0) as ModelElement);
+ if (!view || !title) {
continue;
}
if (tab === active) {
writer.addClass(CLASSES.activeTab, view);
+ writer.setAttribute("aria-current", "true", title);
} else {
writer.removeClass(CLASSES.activeTab, view);
+ writer.removeAttribute("aria-current", title);
}
}
}