Rich text / foldkit-mixins-richtext
Build a full formatting toolbar
formattingToolbar combines undo/redo, the text style menu, marks, link trigger, lists, quote, and an insert menu into a single APG role="toolbar" with roving tabindex:
import { formattingToolbar } from 'foldkit-mixins-richtext'
formattingToolbar<Message>()(
{
id: 'body-bar',
document: model.document,
selection: model.editor.selection,
storedMarks: model.editor.storedMarks,
wrap: edited,
menu: model.menu,
toggled: (menu, open) => ToggledMenu({ menu, open }),
roving: { current: model.tabStop },
onFocus: id => FocusedControl({ id }),
},
h,
)Pass a unique id to prefix button IDs for arrow navigation. Store the ID emitted by onFocus and pass it back in roving.current so Tab returns to the last focused control. Choosing an item in the style or insert menu emits through wrap, where your update closes the menu; clicking link triggers toggled('link', ...) so you can open linkEditor.
Customize the controls with items (defaulting to standardFormattingItems), using 'undo', 'redo', 'style', 'link', 'bulleted-list', 'numbered-list', 'checklist', 'quote', 'insert', 'separator', { mark: 'Underline' }, or { control }. Pass inserts (InsertEntry.Editor or InsertEntry.App) to populate the insert menu, history: { undo, redo } when your app controls undo state, words for custom labels, and shortcuts (foldkit-primitives patterns such as 'mod+u') for aria-keyshortcuts and tooltips.
ArrowLeft/ArrowRight (mirrored for direction: 'rtl'), Home, and End navigate controls; ArrowDown opens menus; unavailable actions use aria-disabled so arrow keys can still reach them.
Add custom application controls
Add { control: { id, label, icon?, shortcut?, pressed?, disabled?, message, popup? } } anywhere in items and InsertEntry.App entries in inserts:
import { InsertEntry } from 'foldkit-mixins-richtext'
formattingToolbar<Message>()(
{
id: 'body-bar',
document: model.document,
selection: model.editor.selection,
wrap: edited,
toggled: (menu, open) => ToggledMenu({ menu, open }),
roving: { current: model.tabStop },
onFocus: id => FocusedControl({ id }),
items: [
'undo',
'redo',
'separator',
{ mark: 'Bold' },
{ control: { id: 'find', label: 'Find', icon: 'search', message: ToggledFind() } },
'insert',
],
inserts: [InsertEntry.App({ id: 'file', label: 'Image, video or file', message: PickedFile() })],
returned: ReturnedToText(),
},
h,
)Also refuse disallowed actions in update when disabled is true, since the view only marks the button aria-disabled.
Collapse low-priority controls on narrow screens
Wrap secondary items in { low: item } and set compact: true (driven by a MediaQuery or Resize observer in your Model) to collapse them into a trailing More menu (menu: 'more'):
formattingToolbar<Message>()(
{
id: 'body-bar',
document: model.document,
selection: model.editor.selection,
wrap: edited,
toggled: (menu, open) => ToggledMenu({ menu, open }),
roving: { current: model.tabStop },
onFocus: id => FocusedControl({ id }),
items: ['undo', 'redo', 'separator', { mark: 'Bold' }, { low: { mark: 'Code' } }, { low: 'quote' }],
compact: true,
},
h,
)Keep popup triggers (style, insert, link, or controls with popup) out of { low } unless narrow screens can omit them, since the More menu only lists direct actions and toggles.
Keep focus predictable across pointer and keyboard
Toolbar roots carry data-editor-controls, so pointer clicks keep focus inside the editor text and leave the browser selection intact while still triggering the button. Keyboard navigation (Tab into the bar, then arrow keys) holds focus on the bar while the editor draws span[data-decoration="selection"] to keep the text range visible; pressing Escape dispatches returned so your update can return focus via focus(hostId, selection). Mark your own custom editor chrome with h.Attribute(CONTROLS_ATTRIBUTE, '') from foldkit-richtext-dom/toolbar.
| Slot | Capability | Renders |
|---|---|---|
root | Container | role="toolbar" root owning keydown |
button | Interactive | Control with data-item, aria-pressed or aria-expanded |
separator | Container | Divider between groups |
anchor | Container | Trigger and panel wrapper for anchoring |
label | Container | Trigger text |
menu | Collection | Open role="menu" panel |
item | Interactive | Entry with data-style or data-insert |
itemLabel, itemDescription | Container | Entry label and description |