tiptap/docs/src/docPages/guide/custom-styling.md

76 lines
1.7 KiB
Markdown
Raw Normal View History

2020-09-16 23:01:47 +08:00
# Custom styling
2020-09-27 16:29:01 +08:00
## Table of Contents
## Introduction
Tiptap is renderless, that doesnt mean there is no styling provided. You can decided how your editor should look like.
2020-09-22 22:03:31 +08:00
## Option 1: Style the plain HTML
The whole editor is rendered inside of a container with the class `.ProseMirror`. You can use that to scope your styling to the editor content:
2020-09-16 23:01:47 +08:00
```css
2020-09-22 22:03:31 +08:00
/* Scoped to the editor */
.ProseMirror p {
2020-09-22 22:03:31 +08:00
margin: 1em 0;
}
```
If youre rendering the stored content somewhere, there wont be a `.ProseMirror` container, so you can just globally add styling to the used HTML tags:
```css
/* Global styling */
p {
2020-09-16 23:01:47 +08:00
margin: 1em 0;
}
```
2020-09-22 22:03:31 +08:00
## Option 2: Add custom classes
Most extensions have a `class` option, which you can use to add a custom CSS class to the HTML tag.
2020-09-16 23:01:47 +08:00
```js
2020-09-22 22:03:31 +08:00
/* Add custom classes */
2020-09-16 23:01:47 +08:00
new Editor({
extensions: [
Document(),
Paragraph({
class: 'my-custom-paragraph',
}),
Heading({
class: 'my-custom-heading',
}),
Text(),
]
})
```
2020-09-22 22:03:31 +08:00
The rendered HTML will look like that:
2020-09-16 23:01:47 +08:00
```html
<h1 class="my-custom-heading">Example Text</p>
<p class="my-custom-paragraph">Wow, thats really custom.</p>
```
2020-09-22 22:03:31 +08:00
## Option 3: Customize the HTML
2020-09-16 23:01:47 +08:00
You can even customize the markup for every extension. This will make a custom bold extension that doesnt render a `<strong>` tag, but a `<b>` tag:
```js
2020-09-22 22:03:31 +08:00
/* Customizing the markup */
2020-09-16 23:01:47 +08:00
import Bold from '@tiptap/extension-bold'
const CustomBold = Bold
.schema(() => ({
toDOM: () => ['b', 0],
}))
.create()
new Editor({
extensions: [
// …
CustomBold(),
]
})
```
You should put your custom extensions in separate files though, but I think youve got the idea.