The text-highlighting engine: renders and persists highlights on any element with the CSS Custom Highlight API. No built-in UI; pair it with highlight-wc and selection-menu for interactive highlighting.
Overview
data-highlights is the engine behind <highlight-wc>. On any element it loads stored highlights, paints them, keeps them in localStorage and exposes a JavaScript API and events.
The attribute itself renders no UI. Nothing appears when the reader selects text. For interactive highlighting, put a <highlight-wc> inside a <selection-menu>. The component sets data-highlights on the menu's targets for you. Use the attribute directly when you only want to show stored highlights, or when you are building your own controls in script.
Highlights are painted with the CSS Custom Highlight API, which does not change the DOM. Browsers without it get <mark> elements instead.
Interactive highlighting (recommended)
Select text in the article and press the highlighter button in the toolbar. The data-highlights attribute on the article is optional here, because <highlight-wc> adds it.
<article id="demo-article" data-highlights> <p>Select any text in this article…</p></article> <selection-menu for="demo-article"> <highlight-wc></highlight-wc> <note-wc></note-wc></selection-menu>
See <highlight-wc> for the palette, its keyboard behaviour and the component attributes.
Attributes
| Attribute | Type | Default | Description |
data-highlights |
string (optional) |
pathname |
Turns the engine on. An optional value is the storage key suffix; highlights are saved under vb:highlights:{key}. |
data-highlights-colors |
comma-separated |
yellow,green,blue,pink |
Palette colour names. Each needs a --highlight-{name} token. Names may only use letters, digits, - and _. |
data-highlights-readonly |
boolean |
— |
Paints stored highlights but ignores pointer input (no highlights:selected or highlights:clicked events). It is read when the engine starts. |
data-highlights-label-* |
string |
see below |
Overrides the announcement and annotation text: -annotation (“You highlighted”), -added (“Highlight added”), -removed (“Highlight removed”), -note-saved (“Note saved”). |
The engine sets data-highlights-init on the element once it is running.
Display stored highlights
With no component, the attribute just paints whatever is already stored under its key. Combined with data-highlights-readonly, that is a read-only view of highlights saved elsewhere, such as on another page that uses the same key.
<article data-highlights="readonly-demo" data-highlights-readonly> <p>Stored highlights are painted; new ones cannot be made.</p></article>
Colours
Each palette colour is a --highlight-{name} token holding a light-dark() pair: a pale wash in light mode, a deep wash in dark mode. Highlighted text takes --highlight-ink (or --highlight-{name}-ink if set), so it meets WCAG AA in both schemes.
Tokens are read where the highlighted text is, so a palette defined on one article only affects that article.
.custom-colors { --highlight-mint: light-dark(#ccfbef, #0f4d3f); --highlight-mint-ink: light-dark(#062e25, #f0fdf9);}
<article class="custom-colors" data-highlights data-highlights-colors="yellow,mint"> …</article>
| Token | Description |
--highlight-yellow | Yellow wash (light-dark() pair) |
--highlight-green | Green wash |
--highlight-blue | Blue wash |
--highlight-pink | Pink wash |
--highlight-ink | Text colour of all highlighted text |
--highlight-{name}-ink | Optional text colour for one palette colour |
In forced-colors mode highlights use the system Mark and MarkText colours, and each palette colour gets a different underline style so they can still be told apart.
JavaScript API
initHighlights() returns the element's controller and is idempotent. Elements with data-highlights are started automatically, including ones added to the page later.
import { initHighlights } from './utils/highlights-init.js'; const controller = initHighlights(document.querySelector('article')); controller.colors; controller.getHighlights(); controller.getUnplaced(); controller.removeHighlight('hn-abc123');controller.clearAll(); const json = controller.exportHighlights();const { imported, rejected } = controller.importHighlights(json); controller.destroy();
importHighlights() drops records with the wrong shape, offsets outside the element's text, or a colour that is not in the palette. If the text changes after highlights are saved, the engine finds each highlight again by its text, using the stored prefix and suffix (up to 32 characters on each side) to choose between several matches. A highlight whose text is gone is not painted, but it stays in storage and is tried again on the next load. getUnplaced() returns those records, and removeHighlight(id) or clearAll() discards them.
The controller has no public method for making a highlight. To do that from your own controls, wrap the element in a standalone <highlight-wc> and call its highlightSelection(color); see highlight-wc: Standalone (programmatic).
Events
Dispatched on the element and bubble.
| Event | Detail | When |
highlights:added | { id, text, color, note } | A highlight was created |
highlights:removed | { id } | A highlight was removed |
highlights:unplaced | { highlights } | Stored highlights were loaded and the text of one or more is no longer in the element. They stay in storage; a console warning is written too. |
highlights:selected | { text, rect } | Text was selected inside the element; a hook for your own UI |
highlights:clicked | { id, highlight } | A highlight or its margin annotation was clicked |
highlights:note-changed | { id, note } | A note was saved via <note-wc> |
Margin annotations
At widths of 64rem and up, each highlight gets a label in the margin (“You highlighted”, starred when it has a note). Clicking the label fires highlights:clicked. When a <note-wc> is present, it also opens the note editor. Below that width the labels are hidden.
Accessibility
- Creating or removing a highlight, and saving a note, are announced through a polite live region.
- Margin annotations are
aria-hidden; they repeat the highlight visually and the announcement covers assistive technology.
- Limitation: Custom Highlight API ranges are not exposed to assistive technology. Highlighted text is not identified when a screen reader reads it back. If your readers need a list of their highlights, build one from
getHighlights().
- The attribute has no keyboard UI of its own. The keyboard-accessible highlighter and palette come from
<highlight-wc>.
Related
- highlight-wc — Highlighter button and palette for selection-menu; standalone programmatic API
- selection-menu — Selection toolbar that hosts the highlighter
- note-wc — Private notes on highlights
- mark — Native highlight element (used as the fallback)
- text-reader — Text-to-speech with word highlighting