|
| 1 | +import { AnimatePresence, motion } from "framer-motion" |
| 2 | +import { useEffect, useRef, useState } from "react" |
| 3 | + |
| 4 | +/** |
| 5 | + * Reproduction for #3746. |
| 6 | + * |
| 7 | + * A bar chart with two datasets that share a key ("persist"). Switching |
| 8 | + * datasets should translate the persisting bar to its new position. It should |
| 9 | + * never replay its enter animation (width from 0) and never re-mount. |
| 10 | + * |
| 11 | + * The reported bug appears on the *second* switch, under React.StrictMode |
| 12 | + * (dev/react already renders every test inside StrictMode). |
| 13 | + */ |
| 14 | + |
| 15 | +interface Datum { |
| 16 | + id: string |
| 17 | + value: number |
| 18 | + color: string |
| 19 | +} |
| 20 | + |
| 21 | +const datasetA: Datum[] = [ |
| 22 | + { id: "a", value: 120, color: "blue" }, |
| 23 | + { id: "persist", value: 200, color: "red" }, |
| 24 | + { id: "b", value: 160, color: "green" }, |
| 25 | +] |
| 26 | + |
| 27 | +const datasetB: Datum[] = [ |
| 28 | + { id: "c", value: 140, color: "purple" }, |
| 29 | + { id: "d", value: 180, color: "orange" }, |
| 30 | + { id: "persist", value: 240, color: "red" }, |
| 31 | +] |
| 32 | + |
| 33 | +interface PresenceDatasetState { |
| 34 | + mounts: Record<string, number> |
| 35 | + resetWidths: () => void |
| 36 | + minWidth: () => number |
| 37 | +} |
| 38 | + |
| 39 | +declare global { |
| 40 | + interface Window { |
| 41 | + presenceDataset: PresenceDatasetState |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Sample the persisting bar every frame so the spec can assert it never |
| 47 | + * collapsed towards 0, rather than catching it at a single lucky moment. |
| 48 | + */ |
| 49 | +const widths: number[] = [] |
| 50 | + |
| 51 | +const state: PresenceDatasetState = { |
| 52 | + mounts: {}, |
| 53 | + resetWidths: () => { |
| 54 | + widths.length = 0 |
| 55 | + }, |
| 56 | + minWidth: () => (widths.length ? Math.min(...widths) : -1), |
| 57 | +} |
| 58 | + |
| 59 | +window.presenceDataset = state |
| 60 | + |
| 61 | +function Bar({ id, value, color }: Datum) { |
| 62 | + useEffect(() => { |
| 63 | + state.mounts[id] = (state.mounts[id] || 0) + 1 |
| 64 | + }, []) |
| 65 | + |
| 66 | + return ( |
| 67 | + <motion.div |
| 68 | + layout |
| 69 | + id={`bar-${id}`} |
| 70 | + initial={{ width: 0 }} |
| 71 | + animate={{ width: value }} |
| 72 | + exit={{ width: 0 }} |
| 73 | + transition={{ duration: 0.4, ease: "linear" }} |
| 74 | + style={{ |
| 75 | + height: 40, |
| 76 | + marginBottom: 8, |
| 77 | + background: color, |
| 78 | + }} |
| 79 | + /> |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +export function App() { |
| 84 | + const [data, setData] = useState(datasetA) |
| 85 | + const raf = useRef(0) |
| 86 | + |
| 87 | + useEffect(() => { |
| 88 | + const sample = () => { |
| 89 | + const el = document.getElementById("bar-persist") |
| 90 | + if (el) widths.push(el.getBoundingClientRect().width) |
| 91 | + raf.current = requestAnimationFrame(sample) |
| 92 | + } |
| 93 | + raf.current = requestAnimationFrame(sample) |
| 94 | + return () => cancelAnimationFrame(raf.current) |
| 95 | + }, []) |
| 96 | + |
| 97 | + return ( |
| 98 | + <div> |
| 99 | + <button |
| 100 | + id="switch" |
| 101 | + onClick={() => |
| 102 | + setData((d) => (d === datasetA ? datasetB : datasetA)) |
| 103 | + } |
| 104 | + > |
| 105 | + Switch dataset |
| 106 | + </button> |
| 107 | + <div style={{ position: "relative" }}> |
| 108 | + <AnimatePresence> |
| 109 | + {data.map((datum) => ( |
| 110 | + <Bar key={datum.id} {...datum} /> |
| 111 | + ))} |
| 112 | + </AnimatePresence> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + ) |
| 116 | +} |
0 commit comments