diff --git a/docs/pages/get-started/nextjs-comments-canvas.mdx b/docs/pages/get-started/nextjs-comments-canvas.mdx index 76ab518299..918430bd8e 100644 --- a/docs/pages/get-started/nextjs-comments-canvas.mdx +++ b/docs/pages/get-started/nextjs-comments-canvas.mdx @@ -163,7 +163,7 @@ components from be toggled open by clicking on the pin. ```tsx file="app/DraggableThread.tsx" - import { useMemo } from "react"; + import { useMemo, useState } from "react"; import { useEditThreadMetadata } from "@liveblocks/react/suspense"; import { FloatingThread, CommentPin } from "@liveblocks/react-ui"; import { ThreadData } from "@liveblocks/client"; @@ -176,6 +176,7 @@ components from const defaultOpen = useMemo(() => { return Number(new Date()) - Number(new Date(thread.createdAt)) <= 100; }, [thread]); + const [isOpen, setIsOpen] = useState(defaultOpen); // Enable drag const { isDragging, attributes, listeners, setNodeRef, transform } = @@ -188,31 +189,27 @@ components from const x = transform ? transform.x + thread.metadata.x : thread.metadata.x; const y = transform ? transform.y + thread.metadata.y : thread.metadata.y; - // Used to set z-index higher than other threads when dragging - const editThreadMetadata = useEditThreadMetadata(); + // Used to set z-index higher than other threads when open or dragging const maxZIndex = useMaxZIndex(); + const currentZIndex = isOpen || isDragging ? maxZIndex + 1 : thread.metadata?.zIndex || 0; return (
- editThreadMetadata({ - threadId: thread.id, - metadata: { zIndex: maxZIndex + 1 }, - }) - } style={{ position: "absolute", top: 0, left: 0, transform: `translate3d(${x}px, ${y}px, 0)`, - zIndex: thread.metadata?.zIndex || 0, + zIndex: currentZIndex, }} > { const thread = (active.data as DataRef<{ thread: ThreadData }>).current ?.thread; - if (!thread) { return; } - editThreadMetadata({ threadId: thread.id, metadata: { x: thread.metadata.x + delta.x, y: thread.metadata.y + delta.y, + zIndex: maxZIndex + 1, }, }); }, - [editThreadMetadata], + [editThreadMetadata, maxZIndex] ); return ( @@ -496,12 +494,12 @@ components from ```tsx file="app/page.tsx" highlight="6-8" import { Room } from "./Room"; - import { CollaborativeCanvas } from "./CollaborativeCanvas"; + import { CommentsCanvas } from "./CommentsCanvas"; export default function Page() { return ( - + ); } diff --git a/examples/nextjs-comments-canvas/src/components/CommentsCanvas.tsx b/examples/nextjs-comments-canvas/src/components/CommentsCanvas.tsx index 95e880f003..f9c6db6a9a 100644 --- a/examples/nextjs-comments-canvas/src/components/CommentsCanvas.tsx +++ b/examples/nextjs-comments-canvas/src/components/CommentsCanvas.tsx @@ -12,10 +12,12 @@ import { import { useCallback } from "react"; import { PlaceThreadButton } from "./PlaceThreadButton"; import { DraggableThread } from "./DraggableThread"; +import { useMaxZIndex } from "../hooks"; export function CommentsCanvas() { const { threads } = useThreads(); const editThreadMetadata = useEditThreadMetadata(); + const maxZIndex = useMaxZIndex(); // Allow click event on avatar if thread moved less than 3px const sensors = useSensors( @@ -31,7 +33,7 @@ export function CommentsCanvas() { }) ); - // On drag end, update thread metadata with new coords + // On drag end, update thread metadata with new coords and highest z-index const handleDragEnd = useCallback( ({ active, delta }: DragEndEvent) => { const thread = (active.data as DataRef<{ thread: ThreadData }>).current @@ -44,10 +46,11 @@ export function CommentsCanvas() { metadata: { x: thread.metadata.x + delta.x, y: thread.metadata.y + delta.y, + zIndex: maxZIndex + 1, }, }); }, - [editThreadMetadata] + [editThreadMetadata, maxZIndex] ); return ( diff --git a/examples/nextjs-comments-canvas/src/components/DraggableThread.tsx b/examples/nextjs-comments-canvas/src/components/DraggableThread.tsx index ba05b93868..94d7e41e96 100644 --- a/examples/nextjs-comments-canvas/src/components/DraggableThread.tsx +++ b/examples/nextjs-comments-canvas/src/components/DraggableThread.tsx @@ -1,4 +1,4 @@ -import { useMemo } from "react"; +import { useMemo, useState } from "react"; import { useEditThreadMetadata } from "@liveblocks/react/suspense"; import { FloatingThread, CommentPin } from "@liveblocks/react-ui"; import { ThreadData } from "@liveblocks/client"; @@ -11,6 +11,7 @@ export function DraggableThread({ thread }: { thread: ThreadData }) { const defaultOpen = useMemo(() => { return Number(new Date()) - Number(new Date(thread.createdAt)) <= 100; }, [thread]); + const [isOpen, setIsOpen] = useState(defaultOpen); // Enable drag const { isDragging, attributes, listeners, setNodeRef, transform } = @@ -23,31 +24,28 @@ export function DraggableThread({ thread }: { thread: ThreadData }) { const x = transform ? transform.x + thread.metadata.x : thread.metadata.x; const y = transform ? transform.y + thread.metadata.y : thread.metadata.y; - // Used to set z-index higher than other threads when dragging - const editThreadMetadata = useEditThreadMetadata(); + // Used to set z-index higher than other threads when open or dragging const maxZIndex = useMaxZIndex(); + const currentZIndex = + isOpen || isDragging ? maxZIndex + 1 : thread.metadata?.zIndex || 0; return (
- editThreadMetadata({ - threadId: thread.id, - metadata: { zIndex: maxZIndex + 1 }, - }) - } style={{ position: "absolute", top: 0, left: 0, transform: `translate3d(${x}px, ${y}px, 0)`, - zIndex: thread.metadata?.zIndex || 0, + zIndex: currentZIndex, }} >