Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions packages/ariakit/src/comments/Comment.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 (
<AriakitGroup
ref={ref}
className={className}
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
>
{doShowActions ? (
<AriakitGroup ref={ref} className={className}>
{actions && (showActions !== false || emojiPickerOpen) ? (
<AriakitGroup
ref={focusRef}
className={"bn-comment-toolbar"}
data-show={emojiPickerOpen ? true : (showActions ?? true)}
style={{
position: "absolute",
right: 0,
Expand Down
18 changes: 5 additions & 13 deletions packages/mantine/src/comments/Comment.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assertEmpty } from "@blocknote/core";
import { ComponentProps, mergeRefs, useDictionary } from "@blocknote/react";
import { ComponentProps, useDictionary } from "@blocknote/react";
import { Avatar, Group, Skeleton, Text } from "@mantine/core";
import { useHover } from "@mantine/hooks";
import { forwardRef } from "react";

const AuthorInfo = forwardRef<
Expand Down Expand Up @@ -64,21 +63,14 @@ export const Comment = forwardRef<
...rest
} = props;

const { hovered, ref: hoverRef } = useHover();
const mergedRef = mergeRefs([ref, hoverRef]);
assertEmpty(rest, false);

const doShowActions =
actions &&
(showActions === true ||
showActions === undefined ||
(showActions === "hover" && hovered) ||
emojiPickerOpen);

return (
<Group pos="relative" ref={mergedRef} className={className}>
{doShowActions ? (
<Group pos="relative" ref={ref} className={className}>
{actions && (showActions !== false || emojiPickerOpen) ? (
<Group
className={"bn-comment-toolbar"}
data-show={emojiPickerOpen ? true : (showActions ?? true)}
style={{
position: "absolute",
right: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
109 changes: 109 additions & 0 deletions packages/react/src/components/Comments/Comment.browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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 { createElement } from "react";
import * as Y from "@y/y";

import { expect, test } from "vite-plus/test";
import { page } from "vite-plus/test/browser";
import { openEditor, pressKey } from "@root/tests/a11y/helpers.js";

const commentDocument: PartialBlock[] = [
{ type: "paragraph", content: "A comment" },
];

const editorDocument: PartialBlock[] = [
{ type: "paragraph", content: "Commented document" },
];

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: 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 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();
});
});
4 changes: 4 additions & 0 deletions packages/react/src/components/Comments/Comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export const Comment = ({
>
<Components.Generic.Toolbar.Button
key={"add-reaction"}
label={dict.comments.actions.add_reaction}
mainTooltip={dict.comments.actions.add_reaction}
variant="compact"
>
Expand All @@ -277,6 +278,7 @@ export const Comment = ({
(thread.resolved ? (
<Components.Generic.Toolbar.Button
key={"reopen"}
label={dict.comments.actions.reopen}
mainTooltip={dict.comments.actions.reopen}
variant="compact"
onClick={onReopen}
Expand All @@ -286,6 +288,7 @@ export const Comment = ({
) : (
<Components.Generic.Toolbar.Button
key={"resolve"}
label={dict.comments.actions.resolve}
mainTooltip={dict.comments.actions.resolve}
variant="compact"
onClick={onResolve}
Expand All @@ -302,6 +305,7 @@ export const Comment = ({
<Components.Generic.Menu.Trigger>
<Components.Generic.Toolbar.Button
key={"more-actions"}
label={dict.comments.actions.more_actions}
mainTooltip={dict.comments.actions.more_actions}
variant="compact"
>
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/components/Comments/EmojiPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export const EmojiPicker = (props: {
>
<Components.Generic.Popover.Trigger>
<div
aria-label={dict.comments.actions.add_reaction}
// The child control handles keyboard activation.
tabIndex={-1}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
Expand Down
12 changes: 12 additions & 0 deletions packages/react/src/editor/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 8 additions & 25 deletions packages/shadcn/src/comments/Comment.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 (
<div
ref={ref}
className={cn(className, "relative flex flex-col gap-2")}
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
>
{doShowActions ? (
<div className={"absolute top-0 right-0 z-10"} ref={focusRef}>
<div ref={ref} className={cn(className, "relative flex flex-col gap-2")}>
{actions && (showActions !== false || emojiPickerOpen) ? (
<div
className={"bn-comment-toolbar absolute top-0 right-0 z-10"}
data-show={emojiPickerOpen ? true : (showActions ?? true)}
>
{actions}
</div>
) : null}
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading