dxbh-uiGitHub

Parallax Gallery

Photo columns that drift at different speeds as the section scrolls past, so a flat grid reads as depth. Pure CSS scroll-driven animation; still under reduced motion.

Preview
Scroll down
  • Placeholder landscape 1
    Frame 01
  • Placeholder landscape 4
    Frame 04
  • Placeholder landscape 7
    Frame 07
  • Placeholder landscape 2
    Frame 02
  • Placeholder landscape 5
    Frame 05
  • Placeholder landscape 8
    Frame 08
  • Placeholder landscape 3
    Frame 03
  • Placeholder landscape 6
    Frame 06
  • Placeholder landscape 9
    Frame 09

Install

npx shadcn@latest add @dxbh-ui/parallax-gallery

Add the @dxbh-ui registry to components.json once:

{
  "registries": {
    "@dxbh-ui": "https://ui.dxbh.dev/r/{name}.json"
  }
}

Usage

demo.tsx
"use client";

import { ParallaxGallery } from "@/components/ui/parallax-gallery/parallax-gallery";
import { placeholder } from "../photo-rail/placeholder";

const photos = Array.from({ length: 9 }, (_, i) => ({
  src: placeholder(i + 1),
  alt: `Placeholder landscape ${i + 1}`,
  caption: `Frame ${String(i + 1).padStart(2, "0")}`,
}));

export default function ParallaxGalleryDemo() {
  return (
    // Its own scroll box so the view timeline has something to track inside
    // the preview. On a page, the document scroller does this job.
    <div className="h-[28rem] w-full max-w-2xl overflow-y-auto rounded-xl border bg-background">
      <div className="flex h-56 items-end justify-center pb-4 text-sm text-muted-foreground">Scroll down</div>
      <ParallaxGallery photos={photos} className="px-3 sm:px-5" />
      <div className="h-56" />
    </div>
  );
}

Props

PropTypeDefault
photos*

Photos in reading order.

{ src: string; alt: string; caption?: ReactNode }[]—
columns

Column count. Photos are dealt left to right.

number3
drift

Travel per column in px, repeating.

{ from: number; to: number }[][30→-40, 90→-110, 10→-25]
renderImage

Render the image yourself. Defaults to a lazy <img>.

(photo, index) => ReactNode—

Polish

  • Generalised from a marketing section: photos come in as props, are dealt round-robin into any number of columns, and render through renderImage instead of next/image.
  • Section copy, brand blooms, brand shadows and heart counts removed. Captions are optional.
  • Drift is a CSS view-timeline animation on transform only: compositor work, no scroll listeners, no JS.
  • Linear timing on purpose: parallax tracks the scroll position 1:1, and easing it would make the columns feel detached from the finger.
  • Travel multiplies by --motion-distance; reduced motion, the data-reduce-motion switch and browsers without view timelines all get the static grid.
  • Every column stays on narrow screens instead of hiding one and dropping its photos. Gaps tighten under sm.
  • overflow: clip contains the drift without creating a scroll container that would capture the timeline.

Source

parallax-gallery.tsx
import type { CSSProperties, HTMLAttributes, ReactNode } from "react";
import { cn } from "@/lib/utils";

export type GalleryPhoto = {
  src: string;
  alt: string;
  /** Optional caption shown under the photo. */
  caption?: ReactNode;
};

/** Travel in px across the column's time in view: from (entering) to (leaving). */
export type ColumnDrift = { from: number; to: number };

export type ParallaxGalleryProps = Omit<HTMLAttributes<HTMLDivElement>, "children"> & {
  photos: GalleryPhoto[];
  /** Number of columns. Photos are dealt round-robin. Defaults to 3. */
  columns?: number;
  /**
   * Drift per column, repeating. The default moves the middle column fastest
   * so the grid reads as depth rather than a slide.
   */
  drift?: ColumnDrift[];
  /** Render the image yourself (next/image, a CDN component). Defaults to <img>. */
  renderImage?: (photo: GalleryPhoto, index: number) => ReactNode;
};

const DEFAULT_DRIFT: ColumnDrift[] = [
  { from: 30, to: -40 },
  { from: 90, to: -110 },
  { from: 10, to: -25 },
];

// Scroll-driven, compositor-only, no JS. Browsers without view timelines and
// anyone who asked for reduced motion see the same grid, standing still.
const STYLES = `
@keyframes dxbh-ui-drift {
  from { transform: translateY(calc(var(--drift-from, 0px) * var(--motion-distance, 1))); }
  to { transform: translateY(calc(var(--drift-to, 0px) * var(--motion-distance, 1))); }
}
@media (prefers-reduced-motion: no-preference) {
  @supports (animation-timeline: view()) {
    :root:not([data-reduce-motion]) [data-drift] {
      animation-name: dxbh-ui-drift;
      animation-duration: auto;
      animation-fill-mode: both;
      animation-timing-function: linear;
      animation-timeline: view();
      will-change: transform;
    }
  }
}
`;

const defaultImage = (photo: GalleryPhoto) => (
  // eslint-disable-next-line @next/next/no-img-element -- registry code stays framework-free; pass renderImage for next/image.
  <img src={photo.src} alt={photo.alt} loading="lazy" decoding="async" className="size-full object-cover" />
);

/** Splits photos into `count` columns, dealing left to right. */
export function dealColumns<T>(items: T[], count: number): { item: T; index: number }[][] {
  const n = Math.max(1, Math.floor(count));
  const cols: { item: T; index: number }[][] = Array.from({ length: n }, () => []);
  items.forEach((item, index) => cols[index % n].push({ item, index }));
  return cols;
}

/**
 * Photo columns that drift at different speeds as the section scrolls past.
 * Pure CSS scroll-driven animation; static under reduced motion.
 */
export function ParallaxGallery({
  photos,
  columns = 3,
  drift = DEFAULT_DRIFT,
  renderImage = defaultImage,
  className,
  style,
  ...props
}: ParallaxGalleryProps) {
  const cols = dealColumns(photos, columns);

  return (
    <div
      // Drift moves columns past the section edges; clip it without making a
      // scroll container.
      className={cn("grid items-start gap-3 overflow-clip py-12 sm:gap-6 sm:py-16", className)}
      style={{ gridTemplateColumns: `repeat(${cols.length}, minmax(0, 1fr))`, ...style } as CSSProperties}
      {...props}
    >
      <style href="dxbh-ui-parallax-gallery" precedence="default">
        {STYLES}
      </style>
      {cols.map((column, c) => {
        const d = drift.length ? drift[c % drift.length] : { from: 0, to: 0 };
        return (
          <ul
            key={c}
            data-drift=""
            className={cn("grid list-none gap-3 sm:gap-6", c % 2 === 1 && "mt-8 sm:mt-14")}
            style={{ "--drift-from": `${d.from}px`, "--drift-to": `${d.to}px` } as CSSProperties}
          >
            {column.map(({ item, index }) => (
              <li key={`${item.src}-${index}`}>
                <figure className="overflow-hidden rounded-2xl border bg-card p-1.5 shadow-sm">
                  <div className="relative aspect-[4/5] overflow-hidden rounded-[calc(var(--radius-2xl,1rem)-0.375rem)] bg-muted">
                    {renderImage(item, index)}
                  </div>
                  {item.caption ? (
                    <figcaption className="truncate px-1.5 pt-2 pb-1 text-sm text-muted-foreground">
                      {item.caption}
                    </figcaption>
                  ) : null}
                </figure>
              </li>
            ))}
          </ul>
        );
      })}
    </div>
  );
}

Extracted from moments-website/components/marketing/gallery-parallax-section.tsx at f1d46a9.