Skip to content

Commit dc9055b

Browse files
refactor(web): derive the toolbar's roving index from the event target
handleToolbarKeyDown read `activeButton` from its closure, which lags a focus change by a render cycle. Take the index from the button the key landed on instead; `activeButton` stays as render state for tabIndex. The name-superset test compared two literals from its own table; it now checks the rendered button's aria-label against its visible text. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RdRwHvDupRLV8GuJpYKzEr
1 parent ab461e0 commit dc9055b

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,18 @@ export function MarkdownEditor({
149149
};
150150

151151
const handleToolbarKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
152+
// Start from the button the key landed on, not from `activeButton`:
153+
// that state is what the render reads for tabIndex, but it lags a
154+
// focus change by a render cycle, so the event target is the truth.
155+
const current = toolbarRefs.current.indexOf(e.target as HTMLButtonElement);
156+
if (current === -1) return;
152157
const last = TOOLBAR.length - 1;
153158
if (e.key === 'ArrowRight') {
154159
e.preventDefault();
155-
focusToolbarButton(activeButton === last ? 0 : activeButton + 1);
160+
focusToolbarButton(current === last ? 0 : current + 1);
156161
} else if (e.key === 'ArrowLeft') {
157162
e.preventDefault();
158-
focusToolbarButton(activeButton === 0 ? last : activeButton - 1);
163+
focusToolbarButton(current === 0 ? last : current - 1);
159164
} else if (e.key === 'Home') {
160165
e.preventDefault();
161166
focusToolbarButton(0);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('MarkdownEditor formatting toolbar', () => {
3838
] as const) {
3939
const btn = within(toolbar).getByRole('button', { name });
4040
expect(btn.textContent).toBe(visible);
41-
expect(name.toLowerCase()).toContain(visible.toLowerCase());
41+
expect(btn.getAttribute('aria-label')?.toLowerCase()).toContain(visible.toLowerCase());
4242
}
4343
});
4444

0 commit comments

Comments
 (0)