Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editorial review: Add docs for scroll snap events #36057

Merged
merged 6 commits into from
Oct 11, 2024

Conversation

chrisdavidmills
Copy link
Contributor

@chrisdavidmills chrisdavidmills commented Sep 26, 2024

Description

Chrome 129 supports scroll snap events — scrollsnapchange and scrollsnapchanging, which enable code being fired in response to a new scroll snap target being snapped to, and the browser identifying a change in the next snap target to be snapped to (if the gesture were to end).

See the associated ChromeStatus entry and the Scroll Snap Events blog post for more context and a guide.

This PR adds:

  • The new events. I've written them up in detail in the versions defined on Element, but I've also put short pages for the events hanging off Document and Window, which link to the Element versions for the full details. See https://drafts.csswg.org/css-scroll-snap-2/#event-handlers-on-elements-document-and-window-objects.
  • The SnapEvent event object
  • A guide to using the events, hung off the CSS scroll snap module landing page. It is about JS much more than it is about CSS, but the events are defined in Scroll Snap Events level 2.

Motivation

Additional details

Related issues and pull requests

@chrisdavidmills chrisdavidmills requested a review from a team as a code owner September 26, 2024 09:50
@chrisdavidmills chrisdavidmills requested review from sideshowbarker and removed request for a team September 26, 2024 09:50
@chrisdavidmills chrisdavidmills marked this pull request as draft September 26, 2024 09:50
@github-actions github-actions bot added Content:WebAPI Web API docs size/l [PR only] 501-1000 LoC changed labels Sep 26, 2024
Copy link
Contributor

github-actions bot commented Sep 26, 2024

Preview URLs (15 pages)
Flaws (17)

Note! 3 documents with no flaws that don't need to be listed. 🎉

URL: /en-US/docs/Web/API/Document
Title: Document
Flaw count: 6

  • macros:
    • /en-US/docs/Web/API/Document/xmlStandalone does not exist
    • /en-US/docs/Web/API/Document/captureEvents does not exist
    • /en-US/docs/Web/API/Document/getBoxQuads does not exist
    • /en-US/docs/Web/API/Document/releaseEvents does not exist
    • /en-US/docs/Web/API/Document/queryCommandIndeterm does not exist
    • and 1 more flaws omitted

URL: /en-US/docs/Web/API/Document/scrollsnapchanging_event
Title: Document: scrollsnapchanging event
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.Document.scrollsnapchanging_event

URL: /en-US/docs/Web/API/Document/scrollsnapchange_event
Title: Document: scrollsnapchange event
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.Document.scrollsnapchange_event

URL: /en-US/docs/Web/API/SnapEvent
Title: SnapEvent
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.SnapEvent

URL: /en-US/docs/Web/API/SnapEvent/snapTargetBlock
Title: SnapEvent: snapTargetBlock property
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.SnapEvent.snapTargetBlock

URL: /en-US/docs/Web/API/SnapEvent/SnapEvent
Title: SnapEvent: SnapEvent() constructor
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.SnapEvent.SnapEvent

URL: /en-US/docs/Web/API/SnapEvent/snapTargetInline
Title: SnapEvent: snapTargetInline property
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.SnapEvent.snapTargetInline

URL: /en-US/docs/Web/API/Window/scrollsnapchanging_event
Title: Window: scrollsnapchanging event
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.Window.scrollsnapchanging_event

URL: /en-US/docs/Web/API/Window/scrollsnapchange_event
Title: Window: scrollsnapchange event
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.Window.scrollsnapchange_event

URL: /en-US/docs/Web/API/Element
Title: Element
Flaw count: 1

  • macros:
    • /en-US/docs/Web/API/Element/getBoxQuads does not exist

URL: /en-US/docs/Web/API/Element/scrollsnapchanging_event
Title: Element: scrollsnapchanging event
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.Element.scrollsnapchanging_event

URL: /en-US/docs/Web/API/Element/scrollsnapchange_event
Title: Element: scrollsnapchange event
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.Element.scrollsnapchange_event
External URLs (11)

