Keep your coding agent up to date with the latest web best practices
Release v0.0.174
github-actions[bot]2354c8c58566
diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.jsonindex 6455fbb..4e8fc2a 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.173"+ "version": "0.0.174"}]}diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.jsonindex f90be83..dd2468e 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.173",+ "version": "0.0.174","author": {"name": "Google Chrome"}diff --git a/.cursor-plugin/plugin.json b/.cursor-plugin/plugin.jsonindex f7a22f6..7a5ad39 100644--- a/.cursor-plugin/plugin.json+++ b/.cursor-plugin/plugin.json@@ -1,7 +1,7 @@{"name": "modern-web-guidance","description": "Modern Web Guidance is an agent skill and CLI tool designed to help AI coding agents build web applications using modern, secure, and high-performance APIs rather than outdated workarounds. Supported by the Google Chrome team, it injects expert-curated web platform best practices directly into an agent's context window to prevent the generation of bloated, legacy code.",- "version": "0.0.173",+ "version": "0.0.174","author": {"name": "Google Chrome"}diff --git a/README.md b/README.mdindex 0b205e3..7494b5c 100644--- a/README.md+++ b/README.md@@ -229,9 +229,9 @@ _View an example:_ [the `navigation-drawer` guide](https://github.com/GoogleChro- **[content-based-styling](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/content-based-styling.md)**: Build a component that changes its layout based on whether it contains specific child elements (or not). For example, if the component contains an image, use a multi-column layout, otherwise default to a single-column layout.- **[coordinate-global-events](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/coordinate-global-events.md)**: Schedule future meetings or events by explicitly binding them to a geographical IANA time zone so that event times remain accurate regardless of Daylight Saving Time (DST) transitions, "skipped" or "repeated" hours during clock changes.- **[cross-document-transitions](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/cross-document-transitions.md)**: Create smooth, seamless transitions between full page navigations, such as cross-fades, custom reveal effects, or morphing of content from one page to the next.+- **[custom-button-actions](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/custom-button-actions.md)**: Declaratively connect a button to any element to trigger custom, application-specific actions using declarative button commands, invoker commands, button commands, custom commands, or declarative toggle actions.- **[customize-scrollbar-color-and-thickness](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/customize-scrollbar-color-and-thickness.md)**: Customize the color or thickness of a scrollbar- **[dark-mode](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/dark-mode.md)**: Implement dark mode support in a way that respects the user's light/dark theme preference and adapts browser UI (e.g. scrollbars, form controls, etc)-- **[declarative-button-actions](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/declarative-button-actions.md)**: Declaratively connect a button to any element to trigger custom, application-specific actions using declarative button commands, invoker commands, button commands, custom commands, or declarative toggle actions.- **[declarative-dialog-popover-control](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/declarative-dialog-popover-control.md)**: Toggle the visibility of a dialog or popover from a button without writing JavaScript.- **[deliver-optimized-decorative-images](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/deliver-optimized-decorative-images.md)**: Deliver optimized decorative images (such as backgrounds, UI icons, or complex masks) by simultaneously providing next-generation image formats (like AVIF or WebP) alongside multiple pixel densities (like 1x and 2x) so the browser can dynamically negotiate the best combination of file size and visual quality for the user's device capabilities.- **[design-token-reactivity](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/design-token-reactivity.md)**: Define higher-order design tokens, like density modes (compact, comfortable, spacious) or themes and have descendant components react to changes directly and in component-appropriate ways.diff --git a/gemini-extension.json b/gemini-extension.jsonindex f90be83..dd2468e 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.173",+ "version": "0.0.174","author": {"name": "Google Chrome"}diff --git a/package.json b/package.jsonindex 802ae3f..006c4b9 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.173",+ "version": "0.0.174","publisher": "GoogleChrome","repository": {"url": "https://github.com/GoogleChrome/modern-web-guidance-src"diff --git a/skills/modern-web-guidance/guides/user-experience/custom-button-actions.md b/skills/modern-web-guidance/guides/user-experience/custom-button-actions.mdnew file mode 100644index 0000000..f9996f1--- /dev/null+++ b/skills/modern-web-guidance/guides/user-experience/custom-button-actions.md@@ -0,0 +1,173 @@+# Custom Button Actions+The Invoker Commands API allows buttons to trigger actions on target elements declaratively using HTML attributes.+This approach reduces the need for manual event listeners and decouples the UI from implementation details.+For custom, application-specific actions, you can define your own command names. Custom commands must be prefixed with a double dash (`--`) to avoid collisions with future built-in browser commands.++## Implementation steps++1. **Define the target element**: Identify the element that will respond to the action. If it doesn’t have a unique `id`, add one.+2. **Configure the invoker button**: Use the `commandfor` attribute to point to the target's `id`, and the `command` attribute to specify the custom command name (prefixed with `--`).+3. **Handle the command event**: Attach a `command` event listener directly on the target element. The event object contains a `command` property and a `target` property (referring to the element identified by `commandfor`).+4. **Handle aria states**: Custom commands do not have inherent semantics, and you must handle states like `aria-pressed` or `aria-expanded`.++## Example: Custom Animation Controls++```html+<!-- The target element that will respond to custom commands -->+<div id="action-target" class="target">+ Action Target+</div>++<!-- Buttons declaratively linked to the target element -->+<!-- Each button sends a unique custom command starting with '--' -->+<button commandfor="action-target" command="--spin">+ Spin+</button>++<button commandfor="action-target" command="--grow">+ Grow+</button>++<button commandfor="action-target" command="--reset">+ Reset All+</button>++<script>+ // 1. **Optional:** Define a registry of requested actions for cleaner logic+const commandRegistry = {+ '--spin': (target, source) => {+ const isSpun = target.classList.toggle('is-spun');+ // Set ARIA states, as custom commands have no inherent semantics.+ source?.setAttribute('aria-pressed', isSpun);+ },+ '--grow': (target, source) => {+ const isGrown = target.classList.toggle('is-grown');+ source?.setAttribute('aria-pressed', isGrown);+ },+ '--reset': (target) => {+ target.classList.remove('is-spun', 'is-grown');+ // Reset all associated buttons' ARIA states+ document.querySelectorAll(`button[commandfor="${target.id}"]`).forEach(btn => {+ btn.setAttribute('aria-pressed', 'false');+ });+ },+};++ // 2. **Mandatory:** Listen for the 'command' event directly on the target element+ // (This is necessary because the native 'command' event does not bubble)+ document.getElementById('action-target').addEventListener('command', (event) => {+ const command = event.command;+ const target = event.target;+ const source = event.source; // event.source refers to the triggering button+ const action = commandRegistry[command];++ if (action) {+ action(target, source);+ }+ });+</script>+```++## Key constraints++* **Prefix custom commands**: MANDATORY: All custom command names must start with `--` (e.g., `command="--my-action"`).+* **Targeting**: The `commandfor` attribute must match the `id` of an element in the same document tree.+* **No bubbling**: The `command` event does not bubble. If there multiple possible targets, add `{ capture: true }` to the event handler and listen on an ancestor.+* **Shadow roots**: If the target may be in a shadow root, use `event.composedPath()[0]` instead of `event.target`.+* **Accessibility**: Custom commands have no inherent semantics, and you must explicitly apply any states.++## Fallback strategies++Baseline status for Invoker commands: Newly available. It's been Baseline since 2025-12-12.+Supported by: Chrome 135 (Apr 2025), Edge 135 (Apr 2025), Firefox 144 (Oct 2025), and Safari 26.2 (Dec 2025).++If the Invoker Commands API is not supported, the `command` event will not fire. For full support across all modern browsers, it is recommended to use the invokers-polyfill from https://github.com/keithamus/invokers-polyfill via `npm install` or CDN.++This polyfill fully supports custom actions (starting with `--`) and dispatches the `command` event exactly like the native API.++### Dynamic Import (Performance Optimization)++For the best performance, you should only load the polyfill if the browser doesn't support the API natively. This saves bandwidth and reduces script execution time for users on modern browsers.++**NOTE:** This polyfill does not manage ARIA states (like `aria-pressed` or `aria-expanded`) for custom commands. You must manually synchronize these states in your event listener to ensure your site is accessible.++```javascript+// 1. Conditionally load the polyfill+const hasNativeSupport = 'commandForElement' in HTMLButtonElement.prototype;++if (!hasNativeSupport) {+ // Wrap in an async IIFE to avoid top-level await issues in older browsers+ (async () => {+ try {+ await import('https://esm.run/invokers-polyfill');+ } catch (err) {+ console.error('Error loading fallback:', err);+ }+ })();+}++// 2. Manually manage ARIA states in your listener+document.getElementById('action-target').addEventListener('command', (event) => {+ const command = event.command;+ const target = event.target;+ const source = event.source; // The button that triggered the command++ if (command === '--spin') {+ const isSpun = target.classList.toggle('is-spun');++ // Polyfill tip: Manually update ARIA to match the new state+ source?.setAttribute('aria-pressed', isSpun);+ }+});+```++### Manual fallback (Traditional pattern)++If you prefer not to use a polyfill, you can use a combination of **event delegation** to dispatch events and a **command registry** to handle the actions. This is a common architectural pattern in traditional JavaScript development that remains highly efficient and scalable.++```javascript+// 1. **Optional:** Define a registry of requested actions for cleaner logic+const commandRegistry = {+ '--spin': (target) => target.classList.toggle('is-spun'),+ '--grow': (target) => target.classList.toggle('is-grown'),+ '--reset': (target) => target.classList.remove('is-spun', 'is-grown'),+};++// 2. If CommandEvent doesn't exist, we assume no native support and provide the fallback+if (!globalThis.CommandEvent) {+ globalThis.CommandEvent = class CommandEvent extends Event {+ constructor(type, { source, command, ...options } = {}) {+ super(type, options);+ this.source = source;+ this.command = command;+ }+ }+}++// 3. The fallback: Dispatch events manually if native support is missing+ document.addEventListener('click', (event) => {+ const button = event.composedPath().find((el) => el.matches?.("button[commandfor]"));+ if (!button) return;++ const target = document.getElementById(button.getAttribute('commandfor'));+ const command = button.getAttribute('command');++ if (target && command) {+ target.dispatchEvent(new CommandEvent('command', {+ command,+ source: button,+ }));+ }+ });++// 4. **Mandatory:** Register the unified listener directly on the target element+document.getElementById('action-target').addEventListener('command', (event) => {+ const command = event.command;+ const target = event.target;+ const action = commandRegistry[command];++ if (action) {+ action(target);+ }+ });+```diff --git a/skills/modern-web-guidance/guides/user-experience/declarative-button-actions.md b/skills/modern-web-guidance/guides/user-experience/declarative-button-actions.mddeleted file mode 100644index 8404a24..0000000--- a/skills/modern-web-guidance/guides/user-experience/declarative-button-actions.md+++ /dev/null@@ -1,130 +0,0 @@-# Declarative Button Actions-The Invoker Commands API allows buttons to trigger actions on target elements declaratively using HTML attributes. This approach reduces the need for manual event listeners and ensures interactivity as soon as the HTML is parsed.--For custom, application-specific actions, you can define your own command names. Custom commands must be prefixed with a double dash (`--`) to avoid collisions with future built-in browser commands.--## Implementation steps--1. **Define the target element**: Identify the element that will respond to the action. It must have a unique `id`.-2. **Configure the invoker button**: Use the `commandfor` attribute to point to the target's `id`, and the `command` attribute to specify the custom command name (prefixed with `--`).-3. **Handle the command event**: Attach a `command` event listener to the `document` (or a common parent). This ensures that the event is captured even if it is dispatched by a polyfill or from a child element. The event object contains a `command` property and a `target` property (referring to the element identified by `commandfor`).--## Example: Custom Animation Controls--```html-<!-- The target element that will respond to custom commands -->-<div id="action-target" class="target">- Action Target-</div>--<!-- Buttons declaratively linked to the target element -->-<!-- Each button sends a unique custom command starting with '--' -->-<button commandfor="action-target" command="--spin">- Spin-</button>--<button commandfor="action-target" command="--grow">- Grow-</button>--<button commandfor="action-target" command="--reset">- Reset All-</button>--<script>- // Listen for the 'command' event directly on the target element- // (This is necessary because the native 'command' event does not bubble)- document.getElementById('action-target').addEventListener('command', (event) => {- // Robustly handle both native API and manual/polyfill fallbacks- const command = event.command || event.detail?.command;- const target = event.currentTarget;-- // Custom commands are checked to identify the requested action- if (command === '--spin') {- target.classList.toggle('is-spun');- } else if (command === '--grow') {- target.classList.toggle('is-grown');- } else if (command === '--reset') {- // Clear all custom classes to return to initial state- target.classList.remove('is-spun', 'is-grown');- }- });-</script>-```--## Key constraints--* **Prefix custom commands**: MANDATORY: All custom command names must start with `--` (e.g., `command="--my-action"`).-* **Targeting**: The `commandfor` attribute must match the `id` of an element in the same document tree.--## Fallback strategies--Baseline status for Invoker commands: Newly available. It's been Baseline since 2025-12-12.-Supported by: Chrome 135 (Apr 2025), Edge 135 (Apr 2025), Firefox 144 (Oct 2025), and Safari 26.2 (Dec 2025).--If the Invoker Commands API is not supported, the `command` event will not fire. For full support across all modern browsers, it is recommended to use the invokers-polyfill from https://github.com/keithamus/invokers-polyfill via `npm install` or CDN.--This polyfill fully supports custom actions (starting with `--`) and dispatches the `command` event exactly like the native API.--### Dynamic Import (Performance Optimization)--For the best performance, you should only load the polyfill if the browser doesn't support the API natively. This saves bandwidth and reduces script execution time for users on modern browsers.--```javascript-// Check for native support first-const hasNativeSupport = 'commandForElement' in HTMLButtonElement.prototype;--if (!hasNativeSupport) {- // Dynamically import the polyfill only when needed- try {- await import('https://cdn.jsdelivr.net/npm/invokers-polyfill@latest/dist/index.min.js');- console.log('Invoker Commands polyfill loaded');- } catch (err) {- console.error('Error loading fallback:', err);- }-}-```--### Manual fallback (Traditional pattern)--If you prefer not to use a polyfill, you can use a combination of **event delegation** to dispatch events and a **command registry** to handle the actions. This is a common architectural pattern in traditional JavaScript development that remains highly efficient and scalable.--```javascript-// 1. Define a registry of requested actions for cleaner logic-const commandRegistry = {- '--spin': (target) => target.classList.toggle('is-spun'),- '--grow': (target) => target.classList.toggle('is-grown'),- '--reset': (target) => target.classList.remove('is-spun', 'is-grown'),-};--const supportsInvokers = 'commandForElement' in HTMLButtonElement.prototype;--// 2. The fallback: Dispatch events manually if native support is missing-if (!supportsInvokers) {- document.addEventListener('click', (event) => {- const button = event.target.closest('button[commandfor]');- if (!button) return;-- const target = document.getElementById(button.getAttribute('commandfor'));- const command = button.getAttribute('command');-- if (target && command) {- target.dispatchEvent(new CustomEvent('command', {- bubbles: true,- detail: { command }- }));- }- });-}--// 3. The unified listener: Registered directly on the target element-document.getElementById('action-target').addEventListener('command', (event) => {- const command = event.command || event.detail?.command;- const target = event.currentTarget;- const action = commandRegistry[command];-- if (action) {- action(target);- }-});-```diff --git a/skills/modern-web-guidance/guides/user-experience/declarative-dialog-popover-control.md b/skills/modern-web-guidance/guides/user-experience/declarative-dialog-popover-control.mdindex 25fa923..9b56bcc 100644--- a/skills/modern-web-guidance/guides/user-experience/declarative-dialog-popover-control.md+++ b/skills/modern-web-guidance/guides/user-experience/declarative-dialog-popover-control.md@@ -112,19 +112,36 @@ This polyfill fully supports custom actions (starting with `--`) and dispatchesFor the best performance, you should only load the polyfill if the browser doesn't support the API natively. This saves bandwidth and reduces script execution time for users on modern browsers.+**NOTE:** This polyfill does not manage ARIA states (like `aria-pressed` or `aria-expanded`) for custom commands. You must manually synchronize these states in your event listener to ensure your site is accessible.+```javascript-// Check for native support first+// 1. Conditionally load the polyfillconst hasNativeSupport = 'commandForElement' in HTMLButtonElement.prototype;if (!hasNativeSupport) {- // Dynamically import the polyfill only when needed- try {- await import('https://cdn.jsdelivr.net/npm/invokers-polyfill@latest/dist/index.min.js');- console.log('Invoker Commands polyfill loaded');- } catch (err) {- console.error('Error loading fallback:', err);- }+ // Wrap in an async IIFE to avoid top-level await issues in older browsers+ (async () => {+ try {+ await import('https://esm.run/invokers-polyfill');+ } catch (err) {+ console.error('Error loading fallback:', err);+ }+ })();}++// 2. Manually manage ARIA states in your listener+document.getElementById('action-target').addEventListener('command', (event) => {+ const command = event.command;+ const target = event.target;+ const source = event.source; // The button that triggered the command++ if (command === '--spin') {+ const isSpun = target.classList.toggle('is-spun');++ // Polyfill tip: Manually update ARIA to match the new state+ source?.setAttribute('aria-pressed', isSpun);+ }+});```### Manual fallback (Traditional pattern)@@ -132,43 +149,50 @@ if (!hasNativeSupport) {If you prefer not to use a polyfill, you can use a combination of **event delegation** to dispatch events and a **command registry** to handle the actions. This is a common architectural pattern in traditional JavaScript development that remains highly efficient and scalable.```javascript-// 1. Define a registry of requested actions for cleaner logic+// 1. **Optional:** Define a registry of requested actions for cleaner logicconst commandRegistry = {'--spin': (target) => target.classList.toggle('is-spun'),'--grow': (target) => target.classList.toggle('is-grown'),'--reset': (target) => target.classList.remove('is-spun', 'is-grown'),};-const supportsInvokers = 'commandForElement' in HTMLButtonElement.prototype;+// 2. If CommandEvent doesn't exist, we assume no native support and provide the fallback+if (!globalThis.CommandEvent) {+ globalThis.CommandEvent = class CommandEvent extends Event {+ constructor(type, { source, command, ...options } = {}) {+ super(type, options);+ this.source = source;+ this.command = command;+ }+ }+}-// 2. The fallback: Dispatch events manually if native support is missing-if (!supportsInvokers) {+// 3. The fallback: Dispatch events manually if native support is missingdocument.addEventListener('click', (event) => {- const button = event.target.closest('button[commandfor]');+ const button = event.composedPath().find((el) => el.matches?.("button[commandfor]"));if (!button) return;const target = document.getElementById(button.getAttribute('commandfor'));const command = button.getAttribute('command');if (target && command) {- target.dispatchEvent(new CustomEvent('command', {- bubbles: true,- detail: { command }+ target.dispatchEvent(new CommandEvent('command', {+ command,+ source: button,}));}});-}-// 3. The unified listener: Registered directly on the target element+// 4. **Mandatory:** Register the unified listener directly on the target elementdocument.getElementById('action-target').addEventListener('command', (event) => {- const command = event.command || event.detail?.command;- const target = event.currentTarget;+ const command = event.command;+ const target = event.target;const action = commandRegistry[command];if (action) {action(target);}-});+ });```### Polyfilling the Popover Attribute