How readers highlight passages and attach private notes to an article, where those are stored, and how to read, export or style them.
What readers get
A reader selects text in an article. A small toolbar appears by the selection. One button highlights the passage in a chosen colour, another highlights it and opens a note. The highlight is still there on the next visit, and a mark in the margin shows which highlights have notes.
Everything is stored in that reader's browser. Nothing is sent to a server, no account is needed, and no other reader sees it. Try it: select a sentence in the demo.
<article id="post"> <h1>The evolution of digital reading</h1> <p>...</p></article> <selection-menu for="post"> <highlight-wc></highlight-wc> <note-wc></note-wc></selection-menu>
The parts
| Part | Job |
<selection-menu> |
Shows a toolbar when text is selected inside the element named by for. It holds whatever action elements you put in it and knows nothing about highlights. Alt + F10 moves focus into the toolbar and Escape returns it. |
<highlight-wc> |
The highlighter button and its colour choices. It turns the menu's target into a highlightable article. |
<note-wc> |
The note button and the panel where a note is written. It also opens that panel when the reader clicks a mark in the margin. |
data-highlights |
The attribute that marks an element as holding highlights. <highlight-wc> sets it on the target for you. Write it yourself to show stored highlights with no toolbar. |
Highlights are drawn with the CSS Custom Highlight API, which paints ranges of text without changing the document. In a browser without it, each highlight is wrapped in a <mark> element.
Common arrangements
Highlights without notes
Leave out <note-wc>.
<selection-menu for="post"> <highlight-wc></highlight-wc></selection-menu>
Showing stored highlights only
Use the attribute alone, with data-highlights-readonly. Highlights saved earlier are drawn and new selections do nothing.
<article data-highlights data-highlights-readonly> ...</article>
Your own colours
The default colours are yellow, green, blue and pink, each a token named --highlight-{name}. To add one, define its token and list the names in colors. A colour with no token is not painted.
<style> :root { --highlight-brand: light-dark(oklch(92% 0.07 20), oklch(42% 0.09 20)); }</style> <selection-menu for="post"> <highlight-wc colors="brand,yellow,blue"></highlight-wc> <note-wc></note-wc></selection-menu>
Give the token a light and a dark value, as the built-in ones have, so highlighted text stays readable in both schemes. The text colour over a highlight is --highlight-ink. Set --highlight-{name}-ink to change it for one colour.
A notes button in the page toolbar
A <note-wc> outside the selection menu, with for, is a button that opens the note panel on the first highlight that has a note, or the first highlight if none has.
<page-tools> <print-page></print-page> <note-wc for="post"></note-wc></page-tools>
Where highlights are kept
Highlights are saved through VBStore, which writes to the browser's local storage by default. Each article's set is saved under a key. The key is the page's path unless you name one.
<highlight-wc></highlight-wc> <highlight-wc storage-key="handbook-ch5"></highlight-wc> <article data-highlights="handbook-ch5">...</article>
Name a key when the same article is served at more than one address, or when two articles share a page.
A highlight is stored as a start and end position in the article's text, with a copy of the highlighted words and of the 32 characters on either side. When the article's text changes, each stored highlight is checked against that copy. If the words are no longer at the stored position, the article is searched for them, and the text on either side picks between several matches. An edit elsewhere in the article therefore moves a highlight and does not remove it.
A highlight whose words are no longer in the article cannot be shown. It is not deleted: it stays in storage, is tried again on each visit, and comes back if the words do. When that happens the article fires highlights:unplaced and a warning is written to the console. removeHighlight(id) or clearAll() discards such a highlight for good.
Working with highlights in script
The <highlight-wc> element is the way in. Its methods act on the article it highlights.
| Method | Does |
getHighlights() | Returns an array of { id, startOffset, endOffset, text, prefix, suffix, color, note, created } |
highlightSelection(color) | Highlights the current selection |
removeHighlight(id) | Removes one |
clearAll() | Removes all of them |
exportHighlights() | Returns a JSON string of the set |
importHighlights(json) | Replaces the set. Records that do not fit the text or the palette are dropped. Returns { imported, rejected }. |
const highlighter = document.querySelector('highlight-wc');const article = document.getElementById('post'); function save() { navigator.sendBeacon('/api/highlights', highlighter.exportHighlights());} article.addEventListener('highlights:added', save);article.addEventListener('highlights:removed', save);article.addEventListener('highlights:note-changed', save);
To build your own controls with no selection toolbar, place <highlight-wc for="post"> anywhere on the page. In that form it shows nothing and gives you the methods and events.
Events
All of these fire on the article and bubble.
| Event | Detail | When |
highlights:added | { id, text, color, note } | A highlight is made |
highlights:removed | { id } | A highlight is removed with removeHighlight() |
highlights:unplaced | { highlights } | After stored highlights load, when the words of one or more are no longer in the article. highlights is an array of those records. |
highlights:note-changed | { id, note } | A note is saved |
highlights:clicked | { id, highlight } | The reader clicks a highlight or its margin mark |
highlights:selected | { text, rect } | The reader finishes selecting text in the article. Use it to place your own toolbar. |
highlights:request-note | { id, highlight, controller } | The reader clicks a margin mark. <note-wc> listens for this and opens its panel. |