The fastest way to add toggle functionality is with the ToggleKit, which includes pre-configured TogglePlugin with indent support and their Plate UI components.
'use client';
import { BaseTogglePlugin } from '@platejs/toggle';
import { TogglePlugin } from '@platejs/toggle/react';
import { ChevronRight } from 'lucide-react';
import {
type PlateElementProps,
PlateElement,
useEditor,
useEditorPlugin,
usePluginStore,
} from 'platejs/react';
import * as React from 'react';
import { Button } from '@/components/ui/button';
import { IndentKit } from '@/components/editor/indent';
export function ToggleElement(props: PlateElementProps<typeof TogglePlugin>) {
const editor = useEditor();
const { element } = props;
const toggleKey = editor.key(element);
const open = usePluginStore(BaseTogglePlugin, 'openKeys').has(toggleKey);
const { api } = useEditorPlugin(BaseTogglePlugin);
return (
<PlateElement {...props} className="pl-6">
<Button
size="icon"
variant="ghost"
className="absolute top-0 -left-0.5 size-6 cursor-pointer items-center justify-center rounded-md p-px text-muted-foreground transition-colors select-none hover:bg-accent [&_svg]:size-4"
contentEditable={false}
onClick={(event) => {
event.preventDefault();
api.toggleKeys([toggleKey]);
}}
onMouseDown={(event) => {
event.preventDefault();
}}
>
<ChevronRight
className={
open
? 'rotate-90 transition-transform duration-75'
: 'rotate-0 transition-transform duration-75'
}
/>
</Button>
{props.children}
</PlateElement>
);
}
export const ToggleKit = [
...IndentKit,
TogglePlugin.configure({ component: ToggleElement }),
];'use client';
import { BaseTogglePlugin } from '@platejs/toggle';
import { TogglePlugin } from '@platejs/toggle/react';
import { ChevronRight } from 'lucide-react';
import {
type PlateElementProps,
PlateElement,
useEditor,
useEditorPlugin,
usePluginStore,
} from 'platejs/react';
import * as React from 'react';
import { Button } from '@/components/ui/button';
import { IndentKit } from '@/components/editor/indent';
export function ToggleElement(
IndentKit: Provides indent support for toggle elements.ToggleElement: Renders toggle elements.Add the kit to your plugins:
import { createPlateEditor } from 'platejs/react';
import { ToggleKit } from '@/components/editor/toggle';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
...ToggleKit,
],
});import { createPlateEditor } from 'platejs/react';
import { ToggleKit } from '@/components/editor/toggle';
const editor = createPlateEditor
Include TogglePlugin and IndentPlugin in your Plate plugins array when creating the editor.
import { IndentPlugin } from '@platejs/indent/react';
import { TogglePlugin } from '@platejs/toggle/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
IndentPlugin,
TogglePlugin,
],
});import { IndentPlugin } from '@platejs/indent/react';
import { TogglePlugin }
Configure the IndentPlugin and TogglePlugin with proper targeting and component assignment.
import { IndentPlugin } from '@platejs/indent/react';
import { TogglePlugin } from '@platejs/toggle/react';
import { PLUGINS } from 'platejs';
import { createPlateEditor } from 'platejs/react';
import { ToggleElement } from '@/components/editor/toggle';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
IndentPlugin.configure({
targetPlugins: [
PLUGINS.heading, PLUGINS.heading, PLUGINS.heading, PLUGINS.heading, PLUGINS.heading, PLUGINS.heading,
PLUGINS.paragraph,
PLUGINS
component: Assigns ToggleElement to render toggle elements.IndentPlugin.targetPlugins: Configures which element plugin names support indentation, including toggles.You can add ToggleToolbarButton to your Toolbar to insert toggle elements.
You can add this item to the Turn Into Toolbar Button to convert blocks into toggles:
{
icon: <ChevronRightIcon />,
label: 'Toggle list',
value: PLUGINS.toggle,
}{
icon: <ChevronRightIcon />,
label: 'Toggle list',
value: PLUGINS.toggle,
}You can add this item to the Insert Toolbar Button to insert toggle elements:
{
icon: <ChevronRightIcon />,
label: 'Toggle list',
value: PLUGINS.toggle,
}{
icon: <ChevronRightIcon />,
label: 'Toggle list',
value: PLUGINS.toggle,
}Plugin for managing toggle functionality.
Toggles the open state of specified toggles.
Hook for getting toggle toolbar button state.
Hook for handling toggle toolbar button behavior.
Hook for getting toggle button state.
Hook for handling toggle button behavior.
import { IndentPlugin } from '@platejs/indent/react';
import { TogglePlugin } from '@platejs/toggle/react';
import { PLUGINS } from 'platejs';
import { createPlateEditor } from 'platejs/react';
import { ToggleElement } from '@/components/editor/toggle';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
IndentPlugin.configure({
targetPlugins: [
PLUGINS.heading, PLUGINS.heading, PLUGINS.heading, PLUGINS.heading, PLUGINS.heading, PLUGINS.heading,
PLUGINS.paragraph,
PLUGINS.toggle,
],
}),
TogglePlugin.configure({ component: ToggleElement }),
],
});