-
-
Notifications
You must be signed in to change notification settings - Fork 110
Decorations api guide #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bdbch
wants to merge
7
commits into
main
Choose a base branch
from
decorations-api-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5c65602
update vanilla javascript docs and remove cdn page for redundancy
bdbch 212c101
fix broken link
bdbch 6dad5c8
fix anchor link
bdbch 6edca39
add decoration docs
bdbch 99ebaa5
added note about spec
bdbch 93028f6
dont mix in react imports
bdbch f46c76a
Update src/content/editor/core-concepts/decorations.mdx
bdbch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| --- | ||
| title: Decorations | ||
| description: How to use Tiptap's Decorations API - implementing decorations in extensions, decoration types, and performance tips. | ||
| meta: | ||
| title: Decorations | Tiptap Editor Docs | ||
| description: Learn how to implement and optimize decorations in Tiptap extensions. | ||
| category: Editor | ||
| --- | ||
|
|
||
| ## TL;DR | ||
|
|
||
| Decorations let you draw styling or UI on top of the document without changing the document content. Add a `decorations()` factory to an extension and return a small array of simple decoration items. Use `shouldUpdate` to avoid unnecessary recalculation. | ||
|
|
||
| ## What is the Decoration API? | ||
|
|
||
| The Decoration API is a small, extension-facing surface that lets extensions describe visual decorations (highlights, badges, widgets) without mutating the document. Tiptap turns those descriptions into ProseMirror decorations and renders them in the editor view. | ||
|
|
||
| ## Decoration types | ||
|
|
||
| - inline – styling applied to a range of text (e.g., highlights). | ||
| - node – attributes applied to an entire node (e.g., add a class to a paragraph). | ||
| - widget – a DOM node rendered at a specific position (e.g., an inline badge or button). Widgets are non-editable. | ||
|
|
||
| ## How to add decorations to an extension | ||
|
|
||
| Add a `decorations()` factory to your extension that returns an object with `create({ state, view, editor })` and an optional `shouldUpdate`. | ||
|
|
||
| Inline example (using the helper): | ||
|
|
||
| ```js | ||
| import { Extension, createInlineDecoration } from '@tiptap/core' | ||
|
|
||
| export const MakeWordRed = Extension.create({ | ||
| name: 'makeWordRed', | ||
|
|
||
| decorations: () => ({ | ||
| create({ state }) { | ||
| // return an array of decoration items | ||
| return [createInlineDecoration(5, 11, { style: 'color: red' })] | ||
| }, | ||
| shouldUpdate: ({ tr }) => tr.docChanged, | ||
| }), | ||
| }) | ||
| ``` | ||
|
|
||
| Node example (using the helper): | ||
|
|
||
| ```js | ||
| import { Extension, createNodeDecoration } from '@tiptap/core' | ||
|
|
||
| export const HighlightParagraph = Extension.create({ | ||
| name: 'highlightParagraph', | ||
|
|
||
| decorations: () => ({ | ||
| create({ state }) { | ||
| // highlight the first paragraph | ||
| const pos = 0 | ||
| const end = 50 | ||
| return [createNodeDecoration(pos, end, { class: 'my-paragraph' })] | ||
| }, | ||
| shouldUpdate: ({ tr }) => tr.docChanged, | ||
| }), | ||
| }) | ||
| ``` | ||
|
|
||
| Widget example (using the helper): | ||
|
|
||
| ```js | ||
| import { Extension, createWidgetDecoration } from '@tiptap/core' | ||
|
|
||
| export const StarAfter = Extension.create({ | ||
| name: 'starAfter', | ||
|
|
||
| decorations: () => ({ | ||
| create() { | ||
| return [ | ||
| createWidgetDecoration(10, () => { | ||
| const el = document.createElement('span') | ||
| el.textContent = ' ⭐' | ||
| el.setAttribute('contenteditable', 'false') | ||
| return el | ||
| }), | ||
| ] | ||
| }, | ||
| shouldUpdate: ({ tr }) => tr.docChanged, | ||
| }), | ||
| }) | ||
| ``` | ||
|
|
||
| ## Utility functions | ||
|
|
||
| - `createInlineDecoration(from, to, attributes)` - returns a small object describing an inline decoration. | ||
| - `createNodeDecoration(from, to, attributes)` - returns a node decoration object. | ||
| - `createWidgetDecoration(at, widget)` - returns a widget item where `widget` is a function that creates a DOM node. | ||
|
|
||
| Use these helpers for concise examples; under the hood Tiptap maps these items to ProseMirror decorations. | ||
|
|
||
| ## Create a decoration without helpers | ||
|
|
||
| You can return the plain decoration items directly if you prefer to avoid helpers. The shape is intentionally small and simple: | ||
|
|
||
| ```js | ||
| // manual inline decoration | ||
| return [{ type: 'inline', from: 5, to: 11, attributes: { style: 'background: yellow' } }] | ||
|
|
||
| // manual widget | ||
| return [ | ||
| { | ||
| type: 'widget', | ||
| from: 20, | ||
| to: 20, | ||
| widget: () => { | ||
| const el = document.createElement('button') | ||
| el.textContent = 'Click' | ||
| el.setAttribute('contenteditable', 'false') | ||
| return el | ||
| }, | ||
| }, | ||
| ] | ||
| ``` | ||
|
|
||
| Notes: | ||
|
|
||
| - `from`/`to` are document positions. | ||
| - For widgets, return a DOM node from the `widget` function. If you mount React components, return a container element and use the React widget helper from the React package. | ||
|
|
||
| ## Best practices & performance | ||
|
|
||
| - Use `shouldUpdate` to limit recalculation. A common simple implementation is `({ tr }) => tr.docChanged`. | ||
| - Keep `spec` data small (strings/numbers) if you add it to items - it's used to decide whether a decoration meaning changed. | ||
| - Avoid scanning the whole document every transaction. Narrow traversal to nodes of interest or cache results per node when possible. | ||
| - For widgets, provide a stable `spec.key` (or ensure your widget markup is stable) to avoid unnecessary remounts. | ||
|
|
||
| More about `spec` and stability | ||
|
|
||
| - The `spec` object on a decoration is used to decide whether a decoration's meaning changed between renders. Keep it tiny and primitive (strings, numbers, booleans). Avoid functions, DOM nodes, or large objects inside `spec`. | ||
| - For widgets, include a stable identifier in `spec` (for example `spec.key: 'comment-123'`) so the renderer can reuse the same DOM node across updates and avoid remounting React components. | ||
| - Do NOT rely only on document positions for stability. Positions move when the doc changes. Prefer a stable id from the node (for example an `id` in `node.attrs`) or a key you manage on the extension side. | ||
|
|
||
| Short widget example with a stable key: | ||
|
|
||
| ```js | ||
| return [ | ||
| { | ||
| type: 'widget', | ||
| from: pos, | ||
| to: pos, | ||
| widget: () => { | ||
| const el = document.createElement('span') | ||
| el.textContent = '⭐' | ||
| el.setAttribute('contenteditable', 'false') | ||
| return el | ||
| }, | ||
| spec: { key: 'my-widget-42' }, | ||
| }, | ||
| ] | ||
| ``` | ||
|
|
||
| - If you need to compute keys from node content, compute a stable id once and store it on the node (attrs/marks) or in a side map. Recomputing ephemeral keys on every `create` will cause remounts. | ||
| - Combine sensible `shouldUpdate` logic with stable `spec` values: `shouldUpdate` decides when to recreate the list, `spec` decides whether individual decorations should be updated/reused. | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| - Widgets flicker or lose internal state: give widgets stable keys (via `spec.key`) or avoid remounting DOM nodes. | ||
| - Widget callbacks see wrong positions after edits: call the provided `getPos()` in widget callbacks (or use the editor APIs) rather than relying on an earlier captured `pos`. | ||
| - Decorations don't update: check `shouldUpdate` and ensure it returns `true` for transactions that should trigger an update. | ||
|
|
||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The imports reference functions that don't exist in the codebase. The helper functions
createInlineDecoration,createNodeDecoration, andcreateWidgetDecorationare documented throughout this file but are not found in the actual Tiptap packages. Either these functions need to be implemented, or the documentation should be updated to show the actual API for creating decorations.