Skip to content

Text Style

The TextStyle mark is a carrier mark that wraps text in a <span> element. It has no visual output on its own - it serves as a container for other extensions (TextColor, FontFamily, FontSize, Highlight) that inject CSS attributes into it via addGlobalAttributes. Not included in StarterKit - must be added separately.

Use TextStyle when you need:

  • A carrier mark that other extensions inject color, font-family, font-size, or background-color attributes into
  • Automatic cleanup of empty <span> wrappers when all child attributes become null

You do not usually configure TextStyle directly. TextColor, FontFamily, and FontSize pull it in for you through addExtensions(). Highlight does not: it only declares dependencies: ['textStyle'], so if you use Highlight without any of those three, add TextStyle yourself or the editor throws Extension "highlight" requires "textStyle" extension. Please add it to your extensions array. at startup. Add it explicitly when:

  • You use Highlight and none of TextColor, FontFamily, or FontSize
  • You implement a custom inline-style extension and want to share the <span> carrier
  • You want to set HTMLAttributes on the carrier <span> (the only option TextStyle takes)

TextStyle is not included in StarterKit. Add it when using TextColor, FontFamily, FontSize, or Highlight:

import { Document, Paragraph, Text, TextStyle, TextColor } from '@domternal/core';
import { DomternalEditor } from '@domternal/vanilla';
import '@domternal/theme';
const dm = new DomternalEditor(document.getElementById('editor')!, {
extensions: [Document, Paragraph, Text, TextStyle, TextColor],
});
PropertyValue
ProseMirror nametextStyle
TypeMark
HTML tag<span>
Groupformatting
Priority101

Membership of the formatting group is what makes TextStyle mutually exclusive with inline code: Code declares excludes: 'formatting' and never names the marks it excludes. Because TextStyle is the carrier, every attribute riding on it inherits that exclusion, so applying code to a range drops its text color, background color, font family and font size in one go, and parsing does the same: <code><span style="color:red">x</span></code> comes in as plain code. See Excluding third-party marks for how a mark of your own joins the group or stays out of it.

SourceCondition
<span>When it has a style attribute, or a data-text-color / data-bg-color attribute
<mark>Always (used by Highlight extension)

A span is parsed when it carries styling: inline CSS, or the named color tokens data-text-color / data-bg-color. Those tokens are the serialized form of the colorToken / backgroundColorToken attributes that Text Color and Highlight add to textStyle, and they are what the Notion Color Picker writes instead of an inline style. A plain <span> with neither is ignored, so unstyled wrappers are not turned into marks.

OptionTypeDefaultDescription
HTMLAttributesRecord<string, unknown>{}Custom HTML attributes added to the rendered <span> element

Applies a text style with specific attributes.

editor.commands.setTextStyle({ color: 'red' });

Removes the text style mark entirely.

editor.commands.removeTextStyle();

Removes text style marks that have no meaningful attributes (all attributes are null/undefined). This prevents empty <span> wrappers from remaining in the document.

editor.commands.removeEmptyTextStyle();

TextStyle has no keyboard shortcuts.

TextStyle has no input rules.

TextStyle has no toolbar items. The extensions that use it (TextColor, FontFamily, FontSize, Highlight) provide their own toolbar items.

TextStyle is a carrier mark. On its own, it renders an empty <span>. Other extensions add attributes to it:

// TextColor adds a 'color' style attribute
editor.commands.setTextColor('#ff0000');
// Renders: <span style="color: #ff0000">text</span>
// FontFamily adds a 'font-family' style attribute
editor.commands.setFontFamily('Georgia');
// Renders: <span style="font-family: Georgia">text</span>
// FontSize adds a 'font-size' style attribute
editor.commands.setFontSize('24px');
// Renders: <span style="font-size: 24px">text</span>

Multiple styles can be combined on a single <span>:

<span style="color: #ff0000; font-family: Georgia; font-size: 24px">styled text</span>
import { TextStyle } from '@domternal/core';
import type { TextStyleOptions } from '@domternal/core';
ExportTypeDescription
TextStyleMark extensionThe text style carrier mark
TextStyleOptionsTypeScript typeOptions for TextStyle.configure()

@domternal/core - TextStyle.ts