{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tear-off-desk-calendar-with-pages-that-peel-curl-and-flutter-away","type":"registry:block","title":"Tear-Off Desk Calendar with Pages That Peel, Curl and Flutter Away","description":"Tear-off day calendar: drag a page up and it curls off the pad in 3D, rips at the binding and flutters away to show tomorrow. Free React and HTML to copy.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/tear-off-desk-calendar-with-pages-that-peel-curl-and-flutter-away.tsx","type":"registry:component","content":"import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n/** First page on the pad (year, month 0-11, day). Every tear moves one day on. */\nconst START = { year: 2026, month: 9, day: 12 };\n/** One short line printed at the foot of each page, used in order and repeated. */\nconst notes = [\n  \"Water the fig before nine.\",\n  \"Post the parcel to Ines.\",\n  \"Bread is ready at four.\",\n  \"Call the framer about the print.\",\n  \"Market closes early today.\",\n  \"Leave the window open tonight.\",\n  \"Return the library books.\",\n];\n\nconst WEEKDAYS = [\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"];\nconst MONTHS = [\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"];\n\nconst PAGE_W = 248;\nconst PAGE_H = 296;\nconst STRIPS = 18;\nconst STRIP_H = PAGE_H / STRIPS;\nconst BAND = 10;\nconst PEEL_DEG = 168;\nconst DAY_MS = 86400000;\n\nfunction pageFor(index: number) {\n  const first = Date.UTC(START.year, START.month, START.day);\n  const date = new Date(first + index * DAY_MS);\n  const yearStart = Date.UTC(date.getUTCFullYear(), 0, 1);\n  const dayOfYear = Math.round((date.getTime() - yearStart) / DAY_MS) + 1;\n  const yearEnd = Date.UTC(date.getUTCFullYear() + 1, 0, 1);\n  const total = Math.round((yearEnd - yearStart) / DAY_MS);\n  return {\n    weekday: WEEKDAYS[date.getUTCDay()],\n    month: MONTHS[date.getUTCMonth()],\n    day: date.getUTCDate(),\n    year: date.getUTCFullYear(),\n    dayOfYear,\n    left: total - dayOfYear,\n    weekend: date.getUTCDay() === 0 || date.getUTCDay() === 6,\n    note: notes[((index % notes.length) + notes.length) % notes.length],\n  };\n}\n\nfunction PageFace({ index }: { index: number }) {\n  const page = pageFor(index);\n  return (\n    <div\n      className=\"flex flex-col items-center px-5 pb-4 pt-5 text-stone-900\"\n      style={{ width: PAGE_W, height: PAGE_H, background: \"linear-gradient(180deg,#fbf9f4 0%,#f6f2ea 100%)\" }}\n    >\n      <div className=\"flex w-full items-baseline justify-between text-[10px] font-semibold uppercase tracking-[0.22em] text-stone-500\">\n        <span>{page.month}</span>\n        <span>{page.year}</span>\n      </div>\n      <div\n        className={`mt-1 font-black leading-none tracking-[-0.06em] ${page.weekend ? \"text-[#c8371f]\" : \"text-stone-900\"}`}\n        style={{ fontSize: 168, fontVariantNumeric: \"tabular-nums\" }}\n      >\n        {page.day}\n      </div>\n      <div className=\"mt-1 text-[13px] font-bold uppercase tracking-[0.34em] text-[#c8371f]\">{page.weekday}</div>\n      <div className=\"mt-auto w-full border-t border-stone-300 pt-2\">\n        <div className=\"flex items-center justify-between text-[10px] font-medium tracking-wide text-stone-500\">\n          <span>Day {page.dayOfYear}</span>\n          <span>{page.left} left</span>\n        </div>\n        <p className=\"mt-1 text-center font-serif text-[12px] italic text-stone-600\">{page.note}</p>\n      </div>\n    </div>\n  );\n}\n\nconst ease = (t: number) => 1 - Math.pow(1 - t, 3);\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\n\nexport default function TearOffDeskCalendar() {\n  const reduce = useReducedMotion();\n  const [index, setIndex] = useState(0);\n  const [touched, setTouched] = useState(false);\n\n  const rootRef = useRef<HTMLDivElement>(null);\n  const sheetRef = useRef<HTMLDivElement>(null);\n  const stripRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const frontShade = useRef<(HTMLDivElement | null)[]>([]);\n  const backShade = useRef<(HTMLDivElement | null)[]>([]);\n  const castRef = useRef<HTMLDivElement>(null);\n  const dropRef = useRef<HTMLDivElement>(null);\n\n  const sim = useRef({\n    mode: \"rest\" as \"rest\" | \"drag\" | \"peel\" | \"back\" | \"fly\",\n    p: 0,\n    from: 0,\n    t0: 0,\n    raf: 0,\n    side: 1,\n    angles: new Array(STRIPS).fill(0) as number[],\n    held: new Array(STRIPS).fill(0) as number[],\n    dragY: 0,\n    moved: false,\n    visible: true,\n  });\n\n  /** Bend the sheet: `angles[i]` is how far strip i folds relative to the strip above it. */\n  const paint = useCallback((angles: number[], cast: number, castAt: number) => {\n    let sum = 0;\n    let frontWas = 0;\n    let backWas = 0.54;\n    for (let i = 0; i < STRIPS; i++) {\n      sum += angles[i];\n      const strip = stripRefs.current[i];\n      if (strip) strip.style.transform = `rotateX(${angles[i].toFixed(2)}deg)`;\n      const rad = (sum * Math.PI) / 180;\n      // Each strip fades from the shade of the strip above to its own, so the bend reads as one curve.\n      const frontNow = clamp(0.5 * (1 - Math.cos(rad)), 0, 0.62);\n      const backNow = clamp(0.04 + 0.5 * Math.pow((1 + Math.cos(rad)) / 2, 0.8) + 0.07 * Math.sin(rad * 2), 0, 0.6);\n      const front = frontShade.current[i];\n      if (front) front.style.background = `linear-gradient(180deg,rgba(42,28,16,${frontWas.toFixed(3)}),rgba(42,28,16,${frontNow.toFixed(3)}))`;\n      const back = backShade.current[i];\n      if (back) back.style.background = `linear-gradient(0deg,rgba(42,28,16,${backWas.toFixed(3)}),rgba(42,28,16,${backNow.toFixed(3)}))`;\n      frontWas = frontNow;\n      backWas = backNow;\n    }\n    const shadow = castRef.current;\n    if (shadow) {\n      shadow.style.opacity = cast.toFixed(3);\n      shadow.style.transform = `translateY(${(castAt * STRIP_H).toFixed(1)}px)`;\n    }\n  }, []);\n\n  /** Angles for a sheet lifted from the bottom edge, p = 0 flat to 1 held up by its top edge. */\n  const peelPose = useCallback((p: number, out: number[]) => {\n    const front = STRIPS - p * (STRIPS - 0.4);\n    const norm = (BAND * 2) / Math.PI;\n    for (let i = 0; i < STRIPS; i++) {\n      const x = (i + 0.5 - front) / BAND;\n      out[i] = x > 0 && x < 1 ? (PEEL_DEG * Math.sin(Math.PI * x)) / norm : 0;\n    }\n    return front;\n  }, []);\n\n  const rest = useCallback(() => {\n    const s = sim.current;\n    s.mode = \"rest\";\n    s.p = 0;\n    s.angles.fill(0);\n    paint(s.angles, 0, STRIPS);\n    const sheet = sheetRef.current;\n    if (sheet) {\n      sheet.style.transform = \"translate3d(0,0,0)\";\n      sheet.style.opacity = \"1\";\n    }\n    if (dropRef.current) dropRef.current.style.opacity = \"0\";\n  }, [paint]);\n\n  const frame = useCallback(\n    (now: number) => {\n      const s = sim.current;\n      s.raf = 0;\n      if (s.mode === \"peel\" || s.mode === \"back\") {\n        const length = s.mode === \"peel\" ? 520 * (1 - s.from) + 120 : 380;\n        const k = clamp((now - s.t0) / length, 0, 1);\n        const target = s.mode === \"peel\" ? 1 : 0;\n        s.p = s.from + (target - s.from) * (s.mode === \"peel\" ? k * k * (3 - 2 * k) : ease(k));\n        const front = peelPose(s.p, s.angles);\n        paint(s.angles, Math.min(1, s.p * 3) * 0.8, front);\n        if (k >= 1) {\n          if (s.mode === \"peel\") {\n            s.mode = \"fly\";\n            s.t0 = now;\n            for (let i = 0; i < STRIPS; i++) s.held[i] = s.angles[i];\n          } else {\n            rest();\n            return;\n          }\n        }\n      } else if (s.mode === \"fly\") {\n        const t = (now - s.t0) / 1000;\n        const blend = ease(clamp(t / 0.32, 0, 1));\n        const flutter = 46 + 30 * Math.sin(t * 9.5);\n        for (let i = 0; i < STRIPS; i++) {\n          const loose = (flutter / STRIPS) * (0.6 + 0.8 * Math.sin((i / STRIPS) * Math.PI));\n          s.angles[i] = s.held[i] + (loose - s.held[i]) * blend;\n        }\n        paint(s.angles, 0.55 * (1 - clamp(t / 0.18, 0, 1)), 0.4);\n        const sheet = sheetRef.current;\n        if (sheet) {\n          const x = s.side * (150 * t + 26 * Math.sin(t * 6));\n          const y = -210 * t + 1050 * t * t;\n          const spin = s.side * (10 + 62 * t);\n          const tip = -34 * Math.sin(t * 5.2);\n          sheet.style.transform = `translate3d(${x.toFixed(1)}px,${y.toFixed(1)}px,${(70 * Math.min(1, t * 3)).toFixed(1)}px) rotateZ(${spin.toFixed(1)}deg) rotateX(${tip.toFixed(1)}deg)`;\n          sheet.style.opacity = clamp((1.02 - t) / 0.16, 0, 1).toFixed(3);\n          const drop = dropRef.current;\n          if (drop) {\n            drop.style.transform = `translate(${(x * 0.8).toFixed(1)}px,${(y * 0.8 + 34).toFixed(1)}px) rotate(${spin.toFixed(1)}deg)`;\n            drop.style.opacity = (0.5 * clamp(t / 0.15, 0, 1) * clamp(1 - t / 0.75, 0, 1)).toFixed(3);\n          }\n        }\n        if (t >= 1.02) {\n          s.side = -s.side;\n          s.mode = \"rest\";\n          if (sheet) sheet.style.opacity = \"0\";\n          setIndex((v) => v + 1);\n          return;\n        }\n      }\n      if (s.mode !== \"rest\" && s.mode !== \"drag\") s.raf = requestAnimationFrame(frame);\n    },\n    [paint, peelPose, rest],\n  );\n\n  const run = useCallback(\n    (mode: \"peel\" | \"back\") => {\n      const s = sim.current;\n      s.mode = mode;\n      s.from = s.p;\n      s.t0 = performance.now();\n      if (!s.raf) s.raf = requestAnimationFrame(frame);\n    },\n    [frame],\n  );\n\n  const tear = useCallback(() => {\n    const s = sim.current;\n    if (s.mode === \"fly\" || s.mode === \"peel\") return;\n    if (reduce) {\n      setIndex((v) => v + 1);\n      return;\n    }\n    run(\"peel\");\n  }, [reduce, run]);\n\n  // Tears a page by itself until someone takes over, and only while it is on screen.\n  useEffect(() => {\n    if (reduce || touched) return;\n    const s = sim.current;\n    const node = rootRef.current;\n    const watcher =\n      node && typeof IntersectionObserver !== \"undefined\"\n        ? new IntersectionObserver(([entry]) => {\n            s.visible = entry.isIntersecting;\n          })\n        : null;\n    if (node && watcher) watcher.observe(node);\n    const first = window.setTimeout(() => s.visible && !document.hidden && tear(), 800);\n    const loop = window.setInterval(() => s.visible && !document.hidden && tear(), 2500);\n    return () => {\n      window.clearTimeout(first);\n      window.clearInterval(loop);\n      watcher?.disconnect();\n    };\n  }, [reduce, touched, tear]);\n\n  // The new top page is laid flat only once React has printed the next date on it.\n  useEffect(() => {\n    rest();\n  }, [index, rest]);\n\n  useEffect(() => {\n    const s = sim.current;\n    return () => {\n      if (s.raf) cancelAnimationFrame(s.raf);\n      s.raf = 0;\n    };\n  }, []);\n\n  const onDown = (event: React.PointerEvent<HTMLDivElement>) => {\n    const s = sim.current;\n    setTouched(true);\n    if (reduce || s.mode === \"fly\" || s.mode === \"peel\") return;\n    event.currentTarget.setPointerCapture(event.pointerId);\n    s.mode = \"drag\";\n    s.dragY = event.clientY + s.p * 230;\n    s.moved = false;\n  };\n  const onMove = (event: React.PointerEvent<HTMLDivElement>) => {\n    const s = sim.current;\n    if (s.mode !== \"drag\") return;\n    const p = clamp((s.dragY - event.clientY) / 230, 0, 0.94);\n    if (Math.abs(p - s.p) > 0.01) s.moved = true;\n    s.p = p;\n    const front = peelPose(p, s.angles);\n    paint(s.angles, Math.min(1, p * 3) * 0.8, front);\n  };\n  const onUp = () => {\n    const s = sim.current;\n    if (s.mode !== \"drag\") return;\n    if (!s.moved) {\n      s.mode = \"rest\";\n      return;\n    }\n    run(s.p > 0.42 ? \"peel\" : \"back\");\n  };\n  const onClick = () => {\n    const s = sim.current;\n    setTouched(true);\n    if (s.moved) {\n      s.moved = false;\n      return;\n    }\n    tear();\n  };\n\n  const today = pageFor(index);\n\n  // Nested strips: each one hangs from the strip above, so small folds add up to a curl.\n  const buildStrip = (i: number): React.ReactNode => (\n    <div\n      ref={(node) => {\n        stripRefs.current[i] = node;\n      }}\n      className=\"absolute left-0\"\n      style={{ top: i === 0 ? 0 : STRIP_H, width: PAGE_W, height: STRIP_H, transformOrigin: \"50% 0\", transformStyle: \"preserve-3d\" }}\n    >\n      <div className=\"absolute left-0 top-0 overflow-hidden\" style={{ width: PAGE_W, height: STRIP_H + 1.2, backfaceVisibility: \"hidden\" }}>\n        <div style={{ transform: `translateY(${-i * STRIP_H}px)` }}>\n          <PageFace index={index} />\n        </div>\n        <div\n          ref={(node) => {\n            frontShade.current[i] = node;\n          }}\n          className=\"absolute inset-0\"\n        />\n      </div>\n      <div\n        className=\"absolute left-0 overflow-hidden\"\n        style={{ top: -0.6, width: PAGE_W, height: STRIP_H + 1.8, backfaceVisibility: \"hidden\", transform: \"rotateX(180deg)\", transformOrigin: `50% ${STRIP_H / 2 + 0.6}px`, background: \"#f3eee5\" }}\n      >\n        <div\n          ref={(node) => {\n            backShade.current[i] = node;\n          }}\n          className=\"absolute inset-0\"\n        />\n      </div>\n      {i < STRIPS - 1 ? buildStrip(i + 1) : null}\n    </div>\n  );\n\n  const edge: string[] = [];\n  for (let i = 1; i <= 13; i++) edge.push(`${(i * 0.35).toFixed(2)}px ${i}px 0 ${i % 2 ? \"#cfc8ba\" : \"#f3efe6\"}`);\n  edge.push(\"6px 26px 30px -8px rgba(40,26,12,0.5)\");\n\n  return (\n    <section\n      ref={rootRef}\n      className=\"relative flex w-full select-none flex-col items-center justify-center overflow-hidden px-4 py-14\"\n      style={{ minHeight: 660, background: \"radial-gradient(120% 90% at 50% 28%,#f1ece3 0%,#e4ddd1 58%,#d3cabb 100%)\" }}\n    >\n      <div className=\"relative z-10\" style={{ width: 288, perspective: 950, perspectiveOrigin: \"50% 40%\" }}>\n        {/* nail and backing card */}\n        <div className=\"absolute left-1/2 top-[-9px] z-10 h-[14px] w-[14px] -translate-x-1/2 rounded-full\" style={{ background: \"radial-gradient(circle at 35% 30%,#fff7d6,#c9a24a 45%,#6d5320)\", boxShadow: \"0 3px 4px rgba(0,0,0,.4)\" }} aria-hidden=\"true\" />\n        <div\n          className=\"relative rounded-[10px] px-5 pb-6 pt-8\"\n          style={{\n            background: \"linear-gradient(160deg,#2e3a33 0%,#1f2924 60%,#19211d 100%)\",\n            boxShadow: \"0 1px 0 rgba(255,255,255,.12) inset, 0 -2px 0 rgba(0,0,0,.35) inset, 0 34px 50px -18px rgba(54,38,20,.55), 0 10px 16px -8px rgba(54,38,20,.4)\",\n            transformStyle: \"preserve-3d\",\n            transform: \"rotateX(7deg) rotateY(-19deg)\",\n          }}\n        >\n          <div className=\"absolute left-1/2 top-[7px] h-[9px] w-[9px] -translate-x-1/2 rounded-full bg-[#0d1210] shadow-[inset_0_1px_2px_rgba(0,0,0,.9),0_1px_0_rgba(255,255,255,.12)]\" aria-hidden=\"true\" />\n\n          {/* binding strip with two staples */}\n          <div\n            className=\"relative z-20 mx-auto flex items-center justify-between rounded-t-[3px] px-9\"\n            style={{ width: PAGE_W, height: 30, background: \"linear-gradient(180deg,#dd4a30 0%,#c8371f 55%,#a82b17 100%)\", boxShadow: \"0 3px 5px -1px rgba(0,0,0,.45), inset 0 1px 0 rgba(255,255,255,.3)\" }}\n            aria-hidden=\"true\"\n          >\n            {[0, 1].map((n) => (\n              <span key={n} className=\"block h-[5px] w-7 rounded-full\" style={{ background: \"linear-gradient(180deg,#f4f4f2,#9b9b96)\", boxShadow: \"0 1px 1px rgba(0,0,0,.5)\" }} />\n            ))}\n          </div>\n          {/* torn stubs left under the binding */}\n          <div className=\"relative z-10 mx-auto\" style={{ width: PAGE_W, height: 5, background: \"repeating-linear-gradient(90deg,#f1ede4 0 5px,#d8d1c3 5px 6px,#f7f4ee 6px 13px,#cfc7b8 13px 14px)\", clipPath: \"polygon(0 0,100% 0,100% 55%,96% 100%,91% 60%,85% 95%,78% 55%,70% 100%,63% 62%,55% 96%,47% 58%,39% 100%,31% 60%,23% 95%,15% 58%,8% 100%,0 60%)\" }} aria-hidden=\"true\" />\n\n          {/* the pad */}\n          <div\n            className=\"relative mx-auto cursor-grab touch-none select-none active:cursor-grabbing\"\n            style={{ width: PAGE_W, height: PAGE_H, marginTop: -3, marginBottom: 14, transformStyle: \"preserve-3d\" }}\n            onPointerDown={onDown}\n            onPointerMove={onMove}\n            onPointerUp={onUp}\n            onPointerCancel={onUp}\n            onClick={onClick}\n          >\n            <div className=\"absolute inset-0 overflow-hidden rounded-b-[2px]\" style={{ boxShadow: edge.join(\",\") }}>\n              <PageFace index={index + 1} />\n              <div\n                ref={castRef}\n                className=\"pointer-events-none absolute left-0 top-0 w-full\"\n                style={{ height: 120, opacity: 0, background: \"linear-gradient(180deg,rgba(42,28,16,.5) 0%,rgba(42,28,16,.16) 38%,rgba(42,28,16,0) 100%)\", transform: `translateY(${PAGE_H}px)` }}\n                aria-hidden=\"true\"\n              />\n            </div>\n            <div\n              ref={dropRef}\n              className=\"pointer-events-none absolute left-[6%] top-[4%] h-[88%] w-[88%] rounded-[30px]\"\n              style={{ opacity: 0, background: \"rgba(30,18,8,.7)\", filter: \"blur(22px)\", transformOrigin: \"50% 0\" }}\n              aria-hidden=\"true\"\n            />\n            <div ref={sheetRef} className=\"absolute inset-0\" style={{ transformOrigin: \"50% 0\", transformStyle: \"preserve-3d\", willChange: \"transform\" }} aria-hidden=\"true\">\n              {buildStrip(0)}\n            </div>\n            <button\n              type=\"button\"\n              className=\"absolute inset-0 z-30 cursor-grab touch-none rounded-[2px] outline-none focus-visible:ring-2 focus-visible:ring-[#c8371f] focus-visible:ring-offset-4 focus-visible:ring-offset-[#1f2924] active:cursor-grabbing\"\n              style={{ transform: \"translateZ(1px)\" }}\n              aria-label={`Tear off ${today.weekday} ${today.day} ${today.month}`}\n            />\n          </div>\n\n          <div className=\"flex items-center justify-between px-1 text-[10px] font-semibold uppercase tracking-[0.3em] text-[#c9b27a]\" aria-hidden=\"true\">\n            <span>Halden &amp; Rowe</span>\n            <span>Stationers</span>\n          </div>\n        </div>\n      </div>\n\n      <p className=\"relative mt-9 text-center text-[13px] text-stone-600\">Pull the page up, or press Enter to tear it off.</p>\n      <p className=\"sr-only\" aria-live=\"polite\">\n        Showing {today.weekday} {today.day} {today.month} {today.year}\n      </p>\n    </section>\n  );\n}\n"}]}
