ebnsina

Dynamic Island

Component 1

About

A pill-shaped overlay that morphs to show a live activity (an upload, a timer, a call) compact or expanded into a detail card, ported from the Sina UI Svelte component.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Dynamic Island to your project and fits it to your theme.

Code

How Dynamic Island is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client";

import DynamicIsland from "@/components/ui/dynamic-island";
import { useEffect, useRef, useState } from "react";
import { Check, Mic, MicOff, Moon } from "lucide-react";

type View = "upload" | "uploaded" | "prayer" | "circle";

const scans = [
  "Book of Optics, folio 12.jpg",
  "Book of Optics, folio 13.jpg",
  "Star tables, plate 4.jpg",
];

const prayers: [string, string][] = [
  ["Fajr", "04:52"],
  ["Dhuhr", "12:08"],
  ["Asr", "15:31"],
  ["Maghrib", "18:14"],
  ["Isha", "19:36"],
];

const minuteOfDay = (hhmm: string) => {
  const [h, m] = hhmm.split(":").map(Number);
  return h * 60 + m;
};

const pct = new Intl.NumberFormat("en", { style: "percent" });
const clock = new Intl.DateTimeFormat("en", {
  hour: "numeric",
  minute: "2-digit",
});
const show12 = (hhmm: string) => clock.format(new Date(`2000-01-01T${hhmm}`));

