Keep your coding agent up to date with the latest web best practices
Release v0.0.144
github-actions[bot]aab205195092
diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.jsonindex 2168d7b..3280c07 100644--- a/.claude-plugin/marketplace.json+++ b/.claude-plugin/marketplace.json@@ -8,7 +8,7 @@"name": "modern-web-guidance","source": "./","description": "Keep your coding agent up to date with the latest web best practices",- "version": "0.0.143"+ "version": "0.0.144"}]}diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.jsonindex 0ac4ef0..c5d0034 100644--- a/.claude-plugin/plugin.json+++ b/.claude-plugin/plugin.json@@ -1,7 +1,7 @@{"name": "modern-web-guidance","description": "Keep your coding agent up to date with the latest web best practices",- "version": "0.0.143",+ "version": "0.0.144","author": {"name": "Google Chrome"}diff --git a/.cursor-plugin/plugin.json b/.cursor-plugin/plugin.jsonindex 0ac4ef0..c5d0034 100644--- a/.cursor-plugin/plugin.json+++ b/.cursor-plugin/plugin.json@@ -1,7 +1,7 @@{"name": "modern-web-guidance","description": "Keep your coding agent up to date with the latest web best practices",- "version": "0.0.143",+ "version": "0.0.144","author": {"name": "Google Chrome"}diff --git a/README.md b/README.mdindex 4f9a481..1cd4904 100644--- a/README.md+++ b/README.md@@ -26,7 +26,7 @@ Our content is evergrowing, we cover the bleeding edge of the web platform as we### Core disciplines-Here's a tiny sampling of the **122 use-case-centric guides**:+Here's a tiny sampling of the **123 use-case-centric guides**:* **User Experience**: Smooth and modern visual states: View Transitions, CSS `scrollbar-color` styling, high-contrast adaptation, entry/exit transition animations, parallax scrolling.* **CSS layout:** container queries (both size and style queries), modern color spaces (`oklch`, `color-mix`) and `subgrid`, text-wrap tuning (`balance`, `pretty`), subgrid, and typography line height trimming (`text-box`)@@ -36,10 +36,10 @@ Here's a tiny sampling of the **122 use-case-centric guides**:* **Accessibility & Security**: accessible error announcements, keyboard focus management.* **Built-in AI**: Leveraging local, on-device client models (native Language Detection, Summarization, and Translation APIs).-#### Full Skill Coverage (v0.0.143)+#### Full Skill Coverage (v0.0.144)<details>-<summary>Includes expert guidance across <strong>97 modern web features</strong></summary>+<summary>Includes expert guidance across <strong>98 modern web features</strong></summary>- [::backdrop](https://web-platform-dx.github.io/web-features-explorer/features/backdrop/)- [:autofill](https://web-platform-dx.github.io/web-features-explorer/features/autofill/)@@ -65,6 +65,7 @@ Here's a tiny sampling of the **122 use-case-centric guides**:- [Container scroll-state queries](https://web-platform-dx.github.io/web-features-explorer/features/container-scroll-state-queries/)- [content-visibility](https://web-platform-dx.github.io/web-features-explorer/features/content-visibility/)- [Cross-document view transitions](https://web-platform-dx.github.io/web-features-explorer/features/cross-document-view-transitions/)+- [Custom highlights](https://web-platform-dx.github.io/web-features-explorer/features/highlight/)- [Customizable <select>](https://web-platform-dx.github.io/web-features-explorer/features/customizable-select/)- [Email, telephone, and URL <input> types](https://web-platform-dx.github.io/web-features-explorer/features/input-email-tel-url/)- [enterkeyhint](https://web-platform-dx.github.io/web-features-explorer/features/enterkeyhint/)@@ -141,7 +142,7 @@ Here's a tiny sampling of the **122 use-case-centric guides**:</details><details>-<summary>Covers <strong>122 real-world developer use cases</strong> with production-ready code patterns</summary>+<summary>Covers <strong>123 real-world developer use cases</strong> with production-ready code patterns</summary><h3>accessibility</h3>@@ -154,6 +155,10 @@ Here's a tiny sampling of the **122 use-case-centric guides**:- **[summarizer](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/built-in-ai/summarizer.md)**: Summarize text content using the on-device Summarizer API.- **[translator](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/built-in-ai/translator.md)**: Translate text between languages using the on-device Translator API.+<h3>css</h3>++- **[css-custom-highlights](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/css/css-custom-highlights.md)**: Highlight arbitrary text ranges on a page without modifying the DOM, using the CSS Custom Highlight API. For example, highlighting search results, spelling errors, or collaborative editing cursors.+<h3>forms</h3>- **[animated-select-picker](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/forms/animated-select-picker.md)**: Create a custom select component whose dropdown is animated. For example, the menu fades or slides into view, or the options animate upon selection.diff --git a/gemini-extension.json b/gemini-extension.jsonindex 0ac4ef0..c5d0034 100644--- a/gemini-extension.json+++ b/gemini-extension.json@@ -1,7 +1,7 @@{"name": "modern-web-guidance","description": "Keep your coding agent up to date with the latest web best practices",- "version": "0.0.143",+ "version": "0.0.144","author": {"name": "Google Chrome"}diff --git a/package.json b/package.jsonindex 0b2f205..0ede9d7 100644--- a/package.json+++ b/package.json@@ -3,7 +3,7 @@"displayName": "Modern Web Guidance","description": "Keep your agent up to date on the latest web best practices.","license": "Apache-2.0",- "version": "0.0.143",+ "version": "0.0.144","publisher": "GoogleChrome","engines": {"vscode": "^1.90.0",diff --git a/skills/modern-web-guidance/guides/css/css-custom-highlights.md b/skills/modern-web-guidance/guides/css/css-custom-highlights.mdnew file mode 100644index 0000000..73cb90d--- /dev/null+++ b/skills/modern-web-guidance/guides/css/css-custom-highlights.md@@ -0,0 +1,112 @@+The CSS Custom Highlight API lets you style arbitrary text ranges on a page without modifying the DOM structure. This enables search-result highlighting, syntax coloring, collaborative editing cursors, or spelling and grammar error markers without wrapping text in extra elements or relying on `innerHTML` manipulation.++### Core implementation++To highlight text ranges, you must collect the target text nodes, create `Range` and `Highlight` objects, register them in the `HighlightRegistry`, and then style them with the `::highlight()` pseudo-element.++#### 1. Collect text nodes and create ranges+Use a `TreeWalker` to collect all text nodes in the target element, then create `Range` objects pointing at the character offsets you want to highlight.++```javascript+const article = document.querySelector("article");++// MANDATORY: Use TreeWalker to collect text nodes — do not manipulate innerHTML.+const treeWalker = document.createTreeWalker(article, NodeFilter.SHOW_TEXT);+const allTextNodes = [];+let currentNode = treeWalker.nextNode();+while (currentNode) {+ allTextNodes.push(currentNode);+ currentNode = treeWalker.nextNode();+}++// MANDATORY: Set range start/end on text nodes, not element nodes.+const range = new Range();+range.setStart(textNode, matchStartIndex);+range.setEnd(textNode, matchEndIndex);+```++Cache the text-node list and only rebuild it when the DOM content actually changes, since walking the tree is expensive.++#### 2. Create a Highlight from the ranges+Group one or more `Range` objects into a `Highlight`. Multiple ranges that share the same style belong in a single highlight.++```javascript+const searchHighlight = new Highlight(...matchingRanges);+```++#### 3. Register the highlight in the registry+Register each `Highlight` under a custom name using `CSS.highlights`, which is a `Map`-like `HighlightRegistry`.++```javascript+// MANDATORY: Clear previous highlights before registering new ones+// to avoid stale ranges persisting on the page.+CSS.highlights.clear();++CSS.highlights.set("search-results", searchHighlight);+```++When multiple highlights overlap, use the `priority` property to control stacking order. Higher priority highlights paint on top.++```javascript+const primary = new Highlight(...primaryRanges);+primary.priority = 1;++const secondary = new Highlight(...secondaryRanges);+secondary.priority = 0; // painted first (behind primary)++CSS.highlights.set("primary", primary);+CSS.highlights.set("secondary", secondary);+```++#### 4. Style with `::highlight()`+Use the `::highlight()` pseudo-element in CSS to style each registered highlight by name.++```css+::highlight(search-results) {+ background-color: #ffdd00;+ color: black;+}+```++Only a limited set of CSS properties work inside `::highlight()`: `color`, `background-color`, `text-decoration` and its longhands, `text-shadow`, `-webkit-text-stroke-color`, `-webkit-text-fill-color`, and `-webkit-text-stroke-width`. Properties like `background-image`, `font-size`, or `padding` are ignored.++### Accessibility++**AVOID**: using custom highlights as a replacement for semantic HTML.++Custom highlights are purely presentational and are not exposed to the accessibility tree. If the highlighted text is semantically relevant to the document (e.g., a user-selected passage), use `<mark>` instead. Reserve custom highlights for transient, visual-only effects like search results or syntax coloring.++Highlights should not rely solely on color to convey meaning. If a highlight indicates an error, pair it with another visual indicator such as `text-decoration: wavy underline` or an adjacent text label. Ensure sufficient contrast between the highlight background and text color to meet WCAG 2.1 requirements (at least 4.5:1 for normal text).++### Fallback strategies+Baseline status for Custom highlights: Newly available. It's been Baseline since 2026-03-24.+Supported by: Chrome 105 (Sep 2022), Edge 105 (Sep 2022), Firefox 149 (Mar 2026), and Safari 17.2 (Dec 2023).++For browsers that do not support the CSS Custom Highlight API, you should provide a functional base experience where text is still legible, even without the visual highlight.++#### Feature detection+You can detect support before using the API:++```javascript+if (CSS.highlights) {+ // CSS Custom Highlight API is supported.+} else {+ // Fallback: wrap matches in <mark> elements.+}+```++#### DOM-based fallback++If the highlight is critical for the user experience, fall back to wrapping matched text in `<mark>` elements. This modifies the DOM, so take care to preserve event listeners and avoid breaking the document structure.++```javascript+if (!CSS.highlights) {+ // IMPORTANT: Escape user input to prevent XSS when using innerHTML.+ const escaped = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");+ const regex = new RegExp(`(${escaped})`, "gi");+ article.innerHTML = article.textContent.replace(+ regex,+ "<mark>$1</mark>"+ );+}+```