Skip to content

Commit 098da4b

Browse files
authored
fix(web): make article editor controls keyboard accessible
Merge pull request #15 from devashish10079/fix/13-keyboard-editor-controls
2 parents 57fabc5 + d139688 commit 098da4b

5 files changed

Lines changed: 97 additions & 12 deletions

File tree

‎web/e2e/article-editor.spec.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
import { loginAsAdmin } from "./helpers";
4+
5+
test.beforeEach(async ({ page }) => {
6+
await loginAsAdmin(page);
7+
await page.goto("/article/editor");
8+
});
9+
10+
test("toggles the article summary with Enter and Space", async ({ page }) => {
11+
const summary = page.getByRole("button", { name: /^(Summary|文章摘要)$/ });
12+
const content = page.getByPlaceholder(/Enter article summary|请输入文章摘要/);
13+
14+
await expect(summary).toHaveAttribute("aria-expanded", "true");
15+
await summary.press("Enter");
16+
await expect(summary).toHaveAttribute("aria-expanded", "false");
17+
await expect(summary).toBeFocused();
18+
await expect(content).toBeHidden();
19+
20+
await summary.press("Space");
21+
await expect(summary).toHaveAttribute("aria-expanded", "true");
22+
await expect(content).toBeVisible();
23+
});
24+
25+
test("moves between editor toolbar buttons and activates them with the keyboard", async ({
26+
page,
27+
}) => {
28+
const split = page.getByRole("button", { name: /^(Split|分屏)$/ });
29+
const toolbar = page.locator("header").filter({ has: split });
30+
const edit = toolbar.getByRole("button", { name: /^(Edit|编辑)$/ });
31+
const preview = toolbar.getByRole("button", { name: /^(Preview|预览)$/ });
32+
const outline = toolbar.getByRole("button", { name: /^(Outline|大纲)$/ });
33+
34+
await expect(split).toHaveAttribute("aria-pressed", "true");
35+
await edit.press("Enter");
36+
await expect(edit).toHaveAttribute("aria-pressed", "true");
37+
await page.keyboard.press("Tab");
38+
await expect(split).toBeFocused();
39+
await page.keyboard.press("Tab");
40+
await expect(preview).toBeFocused();
41+
await page.keyboard.press("Space");
42+
await expect(preview).toHaveAttribute("aria-pressed", "true");
43+
44+
await outline.press("Enter");
45+
await expect(outline).toHaveAttribute("aria-pressed", "false");
46+
await outline.press("Space");
47+
await expect(outline).toHaveAttribute("aria-pressed", "true");
48+
await page.keyboard.press("Tab");
49+
await expect(toolbar.getByRole("button", { name: /^(Fullscreen|全屏)$/ })).toBeFocused();
50+
});

‎web/src/modules/article/components/EditorMetaPanel.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ export function EditorMetaPanel({ title, defaultOpen = true, children }: EditorM
1414

1515
return (
1616
<section className={styles.panel}>
17-
<header className={styles.header} onClick={() => setOpen((v) => !v)}>
17+
<button
18+
type="button"
19+
className={styles.header}
20+
aria-expanded={open}
21+
onClick={() => setOpen((v) => !v)}
22+
>
1823
<span className={styles.title}>{title}</span>
1924
<span className={styles.toggle} aria-hidden>
2025
{open ? <ChevronUp size={16} /> : <ChevronDown size={16} />}
2126
</span>
22-
</header>
27+
</button>
2328
{open ? <div className={styles.body}>{children}</div> : null}
2429
</section>
2530
);

‎web/src/modules/article/components/editor-meta-panel.module.css‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@
1212
display: flex;
1313
align-items: center;
1414
justify-content: space-between;
15+
width: 100%;
1516
padding: 10px 12px;
1617
background: var(--editor-surface-muted);
18+
border: 0;
1719
border-bottom: 1px solid var(--editor-border-secondary);
20+
font: inherit;
21+
text-align: start;
1822
cursor: pointer;
1923
user-select: none;
2024
}
2125

