Heading
Turn paragraphs into headings from H1 to H6.
Try it
Put the caret in “Bring your own toolbar” and click Heading 2. The first block becomes <h2>Bring your own toolbar</h2>. Click Paragraph to restore a paragraph.
Live example
Example result
Add it to your editor
Enable Heading and pass a number from 1 to 6 as the second argument of setHeading. The button calls useIsActive(editor, 'setHeading', 2) to check for the particular level.
import { EditorContent, EditorProvider, Heading, useEditor, useIsActive } from 'yurich';
export function HeadingEditor() {
const editor = useEditor({
extensions: [Heading],
initialValue: "<p>Bring your own toolbar</p>" + "<p>Add a Bold button first.</p>",
});
const isH2 = useIsActive(editor, 'setHeading', 2);
return (
<EditorProvider editor={editor}>
<div role="group" aria-label="Headings">
<button
type="button"
aria-pressed={isH2}
onMouseDown={event => event.preventDefault()}
onClick={() => editor.execute('setHeading', 2)}
>
Heading 2
</button>
<button
type="button"
onMouseDown={event => event.preventDefault()}
onClick={() => editor.execute('removeHeading')}
>
Paragraph
</button>
</div>
<EditorContent ariaLabel="Editor" />
</EditorProvider>
);
}
Behavior to account for
A selection covering multiple blocks applies the heading to each of those blocks. removeHeading converts the selected blocks to paragraphs. A heading applied to a list item takes it out of the list.