Blockleaf

Text styles

Text and background colour, font family and size, subscript, superscript and text alignment, as an opt-in subpath.

Text styles ship as an opt-in subpath, not in the default bundle.

import { presets } from '@blockleaf/editor/extensions';
import { withTextStyle } from '@blockleaf/editor/text-style';

const extensions = withTextStyle(presets.standard());
<BlockleafEditor extensions={extensions} />

They are new in schema version 2.

Text colour

Select text, then Color in the bubble menu. The panel has two radio groups, Text and Background, each Default and nine colours. The arrow keys move within a group, Tab goes to the next, Enter picks, Escape goes back to the toolbar.

{ "type": "textColor", "attrs": { "color": "red" } }
  • A token, not a colour. red resolves through --bl-text-red, which the stylesheet sets to a red that reads on white and a lighter one on the dark theme. Every built-in colour is at least 4.5:1 on its background. A pasted color: #f00 arrives uncoloured.
  • One colour at a time. Picking another replaces it; Default takes it off.
  • Your own colours. Any lower-case token works — setTextColor('brand') — once you define its custom property. The picker offers the built-in nine; add yours with editor.commands.setTextColor from chrome of your own.
.blockleaf-editor {
	--bl-text-brand: #0f766e;
}
  • Rendered as <span data-color="red" style="color: var(--bl-text-red, currentColor)"> by the editor and both @blockleaf/renderer targets. A token with no custom property falls back to the text colour.
TokenLightDark
gray#6b7280#9ca3af
brown#8a5a3c#d4a373
orange#c2410c#fb923c
yellow#a16207#facc15
green#15803d#4ade80
blue#1d4ed8#60a5fa
purple#7e22ce#c084fc
pink#be185d#f472b6
red#b91c1c#f87171

Coloured text on the yellow highlight can fall below 4.5:1 in the light theme — gray, orange, yellow and green do. Use the two together sparingly.

Background colour

The Color panel's second group. A background is the highlight mark the schema has had since 0.1.0, with a token in its color:

{ "type": "highlight", "attrs": { "color": "blue" } }
  • The same nine names as text colour, each with a pale tint for the light theme and a deep one for the dark theme. Body text is at least 4.5:1 on every one of them, and so are links.
  • The plain highlight is unchanged. The bubble menu's Highlight button still stores { "color": null } and draws --bl-color-highlight.
  • A colour stored before tokens still shows. A host that called Tiptap's setHighlight({ color: '#ffee00' }) keeps its yellow: a hex, rgb() or hsl() value is drawn as it is, and a colour name such as gold falls back to itself. Anything else — a value with a ; in it — is dropped and the text is drawn with the plain highlight.
  • Rendered as <mark data-color="blue" style="background-color: var(--bl-highlight-blue, blue); color: inherit"> by the editor and both renderer targets.
  • The background group needs the highlight mark, so the minimal preset shows only the Text group.
TokenLightDark
gray#ebeced#2d333b
brown#f2e6dc#3d2b1f
orange#fde3cf#4a2b12
yellow#fef08a#38330c
green#d9f5e1#12351f
blue#dbe8fe#152b4f
purple#ece6fd#2f2150
pink#fce4f1#47162f
red#fde0e0#4b1818

Colour, font and size are marks of their own rather than attributes of one shared textStyle mark. An editor keeps a mark it does not know but drops an attribute it does not know, so separate marks are never lost on save.

Font family

Select text, then Font in the bubble menu: Default, Sans serif, Serif and Monospace, each shown in its own face.

{ "type": "fontFamily", "attrs": { "family": "serif" } }
  • A token, never a font name. serif resolves through --bl-font-serif. Change the property and every document changes with it; a font the reader does not have never reaches the page by name. A pasted font-family: Arial arrives in the default face.
  • Built in: sans (your --bl-font-body), serif (the system serif) and mono (your --bl-font-mono). Any other lower-case token works once you define --bl-font-<token>:
