{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"pin-rotate-sections","type":"registry:component","title":"Pin rotate sections","description":"Pinned sections that turn in 3D.","author":"DiMaac (HankTheTank) <https://dimaac.com>","dependencies":["gsap","@gsap/react"],"files":[{"path":"registry/dimaac/pin-rotate-sections/pin-rotate-sections.tsx","type":"registry:component","target":"components/dimaac/pin-rotate-sections.tsx","content":"\"use client\";\n\nimport { Children, useRef, type CSSProperties, type ReactNode } from \"react\";\nimport { gsap } from \"gsap\";\nimport { ScrollTrigger } from \"gsap/ScrollTrigger\";\nimport { useGSAP } from \"@gsap/react\";\n\n// Full-screen sections that stack like a deck of cards. Each one pins to the top of the page, and\n// while the next section slides up over it, the pinned one shrinks, tilts back and fades to dark,\n// as if it were being pushed away. Four small pieces:\n//   <PinRotateSections>  wraps everything and runs the animation\n//   <PinRotateIntro> and <PinRotateOutro>  the plain dark openers and closers\n//   <PinRotateSection>  one card in the stack. The animated ones are the sections.\n\nif (typeof window !== \"undefined\") gsap.registerPlugin(useGSAP, ScrollTrigger);\n\nexport type PinRotateSectionsProps = {\n  /** The intro, the sections and the outro, in order. Sections are found by <PinRotateSection>. */\n  children: ReactNode;\n  /** How much each section shrinks while it is pushed away. 0.25 ends at 75% of its size. */\n  shrink?: number;\n  /** How far each section tips back, in degrees. The direction alternates from card to card. */\n  tilt?: number;\n  /** How far each section turns in the page plane, in degrees. Also alternates. */\n  twist?: number;\n  /** How dark a pushed-away section gets, from 0 (not at all) to 1 (black). */\n  dim?: number;\n  /** Depth of the tilt, in pixels. Smaller is more dramatic. Below about 800 the corners can glitch. */\n  perspective?: number;\n  /** The colour that shows behind and around the shrinking sections. */\n  backdrop?: string;\n  /** The scrolling element. Leave it out to use the page; give it a selector or an element for an inner scroller. */\n  scroller?: string | HTMLElement;\n  /** Extra classes for the outer wrapper. */\n  className?: string;\n};\n\nexport function PinRotateSections({\n  children,\n  shrink = 0.25,\n  tilt = 40,\n  twist = 5,\n  dim = 0.4,\n  perspective = 2200,\n  backdrop = \"#1a1a1a\",\n  scroller,\n  className = \"\",\n}: PinRotateSectionsProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  // The effect has to start again when sections are added or removed\n  const childCount = Children.count(children);\n\n  useGSAP(\n    () => {\n      const root = rootRef.current;\n      if (!root) return;\n\n      const mm = gsap.matchMedia();\n\n      // Reduced motion: no pinning and no tilting. The sections are just a normal page.\n      mm.add(\"(prefers-reduced-motion: no-preference)\", () => {\n        const sections = gsap.utils.toArray<HTMLElement>(\"[data-pin-rotate-section]\", root);\n        const last = sections[sections.length - 1];\n        if (!last) return;\n\n        // The scroller can be a selector or an element: find the element once. A selector must be looked up\n        // here, outside the scope, because the scope only searches inside this wrapper and the scroller is\n        // always outside it. Using the element also lets us measure the scroller's height, not the window's.\n        const scrollEl = (typeof scroller === \"string\" ? document.querySelector<HTMLElement>(scroller) : scroller) ?? undefined;\n        // The height we measure sections against: the scroller's, or the window's\n        const viewHeight = () => (scrollEl ? scrollEl.clientHeight : window.innerHeight);\n\n        sections.forEach((section, index) => {\n          const next = sections[index + 1];\n          // The last card doesn't move: it is the one everything is pushed away by\n          if (!next) return;\n\n          const direction = index % 2 === 0 ? 1 : -1;\n          const overlay = section.querySelector<HTMLElement>(\"[data-pin-rotate-overlay]\");\n\n          // Pin the card until the last one arrives, so the next card slides over the top of it.\n          // A card taller than the screen (common on phones) pins by its bottom edge instead,\n          // so its whole content gets read before it is covered.\n          ScrollTrigger.create({\n            trigger: section,\n            scroller: scrollEl,\n            start: () => (section.offsetHeight > viewHeight() ? \"bottom bottom\" : \"top top\"),\n            endTrigger: last,\n            end: \"top top\",\n            pin: true,\n            pinSpacing: false,\n          });\n\n          // While the next card climbs from the bottom edge to the top, the pinned card is pushed away.\n          // scrub ties the animation to the scrollbar, so it runs back when you scroll up.\n          const away = gsap.timeline({\n            defaults: { ease: \"none\" },\n            scrollTrigger: { trigger: next, scroller: scrollEl, start: \"top bottom\", end: \"top top\", scrub: true },\n          });\n          away.to(section, { scale: 1 - shrink, rotation: direction * twist, rotationX: direction * tilt, transformPerspective: perspective || 0, force3D: true }, 0);\n          if (overlay) away.to(overlay, { opacity: dim }, 0);\n        });\n\n        // Pictures arriving late change the page height, so measure the pins again.\n        // A short delay turns a burst of loads into one measurement.\n        let measure: gsap.core.Tween | undefined;\n        const observer = new ResizeObserver(() => {\n          measure?.kill();\n          measure = gsap.delayedCall(0.2, () => ScrollTrigger.refresh());\n        });\n        // Only the wrapper: a pinned card changes its own box while it sticks, which must not re-trigger this\n        observer.observe(root);\n\n        return () => {\n          measure?.kill();\n          observer.disconnect();\n        };\n      });\n\n      return () => mm.revert();\n    },\n    { scope: rootRef, dependencies: [childCount, shrink, tilt, twist, dim, perspective, scroller], revertOnUpdate: true },\n  );\n\n  return (\n    <div ref={rootRef} className={`relative w-full overflow-x-clip ${className}`} style={{ backgroundColor: backdrop }}>\n      {children}\n    </div>\n  );\n}\n\nexport type PinRotateIntroProps = {\n  /** The opening words: usually a heading and a paragraph. */\n  children: ReactNode;\n  /** Extra classes: change the colours, the height or the padding here. */\n  className?: string;\n};\n\n/** A full-height dark opener. It scrolls normally. */\nexport function PinRotateIntro({ children, className = \"\" }: PinRotateIntroProps) {\n  return (\n    <section data-pin-rotate-intro className={`flex min-h-[100svh] flex-col items-center justify-center bg-[#1a1a1a] px-[6vw] text-center text-white md:px-[8vw] ${className}`}>\n      {children}\n    </section>\n  );\n}\n\nexport type PinRotateSectionProps = {\n  /** What the card shows. */\n  children: ReactNode;\n  /** Extra classes: the background, text colour and padding. Cards are paper-coloured by default. */\n  className?: string;\n  /** Inline styles for the card, e.g. a background image. */\n  style?: CSSProperties;\n};\n\n/** One card of the stack. It pins, then shrinks and darkens as the next card covers it. */\nexport function PinRotateSection({ children, className = \"\", style }: PinRotateSectionProps) {\n  return (\n    <section\n      data-pin-rotate-section\n      className={`relative flex min-h-[85svh] flex-col bg-[#fcfcfc] px-[6vw] py-[5vh] text-[#1a1a1a] md:flex-row md:justify-between md:px-[8vw] md:py-[10vh] ${className}`}\n      style={{ borderBottom: \"1px solid rgba(0,0,0,0.24)\", willChange: \"transform\", transformStyle: \"flat\", ...style }}\n    >\n      {children}\n      {/* The shadow that grows over the card as it is pushed away */}\n      <div data-pin-rotate-overlay aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 bg-black opacity-0 [backface-visibility:hidden]\" />\n    </section>\n  );\n}\n\nexport type PinRotateOutroProps = {\n  /** The closing words. */\n  children: ReactNode;\n  /** Extra classes: change the colours, the height or the padding here. */\n  className?: string;\n};\n\n/** A full-height dark closer. The last card slides up over the stack and ends on this. */\nexport function PinRotateOutro({ children, className = \"\" }: PinRotateOutroProps) {\n  return (\n    <section data-pin-rotate-outro className={`relative flex min-h-[100svh] flex-col items-center justify-center bg-[#1a1a1a] px-[6vw] text-center text-white md:px-[8vw] ${className}`}>\n      {children}\n    </section>\n  );\n}\n"}],"categories":["scroll","scrolltrigger","pin","3d","sections","scrub"],"docs":"import { PinRotateSections } from \"@/components/dimaac/pin-rotate-sections\". Give its parent a size: it fills it. Docs, playground and props: https://ui.dimaac.com/components/pin-rotate-sections","meta":{"tier":"free","preview":"https://ui.dimaac.com/components/pin-rotate-sections","credits":[{"by":"Ricardo Matos","url":"https://www.lummi.ai/photo/minimalist-urban-design-abiri","what":"Concrete Corner","source":"Lummi","licence":"Lummi License"},{"by":"Mariate","url":"https://www.lummi.ai/photo/minimalist-architectural-design-b8bbc","what":"Balcony Study","source":"Lummi","licence":"Lummi License"},{"by":"Siren Media","url":"https://www.lummi.ai/photo/modern-interior-with-red-wall-6k-hl","what":"The Red Wall","source":"Lummi","licence":"Lummi License"},{"by":"Mikiwa","url":"https://www.lummi.ai/photo/modern-building-corner-dr5dz","what":"White Corner","source":"Lummi","licence":"Lummi License"},{"by":"Binks AI","url":"https://www.lummi.ai/photo/serene-minimalist-scene-with-palm-trees-pba-c","what":"Palm Court","source":"Lummi","licence":"Lummi License"}]}}