Skip to content
7 changes: 7 additions & 0 deletions .changeset/animated-layout-identity-transform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[fix] With an `animatedLayout` handle, content after a change now glides even when it keeps a transform that leaves it in place. A resting Button (`scale(1)`, computed as `matrix(1, 0, 0, 1, 0, 0)`), a pressed one (`scale(0.98)`) and a row with swipe actions at rest (`translate3d(0, 0, 0)`) used to jump to their new place as if the app were moving them, and a pressed button jumped onto the button gliding beside it. A row a swipe holds open, or anything the app translates or rotates, is still left to the app.

@vjeux
48 changes: 48 additions & 0 deletions packages/core/src/motion/animatedLayout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,54 @@ describe('elements the app moves itself (spec:AST-105 Coverage)', () => {
.map(p => (p.element as HTMLElement).id);
expect(moved).toEqual(['plain']);
});

it('glides a follower whose transform leaves it in place: a swipe row at rest, a resting or pressed button', async () => {
const {area, changed, phase} = setUp(
`<div id="area"><div id="changed"></div>
<div id="swipeRow" style="transform: translate3d(0px, 0px, 0px)"></div>
<div id="button" style="transform: matrix(1, 0, 0, 1, 0, 0)"></div>
<div id="pressed" style="transform: scale(0.98)"></div>
<div id="pressedMatrix" style="transform: matrix(0.98, 0, 0, 0.98, 0, 0)"></div>
<div id="scaled" style="scale: 0.98"></div>
<div id="zero" style="translate: 0px; rotate: 0deg"></div>
<div id="swiped" style="transform: translate3d(-120px, 0px, 0px)"></div>
<div id="pressedAndMoved" style="transform: matrix(0.98, 0, 0, 0.98, 0, 12)"></div></div>`,
);
const ids = [
'swipeRow',
'button',
'pressed',
'pressedMatrix',
'scaled',
'zero',
'swiped',
'pressedAndMoved',
];
for (const id of ids) {
placeAt(
document.getElementById(id) as HTMLElement,
{before: [0, 10, 400, 40], after: [0, 90, 400, 40]},
phase,
);
}
await runChange(
createAnimatedLayout(() => area),
changed,
phase,
);
const moved = played
.filter(p => p.keyframes[0].translate != null)
.map(p => (p.element as HTMLElement).id);
// A row a swipe holds open, or anything translated, is still the app's.
expect(moved.sort()).toEqual([
'button',
'pressed',
'pressedMatrix',
'scaled',
'swipeRow',
'zero',
]);
});
});

describe('siblings before the change (spec:AST-105 FR3, Coverage)', () => {
Expand Down
82 changes: 77 additions & 5 deletions packages/core/src/motion/animatedLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,84 @@ const ours = new WeakSet<Animation>();

const TRANSFORM_PROPERTIES = ['transform', 'translate', 'rotate', 'scale'];

/** The numbers in a CSS value, units dropped. */
function numbersIn(value: string): number[] {
return (value.match(/-?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?/gi) ?? []).map(
Number,
);
}

/**
* Whether a transform value leaves the element where it is: nothing at all
* (a swipe's `translate3d(0, 0, 0)` at rest, a button's `scale(1)`, computed
* as `matrix(1, 0, 0, 1, 0, 0)`), or a scale in place (a pressed button's
* `scale(0.98)`). A glide's `translate` composes with either; a translation,
* rotation or skew of its own is the app moving the element.
*/
function staysInPlace(property: string, value: string): boolean {
if (property === 'scale') {
return true;
}
Comment on lines +256 to +258

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Admitting every scale sends self-scaled followers to the glide, where the unchanged scaleOf (line 896) divides their travel by their own scale as if an ancestor applied it. In Chromium, on an 80 px downward shift, a scale(0.5) follower starts 80 px above its old place and a pressed Button (scale(0.98)) 1.63 px above it. Taking the correction from the follower's containing box, or dividing its own scale out, keeps the glide starting at its old place.

if (property === 'translate') {
return numbersIn(value).every(n => n === 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A Web Animation reports its keyframes as written, so an app's translate: var(--from) keyframe reaches this branch with no digits, [].every(...) is true, and the keyframe reads as zero. Line 300 does the same for translateY(var(--from)), rotate(var(--angle)) and translate3d(var(--x, 0px), 0, 0) (only the fallback's 0 is read). In Chromium such a follower now gets the glide, whose translate masks the app's own slide: at 150 ms it sits at -35px instead of the app's +105px. Counting a value that still holds var() as the app's, for example if (value.includes('var(')) return false; after the scale branch, fixes both.

}
if (property === 'rotate') {
// An angle, after an optional axis: only the angle matters.
const numbers = numbersIn(value);
return numbers.length > 0 && numbers[numbers.length - 1] === 0;
}
// A list of transform functions, as computed (`matrix()`, `matrix3d()`)
// or as written.
const functions = [...value.matchAll(/([a-z0-9]+)\(([^)]*)\)/gi)];
return (
functions.length > 0 &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

none reaches here from an app animation's keyframes (lines 313-316) and returns false, so a follower whose own transition runs from transform: none to a scale still counts as moved; rotate: none does the same at lines 240-241. Treating none as in place for every property would match the static check at line 294.

functions.every(([, name, args]) => {
const numbers = numbersIn(args);
const fn = name.toLowerCase();
if (fn === 'matrix') {
// a b c d e f: no skew, rotation or translation.
return (
numbers.length === 6 &&
[1, 2, 4, 5].every(index => numbers[index] === 0)
);
}
if (fn === 'matrix3d') {
// Only the diagonal: a scale, nothing else.
return (
numbers.length === 16 &&
numbers.every((n, index) => index % 5 === 0 || n === 0)
);
}
if (fn === 'rotate3d') {
return numbers[3] === 0;
}
if (fn.startsWith('scale')) {
return true;
}
if (
fn.startsWith('translate') ||
fn.startsWith('rotate') ||
fn.startsWith('skew')
) {
return numbers.every(n => n === 0);
}
return false;
})
);
}

/**
* Whether the app moves `element` itself: its own `transform`, `translate`,
* `rotate` or `scale`, or a running animation of one this module did not
* start.
* Whether the app moves `element` itself: its own `transform`, `translate`
* or `rotate`, or a running animation of one this module did not start. A
* value that leaves it in place (nothing, or a scale) is not a move.
*/
function movedByApp(element: Element): boolean {
const style = element.ownerDocument.defaultView?.getComputedStyle(element);
if (
style != null &&
TRANSFORM_PROPERTIES.some(property => {
const value = style.getPropertyValue(property);
return value !== '' && value !== 'none';
return value !== '' && value !== 'none' && !staysInPlace(property, value);
})
) {
return true;
Expand All @@ -243,7 +309,13 @@ function movedByApp(element: Element): boolean {
}
return effect
.getKeyframes()
.some(frame => TRANSFORM_PROPERTIES.some(property => property in frame));
.some(frame =>
TRANSFORM_PROPERTIES.some(
property =>
property in frame &&
!staysInPlace(property, String(frame[property])),
),
);
});
}

Expand Down
Loading