Toolbar
The toolbar plugin shows Notion's floating toolbar over text selections, with Turn into, a link panel, a button for every mark that declares one, and text and background colors.
createToolbarPlugin({ toolbar }) shows a formatting toolbar above a text selection, Notion’s [Kind ⌄] | Link | B I U S </> | A ⌄ in the demo. The bar is your toolbar snippet: the plugin ships when it shows, its placement, its keys and every action, and no markup (see Markup). The mark buttons come from the mark definitions: every mark with a toolbar entry is in controller.marks.
<script lang="ts">
import { Edytor, createToolbarPlugin, type ToolbarController } from 'edytor';
const plugins = [createToolbarPlugin({ toolbar })];
</script>
{#snippet toolbar(controller: ToolbarController)}
<div class="bar" {@attach controller.popup} {@attach controller.keys}>
{#each controller.marks as { mark, label, icon } (mark)}
<button aria-label={label} aria-pressed={controller.isActive(mark)} onclick={() => controller.toggleMark(mark)}
>{icon}</button
>
{/each}
<button onclick={() => controller.setColor('highlight', '#fbf3db')}>Highlight</button>
</div>
{/snippet}
<Edytor {plugins} />
It formats through richTextOperations, so it relies on the rich text plugin (on by default), or on your own plugin that defines the link, color and highlight marks if you want the link and color panels to work.
Try it
Select some words to open the bar, hover the link for its card, or press Mod+K over a selection to link it. The editor is local to this page: a reload resets it.
Customize
The bar and the link card are your snippets (toolbar, card). Here, a dark pill with the marks, three highlight dots and a link field, and a matching card under hovered links.
<script lang="ts">
import {
Edytor,
createToolbarPlugin,
type JSONDoc,
type Plugin,
type ToolbarController
} from 'edytor';
// The page's document and the rest of your editor's plugins.
let { value, plugins = [] }: { value?: JSONDoc; plugins?: Plugin[] } = $props();
// A dark pill for the selection, a matching card under hovered links.
const toolbar = createToolbarPlugin({ toolbar: bar, card });
const highlights = [
{ name: 'Yellow', value: '#fde68a' },
{ name: 'Mint', value: '#bbf7d0' },
{ name: 'Sky', value: '#bae6fd' }
];
</script>
{#snippet bar(controller: ToolbarController)}
<div class="frame">
<div class="pill" {@attach controller.popup} {@attach controller.keys}>
{#each controller.marks as { mark, label, icon } (mark)}
<button
aria-label={label}
aria-pressed={controller.isActive(mark)}
onclick={() => controller.toggleMark(mark)}
>
{icon}
</button>
{/each}
<span class="rule" aria-hidden="true"></span>
{#each highlights as swatch (swatch.value)}
<button
class="dot"
aria-label="Highlight {swatch.name}"
style:background={swatch.value}
onclick={() => controller.setColor('highlight', swatch.value)}
></button>
{/each}
<button aria-label="Clear highlight" onclick={() => controller.setColor('highlight', null)}>
✕
</button>
<span class="rule" aria-hidden="true"></span>
<button aria-expanded={controller.panel === 'link'} onclick={() => controller.togglePanel('link')}>
Link
</button>
</div>
{#if controller.panel === 'link'}
<div class="field">
<input
aria-label="Link URL"
placeholder="https://…"
value={controller.linkUrl}
oninput={(event) => controller.setLinkUrl(event.currentTarget.value)}
{@attach controller.linkField}
/>
<button onclick={() => controller.applyLink()}>Apply</button>
<button onclick={() => controller.removeLink()}>Remove</button>
</div>
{/if}
</div>
{/snippet}
{#snippet card(controller: ToolbarController)}
<div class="chip">
<span class="url">{controller.hoveredHref}</span>
<button onclick={() => controller.openHovered()}>Open ↗</button>
<button onclick={() => controller.removeHovered()}>Unlink</button>
</div>
{/snippet}
<Edytor {value} plugins={[toolbar, ...plugins]} aria-label="Toolbar demo" />
<style>
.frame {
display: flex;
flex-direction: column;
align-items: center;
gap: 6px;
}
.pill,
.field,
.chip {
display: flex;
align-items: center;
gap: 2px;
padding: 4px 6px;
border-radius: 999px;
background: #1f2328;
color: #e6edf3;
box-shadow: 0 12px 32px rgb(0 0 0 / 0.25);
font: 500 13px/1 ui-sans-serif, system-ui, sans-serif;
white-space: nowrap;
}
button {
min-width: 26px;
height: 26px;
padding: 0 8px;
border: 0;
border-radius: 999px;
background: transparent;
color: inherit;
font: inherit;
cursor: pointer;
}
button:hover,
button[aria-expanded='true'] {
background: #373e47;
}
button[aria-pressed='true'] {
background: #2f81f7;
color: #fff;
}
.dot {
min-width: 14px;
width: 14px;
height: 14px;
margin: 0 3px;
padding: 0;
border: 1px solid rgb(255 255 255 / 0.3);
}
.rule {
width: 1px;
height: 16px;
margin: 0 4px;
background: #3d444d;
}
.field {
padding: 4px;
border-radius: 12px;
}
input {
width: 200px;
height: 26px;
padding: 0 8px;
border: 1px solid #3d444d;
border-radius: 8px;
background: #151b23;
color: inherit;
font: inherit;
outline: none;
}
.chip {
padding: 4px 4px 4px 12px;
}
.url {
max-width: 200px;
margin-right: 6px;
overflow: hidden;
color: #9198a1;
text-overflow: ellipsis;
}
</style>
What it offers
What the controller offers, as the demo’s bar shows it, from left to right:
- The kind button, labeled with the block’s kind (“Heading 2”, “Text”…): the preset sharing the most data with the block, or drawn with the same tag (a stored
h5heading, drawn as anh3, is “Heading 3”). It opens a “Turn into” panel listing the kinds that keep content, with ✓ on the current one. Picking one converts every block the selection touches, nested ones included, as one undo step, and keeps the selection; a list’s items leave the list (Containers). A closed toggle’s hidden body is not touched, as in Notion; an open toggle’s children are. - Link opens a link panel: a URL field (
data-testid="toolbar-link-input") showing the selection’s current link, with Apply and Remove. The field takes the focus when the panel opens. Enter in the field applies and closes the panel; Escape closes it. Both give the editor its focus back with the text still selected. Applying an empty field removes the link. - One button per mark whose definition has
toolbar: { label, icon }, in registration order. With the rich text plugin: bold, italic, underline, strike and code. A button toggles its mark over the selection, and shows pressed (aria-pressed="true", in blue) when the mark covers the whole selection. - A ⌄ (Color) opens Notion’s colors: ten text colors and ten background colors. A text color sets the
colormark, a background thehighlightmark, and Default removes that mark:controller.setColor('color' | 'highlight', value)takes any CSS colour, so the palette is your markup’s (the demo’s is Notion’s,palette.ts).
One panel is open at a time; clicking its button again closes it. Panels drop below the bar, over the text.
| Color | Text | Background |
|---|---|---|
| Default | none | none |
| Gray | #7d7a75 |
#f0efed |
| Brown | #9f765a |
#f5ede9 |
| Orange | #d27b2d |
#fbebde |
| Yellow | #cb9434 |
#f9f3dc |
| Green | #50946e |
#e8f1ec |
| Blue | #387dc9 |
#e5f2fc |
| Purple | #9a6bb4 |
#f3ebf9 |
| Pink | #c14c8a |
#fae9f1 |
| Red | #cf5148 |
#fce9e7 |
The values are stored in the marks as written, so documents keep them if you restyle the editor.
Add a button for your own mark by declaring toolbar on it:
marks: {
keyboard: { tag: 'kbd', toolbar: { label: 'Keyboard', icon: '⌨' } }
}
The icon is text: a letter, a symbol or an emoji. With run, the button calls it instead of toggling the mark: the comments plugin’s comment mark declares toolbar: { label: 'Comment', icon: '💬', run }, which opens a thread on the selection.
When it shows
The toolbar shows when all of these hold:
- the editor is not readonly (turning it readonly hides the toolbar; it shows again over the same selection when the editor is editable);
- the selection is a non-collapsed text range;
- no whole block and no inline atom is selected;
- the selection is not inside a void or an island block (such as a code block).
It follows the selection as it changes, and repositions on scroll, resize and edits. While it shows, the editor’s root names it (aria-controls) and the key that reaches it (aria-keyshortcuts="Alt+F10"): Alt+F10 moves the focus to its first button, ←/→, Home and End walk the bar (one tab stop), and Escape gives the focus back to the editor over the same selection (Accessibility). Buttons keep the selection: clicking one does not move the caret, and the selection is restored after the format is applied. A press anywhere on the toolbar, its background included, never takes the editor’s focus: typing after it goes on in the editor, replacing the selected text. Only its fields (the link panel’s URL field) take focus.
Keyboard
Mod+K opens the link panel, its field focused (link.mod-k, Notion): over the selected text, or, at a caret inside a link, over that whole link, which it selects first. At a caret outside any link, and wherever the toolbar does not show (a code block, a void, a readonly view), the key is not claimed.
Link card
With a card snippet, hovering a link shows a card just below it (link.card, Notion), in the overlay with data-edytor-link-card-host (z-index: 61): in the demo, the link’s URL, then three buttons.
| Button | Calls | Action |
|---|---|---|
| Open | openHovered() |
Opens the link in a new tab (noopener, noreferrer), as Mod+click does |
| Edit | editHovered() |
Selects the whole link and opens the link panel on it, its field focused |
| Remove | removeHovered() |
Removes the link from the whole link (the characters around it linked to the same URL), one undo step; the selection stays |
The card stays while the pointer moves from the link onto it, and goes when the pointer leaves both. A press on it never takes the editor’s focus. A readonly view shows no card. Without a card, no card shows.
A URL typed in the link panel goes through the same sanitization as every link: a javascript: or other unsafe scheme is ignored. Colors go through setMarkValueAtRange and removeMarkAtRange (see Formatting from code).
Placement
The toolbar mounts in the editor’s overlay, in a position: fixed host with the attribute data-edytor-toolbar-host and z-index: 60. Your bar places itself by controller.anchor (the selection’s box): centered above it, below it when there is no room, computePosition(controller.anchor, bar, { placement: 'top', … }) with Floating UI; the card by controller.cardAnchor (the hovered link). Its look is yours: the demo’s Notion-style bar is Toolbar.svelte (and the card LinkCard.svelte) in the repository’s registry/edytor/chrome (the edytor-chrome registry item, in your app after shadcn-svelte add).
Markup
createToolbarPlugin takes a toolbar snippet, the whole bar (without it, nothing shows; Mod+K still opens the controller’s link panel state). The plugin keeps deciding when it shows (the rules above), follows the selection and places it.
toolbar?Snippet<[ToolbarController]>
The toolbar. Rendered while controller.isVisible, centered above the selection.
Snippet<[ToolbarController]>card?Snippet<[ToolbarController]>
The link card. Rendered while controller.card, just below the hovered link (controller.hoveredHref; openHovered, editHovered, removeHovered). Without it, no card.
Snippet<[ToolbarController]>The snippet receives the ToolbarController. Every action keeps and restores the selection:
marks?{ mark, label, icon }[]
The marks that declare a toolbar button, in registration order.
{ mark, label, icon }[]toggleMark(mark)?(mark: string) => void
Toggle a mark over the selection, or call its toolbar entry's run when it has one.
(mark: string) => voidisActive(mark)?(mark: string) => boolean
Whether the mark covers every character of the selection, for a pressed state.
(mark: string) => booleankinds?KindRow[]
The kinds the block may turn into while keeping its content.
KindRow[]currentKind?KindRow | undefined
The row naming the selection's block: of its kind's rows, the one whose preset data shares the most values with the block's (the first on a tie).
KindRow | undefinedturnInto(kind)?(kind: KindRow) => void
Convert every block the selection touches, as one undo step.
(kind: KindRow) => voidsetColor(mark, value)?('color' | 'highlight', value: string | null) => void
Set the text or background color; null removes it.
('color' | 'highlight', value: string | null) => voidlinkUrl?string
The selection's current link, or the URL being typed.
stringsetLinkUrl(value)?(value: string) => void
Update linkUrl, for a link field.
(value: string) => voidapplyLink()?() => void
Link the selection to linkUrl; an empty URL removes the link.
() => voidremoveLink()?() => void
Remove the link from the selection.
() => voidpanel?'turn' | 'link' | 'color' | null
The open panel, for your own panels.
'turn' | 'link' | 'color' | nulltogglePanel(panel)?(panel: 'turn' | 'link' | 'color') => void
Open a panel, or close it when it is open. Opening the link panel focuses its field.
(panel: 'turn' | 'link' | 'color') => voidopenLinkPanel()?() => boolean
What Mod+K does: open the link panel over the selection, or over the link at the caret (selected first). Answers whether it opened.
() => booleanclosePanel(focus?)?(focus?: boolean) => void
Close the open panel; with focus, give the editor its focus and the held selection back.
(focus?: boolean) => voidtakeFieldFocus()?() => boolean
Whether your link field, mounting now, should take the focus (true once per opening).
() => booleanhovered?HTMLAnchorElement | null
The link under the pointer, for the link card.
HTMLAnchorElement | nullpopup?Attachment<HTMLElement>
On the bar: its id, toolbar role and name, the placement's mark (data-edytor-toolbar-bar), and its publication to the editor's root while it shows (aria-controls, aria-keyshortcuts Alt+F10).
Attachment<HTMLElement>keys?Attachment<HTMLElement>
On the bar: one tab stop (the control it last held), the arrows, Home and End walk its controls, Escape gives the focus back to the editor over the held selection; no key in it reaches the editor.
Attachment<HTMLElement>linkField?Attachment<HTMLInputElement>
On a link field: its id (linkFieldId, unique per editor, for a label), the focus when the link panel was just opened, Enter applies the link and Escape closes the panel, both giving the editor its focus back.
Attachment<HTMLInputElement>linkFieldId?string
The link field's id, for a label's for.
stringThe example at the top of this page uses popup and keys. The plugin cancels the mousedown of every press in its host but in a field (an input, a textarea, a select), so your buttons keep the selection and the editor’s focus without a mousedown handler of their own. See Menus and handles for the other menus.