26+
.header:focus-visible {
27+
outline: 2px solid var(--editor-accent);
28+
outline-offset: -2px;
29+
}
30+
2231
.title {
2332
font-size: 13px;
2433
font-weight: 600;

‎web/src/shared/components/Editor/editor.module.css‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585

8686
.toolbarAction {
8787
padding: 0 10px;
88+
border: 0;
89+
background: transparent;
90+
font-family: inherit;
8891
font-size: 12px;
8992
line-height: 28px;
9093
white-space: nowrap;
@@ -98,6 +101,11 @@
98101
background: var(--editor-hover-bg);
99102
}
100103

104+
.toolbarAction:focus-visible {
105+
outline: 2px solid var(--editor-accent);
106+
outline-offset: -2px;
107+
}
108+
101109
.toolbarActionActive {
102110
background: var(--editor-active-bg);
103111
font-weight: 600;

‎web/src/shared/components/Editor/index.tsx‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ export function MarkdownEditor({
223223
</span>
224224
</div>
225225
<div className={styles.toolbarRight}>
226-
<span
226+
<button
227+
type="button"
228+
aria-pressed={mode === "edit" && !split}
227229
className={cls(
228230
styles.toolbarAction,
229231
mode === "edit" && !split && styles.toolbarActionActive,
@@ -235,8 +237,10 @@ export function MarkdownEditor({
235237
>
236238
<PanelLeft size={14} style={{ verticalAlign: -2, marginRight: 4 }} />
237239
{t("editor.modeEdit")}
238-
</span>
239-
<span
240+
</button>
241+
<button
242+
type="button"
243+
aria-pressed={split}
240244
className={cls(styles.toolbarAction, split && styles.toolbarActionActive)}
241245
onClick={() => {
242246
setSplit(true);
@@ -245,8 +249,10 @@ export function MarkdownEditor({
245249
>
246250
<Columns2 size={14} style={{ verticalAlign: -2, marginRight: 4 }} />
247251
{t("editor.modeSplit")}
248-
</span>
249-
<span
252+
</button>
253+
<button
254+
type="button"
255+
aria-pressed={mode === "preview" && !split}
250256
className={cls(
251257
styles.toolbarAction,
252258
mode === "preview" && !split && styles.toolbarActionActive,
@@ -258,15 +264,22 @@ export function MarkdownEditor({
258264
>
259265
<Eye size={14} style={{ verticalAlign: -2, marginRight: 4 }} />
260266
{t("editor.modePreview")}
261-
</span>
267+
</button>
262268
<Divider type="vertical" />
263-
<span className={styles.toolbarAction} onClick={() => toggleTocVisible()}>
269+
<button
270+
type="button"
271+
className={styles.toolbarAction}
272+
aria-pressed={tocVisible}
273+
onClick={() => toggleTocVisible()}
274+
>
264275
<ListTree size={14} style={{ verticalAlign: -2, marginRight: 4 }} />
265276
{t("editor.toc")}
266-
</span>
277+
</button>
267278
<Divider type="vertical" />
268279
<Tooltip title={fullscreen ? t("editor.exitFullscreen") : t("editor.fullscreen")}>
269-
<span
280+
<button
281+
type="button"
282+
aria-label={fullscreen ? t("editor.exitFullscreen") : t("editor.fullscreen")}
270283
className={cls(styles.toolbarAction, fullscreen && styles.toolbarActionActive)}
271284
onClick={() => void toggleFullscreen()}
272285
>
@@ -275,7 +288,7 @@ export function MarkdownEditor({
275288
) : (
276289
<Maximize2 size={14} style={{ verticalAlign: -2 }} />
277290
)}
278-
</span>
291+
</button>
279292
</Tooltip>
280293
</div>
281294
</header>

0 commit comments

Comments
 (0)