Skip to content

Commit 44bca0a

Browse files
authored
[scroll-animations-1] Use CSSTypedOM instead of strings (#5213) (#5300)
Use appropriate CSSTypedOM instead of DOMStrings in the API. Fixes #5213. Changes: - For offsets use `CSSNumericValue` or `CSSKeywordish` instead of DOMString - When setting the offset, the grammars for these values is check to ensure only the valid subset can be set. - When computing effective scroll offset check the offset actual type and interpret it accordingly - Update examples to showcase the API and add noted to clarify the usage.
1 parent c58e5d5 commit 44bca0a

1 file changed

Lines changed: 71 additions & 17 deletions

File tree

scroll-animations-1/Overview.bs

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Editor: Olga Gerchikov, Microsoft, gerchiko@microsoft.com
1919
2020
Former editor: Mantaroh Yoshinaga
2121
Former editor: Stephen McGruer, Google, smcgruer@google.com
22+
Markup Shorthands: markdown yes
2223
</pre>
2324
<pre class=anchors>
2425
urlPrefix: https://w3c.github.io/web-animations/; type: dfn; spec: web-animations
@@ -183,8 +184,8 @@ if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
183184

184185
const collisionTimeline = new ScrollTimeline({
185186
source: scrollableElement,
186-
start: '200px',
187-
end: '300px'
187+
start: CSS.px(200),
188+
end: CSS.px(300)
188189
});
189190

190191
const left = leftCircle.animate({ transform: 'translate(300px)' }, 1000);
@@ -196,8 +197,8 @@ if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
196197
const union = unionCircle.animate({ opacity: 1 }, { duration: 1000, fill: "forwards" });
197198
union.timeline = new ScrollTimeline({
198199
source: scrollableElement,
199-
start: '250px',
200-
end: '300px'
200+
start: CSS.px(250),
201+
end: CSS.px(300)
201202
});
202203
}
203204
</pre>
@@ -230,6 +231,8 @@ this example could be written as follows:
230231
@media (prefers-reduced-motion: no-preference) {
231232
@scroll-timeline progress-timeline {
232233
source: selector(#body);
234+
start: 0;
235+
end: 100%;
233236
}
234237

235238
@keyframes progress {
@@ -251,7 +254,10 @@ If we use this API for this case, the example code will be as follow:
251254
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
252255
var animation = div.animate({ width: '100%' }, { duration: 1000, fill: "forwards" });
253256
animation.timeline = new ScrollTimeline(
254-
{ start: '0px' }
257+
{
258+
start: 0,
259+
end: CSS.percent(100)
260+
}
255261
);
256262
}
257263
</pre>
@@ -348,11 +354,14 @@ behavior under different directionalities and writing modes.
348354
<pre class="idl">
349355
enum ScrollTimelineAutoKeyword { "auto" };
350356

357+
typedef (CSSNumericValue or CSSKeywordish) ContainerBasedOffset;
358+
typedef (ContainerBasedOffset or ElementBasedOffset) ScrollTimelineOffset;
359+
351360
dictionary ScrollTimelineOptions {
352361
Element? source = null;
353362
ScrollDirection orientation = "block";
354-
(DOMString or ElementBasedOffset) start = "auto";
355-
(DOMString or ElementBasedOffset) end = "auto";
363+
ScrollTimelineOffset start = "auto";
364+
ScrollTimelineOffset end = "auto";
356365
(double or ScrollTimelineAutoKeyword) timeRange = "auto";
357366
};
358367

@@ -361,8 +370,8 @@ interface ScrollTimeline : AnimationTimeline {
361370
constructor(optional ScrollTimelineOptions options = {});
362371
readonly attribute Element? source;
363372
readonly attribute ScrollDirection orientation;
364-
readonly attribute (DOMString or ElementBasedOffset) start;
365-
readonly attribute (DOMString or ElementBasedOffset) end;
373+
readonly attribute ScrollTimelineOffset start;
374+
readonly attribute ScrollTimelineOffset end;
366375
readonly attribute (double or ScrollTimelineAutoKeyword) timeRange;
367376
};
368377
</pre>
@@ -418,11 +427,48 @@ determined not by wall-clock time, but by the progress of scrolling in a
418427
offset=] in the direction specified by {{orientation}} that constitutes the
419428
beginning of the range in which the timeline is active.
420429

430+
On setting, run the procedure to [=set the offset value=] with the provided
431+
value as |val|.
432+
433+
434+
The procedure to <dfn>set the offset value</dfn> with |val| as the provided
435+
value has the following steps:
436+
437+
1. If |val| is a {{DOMString}}, let |val| be the result of
438+
<a lt="rectify a keywordish value">rectifying the keywordish value</a>.
439+
440+
2. Set the offset value to be the result corresponding to the first
441+
matching condition from the following:
442+
443+
<div class="switch">
444+
445+
: If |val| is a {{CSSKeywordValue}} and <a lt="match the grammar">
446+
matches the grammar</a> `auto`:
447+
:: Return |val|.
448+
449+
: If |val| is a {{CSSNumericValue}} and <a lt="match the grammar">
450+
matches the grammar</a> <<length-percentage>>:
451+
:: Return |val|.
452+
453+
: If |val| is an {{ElementBasedOffset}}:
454+
:: Return |val|.
455+
456+
: Otherwise,
457+
:: Do not set the value and throw a {{DOMException}} with error name
458+
{{SyntaxError}}.
459+
460+
</div>
461+
462+
421463
: <dfn attribute for=ScrollTimeline>end</dfn>
422464
:: A [=scroll timeline offset=] which determines the [=effective scroll
423465
offset=] in the direction specified by {{orientation}} that constitutes the
424466
end of the range in which the timeline is active.
425467

468+
On setting, run the procedure to [=set the offset value=] with the provided
469+
value as |val|.
470+
471+
426472
: <dfn attribute for=ScrollTimeline>timeRange</dfn>
427473
:: A time duration that allows mapping between a distance scrolled, and
428474
quantities specified in time units, such as an animation's [=duration=] and
@@ -465,12 +511,8 @@ the [=effective scroll offset=] that is resolved from {{end}}.
465511
A <dfn>container-based offset</dfn> is a scroll timeline offset that is declared
466512
only in relation with the <a>scroll container</a> as specified by {{source}}.
467513

468-
A [=container-based offset=] is provided in the {{DOMString}} form and can have
469-
one the following values:
470-
471-
* auto
472-
* <<length-percentage>>
473-
514+
A [=container-based offset=] is provided in the {{CSSNumericValue}} or
515+
{{CSSKeywordValue}} forms.
474516

475517
The procedure to <dfn>resolve a container-based offset</dfn> given
476518
<var>offset</var> is as follows:
@@ -487,21 +529,33 @@ The procedure to <dfn>resolve a container-based offset</dfn> given
487529
first matching condition from the following:
488530
<div class="switch">
489531

490-
: <var>offset</var> is <code>auto</code>
532+
: If <var>offset</var> is a {{CSSKeywordValue}} and
533+
<a lt="match the grammar">matches</a> `auto`:
491534
:: Either the beginning or the ending of {{source}}'s scroll range in
492535
{{orientation}} depending on whether the offset is {{start}} or {{end}}.
493536

494-
: <var>offset</var> is a <<length-percentage>>
537+
: If <var>offset</var> is a {{CSSNumericValue}} and
538+
<a lt="match the grammar">matches</a> <<length-percentage>>:
495539
:: The distance indicated by the value along {{source}}'s scroll range in
496540
{{orientation}} as expressed by absolute length, a percentage, or a
497541
''calc()'' expression that resolves to a <<length>>.
498542

543+
: Otherwise,
544+
:: null.
545+
499546
</div>
500547

501548
Note: The scroll range of an element is the range defined by its minimum and
502549
maximum scroll offsets which are determined by it [=scrolling box=], [=padding
503550
box=], and [=overflow direction=].
504551

552+
Note: Container-based scroll offsets cannot be provided as bare numbers but
553+
should be {{CSSNumericValue}}. This way the full richness of {{CSSNumericValue}}
554+
APIs can be used to provide the offset in percentages, various length units or
555+
'calc()' expressions. For example `CSS.percent(50)`, `CSS.px(200)`, or
556+
`CSS.vh(10)` are valid and represent `50%`, `200px`, and `10vh`.
557+
558+
505559
Note: It is valid to provide a length or percentage based offset such that it is
506560
outside the source's scroll range and thus not reachable e.g., '120%'.
507561

0 commit comments

Comments
 (0)