# Blinds Reveal
*URL: https://www.grettech.com/components/blinds-reveal*

One image turns into another through 3D slats that flip in sequence, like venetian blinds. Hover, scroll or click to flip.

- Real 3D slats that cascade open and closed
- Horizontal or vertical slats, any number
- Hover (and keyboard focus), in-view or click triggers, or controlled
- Accessible name follows the visible image

## Install with the shadcn CLI
```bash
npx shadcn@latest add https://www.grettech.com/api/registry/blinds-reveal
```
With its demo: `npx shadcn@latest add https://www.grettech.com/api/registry/blinds-reveal-demo`

**Dependencies:** npm: clsx, tailwind-merge

## Manual installation
**Step 1: Install dependencies**
Install the packages this component uses and the shadcn/ui components it builds on. It also needs the `cn` helper in `lib/utils.ts`, which `npx shadcn@latest init` adds.

```bash
npm install clsx tailwind-merge
```

**Step 2: Copy the source code**
Add these files to your project components directory.

File: `components/ui/blinds-reveal.tsx`
```tsx
"use client"

import { cn } from "@/lib/utils"
import * as React from "react"

export interface BlindsImage {
  src: string
  alt: string
}

export interface BlindsRevealProps extends React.HTMLAttributes<HTMLDivElement> {
  /** Shown first */
  front: BlindsImage
  /** Revealed by the slats */
  back: BlindsImage
  /** Number of slats */
  slats?: number
  /** Horizontal slats flip up; vertical slats flip sideways */
  orientation?: "horizontal" | "vertical"
  trigger?: "hover" | "inView" | "click"
  /** Controlled state: true shows the back image */
  revealed?: boolean
  onRevealedChange?: (revealed: boolean) => void
  /** ms each slat takes to turn */
  duration?: number
  /** ms between neighbouring slats starting */
  stagger?: number
  /** CSS aspect ratio of the frame */
  aspectRatio?: string
}

export const BlindsReveal = React.forwardRef<HTMLDivElement, BlindsRevealProps>(
  (
    {
      front,
      back,
      slats = 10,
      orientation = "horizontal",
      trigger = "hover",
      revealed: controlledRevealed,
      onRevealedChange,
      duration = 800,
      stagger = 55,
      aspectRatio = "16 / 10",
      className,
      style,
      ...props
    },
    forwardedRef
  ) => {
    const ref = React.useRef<HTMLDivElement>(null)
    React.useImperativeHandle(forwardedRef, () => ref.current!)
    const [internalRevealed, setInternalRevealed] = React.useState(false)
    const revealed = controlledRevealed ?? internalRevealed
    const setRevealed = React.useCallback(
      (next: boolean) => {
        setInternalRevealed(next)
        onRevealedChange?.(next)
      },
      [onRevealedChange]
    )

    React.useEffect(() => {
      if (trigger !== "inView" || !ref.current) return
      const io = new IntersectionObserver(
        ([entry]) => {
          if (entry.intersectionRatio >= 0.6) setRevealed(true)
        },
        { threshold: 0.6 }
      )
      io.observe(ref.current)
      return () => io.disconnect()
    }, [trigger, setRevealed])

    const horizontal = orientation === "horizontal"
    const axis = horizontal ? "rotateX" : "rotateY"
    const triggerProps =
      trigger === "hover"
        ? {
            tabIndex: 0,
            onPointerEnter: () => setRevealed(true),
            onPointerLeave: () => setRevealed(false),
            onFocus: () => setRevealed(true),
            onBlur: () => setRevealed(false),
          }
        : trigger === "click"
          ? {
              tabIndex: 0,
              role: "button",
              "aria-pressed": revealed,
              onClick: () => setRevealed(!revealed),
              onKeyDown: (e: React.KeyboardEvent) => {
                if (e.key === "Enter" || e.key === " ") {
                  e.preventDefault()
                  setRevealed(!revealed)
                }
              },
            }
          : {}

    // Each face shows its strip of the full image
    const face = (img: BlindsImage, i: number) => ({
      backgroundImage: `url("${img.src}")`,
      backgroundSize: horizontal
        ? `100% ${slats * 100}%`
        : `${slats * 100}% 100%`,
      backgroundPosition: horizontal
        ? `0 ${(i / Math.max(slats - 1, 1)) * 100}%`
        : `${(i / Math.max(slats - 1, 1)) * 100}% 0`,
    })

    return (
      <div
        ref={ref}
        aria-label={`${revealed ? back.alt : front.alt}${trigger === "click" ? ". Activate to flip." : ""}`}
        className={cn(
          "relative w-full overflow-hidden rounded-2xl bg-neutral-900 [perspective:1400px] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:outline-none",
          trigger === "click" && "cursor-pointer",
          className
        )}
        style={{ aspectRatio, ...style }}
        {...(trigger === "inView" ? { role: "img" } : {})}
        {...triggerProps}
        {...props}
      >
        {/* Real images for loading priority and screen readers' context */}
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img src={front.src} alt="" aria-hidden="true" className="hidden" />
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img
          src={back.src}
          alt=""
          aria-hidden="true"
          loading="lazy"
          className="hidden"
        />

        {Array.from({ length: slats }, (_, i) => (
          <div
            key={i}
            aria-hidden="true"
            className="absolute [transform-style:preserve-3d] motion-reduce:!transition-none"
            style={{
              ...(horizontal
                ? {
                    left: 0,
                    right: 0,
                    top: `${(i / slats) * 100}%`,
                    height: `${100 / slats}%`,
                  }
                : {
                    top: 0,
                    bottom: 0,
                    left: `${(i / slats) * 100}%`,
                    width: `${100 / slats}%`,
                  }),
              transform: `${axis}(${revealed ? 180 : 0}deg)`,
              // Longhands only: mixing the transition shorthand with
              // transitionDelay breaks when duration changes on a re-render
              transitionProperty: "transform",
              transitionDuration: `${duration}ms`,
              transitionTimingFunction: "cubic-bezier(0.65, 0, 0.35, 1)",
              // Cascade from the first slat when opening, the last when closing
              transitionDelay: `${(revealed ? i : slats - 1 - i) * stagger}ms`,
            }}
          >
            <div
              className="absolute inset-0 [backface-visibility:hidden]"
              style={face(front, i)}
            />
            <div
              className="absolute inset-0 [backface-visibility:hidden]"
              style={{ ...face(back, i), transform: `${axis}(180deg)` }}
            />
          </div>
        ))}
      </div>
    )
  }
)
BlindsReveal.displayName = "BlindsReveal"

```

