Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

13.7 KB
Raw Blame
"use client";

import { useDetectPlatform } from "hooks/useDetectPlatform";
import { useCallback, useEffect, useRef, useState } from "react";
import {
	InstantIcon,
	ScreenshotIcon,
	StudioIcon,
} from "@/components/pages/HomePage/modeIcons";
import { MacCursor, WindowsCursor } from "./cursors";
import { H_HERO, INK, MODE_THEME, type ModeKey } from "./theme";

type SlideKey = ModeKey | "all";

type Slide = {
	key: SlideKey;
	/** The end of "The open source screen recorder for …". */
	tail: string;
};

const SLIDES: Slide[] = [
	{ key: "all", tail: "everyone." },
	{ key: "instant", tail: "quick shares." },
	{ key: "studio", tail: "polished videos." },
	{ key: "screenshot", tail: "screenshots, too." },
];

const NUDGE = "Click me \u{1F440}";
const NUDGE_TOUCH = "Tap me \u{1F440}";
const TOUCH_QUERY = "(max-width: 767px)";
const TYPE_MS = 26;

const NUDGE_AT = 520;
const TYPE_AT = NUDGE_AT + 260;

type PillKey = Extract<ModeKey, "instant" | "studio" | "screenshot">;

const PILLS: { key: PillKey; label: string; short: string; slide: number }[] = [
	{ key: "instant", label: "Instant Mode", short: "Instant", slide: 1 },
	{ key: "studio", label: "Studio Mode", short: "Studio", slide: 2 },
	{
		key: "screenshot",
		label: "Screenshot Mode",
		short: "Screenshot",
		slide: 3,
	},
];

const PILL_ICON = {
	instant: InstantIcon,
	studio: StudioIcon,
	screenshot: ScreenshotIcon,
} as const;

const STRIKE_MS = 260;

const HOLD_MS = 110;

const LIFT_MS = 260;

const RISE_MS = 520;

const LIFT_AT = STRIKE_MS + HOLD_MS;
const RISE_AT = LIFT_AT + 150;
const WIDTH_AT = LIFT_AT + 60;

type Departing = { text: string; color: string; id: number };

type Swap = { text: string; gen: number; from: Departing | null };

const Slot = ({
	text,
	color,
	strike,
	delay,
	animate,
}: {
	text: string;

	color: string;

	strike: string;
	delay: number;
	animate: boolean;
}) => {
	const ghostRef = useRef<HTMLSpanElement>(null);
	const [width, setWidth] = useState<number>();
	// The swap has to be derived during render, not in an effect: the exit
	// layer, the remount key and the enter animation all have to appear in
	// the same commit as the new word, or the replacement pops in unanimated.
	const [swap, setSwap] = useState<Swap>(() => ({
		text,
		gen: 0,
		from: null,
	}));

	const paintedColor = useRef(color);

	if (swap.text !== text) {
		setSwap({
			text,
			gen: swap.gen + 1,

			from: animate
				? { text: swap.text, color: paintedColor.current, id: swap.gen + 1 }
				: null,
		});
	}

	useEffect(() => {
		paintedColor.current = color;
	}, [color]);

	const measure = useCallback(() => {
		const el = ghostRef.current;
		if (el) setWidth(el.getBoundingClientRect().width);
	}, []);

	// The ghost is in flow and invisible, so it always carries the natural
	// width of whatever the slot is currently showing. Remeasure when the
	// word, the loaded fonts, or the clamped font size changes.
	// biome-ignore lint/correctness/useExhaustiveDependencies: remeasure on word change
	useEffect(() => {
		measure();
	}, [measure, text]);

	useEffect(() => {
		window.addEventListener("resize", measure);
		document.fonts?.ready.then(measure).catch(() => {});
		return () => window.removeEventListener("resize", measure);
	}, [measure]);

	// Retire the struck word once it has finished lifting. The enter class is
	// left in place (it fills forwards) so the timer can't clip the rise.
	const departingId = swap.from?.id;
	useEffect(() => {
		if (departingId === undefined) return;
		const timer = window.setTimeout(
			() =>
				setSwap((s) => (s.from?.id === departingId ? { ...s, from: null } : s)),
			delay + LIFT_AT + LIFT_MS + 40,
		);
		return () => window.clearTimeout(timer);
	}, [departingId, delay]);

	const entering = animate && swap.gen > 0;

	return (
		<span
			className="ht-slot"
			style={{
				width,
				color,
				transitionDelay: `${delay + WIDTH_AT}ms`,
			}}
		>
			<span ref={ghostRef} className="ht-slot-ghost" aria-hidden="true">
				{text}
			</span>

			{swap.from ? (
				<span key={`out-${swap.from.id}`} className="ht-layer">
					<span
						className="ht-out"
						style={{
							color: swap.from.color,
							animationDelay: `${delay + LIFT_AT}ms`,
						}}
					>
						{swap.from.text}
						<span
							className="ht-strike"
							style={{ background: strike, animationDelay: `${delay}ms` }}
						/>
					</span>
				</span>
			) : null}

			<span key={`in-${swap.gen}`} className="ht-layer">
				<span
					className={entering ? "ht-in" : undefined}
					style={
						entering ? { animationDelay: `${delay + RISE_AT}ms` } : undefined
					}
				>
					{text}
				</span>
			</span>
		</span>
	);
};

export const HeroHeadline = () => {
	const { platform } = useDetectPlatform();
	const [index, setIndex] = useState(0);
	const [typed, setTyped] = useState(0);
	const [nudgeText, setNudgeText] = useState(NUDGE);
	const [nudge, setNudge] = useState(false);
	const [asked, setAsked] = useState(true);
	const [reduced, setReduced] = useState(false);

	const Arrow = platform === "windows" ? WindowsCursor : MacCursor;

	useEffect(() => {
		const query = window.matchMedia("(prefers-reduced-motion: reduce)");
		const sync = () => setReduced(query.matches);
		sync();
		query.addEventListener("change", sync);
		return () => query.removeEventListener("change", sync);
	}, []);

	useEffect(() => {
		const text = window.matchMedia(TOUCH_QUERY).matches ? NUDGE_TOUCH : NUDGE;
		setNudgeText(text);
		if (reduced) {
			setNudge(true);
			setTyped(text.length);
			return;
		}
		const lean = window.setTimeout(() => setNudge(true), NUDGE_AT);
		let tick = 0;
		const start = window.setTimeout(() => {
			tick = window.setInterval(() => {
				setTyped((n) => {
					if (n + 1 >= text.length) window.clearInterval(tick);
					return n + 1;
				});
			}, TYPE_MS);
		}, TYPE_AT);
		return () => {
			window.clearTimeout(lean);
			window.clearTimeout(start);
			window.clearInterval(tick);
		};
	}, [reduced]);

	const slide = SLIDES[index] ?? SLIDES[0];
	if (!slide) return null;
	const theme = slide.key === "all" ? null : MODE_THEME[slide.key];
	const accent = theme ? theme.glyph : INK;

	const pick = (target: number) => {
		setIndex((current) => (current === target ? 0 : target));

		setAsked(false);
	};

	return (
		<div className="flex w-full flex-col items-center">
			<style href="ht-hero-headline" precedence="default">
				{CSS}
			</style>

			<div className="ht-rise relative pb-10 [--d:0.08s] md:pb-14">
				<div className="mx-auto flex w-fit flex-nowrap items-center justify-center gap-0.5 rounded-full border border-[#DDE4EB] bg-white/70 p-1 shadow-[0_1px_2px_rgba(17,17,17,0.04)]">
					{PILLS.map((pill) => {
						const active = index === pill.slide;
						const pillTheme = MODE_THEME[pill.key];
						const Icon = PILL_ICON[pill.key];
						return (
							<button
								key={pill.key}
								type="button"
								onClick={() => pick(pill.slide)}
								aria-pressed={active}
								className={`flex h-8 items-center gap-1.5 whitespace-nowrap rounded-full px-2.5 transition-colors duration-300 sm:px-3 ${
									active
										? "text-[#111111]"
										: "text-[rgba(17,17,17,0.5)] hover:bg-[#EDF1F6] hover:text-[#111111]"
								} focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111] focus-visible:ring-offset-1 focus-visible:ring-offset-white`}
								style={active ? { background: pillTheme.chip } : undefined}
							>
								<Icon className="size-3.5" />
								<span className="text-[13px] font-medium leading-none">
									<span className="sm:hidden">{pill.short}</span>
									<span className="hidden sm:inline">{pill.label}</span>
								</span>
							</button>
						);
					})}
				</div>

				<span
					aria-hidden="true"
					className={`ht-nudge ${nudge && asked ? "is-on" : ""}`}
				>
					<span className="ht-nudge-float">
						{/* Width reserved up front so each line types out from a fixed
						    left edge instead of walking as it grows. */}
						<span className={`ht-nudge-chip ${typed > 0 ? "is-typed" : ""}`}>
							<span className="invisible">{nudgeText}</span>
							<span className="absolute inset-0 flex items-center px-[10px]">
								{nudgeText.slice(0, typed)}
								{typed < nudgeText.length ? (
									<span className="ht-caret" />
								) : null}
							</span>
						</span>
						<Arrow className="ht-nudge-arrow" />
					</span>
				</span>
			</div>

			<h1
				className={`ht-rise ht-rise-soft [--d:0.1s] ${H_HERO} max-w-[15ch] text-balance text-[clamp(34px,4.4vw,60px)] sm:max-w-none`}
			>
				<span className="sr-only">
					The open source screen recorder for everyone. Cap has three recording
					modes: Instant, Studio, and Screenshot.
				</span>
				<span aria-hidden="true" className="ht-headline block">
					<span className="block">The open source screen recorder</span>
					{/* The line that changes with the mode, in the subheading's
					    serif so the short line reads as deliberate. */}
					<span className="ht-tail block">
						for{" "}
						<Slot
							text={slide.tail}
							color={accent}
							strike={accent}
							delay={0}
							animate={!reduced}
						/>
					</span>
				</span>
			</h1>
		</div>
	);
};

const CSS = `
/* The hero's entrance, staggered by --d on each piece. */
.ht-rise { animation: ht-rise 0.7s cubic-bezier(0.16, 1, 0.3, 1) var(--d, 0s) both; }
.ht-rise-soft { animation-name: ht-rise-soft; animation-duration: 0.8s; }
@keyframes ht-rise {
	from { opacity: 0; transform: translate3d(0, 14px, 0); }
	to { opacity: 1; transform: none; }
}
@keyframes ht-rise-soft {
	from { opacity: 0; transform: translate3d(0, 18px, 0); filter: blur(10px); }
	to { opacity: 1; transform: none; filter: blur(0); }
}

/* The site's base sheet pins spans to line-height 1.5rem, which would
   collapse the slot line boxes. Restore the H_HERO leading. */
.ht-headline, .ht-headline span { line-height: 0.98; }
.ht-tail {
	margin-top: 0.06em;
	font-family: var(--font-ht-serif), Georgia, serif;
	font-style: italic;
	font-weight: 400;
	font-size: 1.06em;
	letter-spacing: -0.025em;
	white-space: nowrap;
}
/* Never let "for" and the ending split across lines: on narrow phones the
   line shrinks until the longest ending ("for screenshots, too.", about
   8.6em wide) fits inside the hero's padding. */
@media (max-width: 639px) {
	.ht-tail { font-size: min(1.06em, calc((100vw - 64px) / 8.8)); }
}

.ht-slot {
	position: relative;
	display: inline-block;
	white-space: nowrap;
	vertical-align: top;
	transition: width 560ms cubic-bezier(0.22, 0.9, 0.24, 1),
		color 1ms linear;
}
.ht-slot-ghost { visibility: hidden; }
.ht-layer {
	position: absolute;
	inset: 0;
	display: flex;
	align-items: flex-start;
	justify-content: center;
	white-space: nowrap;
	pointer-events: none;
}
.ht-layer > span { position: relative; display: inline-block; }


.ht-strike {
	position: absolute;
	left: -0.04em;
	right: -0.04em;
	top: 0.54em;
	height: 0.055em;
	border-radius: 999px;
	transform-origin: left center;
	animation: ht-strike ${STRIKE_MS}ms cubic-bezier(0.5, 0, 0.2, 1) both;
}

@keyframes ht-strike { from { transform: scaleX(0); } to { transform: scaleX(1); } }
@keyframes ht-out {
	from { opacity: 1; transform: translate3d(0, 0, 0); }
	to { opacity: 0; transform: translate3d(0, -0.55em, 0); }
}
@keyframes ht-in {
	from { opacity: 0; transform: translate3d(0, 0.5em, 0); }
	to { opacity: 1; transform: translate3d(0, 0, 0); }
}
.ht-out { animation: ht-out ${LIFT_MS}ms cubic-bezier(0.4, 0, 0.75, 0.2) both; }
.ht-in { animation: ht-in ${RISE_MS}ms cubic-bezier(0.18, 0.9, 0.22, 1) both; }


.ht-caret {
	display: inline-block;
	width: 1.5px;
	height: 1em;
	margin-left: 2px;
	vertical-align: -0.12em;
	background: rgba(255, 255, 255, 0.85);
	animation: ht-blink 1s steps(1, end) infinite;
}
@keyframes ht-blink { 0%, 50% { opacity: 1; } 50.01%, 100% { opacity: 0; } }


.ht-nudge {
	display: block;
	position: absolute;
	left: 6%;
	right: auto;
	top: 46px;
	z-index: 20;
	pointer-events: none;
	opacity: 0;
	transform: translate3d(9px, 7px, 0) scale(0.94);
	transform-origin: right center;
	transition: opacity 260ms ease,
		transform 340ms cubic-bezier(0.18, 0.9, 0.22, 1);
}
.ht-nudge.is-on {
	opacity: 1;
	transform: translate3d(0, 0, 0) scale(1);
}

.ht-nudge-float {
	display: flex;
	align-items: flex-start;
	gap: 7px;
}
.ht-nudge.is-on .ht-nudge-float {
	animation: ht-nudge-float 3.2s ease-in-out 360ms infinite;
}
@keyframes ht-nudge-float {
	0%, 100% { transform: translate3d(0, 0, 0); }
	50% { transform: translate3d(-3px, -3px, 0); }
}

.ht-nudge-arrow {
	display: none;
	flex: none;
	width: 17px;
	height: auto;
	filter: drop-shadow(0 2px 5px rgba(17, 17, 17, 0.28));
}

.ht-nudge-chip {
	position: relative;
	display: block;
	margin-top: 0;
	white-space: nowrap;
	border-radius: 13px;
	background: linear-gradient(180deg, #2f95ff 0%, #0a7cff 100%);
	padding: 5px 10px;
	font-size: 10.5px;
	font-weight: 500;
	letter-spacing: -0.005em;
	line-height: 1;
	color: #ffffff;
	box-shadow: 0 1px 1px rgba(10, 124, 255, 0.25),
		0 8px 18px -9px rgba(10, 124, 255, 0.55);
	opacity: 0;
	transition: opacity 170ms ease;
}
.ht-nudge-chip::before {
	content: "";
	position: absolute;
	left: 16px;
	top: -3px;
	width: 8px;
	height: 8px;
	border-radius: 2px;
	background: linear-gradient(180deg, #2f95ff 0%, #0a7cff 100%);
	transform: rotate(45deg);
}
/* Only ever seen with type in it, so the swap between the two lines resizes
   the chip while it is invisible. */
.ht-nudge-chip.is-typed { opacity: 1; }
@media (min-width: 768px) {
	.ht-nudge { left: auto; right: 80%; top: 26px; }
	.ht-nudge-arrow { display: block; }
	.ht-nudge-chip { margin-top: 14px; }
	.ht-nudge-chip::before { left: auto; right: -3px; top: 9px; }
}
@media (min-width: 1024px) { .ht-nudge { right: 94%; } }

@media (prefers-reduced-motion: reduce) {
	.ht-rise { animation: none; }
	.ht-slot { transition: none; }
	.ht-nudge, .ht-nudge-chip { transition: none; }
	.ht-strike, .ht-out, .ht-in, .ht-caret { animation: none; }
	.ht-nudge.is-on .ht-nudge-float { animation: none; }
}
`;