.blockleaf-editor {
	--bl-font-display: 'Fraunces', var(--bl-font-serif);
}
  • Rendered as <span data-font-family="serif" style="font-family: var(--bl-font-serif, inherit)"> by the editor and both renderer targets.

Font size

The Font panel's second group: Default, Small, Large and Extra large. Tab moves between the family group and the size group.

{ "type": "fontSize", "attrs": { "size": "lg" } }
TokenDefault valueShown as
sm0.875emSmall
lg1.25emLarge
xl1.5emExtra large
  • A token, never a length. Each resolves through --bl-font-size-<token>, in em, so it scales with the editor's own size and with a reader's text size setting — a stored 18px would do neither. A pasted font-size: 18px arrives at the normal size.
  • There is no "medium". The body size is no mark at all. Anything bigger than xl is a heading's job, and headings carry meaning a big font does not.
  • Your own sizes work like your own colours: any lower-case token, once you define --bl-font-size-<token>.
  • Rendered as <span data-font-size="lg" style="font-size: var(--bl-font-size-lg, 1em)"> by the editor and both renderer targets.

Subscript and superscript

StyleShortcutStored asWritten as
SubscriptMod+,{ "type": "subscript" }<sub>
SuperscriptMod+.{ "type": "superscript" }<sup>
  • One or the other, never both. Text cannot sit above and below the line at once, so turning one on turns the other off. A stored run never carries both.
  • The bubble menu gains two buttons, Subscript and Superscript, after Highlight. An editor without the subpath has no such marks, so the buttons are not offered there; see actions follow the extension set.
  • Pasted <sub> and <sup> arrive as the marks, from a web page or a word processor.
  • @blockleaf/renderer writes <sub> and <sup> on both targets, with no extra import.

Text alignment

AlignmentShortcutStored as
LeftMod+Shift+L"attrs": { "textAlign": "left" }
CenterMod+Shift+E"attrs": { "textAlign": "center" }
RightMod+Shift+R"attrs": { "textAlign": "right" }
JustifyMod+Shift+J"attrs": { "textAlign": "justify" }
  • Paragraphs and headings. Every one in the selection, so a whole section aligns at once. Other blocks are left alone.
  • The same choice again takes it off, and the block goes back to the language's own direction — left in English, right in Arabic. An unaligned block stores nothing at all.
  • The block menu gains an Align group — Left, Center, Right, Justify — for a paragraph or heading.
  • Only those four values are stored. Pasted text-align: center arrives aligned; anything else, such as start, arrives unaligned. A stored value outside the four fails validation, and the renderer ignores it rather than writing it into a style attribute.
  • @blockleaf/renderer writes style="text-align: …", as the editor does.

Why the attribute is not in the subpath

The commands, shortcuts and menu entries come with @blockleaf/editor/text-style. The textAlign attribute itself is in every preset. An editor keeps a mark it does not know, but it drops an attribute it does not know — so if the attribute lived here, opening an aligned document in an editor without the subpath and saving it would remove the alignment, silently. With the attribute in the core, every editor shows and keeps it; only setting it needs the subpath.

Without the subpath

An editor that does not load @blockleaf/editor/text-style still opens a document that uses these styles. Subscript and superscript text shows plainly, and the marks are saved back exactly as they came in. Alignment shows and is kept, as above; it just cannot be changed. See a block the editor does not know.

withTextStyle versus createTextStyle

withTextStyle removes any extension with the same name before adding its own, so adding it twice, or over Tiptap's own subscript, never leaves two extensions fighting over one name. createTextStyle() returns the same extensions as a plain array, for a list you are assembling from scratch.

Cost

Measured with size-limit, minified and brotlied: the editor is 152.58 kB, and the editor with text styles is 153.03 kB, so 0.45 kB for the marks, the alignment, colour and font commands, and their shortcuts. Without the import, the main bundle still carries the alignment attribute and the chrome that offers these styles — the block menu's Align entries and the bubble menu's font and colour panels — and none of it appears.

On this page