Skip to content

Follow alertNew

The line-art coffee pour plays through when someone follows. After it finishes, their name types out on a dashed card. The OBS source hears Twitch. The TikTok Live Studio link hears TikTok. Preview loops a sample follow.

"use client";

import { useEffect, useState, type CSSProperties } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";

import { OverlayRoot } from "@/components/stream/overlay-root";
import { useViewerQueue } from "@/components/stream/use-twitch-alerts";
import { STREAM_COPY } from "@/config/stream-copy";
import type { StreamLayout } from "@/config/stream-components";
import { spring } from "@/lib/motion";
import { cn } from "@/lib/utils";

const DEFAULTS = STREAM_COPY["follow-alert"];
const POUR = "/images/stream/coffee-pour.webp";
const POUR_MS = 3300;
const TYPE_MS = 70;
const HOLD_MS = 1600;

type Phase = "idle" | "pour" | "type" | "hold" | "gone";

const night = {
  "--background": "#000000",
  "--foreground": "oklch(0.985 0 0)",
  "--muted-foreground": "oklch(0.708 0 0)",
  "--border": "oklch(1 0 0 / 18%)",
} as CSSProperties;

export function FollowAlert({
  layout,
  preview = false,
  label = DEFAULTS.label,
  username,
  alertKey,
  demo = false,
}: {
  layout: StreamLayout;
  preview?: boolean;
  label?: string;
  username?: string;
  alertKey?: string;
  demo?: boolean;
}) {
  const mobile = layout === "mobile";
  const reduceMotion = useReducedMotion();
  const { name, cycle, finish, replay } = useViewerQueue({
    preview,
    initialName: preview ? username || DEFAULTS.username || "" : username || "",
    demo,
    kind: "follow",
    alertKey,
    platform: layout === "mobile" ? "tiktok" : "twitch",
  });
  const [phase, setPhase] = useState<Phase>(name ? (reduceMotion ? "hold" : "pour") : "idle");
  const [count, setCount] = useState(reduceMotion ? name.length : 0);
  const token = `${cycle}:${name}:${reduceMotion ? "still" : "move"}`;
  const [seen, setSeen] = useState(token);
  if (seen !== token) {
    setSeen(token);
    if (name) {
      setCount(reduceMotion ? name.length : 0);
      setPhase(reduceMotion ? "hold" : "pour");
    }
  }
  useEffect(() => {
    if (phase !== "pour") return;
    const startType = window.setTimeout(() => setPhase("type"), POUR_MS);
    return () => window.clearTimeout(startType);
  }, [phase]);

  useEffect(() => {
    if (phase !== "type") return;
    if (count >= name.length) {
      const hold = window.setTimeout(() => setPhase("hold"), 0);
      return () => window.clearTimeout(hold);
    }
    const step = window.setTimeout(() => setCount((value) => value + 1), TYPE_MS);
    return () => window.clearTimeout(step);
  }, [count, name.length, phase]);

  useEffect(() => {
    if (phase !== "hold") return;
    const hide = window.setTimeout(() => setPhase("gone"), reduceMotion ? HOLD_MS + 800 : HOLD_MS);
    return () => window.clearTimeout(hide);
  }, [phase, reduceMotion]);

  useEffect(() => {
    if (phase !== "gone") return;
    const wait = window.setTimeout(() => {
      if (preview) replay();
      else finish();
    }, preview ? 700 : 400);
    return () => window.clearTimeout(wait);
  }, [finish, phase, preview, replay]);

  const shown = name.slice(0, count);

  return (
    <OverlayRoot>
      <div
        style={night}
        className={cn(
          "absolute left-1/2 -translate-x-1/2 text-foreground",
          mobile ? "top-[46%]" : "top-1/2 -translate-y-1/2",
        )}
      >
        {phase === "pour" ? (
          // eslint-disable-next-line @next/next/no-img-element
          <img
            key={cycle}
            src={POUR}
            alt=""
            width={784}
            height={779}
            draggable={false}
            className={cn(
              "pointer-events-none w-auto select-none",
              mobile ? "h-[64cqw]" : "h-[32cqw]",
            )}
          />
        ) : null}
        <AnimatePresence>
          {phase === "type" || phase === "hold" ? (
            <motion.div
              key={`${cycle}-name`}
              initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 8 }}
              animate={{ opacity: 1, y: 0 }}
              exit={{ opacity: 0 }}
              transition={reduceMotion ? { duration: 0.15 } : spring.soft}
              className={cn(
                "rounded-lg border border-dashed bg-background/60 text-center font-mono tracking-[0.04em]",
                mobile ? "px-[3.2cqw] py-[1.8cqw]" : "px-[1.4cqw] py-[0.8cqw]",
              )}
            >
              <p
                className={cn(
                  "uppercase text-muted-foreground",
                  mobile ? "text-[2.4cqw]" : "text-[0.9cqw]",
                )}
              >
                {label}
              </p>
              <p
                className={cn(
                  "font-medium tracking-tight text-foreground",
                  mobile ? "min-h-[1.2em] text-[4.2cqw]" : "min-h-[1.2em] text-[1.7cqw]",
                )}
              >
                {shown}
                {phase === "type" ? (
                  <span className="ml-[0.12em] inline-block animate-caret" aria-hidden="true">
                    |
                  </span>
                ) : null}
                <span className="sr-only">{name}</span>
              </p>
            </motion.div>
          ) : null}
        </AnimatePresence>
      </div>
    </OverlayRoot>
  );
}

