{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"scrub-mask-reveal","type":"registry:component","title":"Scrub Mask Reveal","description":"A plate that opens exactly with the scroll: its rounded frame widens from a fifth in on each side to full while the picture inside eases the other way so it seems to stay still, bound with no easing, with a hairline that fills.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/scrub-mask-reveal.tsx","content":"\"use client\";\n\nimport { createContext, useContext, useEffect, useRef } from \"react\";\nimport type { CSSProperties, ReactNode, RefObject } from \"react\";\n\n/**\n * ScrubMaskReveal — a plate that opens exactly with the scroll.\n *\n * Its frame starts inset (a rounded window a fifth in from each side) and\n * opens to full as the plate comes up through the view, while the picture\n * inside eases from 112% to 100% so it seems to stay still. Bound to the\n * scroll with no easing: stop and it stops, scroll back and it closes. A\n * hairline fills with the progress. Renders open without script.\n *\n * Part of the Modern Minimal kit: a paper-white room (#f5f5f2, surfaces\n * #ffffff, ink #141412) and one light, dawn #ffa64d, only ever as light.\n * Two-layer shadows from one light above; scroll binds values directly,\n * with no easing. Inter Tight through var(--font-inter-tight) for display\n * over Geist. Respects prefers-reduced-motion. No dependencies beyond React.\n * Paste it as its own file: it repeats the kit's small helpers, which would\n * clash in one module. Load Inter Tight with next/font (variable:\n * \"--font-inter-tight\") on a parent, or from Google Fonts or @fontsource.\n */\n\n/** A paper-white room and one light. */\nconst PAPER = {\n  ground: \"#f5f5f2\",\n  surface: \"#ffffff\",\n  well: \"#ecebe6\",\n  solid: \"#141412\",\n  ink: \"rgba(20,20,18,0.92)\",\n  ink2: \"rgba(20,20,18,0.6)\",\n  ink3: \"rgba(20,20,18,0.38)\",\n  line: \"rgba(20,20,18,0.08)\",\n  line2: \"rgba(20,20,18,0.14)\",\n  dawn: \"#ffa64d\",\n  night: \"#0e0f11\",\n} as const;\n\nfunction clamp(v: number, lo: number, hi: number): number {\n  return v < lo ? lo : v > hi ? hi : v;\n}\n\n/**\n * The element that scrolls: null for the window, or a frame. Every scroll-bound part also takes a\n * `scroller` ref, which wins over this context, so parts pasted as separate files can share one frame.\n */\nexport const ScrollRootContext = createContext<RefObject<HTMLElement | null> | null>(null);\n\n/** Calls `fn` on scroll (of the scroll root or the window) and on resize; returns the cleanup. */\nfunction onScrollOf(root: HTMLElement | null, fn: () => void): () => void {\n  const target: HTMLElement | Window = root ?? window;\n  target.addEventListener(\"scroll\", fn, { passive: true });\n  window.addEventListener(\"resize\", fn);\n  return () => {\n    target.removeEventListener(\"scroll\", fn);\n    window.removeEventListener(\"resize\", fn);\n  };\n}\n\n/**\n * Binds a value to scroll with no easing: `read` is called on every scroll\n * and resize (and once at mount) and `apply` writes the result straight to the\n * DOM, so the value moves exactly with the scroll, like scrubbing film.\n */\nfunction useScrollBind(\n  ref: RefObject<HTMLElement | null>,\n  read: (el: HTMLElement, root: HTMLElement | null) => number,\n  apply: (v: number) => void,\n  scroller?: RefObject<HTMLElement | null> | null,\n) {\n  const ctx = useContext(ScrollRootContext);\n  const rootRef = scroller ?? ctx;\n  const readRef = useRef(read);\n  const applyRef = useRef(apply);\n  const runRef = useRef<() => void>(() => undefined);\n  // A render may change what is read (a held progress): apply it then, not only on the next scroll.\n  useEffect(() => {\n    readRef.current = read;\n    applyRef.current = apply;\n    runRef.current();\n  });\n  useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const root = rootRef?.current ?? null;\n    let raf = 0;\n    const run = () => {\n      raf = 0;\n      applyRef.current(readRef.current(el, root));\n    };\n    const onScroll = () => {\n      if (!raf) raf = requestAnimationFrame(run);\n    };\n    run();\n    runRef.current = onScroll;\n    const off = onScrollOf(root, onScroll);\n    return () => {\n      off();\n      cancelAnimationFrame(raf);\n      raf = 0;\n      runRef.current = () => undefined;\n    };\n  }, [ref, rootRef]);\n}\n\n/**\n * ScrubMaskReveal — a plate that opens exactly with the scroll.\n *\n * Its frame starts inset (a rounded window a fifth in from each side) and\n * opens to full as the plate comes up through the view, while the picture\n * inside eases the other way, from 112% to 100%, so it seems to stay still\n * while the window widens. Bound to the scroll with no easing: stop\n * scrolling and it stops; scroll back and it closes. A hairline at the edge\n * fills with the progress. Renders open without script; a `progress` prop\n * holds it at a moment.\n */\nexport function ScrubMaskReveal({\n  children,\n  inset = 0.2,\n  radius = 28,\n  rail = true,\n  progress,\n  scroller,\n  className = \"\",\n  style,\n}: {\n  children: ReactNode;\n  /** How far in the frame starts, as a share of each side (0-0.4). */\n  inset?: number;\n  radius?: number;\n  /** A hairline that fills with the progress. */\n  rail?: boolean;\n  /** 0 closed to 1 open; holds it instead of following the scroll. */\n  progress?: number;\n  /** The element that scrolls, when it isn't the window (wins over ScrollRootContext). */\n  scroller?: RefObject<HTMLElement | null>;\n  className?: string;\n  style?: CSSProperties;\n}) {\n  const ref = useRef<HTMLDivElement>(null);\n  const frameRef = useRef<HTMLDivElement>(null);\n  const innerRef = useRef<HTMLDivElement>(null);\n  const railRef = useRef<HTMLSpanElement>(null);\n  useScrollBind(\n    ref,\n    (el, root) => {\n      if (progress !== undefined) return clamp(progress, 0, 1);\n      // Open between the plate's top entering the lower fifth and its middle reaching the middle of the view.\n      const r = el.getBoundingClientRect();\n      const top = root ? root.getBoundingClientRect().top : 0;\n      const h = root ? root.clientHeight : window.innerHeight;\n      return clamp((top + h * 0.95 - r.top) / Math.max(1, h * 0.45 + r.height * 0.5), 0, 1);\n    },\n    (v) => {\n      const k = clamp(inset, 0, 0.4) * (1 - v);\n      const f = frameRef.current;\n      const i = innerRef.current;\n      if (f) {\n        const c = `inset(${(k * 100).toFixed(2)}% ${(k * 110).toFixed(2)}% ${(k * 100).toFixed(2)}% ${(k * 110).toFixed(2)}% round ${radius}px)`;\n        f.style.clipPath = c;\n        f.style.setProperty(\"-webkit-clip-path\", c);\n      }\n      if (i) i.style.transform = `scale(${(1 + 0.12 * (1 - v)).toFixed(4)})`;\n      if (railRef.current) railRef.current.style.transform = `scaleX(${v.toFixed(4)})`;\n    },\n    scroller,\n  );\n  return (\n    <div ref={ref} className={\"relative w-full \" + className} style={style}>\n      <div ref={frameRef} className=\"relative overflow-hidden\" style={{ borderRadius: radius }}>\n        <div ref={innerRef} className=\"origin-center\">\n          {children}\n        </div>\n      </div>\n      {rail ? (\n        <span aria-hidden className=\"mt-3 block h-px w-full overflow-hidden\" style={{ background: PAPER.line }}>\n          <span ref={railRef} className=\"block h-full w-full origin-left\" style={{ background: PAPER.solid }} />\n        </span>\n      ) : null}\n    </div>\n  );\n}\n","type":"registry:component","target":"components/viberdy/scrub-mask-reveal.tsx"}],"meta":{"tier":"free","tags":["animation","scroll","reveal","clip path","image","scrub"],"href":"https://viberdy.dev/library/scrub-mask-reveal","aiPrompt":"Build a React + Tailwind primitive, `ScrubMaskReveal({ children, inset, radius, rail, progress, className, style })`.\n\nOn every scroll (no easing), v = clamp((viewTop + 0.95 x viewHeight - plateTop) / (0.45 x viewHeight + half the plate's height), 0, 1), or the progress prop. The frame gets clip-path inset(k% 1.1k% k% 1.1k% round radius) with k = inset x 100 x (1 - v); the inner picture scale(1 + 0.12 (1 - v)); a 1px rail beneath scaleX(v). Written straight to the elements; nothing is clipped before the first scroll read, so it renders open without script.\n\nModern Minimal grammar: a paper-white room, ground #f5f5f2 (surfaces #ffffff that lift, a recessed well #ecebe6), ink #141412 at 92/60/38% (never grey-400 body copy), hairlines at 8% and 14%; one light, dawn #ffa64d, only ever as light (the lamp's glow, a lit state), never as a fill. Shadows come from one light above in two layers: a contact 0 1px 2px rgba(20,20,18,0.06) and an ambient 0 24px 48px -12px rgba(20,20,18,0.1), both falling the same way; a pressed thing loses the ambient, a lifted one widens it. Type: Inter Tight through var(--font-inter-tight, \"Inter Tight\", ...) for display at 600 with -0.035em tracking and 0.98 leading (tracking tightens only at display sizes), Geist for reading at 16-17px/1.55, Geist Mono uppercase at 11-12px for readouts. Radii 12px controls (pills for buttons), 20px cards, 28px media. Motion: scroll binds values directly with no easing (a value moves exactly with the scroll, like scrubbing film); discrete hover and press states take 150-260ms with a spring back past rest; ambient loops 40s or more. Respect prefers-reduced-motion (ambient scenes hold still; scroll-bound values still follow the scroll, which the visitor drives). No dependencies beyond React.","license":"https://viberdy.dev/license"}}