Rich text / foldkit-mixins-richtext
mixins-richtext
A rich-text editor draws only its editable text box. Everything around it—formatting toolbars, slash command menus, drag handles, table controls, find-and-replace bars, and Markdown source mode—must still be wired to your application state and styled to match your design system without forking the views.
foldkit-mixins-richtext provides those chrome views as styleable Mixins components for foldkit-richtext-dom. Each view exposes named Slots (typed parts such as root, button, or input that accept styles and behaviors from outside), reads state you pass in, and dispatches Messages back to your update. See the full toolbar, slash menu, block handle, and find bar running in the rich-text playground.
Install
pnpm add foldkit-mixins-richtext foldkit-mixins foldkit-richtext foldkit-richtext-domA first mark toolbar
Style the mark buttons via MarkToolbarSlots and render a toolbar that dispatches ToggledMark Messages:
import type { HtmlBuilder } from 'foldkit/html'
import { Style } from 'foldkit-mixins'
import { MarkToolbarSlots, markToolbar } from 'foldkit-mixins-richtext'
import * as RichText from 'foldkit-richtext'
import { Message as EditorMessage } from 'foldkit-richtext-dom/editor'
const Toolbar = markToolbar<EditorMessage>().pipe(
Style.attach(Style.forSlots(MarkToolbarSlots)({ button: Style.class('mark-button') })),
)
const document = RichText.decodeDocument({
version: 1,
children: [{ type: 'Paragraph', id: 'p', children: [{ type: 'Text', id: 't', text: 'Hello', marks: ['Bold'] }] }],
})
const renderToolbar = (h: HtmlBuilder<EditorMessage>) =>
Toolbar(
{
state: { document, selection: null, storedMarks: null },
toggled: mark => EditorMessage.ToggledMark({ mark }),
},
h,
)Toolbar renders a <div role="toolbar"> containing one <button class="mark-button"> per mark (Bold, Italic, Code by default). Each button reads markActive(state, mark) from the document and selection you pass in, carries its mark name as its slot item ID so Behaviors can target it, and emits ToggledMark through toggled when clicked.
Every view in this package keeps the same boundary:
- Your application owns chrome state: Open menus, draft link URLs, palette queries, and tab stops stay in your Model.
- Document state is read, never stored: Active buttons, block styles, links, and slash queries derive directly from
documentandselection. - Every element is a Slot: You attach classes, tokens, and Behaviors from outside without touching the view's click or ARIA logic.
| Slot | Capability | Renders |
|---|---|---|
root | Container | Toolbar wrapper |
toolbar | Collection | Row of mark buttons |
button | Interactive | One button per mark |
Place Toolbar inside a selectionAnchor element from foldkit-richtext-dom/toolbar to float it above the current text selection.