Trying to set up a custom
slatecomponent on v3 beta 125. I'm running into issues that my custom component isn't running within the Slate context.
import type { RichTextCustomElement } from "@payloadcms/richtext-slate";
export const SlateButton: RichTextCustomElement = {
name: 'button',
Button: "@/payload/blocks/content-area/elements/button/toolbar-button#ToolbarButtonButton",
Element: "@/payload/blocks/content-area/elements/button/element#ButtonElement",
plugins: ["@/payload/blocks/content-area/elements/button/plugin#WithInlineButton"],
};export const ButtonElement: React.FC<any> = (props) => {
const { attributes, children, editorRef, element, fieldProps, schemaPath } =
useElement<LinkButtonProps>() // this works fine, new in beta.119
const { style = "primary", button_text, href } = element;
const [showUrl, setShowUrl] = React.useState(false);export const ToolbarButtonButton: React.FC<{ path: string }> = ({ path }) => {
const { openModal, toggleModal } = useModal();
const editor = useSlateStatic() // <--- FAILS HERE'use client';
import { useSlatePlugin } from "@payloadcms/richtext-slate/client";
import type { Editor } from 'slate'
type Plugin = (editor: Editor) => Editor;
const inlineButton: Plugin = (incomingEditor: Editor): Editor => {
const editor = incomingEditor;
const { isVoid } = editor;
editor.isVoid = (element) =>
(element as any).type === "button" ? true : isVoid(element);
return editor;
};
export const WithInlineButton = () => {
useSlatePlugin('withInlineButton', inlineButton)
return null
}Any ideas?
The "Slate.js plugin | Error: The useSlate hook must be used inside the <Slate> component's context." link in the community help solved my issue -- removed those packages from my package.json and everything works.
Star
Discord
online
Get dedicated engineering support directly from the Payload team.