tiptap/docs/guide/styling.md
Dominik Biedebach 2bb371126e merge main
2023-11-23 09:28:26 +01:00

2.9 KiB
Raw Blame History

tableOfContents
true

Styling

Introduction

Tiptap is headless, that means there is no styling provided. That also means, you are in full control of how your editor looks. The following methods allow you to apply custom styles to the editor.

Option 1: Style the plain HTML

The whole editor is rendered inside of a container with the class .tiptap. You can use that to scope your styling to the editor content:

/* Scoped to the editor */
.tiptap p {
  margin: 1em 0;
}

If youre rendering the stored content somewhere, there wont be a .tiptap container, so you can just globally add styling to the used HTML tags:

/* Global styling */
p {
  margin: 1em 0;
}

Option 2: Add custom classes

You can control the whole rendering, including adding classes to everything.

Extensions

Most extensions allow you to add attributes to the rendered HTML through the HTMLAttributes option. You can use that to add a custom class (or any other attribute). Thats also very helpful, when you work with Tailwind CSS.

new Editor({
  extensions: [
    Document,
    Paragraph.configure({
      HTMLAttributes: {
        class: 'my-custom-paragraph',
      },
    }),
    Heading.configure({
      HTMLAttributes: {
        class: 'my-custom-heading',
      },
    }),
    Text,
  ],
})

The rendered HTML will look like this:

<h1 class="my-custom-heading">Example Text</h1>
<p class="my-custom-paragraph">Wow, thats really custom.</p>

If there are already classes defined by the extensions, your classes will be added.

Editor

You can even pass classes to the element which contains the editor:

new Editor({
  editorProps: {
    attributes: {
      class: 'prose prose-sm sm:prose lg:prose-lg xl:prose-2xl mx-auto focus:outline-none',
    },
  },
})

With Tailwind CSS

The editor works fine with Tailwind CSS, too. Find an example thats styled with the @tailwindcss/typography plugin below.

https://embed.tiptap.dev/preview/Experiments/Tailwind

Intellisense

If you're using TailwindCSS Intellisense add the following snippet to your .vscode/setting.json to add intellisense support inside TipTap objects:

"tailwindCSS.experimental.classRegex": [
  "class:\\s*?[\"'`]([^\"'`]*).*?,"
]

Option 3: Customize the HTML

Or you can customize the markup for extensions. The following example will make a custom bold extension that doesnt render a <strong> tag, but a <b> tag:

import Bold from '@tiptap/extension-bold'

const CustomBold = Bold.extend({
  renderHTML({ HTMLAttributes }) {
    // Original:
    // return ['strong', HTMLAttributes, 0]
    return ['b', HTMLAttributes, 0]
  },
})

new Editor({
  extensions: [
    // …
    CustomBold,
  ],
})

You should put your custom extensions in separate files, but I think you got the idea.