qr-code
kibo-ui-registry/qr-codeFreeComponent
QR Code is a component that generates a QR code from a string.
Live preview
live · rendered by the registry
Rendered by Kibo UI Registry on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.kibo-ui.com/r/qr-code.jsonSource
1"use client";23import { formatHex, oklch } from "culori";4import QR from "qrcode";5import { type HTMLAttributes, useEffect, useState } from "react";6import { cn } from "@/lib/utils";78export type QRCodeProps = HTMLAttributes<HTMLDivElement> & {9 data: string;10 foreground?: string;11 background?: string;12 robustness?: "L" | "M" | "Q" | "H";13};1415const oklchRegex = /oklch\(([0-9.]+)\s+([0-9.]+)\s+([0-9.]+)\)/;1617const getOklch = (color: string, fallback: [number, number, number]) => {18 const oklchMatch = color.match(oklchRegex);1920 if (!oklchMatch) {21 return { l: fallback[0], c: fallback[1], h: fallback[2] };22 }2324 return {25 l: Number.parseFloat(oklchMatch[1]),26 c: Number.parseFloat(oklchMatch[2]),27 h: Number.parseFloat(oklchMatch[3]),28 };29};3031export const QRCode = ({32 data,33 foreground,34 background,35 robustness = "M",36 className,37 ...props38}: QRCodeProps) => {39 const [svg, setSVG] = useState<string | null>(null);4041 useEffect(() => {42 const generateQR = async () => {43 try {44 const styles = getComputedStyle(document.documentElement);45 const foregroundColor =46 foreground ?? styles.getPropertyValue("--foreground");47 const backgroundColor =48 background ?? styles.getPropertyValue("--background");4950 const foregroundOklch = getOklch(51 foregroundColor,52 [0.21, 0.006, 285.885]53 );54 const backgroundOklch = getOklch(backgroundColor, [0.985, 0, 0]);5556 const newSvg = await QR.toString(data, {57 type: "svg",58 color: {59 dark: formatHex(oklch({ mode: "oklch", ...foregroundOklch })),60 light: formatHex(oklch({ mode: "oklch", ...backgroundOklch })),61 },62 width: 200,63 errorCorrectionLevel: robustness,64 margin: 0,65 });6667 setSVG(newSvg);68 } catch (err) {69 console.error(err);70 }71 };7273 generateQR();74 }, [data, foreground, background, robustness]);7576 if (!svg) {77 return null;78 }7980 return (81 <div82 className={cn("size-full", "[&_svg]:size-full", className)}83 // biome-ignore lint/security/noDangerouslySetInnerHtml: "Required for SVG"84 dangerouslySetInnerHTML={{ __html: svg }}85 {...props}86 />87 );88};
What it pulls in
npm packages
Files it writes
More from Kibo UI Registry
All 41 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| announcementannouncement | Kibo UI Registry | Components | Free | no deps | |
| avatar-stackavatar-stack | Kibo UI Registry | Components | Free | no deps | |
| bannerbanner | Kibo UI Registry | Components | Free | 2 deps | |
| calendarcalendar | Kibo UI Registry | Components | Free | 3 deps | |
| choiceboxchoicebox | Kibo UI Registry | Components | Free | 2 deps | |
| code-blockcode-block | Kibo UI Registry | Components | Free | 5 deps · 2 files | |
| color-pickercolor-picker | Kibo UI Registry | Components | Free | 3 deps | |
| comboboxcombobox | Kibo UI Registry | Components | Free | 2 deps |
