From 2f54f46f29d20cf88ec3706fe06e1d1cb71cd01d Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Tue, 20 Oct 2026 19:21:09 +0200 Subject: [PATCH 1/4] Fixed action toolbar in comments not being keyboard accessible --- packages/ariakit/src/comments/Comment.tsx | 31 +--- packages/mantine/src/comments/Comment.tsx | 18 +-- packages/react/package.json | 1 + .../react/src/components/Comments/Comment.tsx | 3 + .../Comments/a11y/Comment.a11y.test.ts | 34 ++++ .../src/components/Comments/a11y/helpers.ts | 65 ++++++++ packages/react/src/editor/styles.css | 12 ++ packages/shadcn/src/comments/Comment.tsx | 33 +--- pnpm-lock.yaml | 3 + tests/a11y/helpers.tsx | 145 ++++++++++++++++++ 10 files changed, 282 insertions(+), 63 deletions(-) create mode 100644 packages/react/src/components/Comments/a11y/Comment.a11y.test.ts create mode 100644 packages/react/src/components/Comments/a11y/helpers.ts create mode 100644 tests/a11y/helpers.tsx diff --git a/packages/ariakit/src/comments/Comment.tsx b/packages/ariakit/src/comments/Comment.tsx index 3bf49022e3..58e5fe124e 100644 --- a/packages/ariakit/src/comments/Comment.tsx +++ b/packages/ariakit/src/comments/Comment.tsx @@ -1,12 +1,8 @@ import { Group as AriakitGroup } from "@ariakit/react"; import { assertEmpty } from "@blocknote/core"; -import { - ComponentProps, - useDictionary, - useFocusWithin, -} from "@blocknote/react"; -import { forwardRef, useState } from "react"; +import { ComponentProps, useDictionary } from "@blocknote/react"; +import { forwardRef } from "react"; const AuthorInfo = forwardRef< HTMLDivElement, @@ -64,27 +60,12 @@ export const Comment = forwardRef< assertEmpty(rest, false); - const [hovered, setHovered] = useState(false); - const { focused, ref: focusRef } = useFocusWithin(); - - const doShowActions = - actions && - (showActions === true || - showActions === undefined || - (showActions === "hover" && hovered) || - focused || - emojiPickerOpen); - return ( - setHovered(true)} - onMouseLeave={() => setHovered(false)} - > - {doShowActions ? ( + + {actions && (showActions !== false || emojiPickerOpen) ? ( - {doShowActions ? ( + + {actions && (showActions !== false || emojiPickerOpen) ? ( diff --git a/packages/react/src/components/Comments/a11y/Comment.a11y.test.ts b/packages/react/src/components/Comments/a11y/Comment.a11y.test.ts new file mode 100644 index 0000000000..b117079023 --- /dev/null +++ b/packages/react/src/components/Comments/a11y/Comment.a11y.test.ts @@ -0,0 +1,34 @@ +import { focusCommentAction, openComments } from "./helpers.js"; +import { expect, test } from "vite-plus/test"; +import { page } from "vite-plus/test/browser"; +import { pressKey } from "@root/tests/a11y/helpers.js"; + +test("comment actions toolbar: Add reaction", async () => { + await openComments(); + await focusCommentAction("Add reaction"); + await pressKey("Enter"); + await expect + .element(page.getByRole("dialog", { name: "Add reaction" })) + .toBeVisible(); +}); + +test("comment actions toolbar: Resolve", async () => { + const { threadStore, threadId } = await openComments(); + await focusCommentAction("Resolve"); + await pressKey("Enter"); + await expect.poll(() => threadStore.getThread(threadId).resolved).toBe(true); +}); + +test("comment actions toolbar: Re-open", async () => { + const { threadStore, threadId } = await openComments({ resolved: true }); + await focusCommentAction("Re-open"); + await pressKey("Enter"); + await expect.poll(() => threadStore.getThread(threadId).resolved).toBe(false); +}); + +test("comment actions toolbar: More actions", async () => { + await openComments(); + await focusCommentAction("More actions"); + await pressKey("Enter"); + await expect.element(page.getByRole("menu")).toBeVisible(); +}); diff --git a/packages/react/src/components/Comments/a11y/helpers.ts b/packages/react/src/components/Comments/a11y/helpers.ts new file mode 100644 index 0000000000..17e0989797 --- /dev/null +++ b/packages/react/src/components/Comments/a11y/helpers.ts @@ -0,0 +1,65 @@ +import { createUserStore } from "@blocknote/core"; +import { + CommentsExtension, + DefaultThreadStoreAuth, +} from "@blocknote/core/comments"; +import { YjsThreadStore } from "@blocknote/core/y"; +import { ThreadsSidebar } from "@blocknote/react"; +import { createElement } from "react"; +import * as Y from "@y/y"; + +import { expect } from "vite-plus/test"; +import { page } from "vite-plus/test/browser"; +import { openEditor, pressKey } from "@root/tests/a11y/helpers.js"; + +export async function openComments({ resolved = false } = {}) { + const doc = new Y.Doc(); + const threadStore = new YjsThreadStore( + "user", + doc.get("threads"), + new DefaultThreadStoreAuth("user", "editor"), + ); + const thread = await threadStore.createThread({ + initialComment: { body: [{ type: "paragraph", content: "A comment" }] }, + }); + if (resolved) { + await threadStore.resolveThread({ threadId: thread.id }); + } + const users = createUserStore(async () => [ + { + id: "user", + username: "Test user", + avatarUrl: "/image.svg", + }, + ]); + await users.loadUsers(["user"]); + await openEditor([{ type: "paragraph", content: "Commented document" }], { + extensions: [CommentsExtension({ threadStore, resolveUsers: users })], + children: createElement(ThreadsSidebar, { filter: "all" }), + }); + return { threadStore, threadId: thread.id }; +} + +export async function focusCommentAction(control: string) { + const toolbar = page.getByRole("toolbar"); + const button = toolbar.getByRole("button", { name: control, exact: true }); + await expect.element(button).toBeEnabled(); + // Start at "Before editor" and reach the action without hovering the comment. + for (let step = 0; step < 10; step++) { + if (button.element() === document.activeElement) { + break; + } + await pressKey("Tab"); + } + await expect + .element(button, { message: `${control} is reachable with Tab` }) + .toHaveFocus(); + await expect + .poll( + () => + getComputedStyle(button.element().closest(".bn-comment-toolbar")!) + .opacity, + ) + .toBe("1"); + return { toolbar, button }; +} diff --git a/packages/react/src/editor/styles.css b/packages/react/src/editor/styles.css index 5046c97352..6297311487 100644 --- a/packages/react/src/editor/styles.css +++ b/packages/react/src/editor/styles.css @@ -313,6 +313,18 @@ SideMenuController offsets its position to keep it centered on the line. */ flex-grow: 1; } +/* Keep hover actions in the tab order and reveal them when focus enters. */ +.bn-comment-toolbar[data-show="hover"] { + opacity: 0; + pointer-events: none; +} + +*:hover > .bn-comment-toolbar[data-show="hover"], +*:focus-within > .bn-comment-toolbar[data-show="hover"] { + opacity: 1; + pointer-events: auto; +} + /* Thread sidebar styling */ .bn-threads-sidebar { border-radius: var(--bn-border-radius-medium); diff --git a/packages/shadcn/src/comments/Comment.tsx b/packages/shadcn/src/comments/Comment.tsx index ecde20bb9f..d21f71840a 100644 --- a/packages/shadcn/src/comments/Comment.tsx +++ b/packages/shadcn/src/comments/Comment.tsx @@ -1,10 +1,6 @@ import { assertEmpty } from "@blocknote/core"; -import { - ComponentProps, - useDictionary, - useFocusWithin, -} from "@blocknote/react"; -import { forwardRef, useState } from "react"; +import { ComponentProps, useDictionary } from "@blocknote/react"; +import { forwardRef } from "react"; import { cn } from "../lib/utils.js"; import { useShadCNComponentsContext } from "../ShadCNComponentsContext.js"; @@ -77,26 +73,13 @@ export const Comment = forwardRef< assertEmpty(rest); - const [hovered, setHovered] = useState(false); - const { focused, ref: focusRef } = useFocusWithin(); - - const doShowActions = - actions && - (showActions === true || - showActions === undefined || - (showActions === "hover" && hovered) || - focused || - emojiPickerOpen); - return ( -
setHovered(true)} - onMouseLeave={() => setHovered(false)} - > - {doShowActions ? ( -
+
+ {actions && (showActions !== false || emojiPickerOpen) ? ( +
{actions}
) : null} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ac6f21c9ea..c5067f3490 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5781,6 +5781,9 @@ importers: '@vitejs/plugin-react': specifier: ^6.0.1 version: 6.0.1(babel-plugin-react-compiler@1.0.0)(vite@8.0.8(@types/node@25.9.5)(esbuild@0.27.5)(jiti@2.7.0)(terser@5.51.2)(tsx@4.21.0)(yaml@2.9.0)) + '@y/y': + specifier: 14.0.0-rc.26 + version: 14.0.0-rc.26 react: specifier: ^19.2.5 version: 19.2.5 diff --git a/tests/a11y/helpers.tsx b/tests/a11y/helpers.tsx new file mode 100644 index 0000000000..83567af56d --- /dev/null +++ b/tests/a11y/helpers.tsx @@ -0,0 +1,145 @@ +import type { ExtensionFactoryInstance, PartialBlock } from "@blocknote/core"; +import "@blocknote/core/fonts/inter.css"; +import { BlockNoteView } from "@blocknote/mantine"; +import "@blocknote/mantine/style.css"; +import { useCreateBlockNote } from "@blocknote/react"; +import type { ReactNode } from "react"; +import { render } from "vitest-browser-react"; +import axe from "axe-core"; +import { expect, type TestContext } from "vite-plus/test"; +import { commands, page, server } from "vite-plus/test/browser"; +import "./style.css"; + +export const screenReader = + server.config.name === "voiceover" || server.config.name === "nvda"; +export const selectToLineEndKey = + server.platform === "darwin" ? "Meta+Shift+ArrowRight" : "Shift+End"; + +// Vite+ does not re-export the augmentable BrowserCommands interface. Keep the +// typed RPC boundary here, as the existing browser test utilities do. +const browserCommands = commands as typeof commands & { + startReader(): Promise; + pressKey(key: string): Promise; + readTranscript(): Promise; + stopReader(): Promise; +}; + +function EditorFixture({ + initialContent, + editable, + theme, + extensions, + children, +}: { + initialContent: PartialBlock[]; + editable: boolean; + theme: "light" | "dark"; + extensions?: ExtensionFactoryInstance[]; + children?: ReactNode; +}) { + const editor = useCreateBlockNote({ initialContent, extensions }); + return ( +
+

BlockNote accessibility fixture

+ + + {children} + + +
+ ); +} + +export async function openEditor( + initialContent: PartialBlock[], + { + editable = true, + theme = "light", + extensions, + children, + }: { + editable?: boolean; + theme?: "light" | "dark"; + extensions?: ExtensionFactoryInstance[]; + children?: ReactNode; + } = {}, +) { + await page.viewport(1280, 900); + await render( + + {children} + , + ); + await expect.element(page.getByRole("textbox").first()).toBeVisible(); + await expect + .element(page.getByRole("textbox").first()) + .toHaveAttribute("contenteditable", String(editable)); + await document.fonts.ready; + if (screenReader) { + await browserCommands.startReader(); + } else { + page + .getByRole("button", { name: "Before editor", exact: true }) + .element() + .focus(); + } +} + +export async function checkAxe( + annotate: TestContext["annotate"], + scope?: string, +) { + const results = await axe.run(scope ? { include: [scope] } : document.body, { + rules: { "target-size": { enabled: true } }, + }); + for (const result of results.incomplete) { + for (const node of result.nodes) { + await annotate( + `axe manual review: ${result.id} — ${result.help}\nTarget: ${JSON.stringify(node.target)}\n${node.failureSummary ?? ""}\n${result.helpUrl}`, + "warning", + ); + } + } + expect(results.violations, "axe violations").toEqual([]); +} + +export function readSelection() { + const selection = document.getSelection(); + const node = selection?.focusNode; + const element = node instanceof Element ? node : node?.parentElement; + const cell = element?.closest("td, th"); + const row = cell?.parentElement; + return { + tableCell: + cell instanceof HTMLTableCellElement && row instanceof HTMLTableRowElement + ? { row: row.rowIndex, column: cell.cellIndex } + : null, + blockId: element?.closest("[data-id]")?.getAttribute("data-id") ?? null, + offset: selection?.focusOffset ?? 0, + text: selection?.toString() ?? "", + }; +} + +export async function pressKey(key: string) { + return browserCommands.pressKey(key); +} + +// Register with afterEach in screen-reader tests. Stop the reader even when +// reading or comparing the transcript fails. +export async function saveTranscript() { + if (!screenReader) { + throw new Error("saveTranscript requires a screen-reader test run."); + } + try { + const transcript = await browserCommands.readTranscript(); + expect(transcript.length).toBeGreaterThan(0); + expect(transcript).toMatchSnapshot(); + } finally { + await browserCommands.stopReader(); + } +} From 1ebe8e204bb041f1680cd6a92c0174a4c8971b60 Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Tue, 20 Oct 2026 20:08:48 +0200 Subject: [PATCH 2/4] Reverted `package.json` changes --- packages/react/package.json | 1 - pnpm-lock.yaml | 3 --- 2 files changed, 4 deletions(-) diff --git a/packages/react/package.json b/packages/react/package.json index 054749d915..5766c75b12 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -94,7 +94,6 @@ "@types/react": "^19.2.3", "@types/react-dom": "^19.2.3", "@vitejs/plugin-react": "^6.0.1", - "@y/y": "14.0.0-rc.26", "react": "^19.2.5", "react-dom": "^19.2.5", "rimraf": "^5.0.10", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c5067f3490..ac6f21c9ea 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5781,9 +5781,6 @@ importers: '@vitejs/plugin-react': specifier: ^6.0.1 version: 6.0.1(babel-plugin-react-compiler@1.0.0)(vite@8.0.8(@types/node@25.9.5)(esbuild@0.27.5)(jiti@2.7.0)(terser@5.51.2)(tsx@4.21.0)(yaml@2.9.0)) - '@y/y': - specifier: 14.0.0-rc.26 - version: 14.0.0-rc.26 react: specifier: ^19.2.5 version: 19.2.5 From 6ee020d82b103e2aaa0ed038305cb026c4e1f75c Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Fri, 23 Oct 2026 17:46:56 +0200 Subject: [PATCH 3/4] Re-added missing dep --- packages/react/package.json | 1 + pnpm-lock.yaml | 3 +++ 2 files changed, 4 insertions(+) diff --git a/packages/react/package.json b/packages/react/package.json index 5766c75b12..054749d915 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -94,6 +94,7 @@ "@types/react": "^19.2.3", "@types/react-dom": "^19.2.3", "@vitejs/plugin-react": "^6.0.1", + "@y/y": "14.0.0-rc.26", "react": "^19.2.5", "react-dom": "^19.2.5", "rimraf": "^5.0.10", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ac6f21c9ea..c5067f3490 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5781,6 +5781,9 @@ importers: '@vitejs/plugin-react': specifier: ^6.0.1 version: 6.0.1(babel-plugin-react-compiler@1.0.0)(vite@8.0.8(@types/node@25.9.5)(esbuild@0.27.5)(jiti@2.7.0)(terser@5.51.2)(tsx@4.21.0)(yaml@2.9.0)) + '@y/y': + specifier: 14.0.0-rc.26 + version: 14.0.0-rc.26 react: specifier: ^19.2.5 version: 19.2.5 From 2eac5d72cb33816a48f2baca64be48d3c8d6df0a Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Mon, 9 Nov 2026 19:37:51 +0100 Subject: [PATCH 4/4] Updated test structure --- .../Comments/Comment.browser.test.tsx | 153 ++++++++++++++++++ .../Comments/a11y/Comment.a11y.test.ts | 34 ---- .../src/components/Comments/a11y/helpers.ts | 65 -------- tests/a11y/helpers.tsx | 145 ----------------- 4 files changed, 153 insertions(+), 244 deletions(-) create mode 100644 packages/react/src/components/Comments/Comment.browser.test.tsx delete mode 100644 packages/react/src/components/Comments/a11y/Comment.a11y.test.ts delete mode 100644 packages/react/src/components/Comments/a11y/helpers.ts delete mode 100644 tests/a11y/helpers.tsx diff --git a/packages/react/src/components/Comments/Comment.browser.test.tsx b/packages/react/src/components/Comments/Comment.browser.test.tsx new file mode 100644 index 0000000000..5d316d1849 --- /dev/null +++ b/packages/react/src/components/Comments/Comment.browser.test.tsx @@ -0,0 +1,153 @@ +import { createUserStore, type PartialBlock } from "@blocknote/core"; +import { + CommentsExtension, + DefaultThreadStoreAuth, +} from "@blocknote/core/comments"; +import { YjsThreadStore } from "@blocknote/core/y"; +import { ThreadsSidebar } from "@blocknote/react"; +import * as Y from "@y/y"; + +import { describe, expect, type TestContext } from "vite-plus/test"; +import { page } from "vite-plus/test/browser"; +import type { BrowserContext } from "../../../../../tests/src/fixtures/editor.js"; + +const commentDocument: PartialBlock[] = [ + { type: "paragraph", content: "A comment" }, +]; + +const editorDocument: PartialBlock[] = [ + { type: "paragraph", content: "Commented document" }, +]; + +async function openComments( + { + setupEditor, + onTestFinished, + }: Pick & Pick, + { resolved = false } = {}, +) { + const doc = new Y.Doc(); + onTestFinished(() => doc.destroy()); + const threadStore = new YjsThreadStore( + "user", + doc.get("threads"), + new DefaultThreadStoreAuth("user", "editor"), + ); + const thread = await threadStore.createThread({ + initialComment: { body: commentDocument }, + }); + if (resolved) { + await threadStore.resolveThread({ threadId: thread.id }); + } + const users = createUserStore(async () => [ + { + id: "user", + username: "Test user", + avatarUrl: "/image.svg", + }, + ]); + await users.loadUsers(["user"]); + await setupEditor({ + initialContent: editorDocument, + extensions: [CommentsExtension({ threadStore, resolveUsers: users })], + children: , + outside: ({ children }) => ( +
+ + {children} + +
+ ), + }); + await page + .getByRole("button", { name: "Before editor", exact: true }) + .click(); + return { threadStore, threadId: thread.id }; +} + +async function focusCommentActionWithAssertions( + focusUntil: BrowserContext["focusUntil"], + toolbarButton: string, +) { + const toolbar = page.getByRole("toolbar"); + const button = toolbar.getByRole("button", { + name: toolbarButton, + exact: true, + }); + await expect.element(button).toBeEnabled(); + // Start at "Before editor" and reach the action without hovering the comment. + await focusUntil(button); + await expect + .element(button, { message: `${toolbarButton} is reachable with Tab` }) + .toHaveFocus(); + await expect + .poll( + () => + getComputedStyle(button.element().closest(".bn-comment-toolbar")!) + .opacity, + ) + .toBe("1"); + return { toolbar, button }; +} + +describe("keyboard", () => { + it("comment actions toolbar: Add reaction", async ({ + setupEditor, + onTestFinished, + focusUntil, + pressKey, + }) => { + await openComments({ setupEditor, onTestFinished }); + await focusCommentActionWithAssertions(focusUntil, "Add reaction"); + await pressKey("Enter"); + await expect.element(page.getByRole("dialog")).toBeVisible(); + }); + + it("comment actions toolbar: Resolve", async ({ + setupEditor, + onTestFinished, + focusUntil, + pressKey, + }) => { + const { threadStore, threadId } = await openComments({ + setupEditor, + onTestFinished, + }); + await focusCommentActionWithAssertions(focusUntil, "Resolve"); + await pressKey("Enter"); + await expect + .poll(() => threadStore.getThread(threadId).resolved) + .toBe(true); + }); + + it("comment actions toolbar: Re-open", async ({ + setupEditor, + onTestFinished, + focusUntil, + pressKey, + }) => { + const { threadStore, threadId } = await openComments( + { setupEditor, onTestFinished }, + { + resolved: true, + }, + ); + await focusCommentActionWithAssertions(focusUntil, "Re-open"); + await pressKey("Enter"); + await expect + .poll(() => threadStore.getThread(threadId).resolved) + .toBe(false); + }); + + it("comment actions toolbar: More actions", async ({ + setupEditor, + onTestFinished, + focusUntil, + pressKey, + }) => { + await openComments({ setupEditor, onTestFinished }); + await focusCommentActionWithAssertions(focusUntil, "More actions"); + await pressKey("Enter"); + await expect.element(page.getByRole("menu")).toBeVisible(); + }); +}); diff --git a/packages/react/src/components/Comments/a11y/Comment.a11y.test.ts b/packages/react/src/components/Comments/a11y/Comment.a11y.test.ts deleted file mode 100644 index b117079023..0000000000 --- a/packages/react/src/components/Comments/a11y/Comment.a11y.test.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { focusCommentAction, openComments } from "./helpers.js"; -import { expect, test } from "vite-plus/test"; -import { page } from "vite-plus/test/browser"; -import { pressKey } from "@root/tests/a11y/helpers.js"; - -test("comment actions toolbar: Add reaction", async () => { - await openComments(); - await focusCommentAction("Add reaction"); - await pressKey("Enter"); - await expect - .element(page.getByRole("dialog", { name: "Add reaction" })) - .toBeVisible(); -}); - -test("comment actions toolbar: Resolve", async () => { - const { threadStore, threadId } = await openComments(); - await focusCommentAction("Resolve"); - await pressKey("Enter"); - await expect.poll(() => threadStore.getThread(threadId).resolved).toBe(true); -}); - -test("comment actions toolbar: Re-open", async () => { - const { threadStore, threadId } = await openComments({ resolved: true }); - await focusCommentAction("Re-open"); - await pressKey("Enter"); - await expect.poll(() => threadStore.getThread(threadId).resolved).toBe(false); -}); - -test("comment actions toolbar: More actions", async () => { - await openComments(); - await focusCommentAction("More actions"); - await pressKey("Enter"); - await expect.element(page.getByRole("menu")).toBeVisible(); -}); diff --git a/packages/react/src/components/Comments/a11y/helpers.ts b/packages/react/src/components/Comments/a11y/helpers.ts deleted file mode 100644 index 17e0989797..0000000000 --- a/packages/react/src/components/Comments/a11y/helpers.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { createUserStore } from "@blocknote/core"; -import { - CommentsExtension, - DefaultThreadStoreAuth, -} from "@blocknote/core/comments"; -import { YjsThreadStore } from "@blocknote/core/y"; -import { ThreadsSidebar } from "@blocknote/react"; -import { createElement } from "react"; -import * as Y from "@y/y"; - -import { expect } from "vite-plus/test"; -import { page } from "vite-plus/test/browser"; -import { openEditor, pressKey } from "@root/tests/a11y/helpers.js"; - -export async function openComments({ resolved = false } = {}) { - const doc = new Y.Doc(); - const threadStore = new YjsThreadStore( - "user", - doc.get("threads"), - new DefaultThreadStoreAuth("user", "editor"), - ); - const thread = await threadStore.createThread({ - initialComment: { body: [{ type: "paragraph", content: "A comment" }] }, - }); - if (resolved) { - await threadStore.resolveThread({ threadId: thread.id }); - } - const users = createUserStore(async () => [ - { - id: "user", - username: "Test user", - avatarUrl: "/image.svg", - }, - ]); - await users.loadUsers(["user"]); - await openEditor([{ type: "paragraph", content: "Commented document" }], { - extensions: [CommentsExtension({ threadStore, resolveUsers: users })], - children: createElement(ThreadsSidebar, { filter: "all" }), - }); - return { threadStore, threadId: thread.id }; -} - -export async function focusCommentAction(control: string) { - const toolbar = page.getByRole("toolbar"); - const button = toolbar.getByRole("button", { name: control, exact: true }); - await expect.element(button).toBeEnabled(); - // Start at "Before editor" and reach the action without hovering the comment. - for (let step = 0; step < 10; step++) { - if (button.element() === document.activeElement) { - break; - } - await pressKey("Tab"); - } - await expect - .element(button, { message: `${control} is reachable with Tab` }) - .toHaveFocus(); - await expect - .poll( - () => - getComputedStyle(button.element().closest(".bn-comment-toolbar")!) - .opacity, - ) - .toBe("1"); - return { toolbar, button }; -} diff --git a/tests/a11y/helpers.tsx b/tests/a11y/helpers.tsx deleted file mode 100644 index 83567af56d..0000000000 --- a/tests/a11y/helpers.tsx +++ /dev/null @@ -1,145 +0,0 @@ -import type { ExtensionFactoryInstance, PartialBlock } from "@blocknote/core"; -import "@blocknote/core/fonts/inter.css"; -import { BlockNoteView } from "@blocknote/mantine"; -import "@blocknote/mantine/style.css"; -import { useCreateBlockNote } from "@blocknote/react"; -import type { ReactNode } from "react"; -import { render } from "vitest-browser-react"; -import axe from "axe-core"; -import { expect, type TestContext } from "vite-plus/test"; -import { commands, page, server } from "vite-plus/test/browser"; -import "./style.css"; - -export const screenReader = - server.config.name === "voiceover" || server.config.name === "nvda"; -export const selectToLineEndKey = - server.platform === "darwin" ? "Meta+Shift+ArrowRight" : "Shift+End"; - -// Vite+ does not re-export the augmentable BrowserCommands interface. Keep the -// typed RPC boundary here, as the existing browser test utilities do. -const browserCommands = commands as typeof commands & { - startReader(): Promise; - pressKey(key: string): Promise; - readTranscript(): Promise; - stopReader(): Promise; -}; - -function EditorFixture({ - initialContent, - editable, - theme, - extensions, - children, -}: { - initialContent: PartialBlock[]; - editable: boolean; - theme: "light" | "dark"; - extensions?: ExtensionFactoryInstance[]; - children?: ReactNode; -}) { - const editor = useCreateBlockNote({ initialContent, extensions }); - return ( -
-

BlockNote accessibility fixture

- - - {children} - - -
- ); -} - -export async function openEditor( - initialContent: PartialBlock[], - { - editable = true, - theme = "light", - extensions, - children, - }: { - editable?: boolean; - theme?: "light" | "dark"; - extensions?: ExtensionFactoryInstance[]; - children?: ReactNode; - } = {}, -) { - await page.viewport(1280, 900); - await render( - - {children} - , - ); - await expect.element(page.getByRole("textbox").first()).toBeVisible(); - await expect - .element(page.getByRole("textbox").first()) - .toHaveAttribute("contenteditable", String(editable)); - await document.fonts.ready; - if (screenReader) { - await browserCommands.startReader(); - } else { - page - .getByRole("button", { name: "Before editor", exact: true }) - .element() - .focus(); - } -} - -export async function checkAxe( - annotate: TestContext["annotate"], - scope?: string, -) { - const results = await axe.run(scope ? { include: [scope] } : document.body, { - rules: { "target-size": { enabled: true } }, - }); - for (const result of results.incomplete) { - for (const node of result.nodes) { - await annotate( - `axe manual review: ${result.id} — ${result.help}\nTarget: ${JSON.stringify(node.target)}\n${node.failureSummary ?? ""}\n${result.helpUrl}`, - "warning", - ); - } - } - expect(results.violations, "axe violations").toEqual([]); -} - -export function readSelection() { - const selection = document.getSelection(); - const node = selection?.focusNode; - const element = node instanceof Element ? node : node?.parentElement; - const cell = element?.closest("td, th"); - const row = cell?.parentElement; - return { - tableCell: - cell instanceof HTMLTableCellElement && row instanceof HTMLTableRowElement - ? { row: row.rowIndex, column: cell.cellIndex } - : null, - blockId: element?.closest("[data-id]")?.getAttribute("data-id") ?? null, - offset: selection?.focusOffset ?? 0, - text: selection?.toString() ?? "", - }; -} - -export async function pressKey(key: string) { - return browserCommands.pressKey(key); -} - -// Register with afterEach in screen-reader tests. Stop the reader even when -// reading or comparing the transcript fails. -export async function saveTranscript() { - if (!screenReader) { - throw new Error("saveTranscript requires a screen-reader test run."); - } - try { - const transcript = await browserCommands.readTranscript(); - expect(transcript.length).toBeGreaterThan(0); - expect(transcript).toMatchSnapshot(); - } finally { - await browserCommands.stopReader(); - } -}