Text styling extension for TipTap rich text editor providing color, background, font family, font size, and line height capabilities.
—
The TextStyle mark is the foundational mark for all text styling operations in TipTap. It creates HTML span elements and provides the base functionality that other styling extensions build upon.
Core ProseMirror mark that handles text styling through HTML span elements with advanced nested style parsing.
/**
* Core text style mark for HTML span element handling
* Priority: 101 (high priority to ensure proper layering)
*/
const TextStyle: Mark<TextStyleOptions>;
interface TextStyleOptions {
/** HTML attributes to add to the span element */
HTMLAttributes: Record<string, any>;
/**
* When enabled, merges styles of nested spans into child span during HTML parsing
* Prioritizes the style of the child span, used when parsing content from other editors
*/
mergeNestedSpanStyles: boolean;
}Usage Example:
import { Editor } from "@tiptap/core";
import { TextStyle } from "@tiptap/extension-text-style/text-style";
const editor = new Editor({
extensions: [
TextStyle.configure({
HTMLAttributes: {
class: "custom-text-style"
},
mergeNestedSpanStyles: true
})
]
});Apply or remove text styling attributes on the current selection.
/**
* Toggle text style attributes on the current selection
* @param attributes - Text style attributes to apply or toggle off
* @returns Command result indicating success/failure
*/
toggleTextStyle(attributes?: TextStyleAttributes): Command;Usage Example:
// Apply multiple attributes at once
editor.commands.toggleTextStyle({
color: "#ff0000",
fontSize: "18px",
fontFamily: "Arial"
});
// Toggle off all styling
editor.commands.toggleTextStyle();Clean up empty text style marks that have no meaningful attributes.
/**
* Remove spans without inline style attributes
* Traverses selection and removes textStyle marks with no attribute values
* @returns Command result indicating success/failure
*/
removeEmptyTextStyle(): Command;Usage Example:
// Clean up empty styling after removing attributes
editor.chain()
.setMark("textStyle", { color: null })
.removeEmptyTextStyle()
.run();The TextStyle mark includes sophisticated HTML parsing logic:
<span> elements that have a style attributemergeNestedSpanStyles is enabled, combines parent and child span styles with child taking priority<span> elements with merged HTML attributesMost styling extensions depend on TextStyle:
// Styling extensions import TextStyle
import '../text-style/index.js'TextStyle integrates with TipTap's command system through module augmentation:
declare module '@tiptap/core' {
interface Commands<ReturnType> {
textStyle: {
removeEmptyTextStyle: () => ReturnType;
toggleTextStyle: (attributes?: TextStyleAttributes) => ReturnType;
}
}
}Extends the TextStyleAttributes interface for use by other extensions:
import type { TextStyleAttributes } from '../index.js';Install with Tessl CLI
npx tessl i tessl/npm-tiptap--extension-text-style