URL: /en-US/docs/Web/API/Document/scrollsnapchanging_event
Title: Document: scrollsnapchanging event


URL: /en-US/docs/Web/API/Document/scrollsnapchange_event
Title: Document: scrollsnapchange event


URL: /en-US/docs/Web/API/SnapEvent
Title: SnapEvent


URL: /en-US/docs/Web/API/SnapEvent/snapTargetBlock
Title: SnapEvent: snapTargetBlock property


URL: /en-US/docs/Web/API/SnapEvent/SnapEvent
Title: SnapEvent: SnapEvent() constructor


URL: /en-US/docs/Web/API/SnapEvent/snapTargetInline
Title: SnapEvent: snapTargetInline property


URL: /en-US/docs/Web/API/Window/scrollsnapchanging_event
Title: Window: scrollsnapchanging event


URL: /en-US/docs/Web/API/Window/scrollsnapchange_event
Title: Window: scrollsnapchange event


URL: /en-US/docs/Web/API/Element/scrollsnapchanging_event
Title: Element: scrollsnapchanging event


URL: /en-US/docs/Web/API/Element/scrollsnapchange_event
Title: Element: scrollsnapchange event


URL: /en-US/docs/Web/CSS/CSS_scroll_snap/Using_scroll_snap_events
Title: Using scroll snap events

(comment last updated: 2024-10-11 14:47:25)

@github-actions github-actions bot added Content:CSS Cascading Style Sheets docs size/xl [PR only] >1000 LoC changed and removed size/l [PR only] 501-1000 LoC changed labels Sep 30, 2024
@chrisdavidmills chrisdavidmills removed the request for review from sideshowbarker September 30, 2024 12:35
Copy link
Contributor

@argyleink argyleink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome, looks good to me. I like the way you've phrased the events and distinguished them, well put 💯

@chrisdavidmills chrisdavidmills changed the title Add docs for scroll snap events Editorial review: Add docs for scroll snap events Oct 1, 2024
@chrisdavidmills chrisdavidmills marked this pull request as ready for review October 1, 2024 09:42
@chrisdavidmills chrisdavidmills requested a review from a team as a code owner October 1, 2024 09:42
@chrisdavidmills chrisdavidmills requested review from estelle and removed request for a team October 1, 2024 09:42
@estelle estelle self-assigned this Oct 1, 2024
estelle

This comment was marked as outdated.

Comment on lines 216 to 232

In the JavaScript, we start by grabbing a reference to the `<main>` element and defining the number of `<section>` elements to generate (21), and a variable to begin counting from. We then use a [`while`](/en-US/docs/Web/JavaScript/Reference/Statements/while) loop to generate the `<section>` elements, giving each one an ID of `s` plus the current value of `n`, and a child [`h2`](/en-US/docs/Web/HTML/Element/Heading_Elements) with text that reads `Section` plus the current value of `n`.

```js
const mainElem = document.querySelector("main");
const sectionCount = 21;
let n = 1;

while (n <= sectionCount) {
mainElem.innerHTML += `
<section id="s${n}">
<h2>Section ${n}</h2>
</section>
`;
n++;
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use static HTML (hidden). This way, we can remove this entire section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree with this. If I used static HTML, I'd still have to explain the structure of the <section> elements inside the HTML section, otherwise, the example wouldn't make sense. And then I'd have to add all 21 elements into the HTML, or find a way of hiding the full amount and just showing a single example, which would be awkward.

I'd rather keep it like this. It's not exactly a weird practice — people generate multiple HTML components all the time using frameworks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this were in the JS / API section, i would not have suggested it. But in the CSS docs we avoid the el.style.x="y" and prefer class changes (keeping CSS out of the JS), and use static HTML. I was thinking that perhaps this guide is better served in the API docs anyhow. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of things:

  1. I'm not doing any el.style.x="y" in these examples. It is all class-based.
  2. If I put it in the API docs, where would it go? It is part of the CSS Scroll Snap spec.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about https://codepen.io/estelle/pen/PoMZMMY. The example uses transitions rather than animations, and toggles the classes, and reduces the content the guide needs to explain to the event handlers and the very limited properties the event handlers are causing to change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is a bit shorter, but I don't think it is vastly simpler to explain. I also don't want to get rid of all my styling. I'm happy to hide the stuff that doesn't need explaining.

Tell you what, I will use a transition on the first example, like you've done, but I want to keep the animation on the second example, as it is useful to show how that could be handled. In more complex cases, people might want to use an animation.

Comment on lines +102 to +106
The HTML for the example is a single `<main>` element. We will add the `<section>` elements dynamically with JavaScript later on, to save on page space.

```html
<main></main>
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it static and hidden.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree; see above.