Props

Layout

Desktop 16:9 for OBS, or mobile 9:16 for TikTok Live Studio.

Installation

Follow alert is a browser-source overlay. Copy the URL for OBS or TikTok Live Studio. The live page has no site chrome and a transparent background.

In OBS, add a Browser source, paste the URL, and set the size to 1920 × 1080. Leave the page background transparent.

Usage

example.tsx

import { FollowAlert } from "@/components/stream/follow-alert";

export function Overlay() {
  return (
    <FollowAlert
      layout="desktop"
      label="New follow"
      username="sozo_fan"
      preview
    />
  );
}

Browser source

Paste this URL into OBS or TikTok Live Studio. The query params match the props below.

browser-source.tsx

// OBS Browser source, 1920×1080, transparent. Leave it open. Twitch sends each follower's name.
const src = "https://chandlerking.dev/live/follow-alert?layout=desktop&label=New+follow&username=sozo_fan";

The coffee pour plays through once. When it finishes, the follower's name types out on a dashed card. Desktop stays open for Twitch follows. The TikTok Live Studio link stays open for TikTok follows. Preview loops a sample follow.

Props

PropTypeDefaultNotes
labelstring"New follow"Small line above the username. Query `label`.
usernamestring"sozo_fan"Name on the toast. A `username` query plays that name once. After that, Twitch sends the name.
layout"desktop" | "mobile"RequiredCanvas orientation. Desktop is 1920×1080 for OBS. Mobile is 1080×1920 for TikTok Live Studio. Query `layout`.
previewbooleanfalseSite-only helpers: a sample webcam fill, a looping alert, and a stinger replay control. Leave it off on the live route.

Notes & Features

  • One play on stream. Leave the source open. On OBS, Twitch sends the follower's name. On the TikTok Live Studio link, TikTok sends it. The pour plays, the name types out, and the card leaves. The next follow waits its turn.
  • Preview loop. `preview` repeats the toast so you can watch it on the site. The live route never loops.
  • Reduced motion. The slide becomes a short fade when the viewer asks for less motion.
  • Motion. The toast uses the `motion` package. Install it if you copy the file into another project.

Manual installation

Rather than the browser source, copy Follow alert into a project. Install its dependencies, then copy all 2 files into the matching path.

Dependencies

terminal

npm install motion

Source files

src/components/stream/follow-alert.tsx

"use client";

import { useEffect, useState, type CSSProperties } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";

import { OverlayRoot } from "@/components/stream/overlay-root";
import { useViewerQueue } from "@/components/stream/use-twitch-alerts";
import { STREAM_COPY } from "@/config/stream-copy";
import type { StreamLayout } from "@/config/stream-components";
import { spring } from "@/lib/motion";
import { cn } from "@/lib/utils";

const DEFAULTS = STREAM_COPY["follow-alert"];
const POUR = "/images/stream/coffee-pour.webp";
const POUR_MS = 3300;
const TYPE_MS = 70;
const HOLD_MS = 1600;

type Phase = "idle" | "pour" | "type" | "hold" | "gone";

const night = {
  "--background": "#000000",
  "--foreground": "oklch(0.985 0 0)",
  "--muted-foreground": "oklch(0.708 0 0)",
  "--border": "oklch(1 0 0 / 18%)",
} as CSSProperties;

