Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(website): address the review of the demo loops

Etienne Lescotb879369dc4ca
diff --git a/website/src/components/DemoLoop/index.tsx b/website/src/components/DemoLoop/index.tsx
index 324098b86..9258c7272 100644
--- a/website/src/components/DemoLoop/index.tsx
+++ b/website/src/components/DemoLoop/index.tsx
@@ -14,7 +14,9 @@
* - It plays only while at least half of it is on screen, and pauses when it
* leaves, so a page with ten loops decodes one or two at a time.
* - No autoplay for a reader who asked for reduced motion or for Save-Data:
- * they get the poster and a play button, and the file only on that click.
+ * they get the poster and a play button, and the sources are not even in
+ * the DOM before that click. Turning reduced motion on while the page is
+ * open stops a loop that was playing.
* - A visible pause button on every loop. A loop runs past five seconds and
* never stops, which WCAG 2.2.2 does not allow without one.
*
@@ -48,9 +50,6 @@ type Props = {
onReady?: () => void;
};
-/** Far enough ahead that the poster is there before the box is. */
-const NEAR = "100% 0px";
-
function motionAllowed(): boolean {
try {
const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
@@ -70,20 +69,37 @@ export default function DemoLoop({ name, loop = true, onEnded, onProgress, onRea
// null until the reader decides; then their choice beats visibility.
const [wanted, setWanted] = useState<boolean | null>(null);
const visible = useRef(false);
- const auto = useRef(true);
+ // State, not a ref: it decides whether the sources are in the DOM at all.
+ const [auto, setAuto] = useState(true);
+
+ // Follow the reader's motion setting, including a change made while the
+ // page is open: turning on reduced motion stops a loop that is playing.
+ useEffect(() => {
+ setAuto(motionAllowed());
+ let mq: MediaQueryList;
+ try {
+ mq = window.matchMedia("(prefers-reduced-motion: reduce)");
+ } catch {
+ return;
+ }
+ const onChange = () => setAuto(motionAllowed());
+ mq.addEventListener("change", onChange);
+ return () => mq.removeEventListener("change", onChange);
+ }, []);
- // Attach the sources once the box is near.
+ // Attach the poster (and pick the file) once the box is within one screen
+ // height. In pixels: a percentage rootMargin resolves against the root's
+ // width, which on a portrait phone is well under a screen of lead.
useEffect(() => {
const el = box.current;
if (!el) return;
- auto.current = motionAllowed();
const io = new IntersectionObserver(
(entries) => {
if (!entries.some((e) => e.isIntersecting)) return;
io.disconnect();
setHeight(pickHeight(el.clientWidth, window.devicePixelRatio));
},
- { rootMargin: NEAR },
+ { rootMargin: `${Math.round(window.innerHeight)}px 0px` },
);
io.observe(el);
return () => io.disconnect();
@@ -95,7 +111,7 @@ export default function DemoLoop({ name, loop = true, onEnded, onProgress, onRea
const v = video.current;
if (!el || !v || height === null) return;
const sync = () => {
- const go = wanted ?? (auto.current && visible.current);
+ const go = wanted ?? (auto && visible.current);
if (go && visible.current) {
v.play().catch(() => setPlaying(false));
} else {
@@ -119,7 +135,7 @@ export default function DemoLoop({ name, loop = true, onEnded, onProgress, onRea
io.disconnect();
v.removeEventListener("loadeddata", sync);
};
- }, [height, wanted]);
+ }, [height, wanted, auto]);
// Progress for a stage's tab bar, per frame rather than per `timeupdate`,
// which fires four times a second and would step the bar visibly.
@@ -151,7 +167,7 @@ export default function DemoLoop({ name, loop = true, onEnded, onProgress, onRea
muted
loop={loop}
playsInline
- preload={height && auto.current ? "auto" : "none"}
+ preload={height && auto ? "auto" : "none"}
disablePictureInPicture
disableRemotePlayback
onPlay={() => setPlaying(true)}
@@ -159,7 +175,11 @@ export default function DemoLoop({ name, loop = true, onEnded, onProgress, onRea
onEnded={onEnded}
onLoadedData={onReady}
>
+ {/* Without autoplay (reduced motion, Save-Data) the sources wait for
+ the play button: `preload="none"` is only a hint, and some engines
+ fetch anyway once a source is there. */}
{height &&
+ (auto || wanted === true) &&
loopSources(name, height).map((s) => <source key={s.src} src={s.src} type={s.type} />)}
</video>
{height && (
diff --git a/website/src/components/Films/index.tsx b/website/src/components/Films/index.tsx
index 3c3199d3c..d0d0cfe5f 100644
--- a/website/src/components/Films/index.tsx
+++ b/website/src/components/Films/index.tsx
@@ -11,7 +11,7 @@
import { translate } from "@docusaurus/Translate";
import Heading from "@theme/Heading";
-import { useCallback, useEffect, useRef, useState } from "react";
+import { type KeyboardEvent, useCallback, useEffect, useRef, useState } from "react";
import DemoLoop from "../DemoLoop";
import { type Block, getBlocks, getPair } from "./content";
@@ -23,6 +23,9 @@ function Stage({ block }: { block: Block }) {
const [prev, setPrev] = useState<number | null>(null);
const [fading, setFading] = useState(false);
const bars = useRef<(HTMLSpanElement | null)[]>([]);
+ const tabs = useRef<(HTMLButtonElement | null)[]>([]);
+ const panel = useRef<HTMLDivElement>(null);
+ const tablist = useRef<HTMLDivElement>(null);
const many = block.tabs.length > 1;
// Straight to the DOM: a state update per frame would re-render the block.
@@ -38,6 +41,40 @@ function Stage({ block }: { block: Block }) {
setIndex(i);
};
+ // A clip that ends hands over to the next. If focus was inside it (its pause
+ // button, which is about to fade out and unmount), it moves to the tab now
+ // selected rather than falling back to the page.
+ const advance = () => {
+ const next = (index + 1) % block.tabs.length;
+ // On a tab too: the selection moves, so the one Tab stop moves with it.
+ const hadFocus =
+ (panel.current?.contains(document.activeElement) ||
+ tablist.current?.contains(document.activeElement)) ??
+ false;
+ go(next);
+ if (hadFocus) requestAnimationFrame(() => tabs.current[next]?.focus());
+ };
+
+ // The WAI-ARIA tabs pattern: one Tab stop for the whole list (the selected
+ // tab), arrows and Home/End to move, selection following focus.
+ const onKeyDown = (e: KeyboardEvent) => {
+ const n = block.tabs.length;
+ const to =
+ e.key === "ArrowRight"
+ ? (index + 1) % n
+ : e.key === "ArrowLeft"
+ ? (index - 1 + n) % n
+ : e.key === "Home"
+ ? 0
+ : e.key === "End"
+ ? n - 1
+ : null;
+ if (to === null) return;
+ e.preventDefault();
+ go(to);
+ tabs.current[to]?.focus();
+ };
+
// Without a first frame (reduced motion fetches nothing until play), fade anyway.
useEffect(() => {
if (prev === null) return;
@@ -48,11 +85,17 @@ function Stage({ block }: { block: Block }) {
// Drop the outgoing layer once its fade (0.6 s in the CSS) is over. A timer,
// not transitionend: the layer's descendants transition too, and an event
// from any of them ended the fade early.
+ // Focus left inside it (a pause button) would fall back to the page with
+ // it, so it moves to the selected tab first.
useEffect(() => {
if (!fading || prev === null) return;
- const t = window.setTimeout(() => setPrev(null), 700);
+ const t = window.setTimeout(() => {
+ const out = panel.current?.querySelector("[data-outgoing]");
+ if (out?.contains(document.activeElement)) tabs.current[index]?.focus();
+ setPrev(null);
+ }, 700);
return () => window.clearTimeout(t);
- }, [fading, prev]);
+ }, [fading, prev, index]);
// Both layers are keyed by their loop, so the outgoing one keeps its last
// frame instead of remounting from the start.
@@ -65,7 +108,13 @@ function Stage({ block }: { block: Block }) {
<div className={styles.stage}>
<span className={styles.glow} />
{many && (
- <div className={styles.tabs} role="tablist" aria-label={block.title}>
+ <div
+ ref={tablist}
+ className={styles.tabs}
+ role="tablist"
+ aria-label={block.title}
+ onKeyDown={onKeyDown}
+ >
{block.tabs.map((tab, i) => (
<button
key={tab.loop}
@@ -74,6 +123,10 @@ function Stage({ block }: { block: Block }) {
id={`${block.id}-tab-${i}`}
aria-selected={i === index}
aria-controls={`${block.id}-panel`}
+ tabIndex={i === index ? 0 : -1}
+ ref={(el) => {
+ tabs.current[i] = el;
+ }}
className={styles.tab}
onClick={() => go(i)}
>
@@ -90,6 +143,7 @@ function Stage({ block }: { block: Block }) {
</div>
)}
<div
+ ref={panel}
className={`${styles.frame} ${styles.layers}`}
id={`${block.id}-panel`}
role={many ? "tabpanel" : undefined}
@@ -100,12 +154,13 @@ function Stage({ block }: { block: Block }) {
key={block.tabs[i].loop}
className={`${styles.layer} ${out ? styles.outgoing : ""} ${out && fading ? styles.gone : ""}`}
aria-hidden={out || undefined}
+ data-outgoing={out || undefined}
>
<DemoLoop
name={block.tabs[i].loop}
loop={!many}
onProgress={!out && many ? onProgress : undefined}
- onEnded={!out && many ? () => go((index + 1) % block.tabs.length) : undefined}
+ onEnded={!out && many ? advance : undefined}
onReady={!out ? () => setFading(true) : undefined}
/>
</div>