Calm motion: animations on this site only fade, with no movement
Selection toolbar
Actions above the selected text
A small toolbar that comes up when a person selects text in an area, for example to mark, quote, or comment on it. For a full text editor with its own toolbar, use the editor.
Basic
Select some words in this text. A small toolbar comes up above them, with actions for the words that you selected. Press Tab to reach it from the keyboard, and Escape to close it.
The toolbar goes below the text when there is no room above it, near the top of the window.
vue
<template>
<div style="max-width: 560px">
<BlessSelectionToolbar :actions @action="onAction">
<p>
Select some words in this text. A small toolbar comes up above them, with actions for the
words that you selected. Press Tab to reach it from the keyboard, and Escape to close it.
</p>
<p>
The toolbar goes below the text when there is no room above it, near the top of the window.
</p>
</BlessSelectionToolbar>
<BlessText v-if="last" as="p" size="sm">{{ last }}</BlessText>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { BlessSelectionToolbar, BlessText, type BlessSelection } from "blessing-ui";
const actions = [
{ id: "mark", label: "Mark", icon: "🖍" },
{ id: "quote", label: "Quote" },
{ id: "copy", label: "Copy" },
];
const last = ref("");
function onAction(id: string, sel: BlessSelection) {
if (id === "mark") {
// wrap the selected text in <mark>, when it is inside one paragraph
const mark = document.createElement("mark");
try {
sel.range.surroundContents(mark);
} catch {
// the range crosses paragraphs: nothing to wrap
}
}
if (id === "copy") navigator.clipboard?.writeText(sel.text).catch(() => {});
last.value = `${id}: “${sel.text}”`;
}
</script>- Put the text in the default slot. The toolbar shows when the selection is inside the area and has at least
min-lengthletters (default 1). actionsis a list of{ id, label, icon }. A button with an icon uses the label as its name for screen readers and its tooltip. A click emitsactionwith the id and{ text, range }, whererangeis a copy of the selection. Then the toolbar closes.- The
toolbarslot replaces the buttons. It gets theselectionand arun(id)function. - The toolbar goes above the selection, or below it near the top of the window. It moves when the page scrolls.
- A click on the toolbar keeps the selection. The toolbar comes after the text, so Tab from the selected text reaches it. The arrow keys move between its buttons, and Escape closes it.
- Change the name of the toolbar for screen readers with
labelsor a label pack:label.
Usage
ts
import { BlessSelectionToolbar, type BlessSelection, type BlessSelectionAction } from "blessing-ui";API
Props
| Name | Type | Default | Description |
|---|---|---|---|
actions | BlessSelectionAction[] | [] | |
minLength | number | 1 | the fewest letters a selection needs before the toolbar shows |
labels | Partial<{ label: string; }> | — |
Events
| Name | Payload | Description |
|---|---|---|
action | [id: string, selection: BlessSelection] |
Slots
| Name | Scope | Description |
|---|---|---|
default | — | |
toolbar | { selection: BlessSelection | undefined; run: (id: string) => void; } |