fix(a11y): every sortable list speaks the user's language to a screen reader

dnd-kit's screen-reader text is hard-coded English and names an item by its id ("Draggable item
10357 was dropped", a role of "sortable"). Only the YouTube playlist replaced it; the Plex playlist,
the saved-views list and the side-panel groups read it out under a Hungarian UI — found while
checking the Plex playlist's keyboard reorder on localdev.

`ui/sortableA11y` is now the one place: `useSortableA11y` gives a DndContext its instructions and
announcements (HU/EN, `common.dnd.*`), `useSortableItem` announces the role. The YouTube playlist's
own announcements move onto it, which also fixes them: its lists reorder on drop, so reading the
dragged row's own index said "moved to position 1" at every step and "dropped at position 1"
wherever the row went. The place announced is now the slot the row would land in.

A `local/sortable-a11y` lint rule (with its contract test) refuses a DndContext without
`accessibility` and a direct `useSortable`.
This commit is contained in:
2026-09-19 17:37:21 +02:00
parent 946c8f26db
commit 17a26894b0
13 changed files with 273 additions and 57 deletions
@@ -0,0 +1,55 @@
// @vitest-environment jsdom
import { describe, it, expect, vi } from "vitest";
import { renderHook } from "@testing-library/react";
import type { Active, Over } from "@dnd-kit/core";
vi.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key: string, o?: Record<string, unknown>) =>
o ? `${key}:${o.title}@${o.pos}/${o.total}` : key,
}),
}));
import { useSortableA11y } from "./sortableA11y";
const rows = ["a", "b", "c", "d"];
const describeRow = (id: string | number) => ({
title: `Row ${id}`,
pos: rows.indexOf(String(id)) + 1,
total: rows.length,
});
const active = (id: string) => ({ id }) as unknown as Active;
const over = (id: string) => ({ id }) as unknown as Over;
// The lists here reorder on DROP, so the dragged row's own index stays its starting place for the
// whole drag. The YouTube playlist read exactly that, and announced "moved to position 1" at every
// step and "dropped at position 1" wherever the row went. The place is the `over` row's slot.
describe("useSortableA11y", () => {
const { announcements, screenReaderInstructions } = renderHook(() => useSortableA11y(describeRow))
.result.current;
it("speaks the user's language, not dnd-kit's English", () => {
expect(screenReaderInstructions.draggable).toBe("common.dnd.instructions");
});
it("announces where the row WOULD land while it moves, and where it did land", () => {
expect(announcements.onDragStart({ active: active("a") })).toBe(
"common.dnd.pickedUp:Row a@1/4",
);
expect(announcements.onDragOver({ active: active("a"), over: over("c") })).toBe(
"common.dnd.movedOver:Row a@3/4",
);
expect(announcements.onDragEnd({ active: active("a"), over: over("d") })).toBe(
"common.dnd.dropped:Row a@4/4",
);
});
it("says a drop outside the list, or Escape, put the row back where it was", () => {
expect(announcements.onDragEnd({ active: active("b"), over: null })).toBe(
"common.dnd.cancelled:Row b@2/4",
);
expect(announcements.onDragCancel({ active: active("b"), over: null })).toBe(
"common.dnd.cancelled:Row b@2/4",
);
});
});