Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

perf(Popover): the touch growth plays on the surface (#10717)

Christopher Chedeau5a907b8babad
diff --git a/.changeset/popover-touch-growth-on-surface.md b/.changeset/popover-touch-growth-on-surface.md
new file mode 100644
index 0000000000..2f748026ae
--- /dev/null
+++ b/.changeset/popover-touch-growth-on-surface.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[perf] Under a finger, Popover plays its growth on the surface inside the layer, not on the anchored layer. Chromium restyles the growth's animations twice as they start and once as they end, and each restyle laid the anchored layer out twice, its backdrop included. Under load those layouts landed inside the growth. The popover looks the same.
+
+@vjeux
diff --git a/packages/core/src/Popover/Popover.test.tsx b/packages/core/src/Popover/Popover.test.tsx
index f34532f27a..8ec0376d29 100644
--- a/packages/core/src/Popover/Popover.test.tsx
+++ b/packages/core/src/Popover/Popover.test.tsx
@@ -1778,14 +1778,21 @@ describe('Popover touch presentation', () => {
it('under a finger, grows the popover out of its trigger in place of the layer\u2019s entrance', () => {
isCoarse = true;
const layer = open();
- expect(layer.style.getPropertyValue('animation-name')).toBe('none');
- expect(layer.style.getPropertyValue('transform-origin')).not.toBe('');
+ const surface = screen.getByRole('dialog', {name: 'Filters', hidden: true});
+ expect(surface.style.getPropertyValue('animation-name')).toBe('none');
+ expect(surface.style.getPropertyValue('transform-origin')).not.toBe('');
const scale = recorded.find(
animation =>
- animation.target === layer && 'scale' in animation.keyframes[0],
+ animation.target === surface && 'scale' in animation.keyframes[0],
);
expect(scale).toBeDefined();
expect(String(scale!.keyframes[0].scale).split(' ')[0]).toBe('0.01');
+ // The growth plays on the surface. Chromium lays the anchored layer out
+ // again each time it restyles it, so nothing animates the layer.
+ expect(recorded.filter(animation => animation.target === layer)).toEqual(
+ [],
+ );
+ expect(layer.style.getPropertyValue('transform-origin')).toBe('');
});
it('under a finger, shrinks back into its trigger on a copy that outlives the close', () => {
diff --git a/packages/core/src/Popover/__tests__/PopoverTouchMotion.interaction.browser.spec.ts b/packages/core/src/Popover/__tests__/PopoverTouchMotion.interaction.browser.spec.ts
index 9d38cebb9f..03b1e016a1 100644
--- a/packages/core/src/Popover/__tests__/PopoverTouchMotion.interaction.browser.spec.ts
+++ b/packages/core/src/Popover/__tests__/PopoverTouchMotion.interaction.browser.spec.ts
@@ -37,6 +37,7 @@ import {
freezeTimers,
installLongTaskLog,
installTimerGate,
+ layoutSources,
markElement,
openStory,
pinch,
@@ -110,6 +111,8 @@ interface OpenStory {
driver: TouchDriver;
trigger: Locator;
layer: Locator;
+ /** The box that paints the popover, where the touch growth plays. */
+ surface: Locator;
center: {x: number; y: number};
triggerBox: Box;
}
@@ -127,6 +130,7 @@ async function openDefault(page: Page): Promise<OpenStory> {
driver,
trigger,
layer: page.locator('[popover]:has(.astryx-popover)').first(),
+ surface: page.locator('[popover] .astryx-popover').first(),
center: {
x: triggerBox.x + triggerBox.width / 2,
y: triggerBox.y + triggerBox.height / 2,
@@ -442,8 +446,8 @@ test('the presentation grows out of the trigger on the theme\u2019s entrance tok
const story = await openDefault(page);
// The popover's layout box, from a first presentation run to rest.
await openWithTap(story);
- await waitForRest(story.layer);
- const final = (await story.layer.boundingBox())!;
+ await waitForRest(story.surface);
+ const final = (await story.surface.boundingBox())!;
const entrance = await themeTiming(story.layer, POPOVER_ENTRANCE_TOKENS);
await page.keyboard.press('Escape');
await expect(story.trigger).toHaveAttribute('aria-expanded', 'false');
@@ -451,7 +455,7 @@ test('the presentation grows out of the trigger on the theme\u2019s entrance tok
await expect(exitCopyOf(page)).toHaveCount(0);
const held = await openHeldWhileGrowing(page, story);
const samples = await sampleBoxes(
- story.layer,
+ story.surface,
final,
timeline(entrance.ms + 100),
);
@@ -500,8 +504,8 @@ test('the dismissal shrinks back into the trigger on the theme\u2019s exit token
}, testInfo) => {
const story = await openDefault(page);
await openWithTap(story);
- await waitForRest(story.layer);
- const open = (await story.layer.boundingBox())!;
+ await waitForRest(story.surface);
+ const open = (await story.surface.boundingBox())!;
const exit = await themeTiming(story.layer, POPOVER_EXIT_TOKENS);
// Tap outside: the browser's light dismiss closes the popover at once.
const viewport = page.viewportSize()!;
@@ -590,13 +594,13 @@ test('the presentation keeps advancing every frame while the main thread is bloc
// important declaration outranks the animation. The scale, composited the
// same way, carries the evidence.
await page.addStyleTag({
- content: '[popover]:has(.astryx-popover) { opacity: 1 !important; }',
+ content: '[popover] .astryx-popover { opacity: 1 !important; }',
});
// The growth runs 1.2 s at a constant speed here, so a 1 s block lands
// inside it on a loaded machine too. The marker is 240 px wide, and its
// width grows by about 3 px in every frame.
await page.addStyleTag({content: STEADY_GROWTH});
- await markElement(story.layer, {size: 240, inset: 0});
+ await markElement(story.surface, {size: 240, inset: 0});
const entrance = await themeTiming(story.layer, POPOVER_ENTRANCE_TOKENS);
expect(entrance.ms).toBe(1200);
const cast = await startScreencast(page);
@@ -604,7 +608,7 @@ test('the presentation keeps advancing every frame while the main thread is bloc
// here starts later on a busy machine.
const opened = await pageNow(page);
const blocking = blockMainThread(page, 1000, {
- during: story.layer,
+ during: story.surface,
minProgressMs: 16,
});
await openWithTap(story);
@@ -688,14 +692,18 @@ test('the presentation and the dismissal do no Layout or Paint per frame', async
let closed = 0;
let present = 0;
let dismiss = 0;
- const trace = await traceRendering(page, async () => {
- opened = await pageNow(page);
- await page.keyboard.press('Enter');
- present = await waitPastScaleStart(page, opened, entrance.ms + 150);
- closed = await pageNow(page);
- await page.keyboard.press('Escape');
- dismiss = await waitPastScaleStart(page, closed, exit.ms + 150);
- });
+ const trace = await traceRendering(
+ page,
+ async () => {
+ opened = await pageNow(page);
+ await page.keyboard.press('Enter');
+ present = await waitPastScaleStart(page, opened, entrance.ms + 150);
+ closed = await pageNow(page);
+ await page.keyboard.press('Escape');
+ dismiss = await waitPastScaleStart(page, closed, exit.ms + 150);
+ },
+ {keepEvents: true},
+ );
const insets = await page.evaluate(() => [
...(window as unknown as {__popoverHitAreaInsets: HitAreaInsets[]})
.__popoverHitAreaInsets,
@@ -741,8 +749,27 @@ test('the presentation and the dismissal do no Layout or Paint per frame', async
.filter(e => e.t >= from && e.t <= end)
.map(e => `${e.name}@${Math.round(e.t - base)}`),
})}`;
- expect(during.Layout ?? 0, detail).toBe(0);
- expect(during.Paint ?? 0, detail).toBe(0);
+ // A layout in the window fails with its source: the task that ran it,
+ // and each invalidation before it. A step list that is empty names no
+ // script: the browser scheduled that work itself.
+ const rel = (t: number) => `${(t - base).toFixed(1)} ms`;
+ const steps = (task: ReadonlyArray<string>) =>
+ task.join(' > ') || 'no script';
+ const explained = [
+ detail,
+ ...((during.Layout ?? 0) === 0
+ ? []
+ : layoutSources(trace, from, end)
+ ).flatMap(source => [
+ ` Layout at ${rel(source.t)}, in: ${steps(source.task)}`,
+ ...source.invalidations.map(
+ invalidation =>
+ ` after ${invalidation.what} at ${rel(invalidation.t)}, in: ${steps(invalidation.task)}`,
+ ),
+ ]),
+ ].join('\n');
+ expect(during.Layout ?? 0, explained).toBe(0);
+ expect(during.Paint ?? 0, explained).toBe(0);
}
// A loaded machine stretches a task's wall time, not its thread time. So a
// long task here is one that runs over 50 ms of thread time.
@@ -917,7 +944,7 @@ test('a style read after the opening tap finds no CSS entrance running on the la
);
expect(running).toEqual([]);
// The growth still plays.
- await waitForAnimation(story.layer);
+ await waitForAnimation(story.surface);
});
test('Reduce Motion: the popover cross-fades in at its size and out where it stands, in 200 ms or less with no spring', async ({
@@ -932,7 +959,7 @@ test('Reduce Motion: the popover cross-fades in at its size and out where it sta
const ease = cubicBezier(timing.curves.move);
const fade = (t: number) =>
ease(Math.min(1, Math.max(0, t / timing.reducedMotionCrossFadeMs)));
- const samples = await sampleBoxes(story.layer, unit, timeline(300));
+ const samples = await sampleBoxes(story.surface, unit, timeline(300));
// Resume rather than finish: the dismissal below starts from what the
// animator presents, which follows the clock.
await releaseAnimations(page);
@@ -1001,7 +1028,7 @@ test(
name: 'popover-touch-present',
clip,
held: presented,
- measure: measureOf(story.layer),
+ measure: measureOf(story.surface),
// The growth ends with the entrance duration; the animation holds its
// end state past that.
times: timeline(entrance.ms + 100),
@@ -1010,7 +1037,7 @@ test(
});
await releaseAnimations(page, {finish: true});
await thawTimers(page);
- await waitForRest(story.layer);
+ await waitForRest(story.surface);
const dismissal = await armExitCopyHold(story);
await story.driver.tap({x: 20, y: viewport.height - 20});
const dismissing = await dismissal.held();
@@ -1165,12 +1192,12 @@ async function openHeldWhileGrowing(
story: OpenStory,
property: 'scale' | 'opacity' = 'scale',
) {
- const armed = await armHold(page, story.layer, {
- judges: {target: story.layer, property},
+ const armed = await armHold(page, story.surface, {
+ judges: {target: story.surface, property},
});
await openWithTap(story);
const held = await armed.held();
- expect(await holdsOn(story.layer), 'held on the popover').toBe(true);
+ expect(await holdsOn(story.surface), 'held on the popover').toBe(true);
return held;
}
@@ -1189,7 +1216,7 @@ test('a tap outside while it grows is ignored: the popover stays open and keeps
expect(await shieldTakes(page, outside)).toBe(true);
await story.driver.tap(outside);
await releaseAnimations(page, {finish: true});
- await waitForRest(story.layer);
+ await waitForRest(story.surface);
await expect(story.trigger).toHaveAttribute('aria-expanded', 'true');
const log = await readPressLog(page);
expect(log.closed).toBe(0);
@@ -1212,7 +1239,7 @@ test('a tap on the trigger while it grows does not close the popover', async ({
expect(await shieldTakes(page, story.center)).toBe(true);
await story.driver.tap(story.center);
await releaseAnimations(page, {finish: true});
- await waitForRest(story.layer);
+ await waitForRest(story.surface);
await expect(story.trigger).toHaveAttribute('aria-expanded', 'true');
const log = await readPressLog(page);
expect(log.closed).toBe(0);
@@ -1238,7 +1265,7 @@ test('a pan outside while it grows starts nothing: no scroll, and the popover st
drag(from, {x: from.x, y: from.y - 100}, {durationMs: 150, hz: 60}),
);
await releaseAnimations(page, {finish: true});
- await waitForRest(story.layer);
+ await waitForRest(story.surface);
await expect(story.trigger).toHaveAttribute('aria-expanded', 'true');
const log = await readPressLog(page);
expect(log.closed).toBe(0);
@@ -1272,7 +1299,7 @@ test('a pinch while it grows zooms the page, and the popover grows and stays', a
}),
);
await releaseAnimations(page, {finish: true});
- await waitForRest(story.layer);
+ await waitForRest(story.surface);
// The fingers landed on the shield, which leaves a pinch to the browser.
await expect.poll(scale).toBeGreaterThan(1.1);
await expect(story.trigger).toHaveAttribute('aria-expanded', 'true');
@@ -1299,12 +1326,15 @@ test(
clip,
held,
measure: async () =>
- story.layer.evaluate(node => {
+ story.surface.evaluate(node => {
const el = node as HTMLElement;
const style = getComputedStyle(el);
const [sx] =
style.scale === 'none' ? [1] : style.scale.split(' ').map(Number);
- return {w: el.offsetWidth * sx, open: el.matches(':popover-open')};
+ return {
+ w: el.offsetWidth * sx,
+ open: el.closest('[popover]')?.matches(':popover-open') === true,
+ };
}),
times: timeline(entrance.ms + 100),
columns: 8,
diff --git a/packages/core/src/Popover/popoverSurfaceMotion.ts b/packages/core/src/Popover/popoverSurfaceMotion.ts
index fb3ff9f304..a274885fdf 100644
--- a/packages/core/src/Popover/popoverSurfaceMotion.ts
+++ b/packages/core/src/Popover/popoverSurfaceMotion.ts
@@ -4,8 +4,8 @@
/**
* @file popoverSurfaceMotion.ts
- * @input A Popover's layer element, the source it hangs off and the
- * compositor animator. Also the theme's duration and easing tokens, and
+ * @input A Popover's surface (the box that paints it, inside its layer),
+ * the source it hangs off and the compositor animator. Also the theme's duration and easing tokens, and
* the layer runtime's exit copy and input hold
* @output Exports `popoverCollapsedState`, `popoverFrame` and
* `createPopoverSurfaceMotion`, the touch presentation's present and
diff --git a/packages/core/src/Popover/usePopoverSurfaceMotion.ts b/packages/core/src/Popover/usePopoverSurfaceMotion.ts
index d3e5885a0d..cecc803e72 100644
--- a/packages/core/src/Popover/usePopoverSurfaceMotion.ts
+++ b/packages/core/src/Popover/usePopoverSurfaceMotion.ts
@@ -16,8 +16,11 @@
* coarse primary pointer, on any touch device) the popover grows out of its
* source. When the layer runtime reports that it starts to close, it shrinks
* back into it. A fine primary pointer keeps the layer's own entrance. The
- * motion is created for the popover element the first time it opens and
- * follows it if the element is replaced.
+ * growth plays on the surface, the box that paints the popover, and not on
+ * the layer. The layer is anchored, and Chromium lays an anchored element out
+ * again each time it restyles it, as when an animation on it starts. The
+ * motion is created for the surface the first time the popover opens and
+ * follows it if the surface is replaced.
*
* SYNC: When modified, update:
* - /packages/core/src/Popover/Popover.tsx
@@ -49,7 +52,8 @@ export interface PopoverSurfaceMotionCalls {
/**
* Call as the popover opens, right after the browser shows it and before
* it paints. `entrance` is the surface that carries the pointer
- * presentation's CSS entrance; the touch presentation turns it off there.
+ * presentation's CSS entrance; the touch presentation turns it off there
+ * and plays its growth on it.
*/
present: (
layer: HTMLElement | null,
@@ -113,8 +117,9 @@ export function usePopoverSurfaceMotion(): PopoverSurfaceMotionCalls {
release();
return;
}
- motionFor(layer, entrance).enter(() => ({
- surface: boxOf(layer),
+ const surface = entrance ?? layer;
+ motionFor(surface, entrance).enter(() => ({
+ surface: boxOf(surface),
source: boxOf(source),
}));
},