**Step 3: Framework Adjustments (Optional)**
If you are manually copying this code into a Vite or Remix project (without using the CLI), make sure to remove the `"use client";` directive from the very top of the file.

## Props
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| `front` | `{ src: string; alt: string }` | - | Yes | Image shown first. |
| `front.src` | `string` | - | Yes | - |
| `front.alt` | `string` | - | Yes | - |
| `back` | `{ src: string; alt: string }` | - | Yes | Image the slats reveal. |
| `back.src` | `string` | - | Yes | - |
| `back.alt` | `string` | - | Yes | - |
| `slats` | `number` | `10` | No | Number of slats. |
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | No | Horizontal slats flip up and down; vertical ones flip sideways. |
| `trigger` | `"hover" \| "inView" \| "click"` | `"hover"` | No | What flips the slats. Hover also flips on keyboard focus. |
| `revealed` | `boolean` | - | No | Controlled state: true shows the back image. |
| `onRevealedChange` | `(revealed: boolean) => void` | - | No | Called when the state changes. |
| `duration` | `number` | `800` | No | Milliseconds each slat takes to turn. |
| `stagger` | `number` | `55` | No | Milliseconds between neighbouring slats starting. |
| `aspectRatio` | `string` | `"16 / 10"` | No | CSS aspect ratio of the frame. |
| `className` | `string` | - | No | Extra classes for the frame. |
| `...props` | `React.HTMLAttributes<HTMLDivElement>` | - | No | Any other attributes, passed to the frame. |

## Usage
```tsx
"use client"

import { BlindsReveal } from "@/components/ui/blinds-reveal"

export default function BlindsRevealDemo() {
  return (
    <div className="w-full max-w-2xl p-4">
      <BlindsReveal
        front={{
          src:
            "https://images.unsplash.com/photo-1561070791-2526d30994b5?q=75&w=1200&auto=format&fit=crop",
          alt: "Designers sketching an interface",
        }}
        back={{
          src:
            "https://images.unsplash.com/photo-1460925895917-afdab827c52f?q=75&w=1200&auto=format&fit=crop",
          alt: "The finished product's analytics on a laptop",
        }}
      />
      <p className="mt-3 text-center text-xs text-muted-foreground">
        Hover or focus to flip
      </p>
    </div>
  )
}

```
