Components
A visual overlay that highlights selected blocks in a Plate rich-text editor.
Loading preview...
"use client";
import * as React from "react";
import { BlockSelection } from "@/components/ui/block-selection";
import { BlockSelectionPlugin } from "@platejs/selection/react";
import { Plate, PlateContent, usePlateEditor } from "platejs/react";
export default function BlockSelectionDemo() {
const editor = usePlateEditor({
plugins: [
BlockSelectionPlugin.configure({
options: { enableContextMenu: true },
render: {
belowRootNodes: (props) => <BlockSelection {...(props as any)} />,
},
}),
],
value: [
{
id: "1",
type: "p",
children: [{ text: "This block is selected — note the blue overlay." }],
},
{
id: "2",
type: "p",
children: [
{ text: "Drag from the editor padding to select multiple blocks." },
],
},
],
});
React.useEffect(() => {
editor.api.blockSelection?.set?.(["1"]);
}, [editor]);
return (
<div className="w-full max-w-xl p-6">
<Plate editor={editor}>
<PlateContent className="relative rounded-md border p-4 leading-7 outline-none [&_[data-slate-node=element]]:relative [&_[data-slate-node=element]]:py-1" />
</Plate>
</div>
);
}