QR Code

A rounded-dot QR code that builds itself dot by dot.

Preview

"use client";

import { QrCode } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/motion/button";
import { AnimatedQrCode, type QrCodeVariant } from "@/components/motion/qr-code";
import { Tabs, TabsList, TabsTrigger } from "@/components/motion/tabs";

const VARIANTS: { value: QrCodeVariant; label: string }[] = [
  { value: "swarm", label: "Swarm" },
  { value: "ripple", label: "Ripple" },
  { value: "scan", label: "Scan" },
  { value: "smoke", label: "Smoke" },
];

export function QrCodePreview() {
  const [variant, setVariant] = useState<QrCodeVariant>("swarm");
  const [shown, setShown] = useState(false);

  return (
    <div className="flex w-full flex-col items-center gap-4">
      <Tabs value={variant} onValueChange={(v) => setVariant(v as QrCodeVariant)} variant="pill">
        <TabsList aria-label="Arrival style" wrapperClassName="w-auto">
          {VARIANTS.map((v) => (
            <TabsTrigger key={v.value} value={v.value} className="text-xs">
              {v.label}
            </TabsTrigger>
          ))}
        </TabsList>
      </Tabs>
      <AnimatedQrCode value="https://beui.dev" shown={shown} variant={variant} />
      <Button size="sm" onClick={() => setShown((s) => !s)} aria-pressed={shown}>
        <QrCode className="size-4" aria-hidden />
        {shown ? "Hide code" : "Show code"}
      </Button>
    </div>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/qr-code

API Reference

value
string

Text or URL the code encodes.

shown
boolean

Whether the code is drawn. Turning it on builds the code; turning it off plays the build backwards from wherever it is.

variant?
"ripple" | "swarm" | "scan" | "smoke"

How the code builds: every piece flies in on a curved path, pops in place behind a spreading ring, drops into place behind a bar sweeping down, or rises as smoke and condenses from the bottom row up. Default: "swarm"

Defaultswarm
size?
number

Side of the square canvas, in px, quiet zone included. Default: 296

Default296
origin?
QrCodeOrigin

Where swarm pieces fly from and where the ripple ring starts. Scan and smoke ignore it. Default: bottom centre for swarm, centre for ripple

ink?
string

Dot colour, any CSS colour. Default: "#0a0a0a"

Default#0a0a0a
paper?
string

Background colour, any CSS colour. Keep it light so cameras can read the code. Default: "#ffffff"

Default#ffffff
onSettled?
((shown: boolean) => void)

Called when a build or teardown finishes, with the `shown` value it ended on.

labels?
Partial<{ label: string; }>

Translated strings, merged over the English defaults.

className?
string

Classes merged onto the canvas.

Updated