Keep your coding agent up to date with the latest web best practices
Release v0.0.109
github-actions[bot]ae10309a4220
diff --git a/README.md b/README.mdindex 1a21dbf..b9bb90b 100644--- a/README.md+++ b/README.md@@ -39,7 +39,7 @@ Just a tiny sampling of the **134+ use-case-centric guides**:#### Full Skill Coverage (v0.0.109)<details>-<summary>Includes expert guidance across <strong>83 modern web features</strong></summary>+<summary>Includes expert guidance across <strong>84 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/)@@ -104,6 +104,7 @@ Just a tiny sampling of the **134+ use-case-centric guides**:- [Registered custom properties](https://web-platform-dx.github.io/web-features-explorer/features/registered-custom-properties/)- [Scheduler API](https://web-platform-dx.github.io/web-features-explorer/features/scheduler/)- [Scroll snap](https://web-platform-dx.github.io/web-features-explorer/features/scroll-snap/)+- [Scroll snap events](https://web-platform-dx.github.io/web-features-explorer/features/scroll-snap-events/)- [Scroll-driven animations](https://web-platform-dx.github.io/web-features-explorer/features/scroll-driven-animations/)- [scroll-initial-target](https://web-platform-dx.github.io/web-features-explorer/features/scroll-initial-target/)- [scrollbar-color](https://web-platform-dx.github.io/web-features-explorer/features/scrollbar-color/)@@ -127,7 +128,7 @@ Just a tiny sampling of the **134+ use-case-centric guides**:</details><details>-<summary>Covers <strong>106 real-world developer use cases</strong> with production-ready code patterns</summary>+<summary>Covers <strong>107 real-world developer use cases</strong> with production-ready code patterns</summary><h3>accessibility</h3>@@ -236,6 +237,7 @@ Just a tiny sampling of the **134+ use-case-centric guides**:- **[scroll-entry-exit-effects](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/scroll-entry-exit-effects.md)**: Create fade-in, scale-up, or other complex reveal-type effects on elements as they enter and exit the scrollport (or viewport) while the user is scrolling.- **[scroll-position-aware-elements](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/scroll-position-aware-elements.md)**: Build floating buttons or widgets (back-to-top, scroll-to-bottom, chat launchers, etc.) that appear and disappear based on whether the user has scrolled at all.- **[scroll-progress-indicator](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/scroll-progress-indicator.md)**: Create a scroll progress bar, stepped progress tracker, or any visual affordance that communicates how far through a page or section the user has scrolled.+- **[scroll-snap-state-sync](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/scroll-snap-state-sync.md)**: Synchronize navigation indicators, linked content panels, and analytics tracking with the actively snapped item in a scrollable container.- **[scroll-target-on-load](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/scroll-target-on-load.md)**: Build a scrollable list of elements (e.g. a carousel of images or a chat conversation thread) that can be displayed with a particular element scrolled into view on the initial render.- **[scrollability-affordance-hints](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/scrollability-affordance-hints.md)**: Build scroll-shadow overlays, gradient fades, or directional arrow indicators that appear only when there's actually more content to scroll to in that direction.- **[scrollytelling](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/scrollytelling.md)**: Animate visual properties on a target element — such as fading a backdrop, shifting a background color, or to create scrollytelling experiences — driven entirely by the scrollport position of a completely different element.diff --git a/skills/modern-web-guidance/guides/user-experience/scroll-snap-state-sync.md b/skills/modern-web-guidance/guides/user-experience/scroll-snap-state-sync.mdnew file mode 100644index 0000000..c045af7--- /dev/null+++ b/skills/modern-web-guidance/guides/user-experience/scroll-snap-state-sync.md@@ -0,0 +1,81 @@+Synchronizing UI state with a scrollable container's snap position traditionally required complex scroll event listeners, manual calculations of scroll offsets, and intersection observers. The `scrollsnapchange` event provides a native, efficient way to detect when a scroller has settled on a new snap target, making it useful for synchronizing sidebars or highlighting the active section in a table of contents.++## Implementation++### 1. Configure Scroll Snap in CSS+The container must have `scroll-snap-type` defined, and have children with `scroll-snap-align` for the browser to track snap targets. In a long article with a table of contents, you can use this to snap section headers to the top of the viewport.++```css+main {+ /* Enable scroll snapping on the container */+ scroll-snap-type: y proximity;+ overflow-y: auto;+}++h2 {+ /* Define how headers align when snapped */+ scroll-snap-align: start;+}+```++### 2. Listen for Snap Changes+Use the `scrollsnapchange` event on the scroll container to react when the user finishes scrolling and the browser snaps to a new element. In our TOC demo, we use this to highlight the active link in the sidebar.++```javascript+const main = document.getElementById('main');+const links = document.querySelectorAll('nav a');++// The event fires when the scroller settles on a new snap target+main.addEventListener('scrollsnapchange', (event) => {+ // Use snapTargetBlock for vertical or snapTargetInline for horizontal+ const snappedHeader = event.snapTargetBlock;++ if (snappedHeader) {+ setSelectedParagraph(snappedHeader.id);+ }+});+```+++## Accessibility++Caution: While Scroll Snap Events make it possible to visually synchronize other content to the state of the scroller, it does not automatically expose that information programmatically. Relationships between elements, active states, and live content must be reflected in the Accessibility Tree.++For a table of contents, ensure the sidebar links use `aria-current="true"` or `aria-current="location"` when they are active.++In addition, be careful when using the `mandatory` value for `scroll-snap-type`, as it can cause content in-between snap-points to become inaccessible when longer than the screen.+++## Fallback strategies++Scroll snap events has limited availability.++If `scrollsnapchange` is not supported, use `IntersectionObserver` to detect which element is currently at the top of the scroller. Note this is different behavior than `scrollsnapchange`, as this will trigger while the scroll happens, rather than only when the scroll has settled.++```javascript+// Feature detect support for scroll snap events+if (!('onscrollsnapchange' in HTMLElement.prototype)) {+ const observer = new IntersectionObserver(+ () => {+ // Each time the set of intersecting headers changes, find the top+ // header that is visible.+ const topEntry = [...headers].reduce((currentTop, header) => {+ // Use the bottom to handle scrolling up, when the top is still offscreen+ const {bottom} = header.getBoundingClientRect();+ // Don't match if the header's bottom is aboce the scrollport+ if (bottom < 0) return;+ if (!currentTop) return header;+ return bottom <+ currentTop.getBoundingClientRect().bottom+ ? header+ : currentTop;+ }, undefined);+ if (topEntry) setSelectedParagraph(topEntry.id);+ },+ { root: main, threshold: 0.9 // Adjust based on your use case },+ );++ // Observe all snap targets (e.g., section headers)+ document.querySelectorAll('h2').forEach(header => observer.observe(header));+}+```