export function FollowAlert({
  layout,
  preview = false,
  label = DEFAULTS.label,
  username,
  alertKey,
  demo = false,
}: {
  layout: StreamLayout;
  preview?: boolean;
  label?: string;
  username?: string;
  alertKey?: string;
  demo?: boolean;
}) {
  const mobile = layout === "mobile";
  const reduceMotion = useReducedMotion();
  const { name, cycle, finish, replay } = useViewerQueue({
    preview,
    initialName: preview ? username || DEFAULTS.username || "" : username || "",
    demo,
    kind: "follow",
    alertKey,
    platform: layout === "mobile" ? "tiktok" : "twitch",
  });
  const [phase, setPhase] = useState<Phase>(name ? (reduceMotion ? "hold" : "pour") : "idle");
  const [count, setCount] = useState(reduceMotion ? name.length : 0);
  const token = `${cycle}:${name}:${reduceMotion ? "still" : "move"}`;
  const [seen, setSeen] = useState(token);
  if (seen !== token) {
    setSeen(token);
    if (name) {
      setCount(reduceMotion ? name.length : 0);
      setPhase(reduceMotion ? "hold" : "pour");
    }
  }
  useEffect(() => {
    if (phase !== "pour") return;
    const startType = window.setTimeout(() => setPhase("type"), POUR_MS);
    return () => window.clearTimeout(startType);
  }, [phase]);

  useEffect(() => {
    if (phase !== "type") return;
    if (count >= name.length) {
      const hold = window.setTimeout(() => setPhase("hold"), 0);
      return () => window.clearTimeout(hold);
    }
    const step = window.setTimeout(() => setCount((value) => value + 1), TYPE_MS);
    return () => window.clearTimeout(step);
  }, [count, name.length, phase]);

  useEffect(() => {
    if (phase !== "hold") return;
    const hide = window.setTimeout(() => setPhase("gone"), reduceMotion ? HOLD_MS + 800 : HOLD_MS);
    return () => window.clearTimeout(hide);
  }, [phase, reduceMotion]);

  useEffect(() => {
    if (phase !== "gone") return;
    const wait = window.setTimeout(() => {
      if (preview) replay();
      else finish();
    }, preview ? 700 : 400);
    return () => window.clearTimeout(wait);
  }, [finish, phase, preview, replay]);

  const shown = name.slice(0, count);

  return (
    <OverlayRoot>
      <div
        style={night}
        className={cn(
          "absolute left-1/2 -translate-x-1/2 text-foreground",
          mobile ? "top-[46%]" : "top-1/2 -translate-y-1/2",
        )}
      >
        {phase === "pour" ? (
          // eslint-disable-next-line @next/next/no-img-element
          <img
            key={cycle}
            src={POUR}
            alt=""
            width={784}
            height={779}
            draggable={false}
            className={cn(
              "pointer-events-none w-auto select-none",
              mobile ? "h-[64cqw]" : "h-[32cqw]",
            )}
          />
        ) : null}
        <AnimatePresence>
          {phase === "type" || phase === "hold" ? (
            <motion.div
              key={`${cycle}-name`}
              initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 8 }}
              animate={{ opacity: 1, y: 0 }}
              exit={{ opacity: 0 }}
              transition={reduceMotion ? { duration: 0.15 } : spring.soft}
              className={cn(
                "rounded-lg border border-dashed bg-background/60 text-center font-mono tracking-[0.04em]",
                mobile ? "px-[3.2cqw] py-[1.8cqw]" : "px-[1.4cqw] py-[0.8cqw]",
              )}
            >
              <p
                className={cn(
                  "uppercase text-muted-foreground",
                  mobile ? "text-[2.4cqw]" : "text-[0.9cqw]",
                )}
              >
                {label}
              </p>
              <p
                className={cn(
                  "font-medium tracking-tight text-foreground",
                  mobile ? "min-h-[1.2em] text-[4.2cqw]" : "min-h-[1.2em] text-[1.7cqw]",
                )}
              >
                {shown}
                {phase === "type" ? (
                  <span className="ml-[0.12em] inline-block animate-caret" aria-hidden="true">
                    |
                  </span>
                ) : null}
                <span className="sr-only">{name}</span>
              </p>
            </motion.div>
          ) : null}
        </AnimatePresence>
      </div>
    </OverlayRoot>
  );
}

src/components/stream/overlay-root.tsx

import type { ReactNode } from "react";

import { cn } from "@/lib/utils";

export function OverlayRoot({
  children,
  className,
}: {
  children: ReactNode;
  className?: string;
}) {
  return (
    <div
      className={cn(
        "@container relative h-full w-full overflow-hidden pointer-events-none",
        className,
      )}
    >
      {children}
    </div>
  );
}

Carve that warmth into me.

Vinland Saga
Chandler King

Building AnglerIQ & The Sozo CRM

Open to software engineering roles

Crafted by Chandler King · Kansas City, MO