# Skewed Grid
*URL: https://www.grettech.com/components/skewed-grid*

A hero background of two grid planes tilted in 3D so they cross behind your content, stretching the cells into skewed rectangles that drift toward you.

- Two tilted grid planes in CSS 3D perspective, faded at every edge
- Lines in the text colour, so one grid suits light and dark themes
- Optional drift toward the viewer, on the GPU
- One Motion loop on the GPU; still for reduced motion

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

**Dependencies:** npm: motion, clsx, tailwind-merge; shadcn/ui: button

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

```bash
npm install motion clsx tailwind-merge
npx shadcn@latest add button
```

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

File: `components/ui/skewed-grid.tsx`
```tsx
"use client"

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

export interface SkewedGridProps extends React.HTMLAttributes<HTMLDivElement> {
  /** Content shown over the grid, centred */
  children?: React.ReactNode
  /** Size of one grid cell, in pixels */
  cellSize?: number
  /** Tilt of each plane, in degrees */
  angle?: number
  /** Camera distance: lower is a stronger, wider-angle perspective */
  perspective?: number
  /** Line colour; defaults to the text colour, so it follows the theme */
  lineColor?: string
  /** Strength of the lines, 0–1 */
  lineOpacity?: number
  /** Seconds for the grid to drift one cell toward you; 0 holds it still */
  speed?: number
  /** A soft glow where the two planes cross, behind the content */
  glowColor?: string
  /** Fade the grid out at the left and right edges too */
  fadeEdges?: boolean
}

/**
 * Two grid planes tilted in opposite directions inside a 3D perspective,
 * so they cross behind the content like the inside of a fold: the cells
 * stretch into skewed rectangles toward the edges and fade out. Lines are
 * CSS gradients in the text colour (one set of planes for both themes), and
 * the optional drift is a looping Motion transform, so it runs on the GPU
 * and holds still for reduced motion.
 */
export const SkewedGrid = React.forwardRef<HTMLDivElement, SkewedGridProps>(
  (
    {
      children,
      cellSize = 40,
      angle = 45,
      perspective = 1000,
      lineColor,
      lineOpacity = 0.1,
      speed = 0,
      glowColor,
      fadeEdges = true,
      className,
      style,
      ...props
    },
    ref
  ) => {
    const cell = Math.max(8, cellSize)
    const line =
      lineColor ??
      `color-mix(in oklab, currentColor ${Math.round(Math.min(1, Math.max(0, lineOpacity)) * 100)}%, transparent)`
    const grid: React.CSSProperties = {
      backgroundImage: `linear-gradient(to right, ${line} 1px, transparent 1px), linear-gradient(to bottom, ${line} 1px, transparent 1px)`,
      backgroundSize: `${cell}px ${cell}px`,
      backgroundPosition: "center center",
    }
    const still = useReducedMotion() ?? false
    const drifting = speed > 0 && !still
    // Each plane drifts one cell toward the viewer, then loops seamlessly
    const planes = [
      { tilt: angle, fade: "to top", to: cell },
      { tilt: -angle, fade: "to bottom", to: -cell },
    ]

    return (
      <div
        ref={ref}
        className={cn(
          "relative isolate flex min-h-[560px] w-full items-center justify-center overflow-hidden bg-background px-6 py-24",
          className
        )}
        style={style}
        {...props}
      >
        <div
          aria-hidden="true"
          className="pointer-events-none absolute inset-0 -z-10 overflow-hidden text-foreground [transform-style:preserve-3d]"
          style={{
            perspective: `${perspective}px`,
            maskImage: fadeEdges
              ? "linear-gradient(to right, transparent, black 18%, black 82%, transparent)"
              : undefined,
          }}
        >
          {planes.map(({ tilt, fade, to }) => (
            <div
              key={fade}
              className="absolute inset-0 overflow-hidden"
              style={{
                transform: `rotateX(${tilt}deg)`,
                maskImage: `linear-gradient(${fade}, black, transparent)`,
              }}
            >
              {/* One cell taller than the plane, so the drift loops
                  seamlessly */}
              <motion.div
                className="absolute inset-x-0"
                style={{ ...grid, top: `-${cell}px`, bottom: `-${cell}px` }}
                initial={{ y: 0 }}
                animate={drifting ? { y: [0, to] } : { y: 0 }}
                transition={
                  drifting
                    ? { duration: speed, ease: "linear", repeat: Infinity }
                    : { duration: 0 }
                }
              />
            </div>
          ))}
          {glowColor && (
            <div
              className="absolute inset-x-0 top-1/2 h-1/2 -translate-y-1/2"
              style={{
                backgroundImage: `radial-gradient(50% 50% at 50% 50%, color-mix(in oklab, ${glowColor} 22%, transparent), transparent)`,
              }}
            />
          )}
        </div>

        {children !== undefined && (
          <div className="relative z-10 w-full">{children}</div>
        )}
      </div>
    )
  }
)
SkewedGrid.displayName = "SkewedGrid"

```

**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 |
|---|---|---|---|---|
| `children` | `React.ReactNode` | - | No | Content shown over the grid, centred. |
| `cellSize` | `number` | `40` | No | Size of one grid cell, in pixels. |
| `angle` | `number` | `45` | No | Tilt of each plane, in degrees. |
| `perspective` | `number` | `1000` | No | Camera distance; lower gives a stronger, wider-angle perspective. |
| `lineColor` | `string` | - | No | Line colour; defaults to the text colour, so it follows the theme. |
| `lineOpacity` | `number` | `0.1` | No | Strength of the lines, from 0 to 1. |
| `speed` | `number` | `0` | No | Seconds for the grid to drift one cell toward you; 0 holds it still. |
| `glowColor` | `string` | - | No | A soft glow where the two planes cross, behind the content. |
| `fadeEdges` | `boolean` | `true` | No | Fade the grid out at the left and right edges too. |
| `className` | `string` | - | No | Extra classes for the wrapper. |

## Usage
```tsx
"use client"

import { Button } from "@/components/ui/button"
import { SkewedGrid } from "@/components/ui/skewed-grid"

export default function SkewedGridDemo() {
  return (
    <SkewedGrid
      className="min-h-[600px] rounded-3xl border"
      speed={5}
      glowColor="#6366f1"
    >
      <div className="mx-auto flex max-w-2xl flex-col items-center text-center">
        <h1 className="text-3xl font-bold tracking-tight text-balance text-foreground md:text-5xl">
          Software that feels a step ahead of everyone else
        </h1>
        <p className="mt-4 max-w-xl text-base text-balance text-muted-foreground">
          We design and build web apps that are quick to load, easy to use and
          ready for whatever your business needs next.
        </p>
        <div className="mt-8 flex flex-wrap items-center justify-center gap-3">
          <Button size="lg">Start a project</Button>
          <Button size="lg" variant="outline" className="bg-background/60">
            See our work
          </Button>
        </div>
      </div>
    </SkewedGrid>
  )
}

```
