Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions packages/starpod/src/components/explorer/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
onFlip,
registerFlipTargets,
reducedMotion,
slomo,
type FlipRect
} from '../../motion/propflip';
import { readPosition, resumePoint } from '../media/position';
Expand Down Expand Up @@ -134,14 +135,6 @@ const ANCHORED =
const GLIDE_CLIP =
typeof CSS !== 'undefined' && CSS.supports('overflow-clip-margin', '1px');

function slomo() {
return (
Number(
getComputedStyle(document.documentElement).getPropertyValue('--slomo')
) || 1
);
}

/** The archive's sizes when it was last live, and what they depend on. */
let lastSize: { key: string; width: string; bar: string } | undefined;
const sizeKey = () =>
Expand Down
8 changes: 4 additions & 4 deletions packages/starpod/src/components/media/Island.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { computed } from '@preact/signals';
import { createPortal } from 'preact/compat';
import { useLayoutEffect, useRef } from 'preact/hooks';

import { flip } from '../../motion/propflip';
import { flip, slomo } from '../../motion/propflip';
import {
ArrowOutIcon,
BackIcon,
Expand Down Expand Up @@ -302,9 +302,9 @@ export function go(home: IslandHome) {
// Reads happen where flip() has just measured (style and layout clean) and
// writes inside its update, so go() forces no layout of its own (bar
// WebKit's one read of where the island is going).
let slomo = 0;
let s = 0;
const moves = flip([...island.children] as Array<HTMLElement>, () => {
slomo = parseFloat(getComputedStyle(island).getPropertyValue('--slomo')) || 0;
s = slomo(island);
// The cascade runs from the far end on the way back to a smaller home.
island.dataset.going = ISLAND_HOMES.indexOf(to) === 1 ? 'out' : 'in';
// Its own tween runs only for the journey (and the rail slot's growth);
Expand All @@ -325,7 +325,7 @@ export function go(home: IslandHome) {
island.style.removeProperty('width');
island.style.removeProperty('height');
syncPage();
}, 900 * slomo + 50);
}, 900 * s + 50);
// Parts at rest take their delay from CSS (sibling-index()); parts already
// in flight keep the zero flip() gave them, so an interrupt never freezes.
for (const move of moves) {
Expand Down
10 changes: 4 additions & 6 deletions packages/starpod/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ const publicationLinkTag =
// the page re-lays once and glides, and other areas' registered boxes
// flip. The toggle, "[", double-click and the arrow keys take the same
// glide. Also runs the collapsed rail's travelling tooltip.
import { flipLayout, nudge, reducedMotion } from '../motion/propflip';
import { flipLayout, nudge, reducedMotion, slomo } from '../motion/propflip';

const root = document.documentElement;
const wide = window.matchMedia('(min-width: 1024px)');
Expand Down Expand Up @@ -668,15 +668,14 @@ const publicationLinkTag =
void getComputedStyle(rail).transitionDuration;
delete rail.dataset.anchoring;
clearTimeout(reforming);
const slomo =
parseFloat(getComputedStyle(root).getPropertyValue('--slomo')) || 1;
const s = slomo();
const at = ++reformed;
requestAnimationFrame(() =>
requestAnimationFrame(() => {
if (at === reformed) {
reforming = window.setTimeout(
() => delete rail.dataset.reforming,
760 * slomo
760 * s
);
}
})
Expand Down Expand Up @@ -1179,8 +1178,7 @@ const publicationLinkTag =
apply();
return;
}
const slomo = parseFloat(root.style.getPropertyValue('--slomo')) || 1;
hoverRelease.set(el, window.setTimeout(apply, 700 * slomo));
hoverRelease.set(el, window.setTimeout(apply, 700 * slomo()));
}
for (const [type, on] of [
['pointerenter', true],
Expand Down
9 changes: 9 additions & 0 deletions packages/starpod/src/motion/propflip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ export function reducedMotion(): boolean {
);
}

/**
* The page's `--slomo`, the factor every duration is scaled by: 0 under
* reduced motion (propflip.css), so a wait timed to a transition ends at once.
*/
export function slomo(el: Element = document.documentElement): number {
const s = parseFloat(getComputedStyle(el).getPropertyValue('--slomo'));
return Number.isNaN(s) ? 1 : s;
}

/** The current (possibly mid-transition) values of the four props. */
export function props(el: Element): FlipProps {
const cs = getComputedStyle(el);
Expand Down
50 changes: 50 additions & 0 deletions tests/e2e/shell.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,56 @@ test.describe('desktop rail', () => {
await expect(page.locator(fly)).toBeHidden();
}
});

// Dragging the divider past the middle re-forms the rail: its pieces leave
// their slots to travel, then settle back. How long they stay out, in ms.
const reformHold = async (page: Page) => {
await page.goto('/');
await hydrated(page);
await page.evaluate(() => {
const rail = document.querySelector<HTMLElement>('.rail')!;
const w = window as unknown as { reform: number[] };
w.reform = [];
new MutationObserver(() => w.reform.push(performance.now())).observe(
rail,
{ attributeFilter: ['data-reforming'] }
);
});
const y = 450;
await page.mouse.move(await edgeX(page), y);
await page.mouse.down();
await page.mouse.move(120, y, { steps: 4 });
await page.waitForFunction(
() => (window as unknown as { reform: number[] }).reform.length >= 2,
undefined,
{ timeout: 3000 }
);
await page.mouse.up();
const [out, back] = await page.evaluate(
() => (window as unknown as { reform: number[] }).reform
);
return back! - out!;
};

// Wall-clock holds: headless WebKit and Firefox frame too unevenly (two
// idle frames can take over 500ms) for the reduced-motion bound to mean
// anything there. slomo() itself is unit tested.
test('a re-form holds its pieces out while they travel', async ({
page,
browserName
}) => {
test.skip(browserName !== 'chromium', 'frame timing is only steady in Chromium');
expect(await reformHold(page)).toBeGreaterThan(600);
});

test.describe('with reduced motion', () => {
test.use({ contextOptions: { reducedMotion: 'reduce' } });

test('a re-form settles at once', async ({ page, browserName }) => {
test.skip(browserName !== 'chromium', 'frame timing is only steady in Chromium');
expect(await reformHold(page)).toBeLessThan(200);
});
});
});

test.describe('rail popovers', () => {
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Island.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ describe('Island', () => {
currentEpisode.value = withVideo;
const { rerender } = render(<Island />);
const island = () => document.getElementById('player')!;
// Under reduced motion (propflip.css), so the island lands at once.
island().style.setProperty('--slomo', '0');
const landed = () => new Promise((done) => setTimeout(done, 80));
expect(island()).not.toHaveAttribute('role');

Expand Down
21 changes: 20 additions & 1 deletion tests/unit/propflip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,28 @@ import { afterEach, describe, expect, it } from 'vitest';
import {
flip,
flipLayout,
registerFlipTargets
registerFlipTargets,
slomo
} from 'starpod/src/motion/propflip';

describe('slomo', () => {
afterEach(() => document.documentElement.style.removeProperty('--slomo'));

it('keeps 0 under reduced motion, so waits timed to transitions end at once', () => {
document.documentElement.style.setProperty('--slomo', '0');
expect(slomo()).toBe(0);
});

it('scales by the page value', () => {
document.documentElement.style.setProperty('--slomo', '2');
expect(slomo()).toBe(2);
});

it('runs at normal speed when nothing sets it', () => {
expect(slomo()).toBe(1);
});
});

// A box whose rect is whatever the test says it is right now.
function box(rect: { left: number; top: number }, log?: string[], name = '') {
const el = document.createElement('div');
Expand Down
Loading