Toolbar Drop Down for Headings #1396
-
Hi, would anyone know if there is an example of using a drop down for headings instead of using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I would also like to learn how to do this! |
Beta Was this translation helpful? Give feedback.
-
Define a collection of options and the element types we want to map them to so we can render our dropdown: const elementOptions = [
{ type: ELEMENT_DEFAULT, label: "Normal" },
{ type: ELEMENT_H1, label: "Heading 1" },
{ type: ELEMENT_H2, label: "Heading 2" },
{ type: ELEMENT_H3, label: "Heading 3" },
{ type: ELEMENT_H4, label: "Heading 4" },
{ type: ELEMENT_H5, label: "Heading 5" },
{ type: ELEMENT_H6, label: "Heading 6" },
]; Now, let's create our select box which is configured to toggle the mark type on change. You'll need to import const LevelSelect = () => {
const editor = usePlateEditorRef("body");
const parent = getParent(editor, editor?.selection?.anchor);
const node = parent && Array.isArray(parent) ? parent[0] : null;
return (
<select
id="level"
name="level"
onChange={(e: FormEvent<HTMLSelectElement>) =>
toggleNodeType(editor, {
activeType: e.currentTarget.value,
inactiveType: ELEMENT_DEFAULT,
})
}
value={node?.type}
>
{elementOptions.map((option) => (
<option
value={option.type}
key={option.type}
>
{option.label}
</option>
))}
</select>
);
}; Hopefully this helps send you on the right path! Note that there are different ways to get a (Edit 4: I've updated this to show a way to do this without the |
Beta Was this translation helpful? Give feedback.
Define a collection of options and the element types we want to map them to so we can render our dropdown:
Now, let's create our select box which is configured to toggle the mark type on change. You'll need to import
usePlateEditorRef
andgetParent
: