Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): one transport bar for every in-app player

David Leb9a17798f70f
diff --git a/site/src/cut/components/Lightbox.tsx b/site/src/cut/components/Lightbox.tsx
index f6e1bab4..3d208b86 100644
--- a/site/src/cut/components/Lightbox.tsx
+++ b/site/src/cut/components/Lightbox.tsx
@@ -17,6 +17,8 @@ import {
SPECIMEN_INK,
} from "@/cut/lib/fontSpecimen";
import { PeakStrip } from "./AudioPanel";
+import { MediaTransport } from "./MediaTransport";
+import { cn } from "@/lib/utils";
// The asset lightbox: the big version of a stock, generated, or chat asset
// floating straight on the backdrop — media on top, name and prompt below,
@@ -206,19 +208,7 @@ function LightboxMedia({
const mediaStyle = ratio ? { aspectRatio: ratio } : undefined;
if (item.kind === "video") {
- return (
- <video
- crossOrigin={MEDIA_CORS}
- controls
- autoPlay
- loop
- playsInline
- poster={item.poster}
- src={item.src}
- className={mediaClass}
- style={mediaStyle}
- />
- );
+ return <VideoBody item={item} ratio={ratio} style={mediaStyle} />;
}
return (
// eslint-disable-next-line @next/next/no-img-element -- static/project image, client-only page
@@ -232,11 +222,91 @@ function LightboxMedia({
);
}
+/** The big video: the picture with the app's own transport floating on it,
+ * revealed on hover and held up while it is paused. */
+function VideoBody({
+ item,
+ ratio,
+ style,
+}: {
+ item: LightboxItem;
+ ratio?: number;
+ style?: { aspectRatio: number };
+}) {
+ const videoRef = useRef<HTMLVideoElement>(null);
+ // Playing is what the element reports, never what was asked of it: an
+ // autoplay the browser blocks fires no `pause`, and a transport that
+ // assumed it started would hide itself behind a Pause icon over a still
+ // picture. The `play` event turns it on.
+ const [playing, setPlaying] = useState(false);
+ const [time, setTime] = useState(0);
+ const [duration, setDuration] = useState(0);
+ const [muted, setMuted] = useState(false);
+
+ const toggle = () => {
+ const el = videoRef.current;
+ if (!el) return;
+ if (el.paused) void el.play();
+ else el.pause();
+ };
+
+ return (
+ <div
+ className={cn(
+ "group relative overflow-hidden rounded-2xl bg-black shadow-2xl",
+ ratio ? "w-full" : "max-h-[68vh] w-full"
+ )}
+ style={style}
+ >
+ <video
+ ref={videoRef}
+ crossOrigin={MEDIA_CORS}
+ autoPlay
+ loop
+ playsInline
+ muted={muted}
+ // The browser's own control bar carries chrome of its own — download,
+ // playback speed, picture-in-picture — so the lightbox draws the
+ // transport itself and keeps picture-in-picture out of the context menu.
+ disablePictureInPicture
+ poster={item.poster}
+ src={item.src}
+ className={cn(
+ "block w-full",
+ ratio ? "size-full object-cover" : "max-h-[68vh] object-contain"
+ )}
+ onClick={toggle}
+ onPlay={() => setPlaying(true)}
+ onPause={() => setPlaying(false)}
+ onTimeUpdate={(e) => setTime(e.currentTarget.currentTime)}
+ onLoadedMetadata={(e) => setDuration(e.currentTarget.duration || 0)}
+ />
+ <MediaTransport
+ playing={playing}
+ time={time}
+ duration={duration}
+ muted={muted}
+ onToggle={toggle}
+ onSeek={(t) => {
+ const el = videoRef.current;
+ if (!el) return;
+ el.currentTime = t;
+ setTime(t);
+ }}
+ onToggleMute={() => setMuted((m) => !m)}
+ />
+ </div>
+ );
+}
+
function AudioBody({ item }: { item: LightboxItem }) {
const asset = useEditor((s) =>
item.assetId ? s.assets.find((a) => a.id === item.assetId) : undefined,
);
const audioEl = useRef<HTMLAudioElement>(null);
+ const [playing, setPlaying] = useState(false);
+ const [time, setTime] = useState(0);
+ const [duration, setDuration] = useState(0);
// The big player takes over from any row/card preview, and closing the
// lightbox stops it — a detached media element can keep playing otherwise.
// The element renders with the body, so it exists by the time this runs.
@@ -253,10 +323,32 @@ function AudioBody({ item }: { item: LightboxItem }) {
)}
<audio
ref={audioEl}
- controls
autoPlay
src={item.src}
- className="w-full"
+ className="hidden"
+ onPlay={() => setPlaying(true)}
+ onPause={() => setPlaying(false)}
+ onTimeUpdate={(e) => setTime(e.currentTarget.currentTime)}
+ onLoadedMetadata={(e) => setDuration(e.currentTarget.duration || 0)}
+ onEnded={() => setPlaying(false)}
+ />
+ <MediaTransport
+ variant="inline"
+ playing={playing}
+ time={time}
+ duration={duration}
+ onToggle={() => {
+ const el = audioEl.current;
+ if (!el) return;
+ if (el.paused) void el.play();
+ else el.pause();
+ }}
+ onSeek={(t) => {
+ const el = audioEl.current;
+ if (!el) return;
+ el.currentTime = t;
+ setTime(t);
+ }}
/>
</div>
);
diff --git a/site/src/cut/components/MediaTransport.tsx b/site/src/cut/components/MediaTransport.tsx
new file mode 100644
index 00000000..3f21accc
--- /dev/null
+++ b/site/src/cut/components/MediaTransport.tsx
@@ -0,0 +1,126 @@
+"use client";
+
+import { useRef, useState } from "react";
+import { Pause, Play, Volume2, VolumeX } from "lucide-react";
+import { cn } from "@/lib/utils";
+import { formatElapsed } from "@/cut/lib/time";
+
+/**
+ * The transport every in-app player wears: play/pause, a draggable scrub bar,
+ * the clock, and an optional mute toggle. Media elements in the editor render
+ * with `controls` off and this bar on top, so playback carries the app's own
+ * chrome wherever it appears.
+ *
+ * `overlay` floats it over the picture on a scrim — the parent carries
+ * `group` and `relative`, and the bar shows on hover, on focus, while a scrub
+ * is in flight, and whenever the player is paused. `inline` draws a plain row
+ * wherever it is placed.
+ */
+export function MediaTransport({
+ playing,
+ time,
+ duration,
+ muted,
+ variant = "overlay",
+ className,
+ onToggle,
+ onSeek,
+ onToggleMute,
+}: {
+ playing: boolean;
+ time: number;
+ duration: number;
+ /** Pass alongside `onToggleMute` to draw the volume button. */
+ muted?: boolean;
+ variant?: "overlay" | "inline";
+ className?: string;
+ onToggle: () => void;
+ onSeek: (t: number) => void;
+ onToggleMute?: () => void;
+}) {
+ const barRef = useRef<HTMLDivElement>(null);
+ const [scrubbing, setScrubbing] = useState(false);
+ const pct = duration > 0 ? Math.min(1, Math.max(0, time / duration)) * 100 : 0;
+
+ const seekFrom = (clientX: number) => {
+ const el = barRef.current;
+ if (!el || duration <= 0) return;
+ const r = el.getBoundingClientRect();
+ onSeek(Math.min(1, Math.max(0, (clientX - r.left) / r.width)) * duration);
+ };
+
+ return (
+ <div
+ className={cn(
+ "z-20 flex items-center gap-3",
+ variant === "overlay"
+ ? [
+ "absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/75 via-black/45 to-transparent px-4 pt-10 pb-3.5",
+ "opacity-0 transition-opacity duration-150 group-hover:opacity-100 focus-within:opacity-100",
+ (scrubbing || !playing) && "opacity-100",
+ ]
+ : "px-0.5",
+ className
+ )}
+ >
+ <button
+ type="button"
+ className="grid size-8 shrink-0 place-items-center rounded-full bg-white/15 text-white backdrop-blur-sm transition-colors hover:bg-white/25"
+ aria-label={playing ? "Pause" : "Play"}
+ title={playing ? "Pause" : "Play"}
+ onClick={onToggle}
+ >
+ {playing ? <Pause className="size-4 fill-white" /> : <Play className="size-4 fill-white" />}
+ </button>
+
+ <div
+ ref={barRef}
+ className="relative h-4 flex-1 cursor-pointer touch-none"
+ role="slider"
+ tabIndex={0}
+ aria-label="Seek"
+ aria-valuemin={0}
+ aria-valuemax={Math.max(0, duration)}
+ aria-valuenow={time}
+ onPointerDown={(e) => {
+ e.currentTarget.setPointerCapture(e.pointerId);
+ setScrubbing(true);
+ seekFrom(e.clientX);
+ }}
+ onPointerMove={(e) => scrubbing && seekFrom(e.clientX)}
+ onPointerUp={() => setScrubbing(false)}
+ onPointerCancel={() => setScrubbing(false)}
+ onKeyDown={(e) => {
+ if (e.key === "ArrowLeft") onSeek(Math.max(0, time - 1));
+ else if (e.key === "ArrowRight") onSeek(Math.min(duration, time + 1));
+ else return;
+ e.preventDefault();
+ }}
+ >
+ <div className="absolute inset-x-0 top-1/2 h-1 -translate-y-1/2 overflow-hidden rounded-full bg-white/30">
+ <div className="h-full rounded-full bg-white" style={{ width: `${pct}%` }} />
+ </div>
+ <span
+ className="pointer-events-none absolute top-1/2 size-3 -translate-x-1/2 -translate-y-1/2 rounded-full bg-white shadow-[0_1px_4px_rgba(0,0,0,0.5)]"
+ style={{ left: `${pct}%` }}
+ />
+ </div>
+
+ <span className="shrink-0 text-[11px] font-medium text-white/90 tabular-nums">
+ {formatElapsed(time * 1000)} / {formatElapsed(duration * 1000)}
+ </span>
+
+ {onToggleMute && (
+ <button
+ type="button"
+ className="grid size-7 shrink-0 place-items-center rounded-full text-white/85 transition-colors hover:bg-white/15 hover:text-white"
+ aria-label={muted ? "Unmute" : "Mute"}
+ title={muted ? "Unmute" : "Mute"}
+ onClick={onToggleMute}
+ >
+ {muted ? <VolumeX className="size-3.5" /> : <Volume2 className="size-3.5" />}
+ </button>
+ )}
+ </div>
+ );
+}
diff --git a/site/src/cut/components/PlatformPreview.tsx b/site/src/cut/components/PlatformPreview.tsx
index 03634349..8ef00cb0 100644
--- a/site/src/cut/components/PlatformPreview.tsx
+++ b/site/src/cut/components/PlatformPreview.tsx
@@ -10,8 +10,6 @@ import {
MessageCircle,
MoreHorizontal,
Music2,
- Pause,
- Play,
Search,
Send,
Share2,
@@ -23,7 +21,7 @@ import {
} from "lucide-react";
import { normalizeTags } from "@/cut/lib/publish";
import { useEditor } from "@/cut/lib/store";
-import { formatElapsed } from "@/cut/lib/time";
+import { MediaTransport } from "@/cut/components/MediaTransport";
import { parseRatio } from "@/cut/lib/types";
import { cn } from "@/lib/utils";
@@ -184,7 +182,7 @@ export function PlatformPreviewDialog({
</button>
{original && (
- <Transport
+ <MediaTransport
playing={playing}
time={time}
duration={duration}
@@ -213,89 +211,6 @@ export function PlatformPreviewDialog({
);
}
-/** Hover transport for the Original view: play/pause, a draggable scrub bar,
- * and the clock. It stays up while a drag is in flight so the bar never slips
- * out from under the pointer. */
-function Transport({
- playing,
- time,
- duration,
- onToggle,
- onSeek,
-}: {
- playing: boolean;
- time: number;
- duration: number;
- onToggle: () => void;
- onSeek: (t: number) => void;
-}) {
- const barRef = useRef<HTMLDivElement>(null);
- const [scrubbing, setScrubbing] = useState(false);
- const pct = duration > 0 ? Math.min(1, Math.max(0, time / duration)) * 100 : 0;
-
- const seekFrom = (clientX: number) => {
- const el = barRef.current;
- if (!el || duration <= 0) return;
- const r = el.getBoundingClientRect();
- onSeek(Math.min(1, Math.max(0, (clientX - r.left) / r.width)) * duration);
- };
-
- return (
- <div
- className={cn(
- "absolute inset-x-0 bottom-0 z-20 flex items-center gap-3 bg-gradient-to-t from-black/75 via-black/45 to-transparent px-4 pt-10 pb-3.5 opacity-0 transition-opacity duration-150",
- "group-hover:opacity-100 focus-within:opacity-100",
- scrubbing && "opacity-100"
- )}
- >
- <button
- className="grid size-8 shrink-0 place-items-center rounded-full bg-white/15 text-white backdrop-blur-sm transition-colors hover:bg-white/25"
- aria-label={playing ? "Pause" : "Play"}
- onClick={onToggle}
- >
- {playing ? <Pause className="size-4 fill-white" /> : <Play className="size-4 fill-white" />}
- </button>
-
- <div
- ref={barRef}
- className="relative h-4 flex-1 cursor-pointer touch-none"
- role="slider"
- tabIndex={0}
- aria-label="Seek"
- aria-valuemin={0}
- aria-valuemax={Math.max(0, duration)}
- aria-valuenow={time}
- onPointerDown={(e) => {
- e.currentTarget.setPointerCapture(e.pointerId);
- setScrubbing(true);
- seekFrom(e.clientX);
- }}
- onPointerMove={(e) => scrubbing && seekFrom(e.clientX)}
- onPointerUp={() => setScrubbing(false)}
- onPointerCancel={() => setScrubbing(false)}
- onKeyDown={(e) => {
- if (e.key === "ArrowLeft") onSeek(Math.max(0, time - 1));
- else if (e.key === "ArrowRight") onSeek(Math.min(duration, time + 1));
- else return;
- e.preventDefault();
- }}
- >
- <div className="absolute inset-x-0 top-1/2 h-1 -translate-y-1/2 overflow-hidden rounded-full bg-white/30">
- <div className="h-full rounded-full bg-white" style={{ width: `${pct}%` }} />
- </div>
- <span
- className="pointer-events-none absolute top-1/2 size-3 -translate-x-1/2 -translate-y-1/2 rounded-full bg-white shadow-[0_1px_4px_rgba(0,0,0,0.5)]"
- style={{ left: `${pct}%` }}
- />
- </div>
-
- <span className="shrink-0 text-[11px] font-medium text-white/90 tabular-nums">
- {formatElapsed(time * 1000)} / {formatElapsed(duration * 1000)}
- </span>
- </div>
- );
-}
-
interface ChromeProps {
handle: string;
caption: string;