export default function DynamicIslandDemo() {
  const [view, setView] = useState<View | "">("");
  const [expanded, setExpanded] = useState(false);
  const [progress, setProgress] = useState(0);
  const [muted, setMuted] = useState(false);
  const frame = useRef(0);
  const settle = useRef<ReturnType<typeof setTimeout>>();

  function show(next: View | "") {
    clearTimeout(settle.current);
    cancelAnimationFrame(frame.current);
    setExpanded(false);
    setView(next);
  }

  // Upload: three scans, sent one after another; when done it says so, then steps aside.
  function upload() {
    show("upload");
    setProgress(0);
    let last = performance.now();
    const step = (now: number) => {
      setProgress((p) => {
        const next = Math.min(
          1,
          p + ((now - last) / 1000) * (0.14 + 0.05 * Math.sin(now / 700)),
        );
        if (next < 1) {
          frame.current = requestAnimationFrame(step);
        } else {
          setView("uploaded");
          setExpanded(false);
          settle.current = setTimeout(() => setView(""), 2600);
        }
        return next;
      });
      last = now;
    };
    frame.current = requestAnimationFrame(step);
  }
  useEffect(() => () => cancelAnimationFrame(frame.current), []);
  const current = Math.min(
    scans.length - 1,
    Math.floor(progress * scans.length),
  );

  // Next prayer: today's times; the next one is whichever hasn't passed yet.
  const [now, setNow] = useState(() => new Date());
  useEffect(() => {
    const id = setInterval(() => setNow(new Date()), 30_000);
    return () => clearInterval(id);
  }, []);
  const here = now.getHours() * 60 + now.getMinutes();
  const nextIndex = (() => {
    const i = prayers.findIndex(([, t]) => minuteOfDay(t) > here);
    return i === -1 ? 0 : i;
  })();
  const wait = (minuteOfDay(prayers[nextIndex][1]) - here + 1440) % 1440;
  const until = (() => {
    const unit = (u: string, n: number) =>
      new Intl.NumberFormat("en", {
        style: "unit",
        unit: u,
        unitDisplay: "short",
      }).format(n);
    const h = Math.floor(wait / 60);
    const m = wait % 60;
    return `in ${[h && unit("hour", h), m && unit("minute", m)].filter(Boolean).join(" ") || "a moment"}`;
  })();

  return (
    <div className="w-full h-screen flex flex-col items-center justify-center gap-5 bg-[var(--ui-bg,#fff)] px-6">
      <div
        className="w-full flex items-start justify-center"
        style={{ height: "15.5rem" }}
      >
        <DynamicIsland
          label="Live activity"
          view={view}
          expanded={expanded}
          onExpandedChange={setExpanded}
          expandable={(v) => v !== "uploaded"}
        >
          {(v, open) => {
            if (v === "upload") {
              if (!open) {
                return (
                  <div className="flex items-center gap-2 h-[2.3rem] px-3 pr-4 whitespace-nowrap">
                    <svg
                      className="w-[1.125rem] -rotate-90"
                      viewBox="0 0 20 20"
                      aria-hidden="true"
                    >
                      <circle
                        cx="10"
                        cy="10"
                        r="8"
                        pathLength={1}
                        fill="none"
                        strokeWidth={3}
                        stroke="var(--ui-hover)"
                      />
                      <circle
                        cx="10"
                        cy="10"
                        r="8"
                        pathLength={1}
                        fill="none"
                        strokeWidth={3}
                        stroke="var(--ui-accent)"
                        strokeLinecap="round"
                        strokeDasharray={1}
                        strokeDashoffset={1 - progress}
                      />
                    </svg>
                    <span className="w-[4.5rem]" />
                    <span className="font-semibold tabular-nums min-w-[3.25em] text-right">
                      {pct.format(progress)}
                    </span>
                  </div>
                );
              }
              return (
                <div
                  className="grid gap-3.5 box-border p-5"
                  style={{ width: "min(22rem, 100%)" }}
                >
                  <div>
                    <p className="text-sm font-medium mb-1">
                      Uploading {scans.length} scans
                    </p>
                    <div className="h-2 rounded-full bg-[var(--ui-hover)] overflow-hidden">
                      <div
                        className="h-full rounded-full bg-[var(--ui-accent)]"
                        style={{ width: `${progress * 100}%` }}
                      />
                    </div>
                  </div>
                  <p className="m-0 text-[0.8125rem] text-[var(--ui-muted)]">
                    {current + 1} of {scans.length}: {scans[current]}
                  </p>
                  <button
                    type="button"
                    onClick={() => show("")}
                    className="rounded-[var(--ui-radius-control)] bg-[var(--ui-danger)] text-[var(--ui-on-danger)] px-3 py-2 text-sm font-medium"
                  >
                    Cancel upload
                  </button>
                </div>
              );
            }
            if (v === "uploaded") {
              return (
                <div className="flex items-center gap-2 h-[2.3rem] px-3 pr-4 whitespace-nowrap">
                  <span className="flex text-[color-mix(in_srgb,var(--ui-accent)_80%,var(--ui-fg))]">
                    <Check size={16} strokeWidth={2.5} />
                  </span>
                  <span className="w-[4.5rem]" />
                  <span className="font-semibold tabular-nums">Uploaded</span>
                </div>
              );
            }
            if (v === "prayer") {
              if (!open) {
                return (
                  <div className="flex items-center gap-2 h-[2.3rem] px-3 pr-4 whitespace-nowrap">
                    <span className="flex text-[color-mix(in_srgb,var(--ui-accent)_80%,var(--ui-fg))]">
                      <Moon size={16} />
                    </span>
                    <span className="font-semibold tabular-nums">
                      {prayers[nextIndex][0]}
                    </span>
                    <span className="w-[4.5rem]" />
                    <span className="text-[var(--ui-muted)]">{until}</span>
                  </div>
                );
              }
              return (
                <div
                  className="grid gap-3.5 box-border p-5"
                  style={{ width: "min(22rem, 100%)" }}
                >
                  <p className="m-0 font-semibold">Prayer times · Baghdad</p>
                  <ol className="grid gap-0.5 m-0 p-0 list-none text-sm tabular-nums">
                    {prayers.map(([name, time], i) => (
                      <li
                        key={name}
                        className={[
                          "flex justify-between px-2.5 py-1.5 rounded-[var(--ui-radius)]",
                          i === nextIndex &&
                            "bg-[color-mix(in_srgb,var(--ui-accent)_12%,transparent)] text-[color-mix(in_srgb,var(--ui-accent)_80%,var(--ui-fg))] font-semibold",
                          i < nextIndex && "text-[var(--ui-muted)]",
                        ]
                          .filter(Boolean)
                          .join(" ")}
                      >
                        <span>{name}</span>
                        <span>{show12(time)}</span>
                      </li>
                    ))}
                  </ol>
                </div>
              );
            }
            if (v === "circle") {
              if (!open) {
                return (
                  <div className="flex items-center gap-2 h-[2.3rem] px-3 pr-4 whitespace-nowrap">
                    <span
                      className="flex items-center gap-[2px] h-4"
                      aria-hidden="true"
                    >
                      {[0.8, 0.5, 0.95, 0.6].map((d, i) => (
                        <span
                          key={i}
                          className={`w-[3px] h-full rounded-sm ${muted ? "" : "animate-pulse"}`}
                          style={{
                            background: muted
                              ? "var(--ui-muted)"
                              : "var(--ui-accent)",
                            transform: muted ? "scaleY(0.25)" : undefined,
                            animationDuration: `${d}s`,
                          }}
                        />
                      ))}
                    </span>
                    <span className="font-semibold">Live</span>
                    <span className="w-[4.5rem]" />
                    <span className="text-[var(--ui-muted)]">12 listening</span>
                  </div>
                );
              }
              return (
                <div
                  className="grid gap-3.5 box-border p-5"
                  style={{ width: "min(22rem, 100%)" }}
                >
                  <div>
                    <p className="m-0 font-semibold">Reading circle</p>
                    <p className="m-0 text-[0.8125rem] text-[var(--ui-muted)]">
                      The Book of Optics, chapter 3 · 12 listening
                    </p>
                  </div>
                  <div className="grid grid-cols-2 gap-2">
                    <button
                      type="button"
                      aria-pressed={muted}
                      onClick={() => setMuted((m) => !m)}
                      className="flex items-center justify-center gap-1.5 rounded-[var(--ui-radius-control)] border border-[var(--ui-field-line)] px-3 py-2 text-sm font-medium"
                    >
                      {muted ? <MicOff size={16} /> : <Mic size={16} />}
                      {muted ? "Unmute" : "Mute"}
                    </button>
                    <button
                      type="button"
                      onClick={() => show("")}
                      className="rounded-[var(--ui-radius-control)] bg-[var(--ui-danger)] text-[var(--ui-on-danger)] px-3 py-2 text-sm font-medium"
                    >
                      Leave
                    </button>
                  </div>
                </div>
              );
            }
            return null;
          }}
        </DynamicIsland>
      </div>

      <div
        className="flex flex-wrap justify-center gap-2"
        role="group"
        aria-label="Start an activity"
      >
        <button
          type="button"
          onClick={upload}
          className="rounded-[var(--ui-radius-control)] border border-[var(--ui-field-line)] px-3 py-1.5 text-sm font-medium text-[var(--ui-fg)]"
        >
          Upload scans
        </button>
        <button
          type="button"
          onClick={() => show("prayer")}
          className="rounded-[var(--ui-radius-control)] border border-[var(--ui-field-line)] px-3 py-1.5 text-sm font-medium text-[var(--ui-fg)]"
        >
          Next prayer
        </button>
        <button
          type="button"
          onClick={() => show("circle")}
          className="rounded-[var(--ui-radius-control)] border border-[var(--ui-field-line)] px-3 py-1.5 text-sm font-medium text-[var(--ui-fg)]"
        >
          Join reading circle
        </button>
        <button
          type="button"
          onClick={() => show("")}
          className="rounded-[var(--ui-radius-control)] px-3 py-1.5 text-sm font-medium text-[var(--ui-muted)]"
        >
          Clear
        </button>
      </div>
      <p className="m-0 text-[0.8125rem] text-[var(--ui-muted)]">
        Tap the island for details.
      </p>
    </div>
  );
}

Dependencies

More by ebnsina

View all
  • Countdown Timer

    Countdown Timer

    35
  • Video Player

    Video Player

    26
  • Alarm Clock

    Alarm Clock

    24
  • Hourglass Timer

    Hourglass Timer

    19
  • Voice Note

    Voice Note

    19
  • Sortable List

    Sortable List

    17
  • Sign In Auth Card

    Sign In Auth Card

    13
  • Split Button

    Split Button

    13