{{CSSRef}}

The [CSS Scroll Snap Module Level 2](drafts.csswg.org/css-scroll-snap-2) specification defines **scroll snap events**: {{domxref("Element/scrollsnapchanging_event", "scrollsnapchanging")}} and {{domxref("Element/scrollsnapchange_event", "scrollsnapchange")}}. These make it easy to run JavaScript in response to the browser determining that a new snap target is pending, and when a new snap target is selected, respectively.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The [CSS Scroll Snap Module Level 2](drafts.csswg.org/css-scroll-snap-2) specification defines **scroll snap events**: {{domxref("Element/scrollsnapchanging_event", "scrollsnapchanging")}} and {{domxref("Element/scrollsnapchange_event", "scrollsnapchange")}}. These make it easy to run JavaScript in response to the browser determining that a new snap target is pending, and when a new snap target is selected, respectively.
The [CSS scroll snap](en-US/docs/Web/CSS/CSS_scroll_snap) module defines two **scroll snap events**: {{domxref("Element/scrollsnapchanging_event", "scrollsnapchanging")}} and {{domxref("Element/scrollsnapchange_event", "scrollsnapchange")}}. These enable running JavaScript in response to the browser determining that a new snap target is pending and when a new [scroll snap target](/en-US/docs/Web/CSS/CSS_scroll_snap/Basic_concepts) is selected, respectively.

avoid "easy"
added "scroll" so i could link to the basic intro

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I've basically used your wording, except that I've cut down the two mentions of (scroll) snap target into one.


The [CSS Scroll Snap Module Level 2](drafts.csswg.org/css-scroll-snap-2) specification defines **scroll snap events**: {{domxref("Element/scrollsnapchanging_event", "scrollsnapchanging")}} and {{domxref("Element/scrollsnapchange_event", "scrollsnapchange")}}. These make it easy to run JavaScript in response to the browser determining that a new snap target is pending, and when a new snap target is selected, respectively.

This article provides a guide to using these events, along with complete examples.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This article provides a guide to using these events, along with complete examples.
This guide provides an overview of these events, along with complete examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


## Events overview

The two events are set on a scrolling container that contains potential scroll snap targets, and are as follows:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The two events are set on a scrolling container that contains potential scroll snap targets, and are as follows:
Scroll snap events are set on a [scrolling container](/en-US/docs/Glossary/Scroll_container) that contains potential scroll snap targets. The events include:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


- Try slowly scrolling the container up and down, without releasing the scrolling gesture, For example, drag your finger(s) on a touch screen device or trackpad, or hold down the mouse button on the scroll bar and move the mouse. You should see the boxes you move over turn a darker gray color, and then return to normal as you move away from them again. This is the `scrollsnapchanging` event in action.
- Now try releasing the scrolling gesture; the nearest box to your scrolling position should animate to a purple color, with white text. This is due to code run in response to the `scrollsnapchange` event firing.
- Last of all, try scrolling fast, for example by flicking your finger hard on the screen, to scroll past several potential targets before starting to come to rest near a target further down the scroll container. You should only see one `scrollsnapchanging` event fire as the scrolling starts to slow, before the `scrollsnapchange` event then fires and the selected snap target turns purple.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Last of all, try scrolling fast, for example by flicking your finger hard on the screen, to scroll past several potential targets before starting to come to rest near a target further down the scroll container. You should only see one `scrollsnapchanging` event fire as the scrolling starts to slow, before the `scrollsnapchange` event then fires and the selected snap target turns purple.
- Last of all, try scrolling fast, for example by flicking your finger hard on the screen, to scroll past several potential targets before starting to come to rest near a target further down the scroll container. You should only see one `scrollsnapchanging` event fire as the scrolling starts to slow, before the `scrollsnapchange` event fires and the selected snap target turns purple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


