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) ? ( [ + { + id: "user", + username: "Test user", + avatarUrl: "/image.svg", + }, + ]); + await users.loadUsers(["user"]); + await openEditor(editorDocument, { + extensions: [CommentsExtension({ threadStore, resolveUsers: users })], + children: createElement(ThreadsSidebar, { filter: "all" }), + }); + return { threadStore, threadId: thread.id }; +} + +async function focusCommentActionWithAssertions(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. + while (document.activeElement !== button.element()) { + await pressKey("Tab"); + } + 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 }; +} + +test.describe("keyboard", () => { + test("comment actions toolbar: Add reaction", async () => { + await openComments(); + await focusCommentActionWithAssertions("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 focusCommentActionWithAssertions("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 focusCommentActionWithAssertions("Re-open"); + await pressKey("Enter"); + await expect + .poll(() => threadStore.getThread(threadId).resolved) + .toBe(false); + }); + + test("comment actions toolbar: More actions", async () => { + await openComments(); + await focusCommentActionWithAssertions("More actions"); + await pressKey("Enter"); + await expect.element(page.getByRole("menu")).toBeVisible(); + }); +}); diff --git a/packages/react/src/components/Comments/Comment.tsx b/packages/react/src/components/Comments/Comment.tsx index 0e80820d43..e05cf95235 100644 --- a/packages/react/src/components/Comments/Comment.tsx +++ b/packages/react/src/components/Comments/Comment.tsx @@ -266,6 +266,7 @@ export const Comment = ({ > @@ -277,6 +278,7 @@ export const Comment = ({ (thread.resolved ? ( diff --git a/packages/react/src/components/Comments/EmojiPicker.tsx b/packages/react/src/components/Comments/EmojiPicker.tsx index 58a81cac62..a6bbec8b42 100644 --- a/packages/react/src/components/Comments/EmojiPicker.tsx +++ b/packages/react/src/components/Comments/EmojiPicker.tsx @@ -30,6 +30,9 @@ export const EmojiPicker = (props: { >
{ event.preventDefault(); event.stopPropagation(); 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 4f107a35cf..44fda10c47 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5787,6 +5787,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 @@ -10734,6 +10737,7 @@ packages: '@smithy/smithy-client@4.12.8': resolution: {integrity: sha512-aJaAX7vHe5i66smoSSID7t4rKY08PbD8EBU7DOloixvhOozfYWdcSYE4l6/tjkZ0vBZhGjheWzB2mh31sLgCMA==} engines: {node: '>=18.0.0'} + deprecated: Deprecated due to a bug in custom logger data redaction. See https://github.com/aws/aws-sdk-js-v3/issues/8393. '@smithy/types@4.13.1': resolution: {integrity: sha512-787F3yzE2UiJIQ+wYW1CVg2odHjmaWLGksnKQHUrK/lYZSEcy1msuLVvxaR/sI2/aDe9U+TBuLsXnr3vod1g0g==} diff --git a/tests/a11y/helpers.tsx b/tests/a11y/helpers.tsx index b25893f00f..43a30ede91 100644 --- a/tests/a11y/helpers.tsx +++ b/tests/a11y/helpers.tsx @@ -1,9 +1,13 @@ -import type { BlockNoteEditor, PartialBlock } from "@blocknote/core"; +import type { + BlockNoteEditor, + 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 { useEffect } from "react"; +import { type ReactNode, useEffect } from "react"; import { render } from "vitest-browser-react"; import axe from "axe-core"; import { expect, type TestContext } from "vite-plus/test"; @@ -31,21 +35,28 @@ function EditorFixture({ editable, theme, onReady, + extensions, + children, }: { initialContent: PartialBlock[]; editable: boolean; theme: "light" | "dark"; onReady?: (editor: BlockNoteEditor) => void; + extensions?: ExtensionFactoryInstance[]; + children?: ReactNode; }) { - const editor = useCreateBlockNote({ initialContent }); + const editor = useCreateBlockNote({ initialContent, extensions }); useEffect(() => { onReady?.(editor); }, [editor, onReady]); + return (

BlockNote accessibility fixture

- + + {children} +
); @@ -57,10 +68,14 @@ export async function openEditor( editable = true, theme = "light", onReady, + extensions, + children, }: { editable?: boolean; theme?: "light" | "dark"; onReady?: (editor: BlockNoteEditor) => void; + extensions?: ExtensionFactoryInstance[]; + children?: ReactNode; } = {}, ) { let editor!: BlockNoteEditor; @@ -74,11 +89,14 @@ export async function openEditor( editor = value; onReady?.(value); }} - />, + extensions={extensions} + > + {children} + , ); - await expect.element(page.getByRole("textbox")).toBeVisible(); + await expect.element(page.getByRole("textbox").first()).toBeVisible(); await expect - .element(page.getByRole("textbox")) + .element(page.getByRole("textbox").first()) .toHaveAttribute("contenteditable", String(editable)); await document.fonts.ready; if (screenReader) {