Skip to content

Commit 7750024

Browse files
heyoubclaude
authored andcommitted
fix(web): give the toolbar semantics and announce silent state changes
The six formatting controls were an unlabelled div of buttons named "B", "I", "Link" — meaningless out of visual context — and six consecutive tab stops between the label and the textarea. They are now a labelled toolbar with one tab stop and arrow-key movement (ARIA APG). Each accessible name is a superset of its visible label so speech input still works. Four places changed state with nothing announced: - ProjectDetail's "Copy link" and "Share to Slack" gave no feedback at all, to anyone — the clipboard write was the entire interaction. They now raise a sonner toast, which the modals this screen already renders use for the same purpose, and surface a failed clipboard write instead of swallowing it. - Sponsor's "Copy email" signals success by renaming itself, and a control's own name changing is not announced. An sr-only live region mirrors it. - ProfileEdit's "Uploading…" appeared and vanished silently; it is now a status region that persists across both states so it can announce. - ConnectGitHubBanner was role="region", which is a landmark: it is only reachable by going looking for it. The banner renders after auth resolves, i.e. after first paint, so it needs role="status" to be heard at all. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 42cffe6 commit 7750024

7 files changed

Lines changed: 160 additions & 23 deletions

File tree

‎apps/web/src/components/ConnectGitHubBanner.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ export function ConnectGitHubBanner() {
3030
if (dismissed) return null;
3131

3232
return (
33+
// role="status", not "region": the banner appears only once auth has
34+
// resolved, so it arrives after first paint and a landmark would never
35+
// announce it. The aria-label stays as its accessible name.
3336
<div
34-
role="region"
37+
role="status"
3538
aria-label="Connect GitHub"
3639
className="border-b border-primary/40 bg-primary/5 print:hidden"
3740
>

‎apps/web/src/components/MarkdownEditor.tsx‎

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useId, useRef, useState } from 'react';
1+
import { useEffect, useId, useRef, useState, type KeyboardEvent } from 'react';
22
import { Label } from '@/components/ui/label';
33
import { Textarea } from '@/components/ui/textarea';
44
import { Button } from '@/components/ui/button';
@@ -20,17 +20,23 @@ interface MarkdownEditorProps {
2020

2121
interface ToolbarButton {
2222
label: string;
23+
/**
24+
* Accessible name. Each one *contains* the visible label so that a
25+
* speech-input user saying what they see still hits the control
26+
* (WCAG 2.5.3 Label in Name).
27+
*/
28+
name: string;
2329
insert: string;
2430
wrap?: { before: string; after: string };
2531
}
2632

2733
const TOOLBAR: ToolbarButton[] = [
28-
{ label: 'B', insert: 'bold text', wrap: { before: '**', after: '**' } },
29-
{ label: 'I', insert: 'italic text', wrap: { before: '_', after: '_' } },
30-
{ label: 'Link', insert: 'link text', wrap: { before: '[', after: '](https://)' } },
31-
{ label: 'List', insert: '- item' },
32-
{ label: 'Code', insert: 'code', wrap: { before: '`', after: '`' } },
33-
{ label: 'Quote', insert: '> quote' },
34+
{ label: 'B', name: 'Bold', insert: 'bold text', wrap: { before: '**', after: '**' } },
35+
{ label: 'I', name: 'Italic', insert: 'italic text', wrap: { before: '_', after: '_' } },
36+
{ label: 'Link', name: 'Insert link', insert: 'link text', wrap: { before: '[', after: '](https://)' } },
37+
{ label: 'List', name: 'Bulleted list', insert: '- item' },
38+
{ label: 'Code', name: 'Code', insert: 'code', wrap: { before: '`', after: '`' } },
39+
{ label: 'Quote', name: 'Quote', insert: '> quote' },
3440
];
3541

3642
/**
@@ -55,6 +61,11 @@ export function MarkdownEditor({
5561
const id = useId();
5662
const errorId = `${id}-error`;
5763
const textareaRef = useRef<HTMLTextAreaElement>(null);
64+
const toolbarRefs = useRef<Array<HTMLButtonElement | null>>([]);
65+
// Roving tabindex: a toolbar is one tab stop, and the arrow keys move
66+
// within it (ARIA APG Toolbar pattern). Without this the six formatting
67+
// buttons sat between the label and the textarea as six separate tab stops.
68+
const [activeButton, setActiveButton] = useState(0);
5869
const [previewHtml, setPreviewHtml] = useState<string>('');
5970
const [previewLoading, setPreviewLoading] = useState(false);
6071
const [previewError, setPreviewError] = useState<string | null>(null);
@@ -132,6 +143,28 @@ export function MarkdownEditor({
132143
});
133144
};
134145

146+
const focusToolbarButton = (index: number) => {
147+
setActiveButton(index);
148+
toolbarRefs.current[index]?.focus();
149+
};
150+
151+
const handleToolbarKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
152+
const last = TOOLBAR.length - 1;
153+
if (e.key === 'ArrowRight') {
154+
e.preventDefault();
155+
focusToolbarButton(activeButton === last ? 0 : activeButton + 1);
156+
} else if (e.key === 'ArrowLeft') {
157+
e.preventDefault();
158+
focusToolbarButton(activeButton === 0 ? last : activeButton - 1);
159+
} else if (e.key === 'Home') {
160+
e.preventDefault();
161+
focusToolbarButton(0);
162+
} else if (e.key === 'End') {
163+
e.preventDefault();
164+
focusToolbarButton(last);
165+
}
166+
};
167+
135168
const count = value.length;
136169
const overSoftLimit = maxLength !== undefined && count > maxLength;
137170

@@ -147,14 +180,25 @@ export function MarkdownEditor({
147180
<p className="text-xs text-muted-foreground">{description}</p>
148181
)}
149182
<div className="border border-border rounded-md overflow-hidden">
150-
<div className="flex flex-wrap gap-1 bg-muted/50 border-b border-border px-2 py-1.5">
151-
{TOOLBAR.map((btn) => (
183+
<div
184+
role="toolbar"
185+
aria-label="Formatting"
186+
onKeyDown={handleToolbarKeyDown}
187+
className="flex flex-wrap gap-1 bg-muted/50 border-b border-border px-2 py-1.5"
188+
>
189+
{TOOLBAR.map((btn, i) => (
152190
<Button
153191
key={btn.label}
192+
ref={(el: HTMLButtonElement | null) => {
193+
toolbarRefs.current[i] = el;
194+
}}
154195
type="button"
155196
size="sm"
156197
variant="ghost"
157198
className="h-7 px-2 text-xs"
199+
aria-label={btn.name}
200+
tabIndex={i === activeButton ? 0 : -1}
201+
onFocus={() => setActiveButton(i)}
158202
onClick={() => applyToolbar(btn)}
159203
>
160204
{btn.label}

‎apps/web/src/screens/ProfileEdit.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,11 @@ export function ProfileEdit() {
210210
disabled={avatarUploading}
211211
className="block"
212212
/>
213-
{avatarUploading && (
214-
<span className="block mt-1 text-xs text-muted-foreground">Uploading…</span>
215-
)}
213+
{/* role="status" so the upload's progress is announced rather
214+
than only appearing next to the file input. */}
215+
<span role="status" className="block mt-1 text-xs text-muted-foreground">
216+
{avatarUploading ? 'Uploading…' : ''}
217+
</span>
216218
</div>
217219
</div>
218220
</div>

‎apps/web/src/screens/ProjectDetail.tsx‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useMemo, useState } from 'react';
22
import { Link, useParams, useSearchParams } from 'react-router';
33
import { useQuery } from '@tanstack/react-query';
4+
import { toast } from 'sonner';
45
import { Button } from '@/components/ui/button';
56
import {
67
DropdownMenu,
@@ -543,10 +544,17 @@ export function ProjectDetail({ anchor }: ProjectDetailProps = {}) {
543544
Share
544545
</h2>
545546
<div className="flex flex-col gap-2">
547+
{/* Both buttons used to copy silently — nothing changed on
548+
screen, so nobody (sighted or not) could tell it worked.
549+
sonner is what this screen's own modals already use for
550+
action confirmations. */}
546551
<Button
547552
variant="outline"
548553
onClick={() => {
549-
void navigator.clipboard.writeText(`https://codeforphilly.org/projects/${slug}`);
554+
void navigator.clipboard
555+
.writeText(`https://codeforphilly.org/projects/${slug}`)
556+
.then(() => toast.success('Link copied'))
557+
.catch(() => toast.error("Couldn't copy the link"));
550558
}}
551559
>
552560
Copy link
@@ -557,9 +565,12 @@ export function ProjectDetail({ anchor }: ProjectDetailProps = {}) {
557565
// Copy a pre-formatted Slack message. Spec calls this
558566
// out as either system-share or copy; copy works in every
559567
// browser context without a Web Share API gate.
560-
void navigator.clipboard.writeText(
561-
`Check out ${project.title} on Code for Philly: https://codeforphilly.org/projects/${slug}`,
562-
);
568+
void navigator.clipboard
569+
.writeText(
570+
`Check out ${project.title} on Code for Philly: https://codeforphilly.org/projects/${slug}`,
571+
)
572+
.then(() => toast.success('Slack message copied'))
573+
.catch(() => toast.error("Couldn't copy the message"));
563574
}}
564575
>
565576
Share to Slack

‎apps/web/src/screens/Sponsor.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ export function Sponsor() {
106106
<Button variant="outline" size="sm" onClick={handleCopy}>
107107
{copied ? 'Copied ✓' : 'Copy email'}
108108
</Button>
109+
{/* The label swap is the only success signal, and a control's own
110+
name changing is not announced. Mirror it in a live region. */}
111+
<span role="status" className="sr-only">
112+
{copied ? `${email} copied to clipboard` : ''}
113+
</span>
109114
</div>
110115
</div>
111116
</section>

‎apps/web/tests/ConnectGitHubBanner.test.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ describe('ConnectGitHubBanner', () => {
7070
render();
7171
await waitFor(() => {
7272
expect(
73-
screen.getByRole('region', { name: /connect github/i }),
73+
screen.getByRole('status', { name: /connect github/i }),
7474
).toBeInTheDocument();
7575
});
7676
// CTA form posts to the link endpoint.
77-
const region = screen.getByRole('region', { name: /connect github/i });
77+
const region = screen.getByRole('status', { name: /connect github/i });
7878
expect(region.querySelector('form[action="/api/auth/link-github"]')).not.toBeNull();
7979
expect(screen.getByRole('button', { name: /dismiss/i })).toBeInTheDocument();
8080
});
@@ -84,7 +84,7 @@ describe('ConnectGitHubBanner', () => {
8484
render();
8585
await waitFor(() => {
8686
expect(
87-
screen.getByRole('region', { name: /connect github/i }),
87+
screen.getByRole('status', { name: /connect github/i }),
8888
).toBeInTheDocument();
8989
});
9090
});
@@ -101,7 +101,7 @@ describe('ConnectGitHubBanner', () => {
101101
// microtask gap.
102102
await new Promise((r) => setTimeout(r, 0));
103103
expect(
104-
screen.queryByRole('region', { name: /connect github/i }),
104+
screen.queryByRole('status', { name: /connect github/i }),
105105
).not.toBeInTheDocument();
106106
});
107107

@@ -110,7 +110,7 @@ describe('ConnectGitHubBanner', () => {
110110
render();
111111
await new Promise((r) => setTimeout(r, 0));
112112
expect(
113-
screen.queryByRole('region', { name: /connect github/i }),
113+
screen.queryByRole('status', { name: /connect github/i }),
114114
).not.toBeInTheDocument();
115115
});
116116

@@ -121,7 +121,7 @@ describe('ConnectGitHubBanner', () => {
121121
fireEvent.click(dismissBtn);
122122
await waitFor(() => {
123123
expect(
124-
screen.queryByRole('region', { name: /connect github/i }),
124+
screen.queryByRole('status', { name: /connect github/i }),
125125
).not.toBeInTheDocument();
126126
});
127127
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
2+
import { screen, within } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
import { renderWithRouter } from './test-utils.js';
5+
import { MarkdownEditor } from '../src/components/MarkdownEditor.js';
6+
7+
function Harness() {
8+
return <MarkdownEditor label="Overview" value="" onChange={() => {}} />;
9+
}
10+
11+
describe('MarkdownEditor formatting toolbar', () => {
12+
beforeEach(() => {
13+
// The preview round-trip is skipped for empty content, but stub fetch
14+
// anyway so a stray call can never reach the network.
15+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(null, { status: 404 }));
16+
});
17+
18+
afterEach(() => {
19+
vi.restoreAllMocks();
20+
});
21+
22+
it('exposes a labelled toolbar whose buttons have real names', () => {
23+
renderWithRouter(<Harness />);
24+
const toolbar = screen.getByRole('toolbar', { name: 'Formatting' });
25+
for (const name of ['Bold', 'Italic', 'Insert link', 'Bulleted list', 'Code', 'Quote']) {
26+
expect(within(toolbar).getByRole('button', { name })).toBeInTheDocument();
27+
}
28+
});
29+
30+
it('keeps every accessible name a superset of the visible label (SC 2.5.3)', () => {
31+
renderWithRouter(<Harness />);
32+
const toolbar = screen.getByRole('toolbar', { name: 'Formatting' });
33+
for (const [visible, name] of [
34+
['B', 'Bold'],
35+
['I', 'Italic'],
36+
['Link', 'Insert link'],
37+
['List', 'Bulleted list'],
38+
] as const) {
39+
const btn = within(toolbar).getByRole('button', { name });
40+
expect(btn.textContent).toBe(visible);
41+
expect(name.toLowerCase()).toContain(visible.toLowerCase());
42+
}
43+
});
44+
45+
it('is a single tab stop with a roving tabindex', async () => {
46+
const user = userEvent.setup();
47+
renderWithRouter(<Harness />);
48+
const toolbar = screen.getByRole('toolbar', { name: 'Formatting' });
49+
const buttons = within(toolbar).getAllByRole('button');
50+
51+
// Only the active button is reachable by Tab.
52+
expect(buttons.filter((b) => b.getAttribute('tabindex') === '0')).toHaveLength(1);
53+
expect(buttons[0]).toHaveAttribute('tabindex', '0');
54+
55+
buttons[0]!.focus();
56+
await user.keyboard('{ArrowRight}');
57+
expect(buttons[1]).toHaveFocus();
58+
expect(buttons[1]).toHaveAttribute('tabindex', '0');
59+
expect(buttons[0]).toHaveAttribute('tabindex', '-1');
60+
61+
await user.keyboard('{End}');
62+
expect(buttons[buttons.length - 1]).toHaveFocus();
63+
64+
// Wraps forward off the end, and Home returns to the first.
65+
await user.keyboard('{ArrowRight}');
66+
expect(buttons[0]).toHaveFocus();
67+
await user.keyboard('{ArrowLeft}');
68+
expect(buttons[buttons.length - 1]).toHaveFocus();
69+
await user.keyboard('{Home}');
70+
expect(buttons[0]).toHaveFocus();
71+
});
72+
});

0 commit comments

Comments
 (0)