Photo Rail
A fanned rail of overlapping photo cards for a hero. The centre card is largest, the rest tilt alternately, and each straightens and lifts under a fine pointer.
Preview
Install
npx shadcn@latest add @dxbh-ui/photo-rail
Add the @dxbh-ui registry to components.json once:
{
"registries": {
"@dxbh-ui": "https://ui.dxbh.dev/r/{name}.json"
}
}Usage
"use client";
import { PhotoRail } from "@/components/ui/photo-rail/photo-rail";
import { placeholder } from "./placeholder";
const photos = Array.from({ length: 5 }, (_, i) => ({
src: placeholder(i),
alt: `Placeholder landscape ${i + 1}`,
}));
export default function PhotoRailDemo() {
return (
<div className="w-full">
<PhotoRail photos={photos}>
<span className="absolute bottom-2 left-[12%] z-30 rounded-full border bg-background px-3 py-1.5 text-xs font-medium shadow-sm tabular-nums">
24 new photos
</span>
</PhotoRail>
</div>
);
}Props
| Prop | Type | Default |
|---|---|---|
photos*Photos in display order. Odd counts centre best. | { src: string; alt: string }[] | — |
renderImageRender the image yourself, e.g. next/image with fill. Defaults to <img>. | (photo, index) => ReactNode | — |
entrancePlay the one-time staggered rise on mount. | boolean | true |
cardSizeWidth of the centre card; the rest scale from it. | string | "min(15rem, 22vw)" |
childrenOverlays positioned against the rail. | ReactNode | — |
Polish
- Photos, alt text and the image element come in through props; renderImage replaces the hard-wired next/image.
- Any number of cards: size, tilt, offset and stacking derive from distance to the centre, deterministically so server and client agree.
- Brand blobs, brand shadows, the sticker and the live badge are gone. Overlays go in as children.
- Entrance animates transform only, never opacity, so a rail holding the LCP image doesn't delay it.
- Static tilt and offset use the individual rotate and translate properties; the entrance owns transform, so they never fight.
- Hover transitions only scale and rotate with the popover duration and dxbh-ui ease-out. Tailwind's hover: keeps it off touch screens.
- The infinite float loop was removed: a hero seen on every visit shouldn't bob forever.
- Card width defaults to min(15rem, 22vw) so five cards fit at 390px with no horizontal scroll.
- Reduced motion: no entrance, instant hover.
Source
import type { CSSProperties, HTMLAttributes, ReactNode } from "react";
import { cn } from "@/lib/utils";
export type RailPhoto = {
src: string;
alt: string;
};
export type PhotoRailProps = Omit<HTMLAttributes<HTMLDivElement>, "children"> & {
photos: RailPhoto[];
/**
* Render the image yourself (next/image, a CDN component). It fills the
* card; give it `object-cover` and full width and height. Defaults to <img>.
*/
renderImage?: (photo: RailPhoto, index: number) => ReactNode;
/** Play the one-time rise on mount. Defaults to true. */
entrance?: boolean;
/** Width of the centre card. Defaults to min(15rem, 22vw) so five fit at 390px. */
cardSize?: string;
/** Overlays (a badge, a caption) positioned against the rail. */
children?: ReactNode;
};
// Transform only, no opacity: a hero rail often holds the LCP image, and an
// opacity-0 first frame delays recorded LCP by the whole animation. The rise
// runs on `transform`, so the static layout below can use the individual
// `translate` / `rotate` / `scale` properties without the two fighting.
const STYLES = `
@keyframes dxbh-ui-rail-in {
from { transform: translateY(calc(64px * var(--motion-distance, 1))) rotate(calc(4deg * var(--motion-distance, 1))); }
}
@media (prefers-reduced-motion: no-preference) {
:root:not([data-reduce-motion]) [data-rail-enter] {
animation: dxbh-ui-rail-in 700ms var(--ease-drawer, cubic-bezier(0.32, 0.72, 0, 1)) both;
animation-delay: calc(var(--i, 0) * 60ms);
}
}
`;
// Deterministic tilt and offset per position, so server and client agree.
const TILTS = [-5, 3, -2, 4, -4, 2, -3, 5];
const LIFTS = [16, -10, 0, -8, 18, -6, 12, -12];
const defaultImage = (photo: RailPhoto) => (
// 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} draggable={false} className="size-full object-cover" />
);
/**
* A fanned rail of overlapping photo cards. The centre card is largest and
* sits on top; cards tilt alternately and straighten under a fine pointer.
*/
export function PhotoRail({
photos,
renderImage = defaultImage,
entrance = true,
cardSize = "min(15rem, 22vw)",
className,
style,
children,
...props
}: PhotoRailProps) {
const centre = (photos.length - 1) / 2;
return (
<div
className={cn("relative mx-auto flex max-w-full items-center justify-center overflow-x-clip px-4 py-6", className)}
style={{ "--rail-card": cardSize, ...style } as CSSProperties}
{...props}
>
{entrance ? (
<style href="dxbh-ui-photo-rail" precedence="default">
{STYLES}
</style>
) : null}
<ul className="relative flex list-none items-center justify-center">
{photos.map((photo, i) => {
const distance = Math.abs(i - centre);
const size = Math.max(1 - distance * 0.13, 0.6);
return (
<li
key={`${photo.src}-${i}`}
data-rail-enter={entrance ? "" : undefined}
className={cn(
"relative z-(--rail-z) shrink-0 overflow-hidden rounded-[1.25rem] bg-muted shadow-[0_24px_48px_-24px_rgb(0_0_0/0.35)]",
"aspect-[5/6] w-[calc(var(--rail-card)*var(--rail-size))] [&:not(:first-child)]:-ml-[calc(var(--rail-card)*0.08)]",
"[rotate:var(--rail-tilt)] [translate:0_var(--rail-lift)]",
// Hover straightens and lifts the card. Tailwind's hover: only
// applies where a fine pointer can hover, so touch never sticks.
"transition-[scale,rotate] duration-(--duration-popover,180ms) ease-out",
"hover:z-20 hover:scale-105 hover:rotate-0 motion-reduce:transition-none",
)}
style={
{
"--i": i,
"--rail-size": size,
"--rail-tilt": `${TILTS[i % TILTS.length]}deg`,
"--rail-lift": `${LIFTS[i % LIFTS.length]}px`,
"--rail-z": 10 - Math.ceil(distance),
} as CSSProperties
}
>
{renderImage(photo, i)}
</li>
);
})}
</ul>
{children}
</div>
);
}Extracted from moments-website/components/marketing/hero-photo-rail.tsx at f1d46a9.