Overview
Components
Text Animations
QR code generator with rounded finder patterns and dot-style data modules.
qrcode
preview.svelte
<script lang="ts"> import { QRCode } from "$lib/components/spell/qrcode/index.js";</script>
<div class="dark:border-input w-[140px] rounded-lg p-2 shadow-[0_0_0_1px_rgba(0,0,0,.08),_0px_2px_2px_rgba(0,0,0,.04)] dark:border [&_svg]:h-auto [&_svg]:w-full"> <QRCode value="https://sv-agentation.com" size={140} /></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/s/qrcode.json
Install dependencies
Install the required packages for this component:
pnpm add qrcode
Copy the Source Code
Copy and paste the following code into your project:
qrcode.svelte
<script lang="ts"> import { cn } from "#lib/utils.js"; import type { ClassValue, SVGAttributes } from "svelte/elements"; // @ts-expect-error The qrcode package used here does not ship bundled TypeScript declarations. import QRCodePackage from "qrcode";
type ErrorCorrectionLevel = "L" | "M" | "Q" | "H";
interface QRModules { size: number; get(row: number, col: number): boolean; }
interface QRData { modules: QRModules; }
interface CirclePoint { cx: number; cy: number; }
interface Props extends SVGAttributes<SVGSVGElement> { value: string; size?: number; fgColor?: string; bgColor?: string; errorCorrectionLevel?: ErrorCorrectionLevel; class?: ClassValue; }
const QRCodeLib = QRCodePackage as { create(value: string, options: { errorCorrectionLevel: ErrorCorrectionLevel }): QRData; };
function isInFinderPattern(row: number, col: number, size: number): boolean { return (row < 7 && col < 7) || (row < 7 && col >= size - 7) || (row >= size - 7 && col < 7); }
let { value, size = 268, fgColor = "var(--foreground)", bgColor = "var(--background)", errorCorrectionLevel = "M", class: className, ...restProps }: Props = $props();
let qrData = $derived.by(() => { try { return QRCodeLib.create(value, { errorCorrectionLevel }); } catch { return null; } });
let moduleCount = $derived(qrData?.modules.size ?? 0); let moduleSize = $derived(moduleCount ? size / moduleCount : 0); let totalSize = $derived(size); let circleRadius = $derived(moduleSize / 3);
let finderPositions = $derived([ [0, 0], [0, moduleCount - 7], [moduleCount - 7, 0], ] as [number, number][]);
let finderSize = $derived(7 * moduleSize); let innerPadding = $derived(moduleSize); let innerWhiteSize = $derived(5 * moduleSize); let innerBlackSize = $derived(3 * moduleSize);
let circles = $derived.by(() => { if (!qrData) { return [] as CirclePoint[]; }
const nextCircles: CirclePoint[] = [];
for (let row = 0; row < moduleCount; row++) { for (let col = 0; col < moduleCount; col++) { if (qrData.modules.get(row, col) && !isInFinderPattern(row, col, moduleCount)) { nextCircles.push({ cx: (col + 0.5) * moduleSize, cy: (row + 0.5) * moduleSize, }); } } }
return nextCircles; });</script>
{#if qrData} <svg width={totalSize} height={totalSize} viewBox={`0 0 ${totalSize} ${totalSize}`} xmlns="http://www.w3.org/2000/svg" aria-label={`QR code for ${value}`} class={cn("block", className)} {...restProps} > <rect width={totalSize} height={totalSize} fill={bgColor} rx="12" ry="12" />
{#each finderPositions as [row, col] (`${row}-${col}`)} {@const x = col * moduleSize} {@const y = row * moduleSize}
<g> <rect {x} {y} width={finderSize} height={finderSize} fill={fgColor} rx="12" ry="12" /> <rect x={x + innerPadding} y={y + innerPadding} width={innerWhiteSize} height={innerWhiteSize} fill={bgColor} rx="8" ry="8" /> <rect x={x + innerPadding * 2} y={y + innerPadding * 2} width={innerBlackSize} height={innerBlackSize} fill={fgColor} rx="3" ry="3" /> </g> {/each}
{#each circles as { cx, cy }, index (`${cx}-${cy}-${index}`)} <circle {cx} {cy} r={circleRadius} fill={fgColor} /> {/each} </svg>{/if} index.ts
import QRCode from "./qrcode.svelte";export { QRCode };Folder Structure
Folder Structure
src/└── lib/ └── components/ └── spell/ └── qrcode/ ├── qrcode.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/spell/qrcode/llms.txt
Follow the guide's installation and usage instructions.Match the project's existing structure, imports, and styles.Keep changes scoped to the component and verify it works.Reply with a short bullet list of completed changes.Usage
<script lang="ts"> import { QRCode } from "#lib/components/spell/qrcode/index.js";</script>
<QRCode value="https://sv-animations.vercel.app" />Examples
Custom Colors
Override the foreground, background, size, and error-correction level to fit branded tickets, packaging, or event cards.
custom-colors-example.svelte
<script lang="ts"> import { QRCode } from "$lib/components/spell/qrcode/index.js";</script>
<div class="flex flex-wrap items-center justify-center gap-6"> <div class="dark:border-input w-[105px] rounded-lg p-2 shadow-[0_0_0_1px_rgba(0,0,0,.08),_0px_2px_2px_rgba(0,0,0,.04)] md:w-[140px] dark:border [&_svg]:h-auto [&_svg]:w-full" > <QRCode value="https://sv-blocks.vercel.app" size={140} fgColor="#c2410c" bgColor="var(--background)" /> </div> <div class="dark:border-input w-[105px] rounded-lg p-2 shadow-[0_0_0_1px_rgba(0,0,0,.08),_0px_2px_2px_rgba(0,0,0,.04)] md:w-[140px] dark:border [&_svg]:h-auto [&_svg]:w-full" > <QRCode value="https://sv-efferd.pages.dev" size={140} fgColor="#1d4ed8" bgColor="var(--background)" /> </div> <div class="dark:border-input w-[105px] rounded-lg p-2 shadow-[0_0_0_1px_rgba(0,0,0,.08),_0px_2px_2px_rgba(0,0,0,.04)] md:w-[140px] dark:border [&_svg]:h-auto [&_svg]:w-full" > <QRCode value="https://svelte-ai-elements.vercel.app" size={140} fgColor="#15803d" bgColor="var(--background)" /> </div></div>Props
| Name | Type | Default | Description |
|---|---|---|---|
value | string | required | The encoded text or URL used to generate the QR code. |
size | number | 268 | Controls the rendered width and height of the SVG in pixels. |
fgColor | string | "var(--foreground)" | Sets the foreground color used for the modules and finder patterns. |
bgColor | string | "var(--background)" | Sets the background color of the QR surface. |
errorCorrectionLevel | "L" | "M" | "Q" | "H" | "M" | Controls the QR error correction level passed to the generator. |
class | ClassValue | '' | Custom classes merged onto the root SVG element. |