0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Text highlighting with a colour palette in the selection-menu toolbar, persisted to localStorage; standalone it is a programmatic API.
Overview
<highlight-wc> lets readers highlight passages of an article. Highlights are painted with the CSS Custom Highlight API (no DOM changes; a <mark> fallback elsewhere) and persist in localStorage per page.
It has two modes:
Inside <selection-menu> (primary usage) — it renders a highlighter button in the selection toolbar. The button opens a colour palette; choosing a swatch highlights the selection.
Standalone — it renders no UI. It turns on persistence and rendering for its target and exposes a JavaScript API and events, so you can build your own controls.
Select any text in the article, press the highlighter button in the toolbar and pick a colour.
With selection-menu
Put <highlight-wc> inside a <selection-menu> that targets your content. Every target of the menu can be highlighted, each with its own set of highlights; stored highlights are painted as soon as the page loads.
Selection toolbar with highlighter
<articleid="post"><h2>My Article</h2><p>Select any text to highlight it.</p></article><selection-menufor="post"><highlight-wc></highlight-wc><note-wc></note-wc></selection-menu>
To cover several articles, give the menu an id and mark each article with data-selection-menu:
Add readonly to show stored highlights without the highlighter button.
Standalone (programmatic)
Without a selection-menu, <highlight-wc> wraps its target (or points at one with for) and renders nothing itself. Stored highlights are painted, and you drive creation from your own UI with highlightSelection() or listen for highlights:selected.
Standalone wrapper
<highlight-wcid="notes"colors="yellow,blue"><article><p>Highlights on this article are created from script.</p></article></highlight-wc><buttontype="button"id="mark-it">Highlight selection</button>
Palette colour names; each needs a --highlight-{name} token.
readonly
boolean
—
Render stored highlights, disable new ones (no highlighter button in a selection-menu).
storage-key
string
pathname
Custom localStorage key suffix.
for
string
—
Standalone only: ID of the element to annotate (defaults to the first child).
data-label-button
string
Highlight selection
Accessible name of the highlighter button.
data-label-palette
string
Highlight color
Accessible name of the colour palette.
data-label-annotation
string
You highlighted
Margin annotation text.
data-label-added / data-label-removed
string
Highlight added / removed
Screen-reader announcements.
Colours
Each colour is a light-dark() pair: a pale wash with dark text in light mode, a deep wash with light text in dark mode. Highlighted text takes --highlight-ink so it meets WCAG AA in both schemes. Override a colour, or add your own, with a matching pair:
Inside a selection-menu the methods act on the most recently highlighted target (initially the first).
Component API
const hw = document.querySelector('highlight-wc');hw.highlightSelection('yellow'); // highlight the current selectionhw.getHighlights(); // [{ id, startOffset, endOffset, text, prefix, suffix, color, note, created }]hw.removeHighlight('hn-abc123');hw.clearAll();const json = hw.exportHighlights();const { imported, rejected } = hw.importHighlights(json);
importHighlights() drops records with the wrong shape, offsets outside the article text, or a colour that is not in the palette.
Events
Dispatched on the highlighted element (the article) 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 article. They stay in storage; removeHighlight(id) discards one.
highlights:selected
{ text, rect }
Text was selected in the target (standalone UI hook)
highlights:clicked
{ id, highlight }
A highlight or its margin annotation was clicked
highlights:note-changed
{ id, note }
A note was saved via <note-wc>
Accessibility
The highlighter is a <button> named “Highlight selection” with aria-expanded and aria-controls pointing at the palette.
The palette is a named group of swatch buttons. Each swatch is named after its colour (also shown as a tooltip) and the last-used colour carries aria-pressed="true". Opening the palette moves focus to that swatch.
Escape closes the palette and returns focus to the button; clicking outside or dismissing the toolbar also closes it.
Creating or removing a highlight is announced through a polite live region.
In forced-colors mode swatches keep their fills, highlights use the system Mark colours, and each palette colour gets a different underline style.
Limitations: Custom Highlight API ranges are not exposed to assistive technology — a screen reader hears the announcement when a highlight is made, but highlighted text is not otherwise identified when read back. Margin annotations are aria-hidden. Use getHighlights() to render an accessible list of highlights if your readers need one.
Related
selection-menu — Selection toolbar that hosts the highlighter