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) ? ( & 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/Comment.tsx b/packages/react/src/components/Comments/Comment.tsx index c6f08b75ff..c13d094252 100644 --- a/packages/react/src/components/Comments/Comment.tsx +++ b/packages/react/src/components/Comments/Comment.tsx @@ -279,6 +279,7 @@ export const Comment = ({ (thread.resolved ? ( 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