Toolbar Plugin

PreviousNext

A plugin for the toolbar.

Docs
shadcn-editorui

Preview

Loading preview…
registry/new-york-v4/editor/plugins/toolbar/toolbar-plugin.tsx
"use client"

import { useEffect, useState } from "react"
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"
import { COMMAND_PRIORITY_CRITICAL, SELECTION_CHANGE_COMMAND } from "lexical"

import { ToolbarContext } from "@/registry/new-york-v4/editor/context/toolbar-context"
import { useEditorModal } from "@/registry/new-york-v4/editor/editor-hooks/use-modal"

export function ToolbarPlugin({
  children,
}: {
  children: (props: { blockType: string }) => React.ReactNode
}) {
  const [editor] = useLexicalComposerContext()

  const [activeEditor, setActiveEditor] = useState(editor)
  const [blockType, setBlockType] = useState<string>("paragraph")

  const [modal, showModal] = useEditorModal()

  const $updateToolbar = () => {}

  useEffect(() => {
    return activeEditor.registerCommand(
      SELECTION_CHANGE_COMMAND,
      (_payload, newEditor) => {
        setActiveEditor(newEditor)
        return false
      },
      COMMAND_PRIORITY_CRITICAL
    )
  }, [editor])

  return (
    <ToolbarContext
      activeEditor={activeEditor}
      $updateToolbar={$updateToolbar}
      blockType={blockType}
      setBlockType={setBlockType}
      showModal={showModal}
    >
      {modal}

      {children({ blockType })}
    </ToolbarContext>
  )
}

Installation

npx shadcn@latest add @shadcn-editor/toolbar-plugin

Usage

import { ToolbarPlugin } from "@/components/ui/toolbar-plugin"
<ToolbarPlugin />