## The `SnapEvent` event object

Both of the above events share the same event object: {{domxref("SnapEvent")}}. This has two distinct properties that are key to how scroll snap events work:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Both of the above events share the same event object: {{domxref("SnapEvent")}}. This has two distinct properties that are key to how scroll snap events work:
Both of the above events share the {{domxref("SnapEvent")}} event object which has two distinct properties that are key to how scroll snap events work:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated somewhat. I ended up with:

Both of the above events share the {{domxref("SnapEvent")}} event object. This has two properties that are key to how scroll snap events work:

Splitting into two sentences feels cleaner to read, to me.


Both of the above events share the same event object: {{domxref("SnapEvent")}}. This has two distinct properties that are key to how scroll snap events work:

- {{domxref("SnapEvent.snapTargetBlock", "snapTargetBlock")}} returns a reference to the element snapped to in the block direction when the event fired.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- {{domxref("SnapEvent.snapTargetBlock", "snapTargetBlock")}} returns a reference to the element snapped to in the block direction when the event fired.
- {{domxref("SnapEvent.snapTargetBlock", "snapTargetBlock")}} returns a reference to the element snapped to in the [block direction](/en-US/docs/Glossary/Flow_relative_values#block_direction) when the event fired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Both of the above events share the same event object: {{domxref("SnapEvent")}}. This has two distinct properties that are key to how scroll snap events work:

- {{domxref("SnapEvent.snapTargetBlock", "snapTargetBlock")}} returns a reference to the element snapped to in the block direction when the event fired.
- {{domxref("SnapEvent.snapTargetInline", "snapTargetInline")}} returns a reference to the element snapped to in the inline direction when the event fired.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- {{domxref("SnapEvent.snapTargetInline", "snapTargetInline")}} returns a reference to the element snapped to in the inline direction when the event fired.
- {{domxref("SnapEvent.snapTargetInline", "snapTargetInline")}} returns a reference to the element snapped to in the [inline direction](/en-US/docs/Glossary/Flow_relative_values#inline_direction) when the event fired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Comment on lines 27 to 28
- The {{domxref("Document/scrollsnapchange_event", "scrollsnapchange")}} event
- {{domxref("SnapEvent")}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- The {{domxref("Document/scrollsnapchange_event", "scrollsnapchange")}} event
- {{domxref("SnapEvent")}}
- {{domxref("Document/scrollsnapchange_event", "scrollsnapchange")}} event
- {{domxref("SnapEvent")}}
- {{domxref("SnapEvent.snapTargetBlock")}}
- {{domxref("SnapEvent.snapTargetInline")}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced these are really needed, when we already link to the event object?

@Josh-Cena
Copy link
Member

Could you add a group for it in GroupData.json (and write an API overview page) so we can have nice sidebars?

estelle

This comment was marked as outdated.

files/en-us/web/api/snapevent/index.md Outdated Show resolved Hide resolved
files/en-us/web/api/snapevent/index.md Outdated Show resolved Hide resolved
files/en-us/web/api/snapevent/index.md Outdated Show resolved Hide resolved
files/en-us/web/api/snapevent/snapevent/index.md Outdated Show resolved Hide resolved
Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it was seen:

Could you add a group for it in GroupData.json (and write an API overview page) so we can have nice sidebars?

Comment on lines 28 to 30
- : Returns a reference to the element snapped to in the block direction when the event fired, or `null` if no element is snapped to in the block direction.
- {{domxref("SnapEvent.snapTargetInline", "snapTargetInline")}} {{ReadOnlyInline}} {{Experimental_Inline}}
- : Returns a reference to the element snapped to in the inline direction when the event fired.
- : Returns a reference to the element snapped to in the inline direction when the event fired, or `null` if no element is snapped to in the inline direction.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in my tests, i get a reference to the first element. I haven't been able to get a null

Copy link
Member

@estelle estelle Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, was able to get a null, but only if there was no way to snap in the inline direction. If there is the possibility of inline scrolling as well as block scrolling, i get a value for both. If you can only scroll in one direction, the other direction is null.

It turns out, it is based on the value of scroll-snap-type:.
If the value is block, we get a value for snapTargetBlock.
if the value is inline, we get a value of snapTargetInline.
if the value is both, we get a value for both.

I think we need to clarify this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a really good idea to call this out explicitly. I've added an explanation of this on the SnapEvent page and on the scroll snap events guide page. I've also mentioned it on the snapTargetBlock/ snapTargetInline pages.

Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two issues and otherwise just nits.

The two issues:

  1. do events happen on Window? Those pages may need to go based on Adam's response.
  2. We should clarify how we get null

Otherwise, good to go


- Try slowly scrolling the container up and down without releasing the scrolling gesture. For example, drag your finger(s) over the scrolling area on a touchscreen device or trackpad, or hold down the mouse button on the scroll bar and move the mouse. The boxes you move over should turn a darker gray color as you move over them, and then return to normal as you move away from them again. This is the `scrollsnapchanging` event in action.
- Now try releasing the scrolling gesture; the nearest box to your scrolling position should animate to a purple color, with white text. The animation occurs when the `scrollsnapchange` event fires.
- Finally try scrolling fast, for example by flicking your finger hard on the screen, to scroll past several potential targets before starting to come to rest near a target further down the scroll container. You should only see one `scrollsnapchanging` event fire as the scrolling starts to slow, before the `scrollsnapchange` event fires and the selected snap target turns purple.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Finally try scrolling fast, for example by flicking your finger hard on the screen, to scroll past several potential targets before starting to come to rest near a target further down the scroll container. You should only see one `scrollsnapchanging` event fire as the scrolling starts to slow, before the `scrollsnapchange` event fires and the selected snap target turns purple.
- Finally, try scrolling fast. For example, try flicking your finger hard on the screen, to scroll past several potential targets before starting to come to rest near a target further down the scroll container. You should only see one `scrollsnapchanging` event fire as the scrolling starts to slow, before the `scrollsnapchange` event fires and the selected snap target turns purple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


Both of the above events share the {{domxref("SnapEvent")}} event object. This has two properties that are key to how scroll snap events work:

- {{domxref("SnapEvent.snapTargetBlock", "snapTargetBlock")}} returns a reference to the element snapped to in the [block direction](/en-US/docs/Glossary/Flow_relative_values#block_direction) when the event fired, or `null` if no element is snapped to in the block direction (i.e. scroll snapping only occurs in the inline direction).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- {{domxref("SnapEvent.snapTargetBlock", "snapTargetBlock")}} returns a reference to the element snapped to in the [block direction](/en-US/docs/Glossary/Flow_relative_values#block_direction) when the event fired, or `null` if no element is snapped to in the block direction (i.e. scroll snapping only occurs in the inline direction).
- {{domxref("SnapEvent.snapTargetBlock", "snapTargetBlock")}} returns a reference to the element snapped to in the [block direction](/en-US/docs/Glossary/Flow_relative_values#block_direction) when the event fired, or `null` if scroll snapping only occurs in the inline direction so no element is snapped to in the block direction.

The i.e. confused me. I thought "as an example," but that we might get null in some cases in 2-dimensional. So suggesting this change (for every occurrence), but I may be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think saying it this way round is easier to parse. Changed in all cases.


### Handling one-dimensional scrollers

If you are dealing with a horizontal scroller, only the event object's `snapTargetInline` property will change as the snapped element changes if the content has a horizontal {{cssxref("writing-mode")}}, or the `snapTargetBlock` property if the content has a vertical `writing-mode`. Conversely, if you are dealing with a vertical scroller, only the `snapTargetBlock` property will change as the snapped element changes if the content has a horizontal `writing-mode`, or the `snapTargetInline` property if the content has a vertical `writing-mode`. In each case, the non-changing property of the two returns `null`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you are dealing with a horizontal scroller, only the event object's `snapTargetInline` property will change as the snapped element changes if the content has a horizontal {{cssxref("writing-mode")}}, or the `snapTargetBlock` property if the content has a vertical `writing-mode`. Conversely, if you are dealing with a vertical scroller, only the `snapTargetBlock` property will change as the snapped element changes if the content has a horizontal `writing-mode`, or the `snapTargetInline` property if the content has a vertical `writing-mode`. In each case, the non-changing property of the two returns `null`.
If you are dealing with a horizontal scroller, only the event object's `snapTargetInline` property will change as the snapped element changes if the content has a horizontal {{cssxref("writing-mode")}}, or the `snapTargetBlock` property if the content has a vertical `writing-mode`. Conversely, if you are dealing with a vertical scroller, only the `snapTargetBlock` property will change as the snapped element changes if the content has a horizontal `writing-mode`, or the `snapTargetInline` property if the content has a vertical `writing-mode`. In each case, the non-changing property of the two returns in a one-dimensional scroller always returns `null`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about this. We are talking about one-dimensional scrollers in this section. I've updated the two-dimensional scroller section to say that in that case, both properties return an element reference, and neither returns null, to provide a more obvious contrast.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for leaving that in. I edited this before I realized that it has nothing to do with "one dimensional", but rather has to do with the property value of inline, block, or both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np!


If the values are different, it means that the scroller has been scrolled in that direction (block or inline), and we log a message to console to indicate this. In a real example, you'd likely style the snapped element in some way to indicate that it has been snapped to.

We then update the values of `prevState.snapTargetBlock` and `prevState.snapTargetInline`, ready for the next time the event handler is run.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We then update the values of `prevState.snapTargetBlock` and `prevState.snapTargetInline`, ready for the next time the event handler is run.
We then update the values of `prevState.snapTargetBlock` and `prevState.snapTargetInline` for the next time the event handler is run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this one. I've had a think, and ended up updating it to:

We then update the values of prevState.snapTargetBlock and prevState.snapTargetInline ready for when the event handler next runs.

}
```

Each `<section>` element is given a {{cssxref("margin")}} of `50px` to separate out the `<section>` elements and make the scroll snapping behavior a bit easier to experience. We then set {{cssxref("scroll-snap-align")}} to `center`, to specify that we want to snap to the center of each snap target. Finally, we apply a {{cssxref("transition")}} to smoothly animate to and from the style changes applied when a snap target selection has been made or is pending.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Each `<section>` element is given a {{cssxref("margin")}} of `50px` to separate out the `<section>` elements and make the scroll snapping behavior a bit easier to experience. We then set {{cssxref("scroll-snap-align")}} to `center`, to specify that we want to snap to the center of each snap target. Finally, we apply a {{cssxref("transition")}} to smoothly animate to and from the style changes applied when a snap target selection has been made or is pending.
Each `<section>` element is given a {{cssxref("margin")}} of `50px` to separate out the `<section>` elements and make the scroll snapping behavior a bit more apparent. We then set {{cssxref("scroll-snap-align")}} to `center`, to specify that we want to snap to the center of each snap target. Finally, we apply a {{cssxref("transition")}} to smoothly animate to and from the style changes applied when a snap target selection has been made or is pending.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. I got rid of "a bit" too.


### JavaScript

In the JavaScript, we start by grabbing a reference to the `<main>` element and defining the number of `<section>` elements to generate (21), and a variable to begin counting from. We then use a [`while`](/en-US/docs/Web/JavaScript/Reference/Statements/while) loop to generate the `<section>` elements, giving each one a child [`h2`](/en-US/docs/Web/HTML/Element/Heading_Elements) with text that reads `Section` plus the current value of `n`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In the JavaScript, we start by grabbing a reference to the `<main>` element and defining the number of `<section>` elements to generate (21), and a variable to begin counting from. We then use a [`while`](/en-US/docs/Web/JavaScript/Reference/Statements/while) loop to generate the `<section>` elements, giving each one a child [`h2`](/en-US/docs/Web/HTML/Element/Heading_Elements) with text that reads `Section` plus the current value of `n`.
In the JavaScript, we start by grabbing a reference to the `<main>` element and defining the number of `<section>` elements to generate and a variable to begin counting from. We then use a [`while`](/en-US/docs/Web/JavaScript/Reference/Statements/while) loop to generate the `<section>` elements, giving each one a child [`h2`](/en-US/docs/Web/HTML/Element/Heading_Elements) with text that reads `Section` plus the current value of `n`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, "generate (in this case, 21) and...". No comma needed before the "and"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that works nicely. Updated.

```

> [!NOTE]
> We don't need to worry about the `snapTargetInline` event object property for this demo — we are only scrolling vertically and the demo is using a horizontal writing mode, therefore only the `snapTargetBlock` value will change. `snapTargetInline` will always return `null`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> We don't need to worry about the `snapTargetInline` event object property for this demo — we are only scrolling vertically and the demo is using a horizontal writing mode, therefore only the `snapTargetBlock` value will change. `snapTargetInline` will always return `null`.
> We don't need to worry about the `snapTargetInline` event object property for this demo — we are only scrolling vertically and the demo is using a horizontal writing mode, therefore only the `snapTargetBlock` value will change. In this case, `snapTargetInline` will always return `null`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


### CSS

```css hidden
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional, but if you encase all the hidden css in a layer:

@layer {
  /* put hidden css here */
  }
  

You can then move the layer / hidden CSS to after the visible css in the guide.

That ways, if and when the reader hits "play", the relevant CSS will be at the top

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a habit I want to get into. I don't think it is that big a deal making a web developer search through a small stylesheet, and it complicates the CSS unnecessarily.

80% {
background: #eee;
color: black;
opacity: 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
opacity: 0;
opacity: 0.1;

let's not fully hide it.

Copy link
Contributor Author

@chrisdavidmills chrisdavidmills Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it, but I don't really see why you are suggesting this change — it makes very little difference to the look and feel. What is your reasoning here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to suggest to a dev that it's ever good practice to make content disappear (unless you actually want to end up on display none)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, ok.


### JavaScript

In the JavaScript, we start off in the same way as with the previous example, except that this time we generate 49 `<section>` elements, and we give each one an ID of `s` plus the current value of `n` to help track them later on. With the grid layout we specified above, this gives us seven columns of seven `<section>`s.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In the JavaScript, we start off in the same way as with the previous example, except that this time we generate 49 `<section>` elements, and we give each one an ID of `s` plus the current value of `n` to help track them later on. With the grid layout we specified above, this gives us seven columns of seven `<section>`s.
In the JavaScript, we start off in the same way as with the previous example, except that this time we generate 49 `<section>` elements, and we give each one an ID of `s` plus the current value of `n` to help track them later on. With the grid layout we specified above, we will have seven columns of seven `<section>`s.

just to make it not sound like the js is giving us the grid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@chrisdavidmills
Copy link
Contributor Author

Could you add a group for it in GroupData.json (and write an API overview page) so we can have nice sidebars?

@Josh-Cena we could do, but I'd rather not. It is part of the CSS Scroll Snap Module and very closely related to the other features available there. Having another landing page for it IMO would confuse matters and also possibly have negative SEO effects.

Also, we are effectively talking about an interface and two events. If it was significantly more content, I'd be a bit more inclined to agree.

@Josh-Cena
Copy link
Member

OK; perhaps it should be possible for APIRef sidebars to have CSS modules as landing pages. But that's for a future discussions

@chrisdavidmills
Copy link
Contributor Author

OK; perhaps it should be possible for APIRef sidebars to have CSS modules as landing pages. But that's for a future discussions

And to be able to list guides from elsewhere in the guides list. Certainly worth considering in the future.

@estelle estelle merged commit 3b3394b into mdn:main Oct 11, 2024
8 checks passed
@chrisdavidmills chrisdavidmills deleted the scroll-snap-events branch October 11, 2024 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:CSS Cascading Style Sheets docs Content:WebAPI Web API docs size/xl [PR only] >1000 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants