Skip to content

Open source CapCut alternative.

TypeScript

feat(site): justified mosaic, lightbox preview, and in-view autoplay

David Lec1ba8d007432
diff --git a/site/src/app/_components/landing/media-showcase/MediaCard.tsx b/site/src/app/_components/landing/media-showcase/MediaCard.tsx
index e8c11637..a55f52e3 100644
--- a/site/src/app/_components/landing/media-showcase/MediaCard.tsx
+++ b/site/src/app/_components/landing/media-showcase/MediaCard.tsx
@@ -1,56 +1,124 @@
+import { useEffect, useRef } from "react";
import Image from "next/image";
import { Film, ImageIcon, Maximize2 } from "lucide-react";
import { cn } from "@/lib/utils";
-import {
- type MediaAspect,
- type MediaShowcaseItem,
-} from "@/app/_components/landing/media-showcase/data";
-
-export const aspectClass: Record<MediaAspect, string> = {
- portrait: "aspect-[3/4]",
- landscape: "aspect-[16/9]",
- square: "aspect-square",
-};
+import { type MediaShowcaseItem } from "@/app/_components/landing/media-showcase/data";
+
+// Width-over-height ratio of the tile, from the exact ratio the item was
+// generated at (the "W:H" settings tag), falling back to its aspect bucket.
+export function tileRatio(item: MediaShowcaseItem): number {
+ const token = item.settings?.find((s) => /^\d+:\d+$/.test(s));
+ if (token) {
+ const [w, h] = token.split(":").map(Number);
+ if (w && h) return w / h;
+ }
+ return item.aspect === "portrait" ? 3 / 4 : item.aspect === "square" ? 1 : 16 / 9;
+}
+
+// Ratio used for row layout: very tall media (9:16) gets a floor of 0.7 so it
+// never collapses into a sliver inside a mixed row — the clip center-crops
+// into the wider box via object-cover. Uniform vertical walls (UGC filter)
+// keep the true ratio.
+export function layoutRatio(item: MediaShowcaseItem): number {
+ return Math.max(0.7, tileRatio(item));
+}
type Props = {
item: MediaShowcaseItem;
onOpen: (item: MediaShowcaseItem) => void;
+ // Flex sizing from the row packer: grow shares row width by aspect ratio so
+ // tiles in a row end up the same height; a sparse last row passes grow 0
+ // with a fixed pixel basis instead.
+ pack?: { grow: number; basis: string };
+ // Uniform-grid mode for all-vertical sets (e.g. the UGC & Ads filter):
+ // fixed 4-up columns (2-up on small screens, 5-up on very wide) instead of
+ // justified sizing, so 8 clips land as balanced rows like a vertical-video
+ // wall.
+ uniformGrid?: boolean;
};
-// One masonry tile showing the item's media. Clicking opens the prompt dialog.
-export function MediaCard({ item, onOpen }: Props) {
+// One tile in the justified mosaic. The flex-grow/flex-basis pair is the
+// classic justified-gallery trick: both scale with the tile's aspect ratio, so
+// every tile in a flex row ends up the same height and each row fills the full
+// width — landscape rows come out short, vertical-video rows tall. The row
+// height scale lives in the grid container's --row-h variable.
+export function MediaCard({ item, onOpen, pack, uniformGrid = false }: Props) {
const isVideo = item.kind === "video";
const KindIcon = isVideo ? Film : ImageIcon;
+ const ratio = uniformGrid ? tileRatio(item) : layoutRatio(item);
+ const videoRef = useRef<HTMLVideoElement>(null);
+
+ // Every clip plays while it's on screen and pauses off screen.
+ useEffect(() => {
+ const video = videoRef.current;
+ if (!video) return;
+ const observer = new IntersectionObserver(
+ ([entry]) => {
+ if (entry.isIntersecting) video.play().catch(() => {});
+ else video.pause();
+ },
+ { threshold: 0.35 },
+ );
+ observer.observe(video);
+ return () => observer.disconnect();
+ }, []);
return (
<button
- className="group relative mb-5 block w-full break-inside-avoid overflow-hidden rounded-2xl border-2 border-ink text-left"
+ className={cn(
+ "group relative block overflow-hidden rounded-2xl border-2 border-ink text-left",
+ uniformGrid &&
+ "grow basis-[calc(50%-8px)] md:basis-[calc(25%-12px)] 2xl:basis-[calc(20%-13px)]",
+ )}
onClick={() => onOpen(item)}
+ style={{
+ aspectRatio: String(ratio),
+ ...(uniformGrid
+ ? {}
+ : pack
+ ? { flexGrow: pack.grow, flexBasis: pack.basis, minWidth: 0 }
+ : {
+ flexGrow: ratio * 100,
+ flexBasis: `calc(var(--row-h) * ${ratio})`,
+ }),
+ }}
type="button"
>
- <div className={cn("relative w-full", aspectClass[item.aspect])}>
- <Image
- alt={item.title}
- className="h-full w-full object-cover transition-transform duration-300 group-hover:scale-[1.03]"
- fill
- sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 33vw"
- src={item.thumbnailSrc}
+ <Image
+ alt={item.title}
+ className="object-cover transition-transform duration-300 group-hover:scale-[1.03]"
+ fill
+ sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 33vw"
+ src={item.thumbnailSrc}
+ />
+
+ {/* Preview clip over the poster image. preload="none" keeps the grid
+ from downloading every clip up front; play() loads on first view. */}
+ {isVideo ? (
+ <video
+ className="absolute inset-0 h-full w-full object-cover"
+ loop
+ muted
+ playsInline
+ preload="none"
+ ref={videoRef}
+ src={item.mediaSrc}
/>
+ ) : null}
- <span className="absolute top-3 left-3 inline-flex items-center gap-1.5 rounded-full border-2 border-ink bg-cream px-2.5 py-1 text-[11px] font-semibold tracking-[0.04em] uppercase">
- <KindIcon size={12} />
- {item.kind}
- </span>
+ <span className="absolute top-3 left-3 inline-flex items-center gap-1.5 rounded-full border-2 border-ink bg-cream px-2.5 py-1 text-[11px] font-semibold tracking-[0.04em] uppercase">
+ <KindIcon size={12} />
+ {item.kind}
+ </span>
- <div className="absolute inset-x-0 bottom-0 flex items-end justify-between gap-2 bg-gradient-to-t from-black/75 via-black/25 to-transparent p-3 pt-8">
- <span className="text-[14px] leading-tight font-semibold text-white">
- {item.title}
- </span>
- <span className="shrink-0 rounded-full bg-white/90 p-1.5 text-ink opacity-0 transition-opacity duration-200 group-hover:opacity-100">
- <Maximize2 size={14} />
- </span>
- </div>
+ <div className="absolute inset-x-0 bottom-0 flex items-end justify-between gap-2 bg-gradient-to-t from-black/75 via-black/25 to-transparent p-3 pt-8">
+ <span className="text-[14px] leading-tight font-semibold text-white">
+ {item.title}
+ </span>
+ <span className="shrink-0 rounded-full bg-white/90 p-1.5 text-ink opacity-0 transition-opacity duration-200 group-hover:opacity-100">
+ <Maximize2 size={14} />
+ </span>
</div>
</button>
);
diff --git a/site/src/app/_components/landing/media-showcase/MediaDetailDialog.tsx b/site/src/app/_components/landing/media-showcase/MediaDetailDialog.tsx
index 6759cd14..de1e2982 100644
--- a/site/src/app/_components/landing/media-showcase/MediaDetailDialog.tsx
+++ b/site/src/app/_components/landing/media-showcase/MediaDetailDialog.tsx
@@ -30,29 +30,40 @@ export function MediaDetailDialog({ item, onClose }: Props) {
if (!open) onClose();
}}
>
- <DialogContent className="flex h-dvh w-screen max-w-none flex-col overflow-hidden rounded-none border-0 bg-cream p-0 text-ink ring-0 md:flex-row">
+ {/* Lightbox: a centered card over a dark backdrop, so clicking outside
+ it dismisses. sm:max-w-none: the base DialogContent sets sm:max-w-sm,
+ which an unprefixed max-w-none loses to at ≥sm widths; gap-0 clears
+ its gap-4 so the panel's border sits flush against the media pane. */}
+ <DialogContent
+ className="flex h-[92dvh] w-[95vw] max-w-none flex-col gap-0 overflow-hidden rounded-2xl border-2 border-ink bg-cream p-0 text-ink ring-0 sm:max-w-none md:flex-row"
+ overlayClassName="bg-black/70"
+ >
{item ? (
<>
- <div className="relative h-[45vh] w-full shrink-0 bg-ink md:h-auto md:w-[68%] md:flex-1">
- {item.kind === "video" ? (
- <video
- autoPlay
- className="absolute inset-0 h-full w-full object-contain"
- controls
- loop
- muted
- playsInline
- src={item.mediaSrc}
- />
- ) : (
- <Image
- alt={item.title}
- className="object-contain"
- fill
- sizes="(max-width: 768px) 100vw, 70vw"
- src={item.mediaSrc}
- />
- )}
+ <div className="flex h-[45vh] w-full shrink-0 items-center justify-center bg-ink md:h-auto md:flex-1">
+ {/* Cap the media's rendered size so it stays near source
+ resolution on huge screens instead of stretching wall to
+ wall; it still shrinks to fit smaller panes. */}
+ <div className="relative h-full max-h-[1080px] w-full max-w-[1440px]">
+ {item.kind === "video" ? (
+ <video
+ autoPlay
+ className="absolute inset-0 h-full w-full object-contain"
+ loop
+ muted
+ playsInline
+ src={item.mediaSrc}
+ />
+ ) : (
+ <Image
+ alt={item.title}
+ className="object-contain"
+ fill
+ sizes="(max-width: 768px) 100vw, 70vw"
+ src={item.mediaSrc}
+ />
+ )}
+ </div>
</div>
<div className="flex w-full flex-1 flex-col gap-5 overflow-y-auto p-6 md:w-[420px] md:max-w-[420px] md:flex-none md:border-l-2 md:border-ink md:p-8 lg:w-[480px] lg:max-w-[480px]">
@@ -100,7 +111,7 @@ export function MediaDetailDialog({ item, onClose }: Props) {
</div>
) : null}
- <div className="mt-auto rounded-xl border-2 border-dashed border-ink/40 bg-white/70 p-4">
+ <div className="mt-auto pt-4">
<p className="text-[14px] leading-[1.5] text-[#333]">
Paste this prompt into Donkey to generate your own.
</p>
diff --git a/site/src/app/_components/landing/media-showcase/MediaShowcase.tsx b/site/src/app/_components/landing/media-showcase/MediaShowcase.tsx
index 43ff0d10..47a4c5f9 100644
--- a/site/src/app/_components/landing/media-showcase/MediaShowcase.tsx
+++ b/site/src/app/_components/landing/media-showcase/MediaShowcase.tsx
@@ -1,14 +1,17 @@
"use client";
-import { useMemo, useState } from "react";
-import { ArrowRight } from "lucide-react";
+import { useEffect, useMemo, useRef, useState } from "react";
-import { Headline, PillButton } from "@/app/_components/landing/LandingPrimitives";
+import { Headline } from "@/app/_components/landing/LandingPrimitives";
import {
CategoryChips,
type CategoryFilter,
} from "@/app/_components/landing/media-showcase/CategoryChips";
-import { MediaCard } from "@/app/_components/landing/media-showcase/MediaCard";
+import {
+ layoutRatio,
+ MediaCard,
+ tileRatio,
+} from "@/app/_components/landing/media-showcase/MediaCard";
import { MediaDetailDialog } from "@/app/_components/landing/media-showcase/MediaDetailDialog";
import {
getItemsByCategory,
@@ -16,43 +19,115 @@ import {
} from "@/app/_components/landing/media-showcase/data";
type Props = {
- // Section heading, e.g. "Media Donkey can make".
+ // Section heading, e.g. "Donkey can make".
heading: string;
blurb?: string;
- // Cap the number of tiles shown (homepage teaser). Omit to show all.
- limit?: number;
- // When set, renders a "See all" pill (homepage → /use-cases).
- viewAllHref?: string;
};
+const GAP = 16;
+
+// Justified row packing: balance the items (in order) into rows whose heights
+// land near a clamped target, so tiles never degenerate-stretch far past it.
+// Row count comes from how many target-height rows the items naturally fill;
+// a small DP then splits the items into that many contiguous rows with the
+// most even ratio sums, so no row ends up starved and over-stretched.
+function packRows(
+ items: MediaShowcaseItem[],
+ width: number,
+): { rows: MediaShowcaseItem[][]; targetHeight: number } {
+ const targetHeight = Math.min(470, Math.max(240, width / 4.8));
+ const ratios = items.map(layoutRatio);
+ const naturalWidth = ratios.reduce(
+ (sum, ratio) => sum + ratio * targetHeight + GAP,
+ 0,
+ );
+ const rowCount = Math.min(
+ items.length,
+ Math.max(1, Math.round(naturalWidth / width)),
+ );
+ if (rowCount === 1) return { rows: [items], targetHeight };
+
+ const prefix = [0];
+ for (const ratio of ratios) prefix.push(prefix[prefix.length - 1] + ratio);
+ const rangeRatio = (from: number, to: number) => prefix[to] - prefix[from];
+ const targetPerRow = prefix[items.length] / rowCount;
+
+ // cost[k][i] = best squared deviation splitting the first i items into k
+ // rows; split[k][i] remembers where row k starts.
+ const cost = Array.from({ length: rowCount + 1 }, () =>
+ new Array<number>(items.length + 1).fill(Infinity),
+ );
+ const split = Array.from({ length: rowCount + 1 }, () =>
+ new Array<number>(items.length + 1).fill(0),
+ );
+ cost[0][0] = 0;
+ for (let k = 1; k <= rowCount; k += 1) {
+ for (let end = k; end <= items.length; end += 1) {
+ for (let start = k - 1; start < end; start += 1) {
+ if (cost[k - 1][start] === Infinity) continue;
+ const candidate =
+ cost[k - 1][start] + (rangeRatio(start, end) - targetPerRow) ** 2;
+ if (candidate < cost[k][end]) {
+ cost[k][end] = candidate;
+ split[k][end] = start;
+ }
+ }
+ }
+ }
+
+ const bounds: number[] = [];
+ let end = items.length;
+ for (let k = rowCount; k >= 1; k -= 1) {
+ bounds.unshift(end);
+ end = split[k][end];
+ }
+ const rows: MediaShowcaseItem[][] = [];
+ let start = 0;
+ for (const bound of bounds) {
+ rows.push(items.slice(start, bound));
+ start = bound;
+ }
+ return { rows, targetHeight };
+}
+
// Media-generation showcase: filterable grid of example images/videos. Clicking
// a tile reveals its prompt in a dialog with a copy-to-clipboard action.
-export function MediaShowcase({ blurb, heading, limit, viewAllHref }: Props) {
+export function MediaShowcase({ blurb, heading }: Props) {
const [category, setCategory] = useState<CategoryFilter>("All");
const [selected, setSelected] = useState<MediaShowcaseItem | null>(null);
+ const gridRef = useRef<HTMLDivElement>(null);
+ const [gridWidth, setGridWidth] = useState<number | null>(null);
+
+ useEffect(() => {
+ const grid = gridRef.current;
+ if (!grid) return;
+ const observer = new ResizeObserver(([entry]) =>
+ setGridWidth(entry.contentRect.width),
+ );
+ observer.observe(grid);
+ return () => observer.disconnect();
+ }, []);
+
+ const items = useMemo(() => getItemsByCategory(category), [category]);
- const items = useMemo(() => {
- const filtered = getItemsByCategory(category);
- return limit ? filtered.slice(0, limit) : filtered;
- }, [category, limit]);
+ // An all-vertical selection (e.g. UGC & Ads) renders as a uniform column
+ // grid instead of justified rows, so its clips split into balanced rows.
+ const uniformGrid =
+ items.length > 0 && items.every((item) => tileRatio(item) < 1);
+
+ const packed =
+ !uniformGrid && gridWidth ? packRows(items, gridWidth) : null;
+ const totalRatio = items.reduce((sum, item) => sum + layoutRatio(item), 0);
return (
<section className="w-full py-16 md:py-24">
<div className="mx-auto w-full max-w-[1400px] px-6 md:px-12">
- <div className="flex flex-col gap-6 md:flex-row md:items-end md:justify-between">
- <div className="max-w-[720px]">
- <Headline size="lg">{heading}</Headline>
- {blurb ? (
- <p className="mt-5 text-[17px] leading-[1.55] text-[#454545] md:text-[19px]">
- {blurb}
- </p>
- ) : null}
- </div>
- {viewAllHref ? (
- <PillButton href={viewAllHref} variant="secondary">
- See all use cases
- <ArrowRight size={16} />
- </PillButton>
+ <div className="max-w-[720px]">
+ <Headline size="lg">{heading}</Headline>
+ {blurb ? (
+ <p className="mt-5 text-[17px] leading-[1.55] text-[#454545] md:text-[19px]">
+ {blurb}
+ </p>
) : null}
</div>
@@ -61,11 +136,57 @@ export function MediaShowcase({ blurb, heading, limit, viewAllHref }: Props) {
</div>
</div>
- {/* The grid alone bleeds to the viewport edge; heading and chips stay in the page container. */}
- <div className="mt-8 columns-1 gap-5 px-6 sm:columns-2 md:px-12 lg:columns-3 xl:columns-4 2xl:columns-5">
- {items.map((item) => (
- <MediaCard item={item} key={item.id} onOpen={setSelected} />
- ))}
+ {/* The mosaic alone bleeds to the viewport edge; heading and chips stay
+ in the page container. */}
+ <div className="mt-8 px-6 md:px-12" ref={gridRef}>
+ {packed ? (
+ <div className="flex flex-col gap-4">
+ {packed.rows.map((row, rowIndex) => {
+ const rowRatio = row.reduce(
+ (sum, item) => sum + layoutRatio(item),
+ 0,
+ );
+ // Stretch rows to fill the full width, except a sparse final
+ // row, whose tiles keep their natural target size instead of
+ // blowing up.
+ const stretch =
+ rowIndex < packed.rows.length - 1 ||
+ rowRatio >= 0.6 * (totalRatio / packed.rows.length);
+ return (
+ <div className="flex gap-4" key={row[0].id}>
+ {row.map((item) => (
+ <MediaCard
+ item={item}
+ key={item.id}
+ onOpen={setSelected}
+ pack={
+ stretch
+ ? { grow: layoutRatio(item), basis: "0px" }
+ : {
+ grow: 0,
+ basis: `${Math.round(layoutRatio(item) * packed.targetHeight)}px`,
+ }
+ }
+ />
+ ))}
+ </div>
+ );
+ })}
+ </div>
+ ) : (
+ // Uniform column grid for all-vertical sets, and the pre-measure
+ // fallback frame for the packed layout's first paint.
+ <div className="flex flex-wrap gap-4 [--row-h:180px] after:grow-[9999] after:content-[''] sm:[--row-h:220px] lg:[--row-h:260px] xl:[--row-h:320px] 2xl:[--row-h:400px]">
+ {items.map((item) => (
+ <MediaCard
+ item={item}
+ key={item.id}
+ onOpen={setSelected}
+ uniformGrid={uniformGrid}
+ />
+ ))}
+ </div>
+ )}
</div>
<MediaDetailDialog item={selected} onClose={() => setSelected(null)} />
diff --git a/site/src/components/ui/dialog.tsx b/site/src/components/ui/dialog.tsx
index 014f5aa3..03d0c60f 100644
--- a/site/src/components/ui/dialog.tsx
+++ b/site/src/components/ui/dialog.tsx
@@ -42,14 +42,16 @@ function DialogOverlay({
function DialogContent({
className,
children,
+ overlayClassName,
showCloseButton = true,
...props
}: DialogPrimitive.Popup.Props & {
+ overlayClassName?: string
showCloseButton?: boolean
}) {
return (
<DialogPortal>
- <DialogOverlay />
+ <DialogOverlay className={overlayClassName} />
<DialogPrimitive.Popup
data-slot="dialog-content"
className={cn(