Flashcard flip: paint one face explicitly, never rely on backface-visibility

On a GPU-composited desktop the flipped card showed the question mirrored
over the answer. Both faces are glass layers with backdrop-filter, and on
some compositing paths the front's backface keeps rendering, so the
pre-rotated back face and the mirrored front face both painted.

- The faces are now painted by an explicit visibility swap driven by the
  animation's own rotation (onUpdate): the swap lands at the 90° edge-on
  moment, where it cannot be seen, so exactly one face is ever in the
  render tree — independent of backface-visibility and of the browser's
  3D flattening rules. onAnimationComplete lands the swap for instant
  flips (reduced motion now skips the spring, matching the app's motion
  rules).
- PromptCard takes an optional style so the face can carry the visibility.
- phase-2 suite: the painted-face invariant at rest, flipped and flipped
  back, plus the answer reading inside the card (56 checks).
- README: decision 53. SW -> freezmo-v39.
This commit is contained in:
Martin Dvořák 2026-10-12 07:34:38 +00:00
commit 396da6528a
5 changed files with 82 additions and 6 deletions

View file

@ -1,6 +1,6 @@
"use client";
import { motion } from "framer-motion";
import { motion, useReducedMotion } from "framer-motion";
import { useState } from "react";
import { useI18n } from "@/lib/i18n";
@ -20,8 +20,21 @@ type FlashcardActivity = Extract<Activity, { type: "flashcard" }>;
export function Flashcard({ activity, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<FlashcardActivity>) {
const { t } = useI18n();
const reduceMotion = useReducedMotion();
const [flipped, setFlipped] = useState(false);
const [result, setResult] = useState<boolean | null>(null);
/**
* Which face is painted. The swap happens at the 90° edge-on moment instead
* of relying on backface-visibility: a backdrop-filter layer (the card
* glass) keeps rendering its backface on some GPU/compositing paths, which
* showed the question mirrored over the answer.
*/
const [face, setFace] = useState<"front" | "back">("front");
const showFaceFor = (angle: number) => {
const next = angle > 90 && angle < 270 ? "back" : "front";
setFace((current) => (current === next ? current : next));
};
const settle = (correct: boolean) => {
if (result !== null) return;
@ -42,10 +55,15 @@ export function Flashcard({ activity, onAnswered, onNext, isLast, nextLabel }: A
>
<motion.div
animate={{ rotateX: flipped ? 180 : 0 }}
transition={{ type: "spring", stiffness: 220, damping: 24 }}
transition={reduceMotion ? { duration: 0 } : { type: "spring", stiffness: 220, damping: 24 }}
onUpdate={(latest) => showFaceFor(Number(latest.rotateX ?? 0))}
onAnimationComplete={() => showFaceFor(flipped ? 180 : 0)}
className="relative min-h-56 w-full [transform-style:preserve-3d]"
>
<PromptCard className="absolute inset-0 flex flex-col justify-center gap-3 group-hover:border-frost-300 [backface-visibility:hidden]">
<PromptCard
className="absolute inset-0 flex flex-col justify-center gap-3 group-hover:border-frost-300"
style={{ visibility: face === "front" ? "visible" : "hidden" }}
>
<span className="text-xs font-bold uppercase tracking-widest text-frost-600 dark:text-frost-200">
{t("flashcard.question")}
</span>
@ -55,7 +73,7 @@ export function Flashcard({ activity, onAnswered, onNext, isLast, nextLabel }: A
</span>
</PromptCard>
<div className="[transform:rotateX(180deg)] [backface-visibility:hidden]">
<div className="[transform:rotateX(180deg)]" style={{ visibility: face === "back" ? "visible" : "hidden" }}>
<div className="card flex min-h-56 flex-col justify-center gap-3 px-5 py-6 sm:px-6">
<span className="text-xs font-bold uppercase tracking-widest text-frost-600 dark:text-frost-200">
{t("flashcard.answer")}

View file

@ -70,12 +70,19 @@ export function ActivityHeader({
export function PromptCard({
children,
className,
style,
}: {
children: React.ReactNode;
className?: string;
style?: React.CSSProperties;
}) {
return (
<div className={cn("glass-panel glass-solid rounded-[1.5rem] px-5 py-6 text-balance sm:px-6", className)}>{children}</div>
<div
className={cn("glass-panel glass-solid rounded-[1.5rem] px-5 py-6 text-balance sm:px-6", className)}
style={style}
>
{children}
</div>
);
}