Skip to content

Commit 54203ae

Browse files
committed
Add comments
1 parent 3f554ed commit 54203ae

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

packages/sdk-components-animation/src/scroll.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ const createSelector = (id: string) => `[${componentAttributeId}="${id}"] > *`;
2525
const cartesian = <A, B>(a: A[], b: B[]) =>
2626
a.flatMap((x) => b.map((y) => [x, y] as const));
2727

28+
/**
29+
* Scroll does not support https://drafts.csswg.org/scroll-animations/#named-ranges
30+
* However, for simplicity and type unification with the view, we will use the names "start" and "end,"
31+
* which will be transformed as follows:
32+
* - "start" → `calc(0% + range)`
33+
* - "end" → `calc(100% - range)`
34+
*/
2835
const getScrollRangeValue = ([name, value]: ScrollRangeValue) => {
2936
// See also WAAPI has bug https://issues.chromium.org/issues/40929569#comment9
3037
// so we are using calc even it it's not needed in some cases
@@ -79,6 +86,7 @@ const Animate = ({ id, action }: { id: string; action: AnimationAction }) => {
7986
const keyframe: Keyframe = {
8087
offset: animationKeyframe.offset,
8188
composite: "auto",
89+
// Probably having linear() easing we don't need easing per keyframe
8290
// easing: animationKeyframe.easing,
8391
};
8492

@@ -114,6 +122,16 @@ const Animate = ({ id, action }: { id: string; action: AnimationAction }) => {
114122
return undefined;
115123
};
116124

125+
/**
126+
* We use JavaScript to execute animations.
127+
* For animations that run initially, such as scroll or view-based animations,
128+
* we need to set the initial position during server rendering.
129+
*
130+
* On the client side, there's another issue: we use Scroll and View Timeline polyfills.
131+
* These polyfills must be loaded dynamically because they rely on the `window` object globally,
132+
* making them incompatible with standard imports.
133+
* Similarly, for SSR, we need to set styles if the polyfill hasn't loaded yet.
134+
*/
117135
const StylesBeforeAnimate = ({
118136
id,
119137
action,
@@ -141,6 +159,8 @@ const StylesBeforeAnimate = ({
141159
([property, value]) => `${hyphenateProperty(property)}:${toValue(value)};`
142160
);
143161

162+
// We use animation to set initial styles to ensure the exact same behavior
163+
// (including specificity and keyframe composition) as the animation executed afterward.
144164
styleContent = `
145165
${styleContent}
146166
@@ -171,6 +191,7 @@ type ScrollProps = {
171191
action: AnimationAction;
172192
};
173193

194+
// @todo: With high probablity it will be unified animation object
174195
export const Scroll = forwardRef<ElementRef<"div">, ScrollProps>(
175196
({ debug = false, children, action }, ref) => {
176197
const nakedId = useId();

0 commit comments

Comments
 (0)