Text styling extension for TipTap rich text editor providing color, background, font family, font size, and line height capabilities.
—
The BackgroundColor extension provides background color styling capabilities with advanced HTML parsing that handles nested span elements and preserves original color formats.
Extension for applying background colors to text with sophisticated CSS parsing and nested span support.
/**
* Extension for setting background color on specified node types
* Extends textStyle mark with backgroundColor attribute
*/
const BackgroundColor: Extension<BackgroundColorOptions>;
interface BackgroundColorOptions {
/**
* Node types where background color can be applied
* @default ['textStyle']
* @example ['heading', 'paragraph', 'textStyle']
*/
types: string[];
}Usage Example:
import { Editor } from "@tiptap/core";
import { BackgroundColor } from "@tiptap/extension-text-style/background-color";
import { TextStyle } from "@tiptap/extension-text-style/text-style";
const editor = new Editor({
extensions: [
TextStyle, // Required dependency
BackgroundColor.configure({
types: ['textStyle', 'paragraph']
})
]
});Apply a specific background color to the current text selection.
/**
* Set the text background color
* @param backgroundColor - CSS color value (hex, rgb, hsl, named colors, etc.)
* @returns Command result indicating success/failure
*/
setBackgroundColor(backgroundColor: string): Command;Usage Examples:
// Hex colors
editor.commands.setBackgroundColor("#ffff00");
editor.commands.setBackgroundColor("#f0f8ff");
// RGB/RGBA colors
editor.commands.setBackgroundColor("rgb(255, 255, 0)");
editor.commands.setBackgroundColor("rgba(240, 248, 255, 0.8)");
// HSL colors
editor.commands.setBackgroundColor("hsl(60, 100%, 50%)");
// Named colors
editor.commands.setBackgroundColor("yellow");
editor.commands.setBackgroundColor("lightcyan");
// Chain with other styling commands
editor.chain()
.setBackgroundColor("#ffffcc")
.setColor("#333333")
.run();Remove background color styling from the current text selection.
/**
* Unset the text background color, removing backgroundColor attribute and cleaning up empty textStyle marks
* @returns Command result indicating success/failure
*/
unsetBackgroundColor(): Command;Usage Example:
// Remove background color and clean up empty styling
editor.commands.unsetBackgroundColor();
// Chain removal with other operations
editor.chain()
.unsetBackgroundColor()
.setColor("#000000")
.run();The BackgroundColor extension includes sophisticated HTML parsing that:
background-color: declarations, using the last onebackground-color: declarations in reverse order to prioritize child styles// Example of nested span parsing with multiple background colors
// <span style="background-color: blue;"><span style="background-color: yellow;">text</span></span>
// Result: backgroundColor will be "yellow" (child takes priority)
// Example of multiple declarations in same element
// <span style="font-size: 14px; background-color: red; background-color: green;">text</span>
// Result: backgroundColor will be "green" (last declaration wins)style="background-color: {value}" attributeThe BackgroundColor extension augments TipTap's type system:
// Extends core Commands interface
declare module '@tiptap/core' {
interface Commands<ReturnType> {
backgroundColor: {
setBackgroundColor: (backgroundColor: string) => ReturnType;
unsetBackgroundColor: () => ReturnType;
}
}
}
// Extends TextStyleAttributes interface
declare module '@tiptap/extension-text-style' {
interface TextStyleAttributes {
backgroundColor?: string | null;
}
}import { BackgroundColor } from "@tiptap/extension-text-style/background-color";
const backgroundColorExtension = BackgroundColor.configure({
types: ['textStyle']
});const backgroundColorExtension = BackgroundColor.configure({
types: ['textStyle', 'heading', 'paragraph']
});This allows background colors to be applied directly to headings and paragraphs in addition to inline text styling.
The extension processes HTML style attributes with special logic:
element.style.backgroundColor if style attribute parsing failsThis approach ensures compatibility with content from various rich text editors and maintains styling priority correctly.
Install with Tessl CLI
npx tessl i tessl/npm-tiptap--extension-text-style