Get Started
Input
Feedback
Display
Installation
i18n
文字列や URL をエンコードして QR コードを生成します。3 つのファインダーパターン(角の四角)は角丸のリングで、データモジュールは円(ドット)で描画されるため、通常の四角い QR よりも柔らかい印象になります。色は CSS 変数をそのまま渡せるので、テーマに追従させることもできます。
import { QRCode } from "@/components/ui/qr-code"
export function QRCodeDemo() {インストール#
pnpm dlx shadcn@latest add https://ui.tyap.me/r/styles/base/qr-code.jsonnpx shadcn@latest add https://ui.tyap.me/r/styles/base/qr-code.jsonyarn dlx shadcn@latest add https://ui.tyap.me/r/styles/base/qr-code.jsonbunx --bun shadcn@latest add https://ui.tyap.me/r/styles/base/qr-code.json
以下の依存パッケージをインストールします:
pnpm add qrcodenpm install qrcodeyarn add qrcodebun add qrcode
以下のコードをプロジェクトにコピーします。
"use client"
import * as React from "react"
import QRCode from "qrcode"
import { cn } from "@/lib/utils"
export interface QRCodeProps extends Omit<React.ComponentProps<"svg">, "ref"> {
/** The text or URL to encode in the QR code. */
value: string
/** QR code size in pixels. */
size?: number
/** Foreground (dark) color. Accepts any CSS color, including CSS variables. */
fgColor?: string
/** Background (light) color. Accepts any CSS color, including CSS variables. */
bgColor?: string
/** Error correction level. L: 7%, M: 15%, Q: 25%, H: 30%. */
errorCorrectionLevel?: "L" | "M" | "Q" | "H"
}
// Quiet-zone width, expressed in modules.
const MARGIN = 2
// The three finder patterns occupy the top-left, top-right and bottom-left
// 7×7 corners. Their modules are drawn as a stylised ring instead of dots.
function isInFinder(x: number, y: number, count: number) {
return (
(x < 7 && y < 7) ||
(x >= count - 7 && y < 7) ||
(x < 7 && y >= count - 7)
)
}
function FinderPattern({
x,
y,
cell,
color,
}: {
x: number
y: number
cell: number
color: string
}) {
return (
<>
{/* 1-module-thick rounded ring centred on the border modules. */}
<rect
x={(x + 0.5) * cell}
y={(y + 0.5) * cell}
width={cell * 6}
height={cell * 6}
rx={cell * 2}
fill="none"
stroke={color}
strokeWidth={cell}
/>
{/* 3×3 filled centre. */}
<rect
x={(x + 2) * cell}
y={(y + 2) * cell}
width={cell * 3}
height={cell * 3}
rx={cell}
fill={color}
/>
</>
)
}
function QRCodeView({
value,
size = 268,
fgColor = "var(--foreground)",
bgColor = "var(--background)",
errorCorrectionLevel = "M",
className,
...props
}: QRCodeProps) {
const { count, modules } = React.useMemo(() => {
const qr = QRCode.create(value || " ", { errorCorrectionLevel })
return { count: qr.modules.size, modules: qr.modules.data }
}, [value, errorCorrectionLevel])
const cell = size / (count + MARGIN * 2)
const offset = MARGIN * cell
const dots: React.ReactNode[] = []
for (let y = 0; y < count; y++) {
for (let x = 0; x < count; x++) {
if (!modules[y * count + x] || isInFinder(x, y, count)) continue
dots.push(
<circle
key={`${x}-${y}`}
cx={offset + (x + 0.5) * cell}
cy={offset + (y + 0.5) * cell}
r={cell * 0.46}
fill={fgColor}
/>
)
}
}
return (
<svg
role="img"
aria-label={`QR code for ${value}`}
width={size}
height={size}
viewBox={`0 0 ${size} ${size}`}
className={cn("rounded-xl", className)}
{...props}
>
<rect width={size} height={size} rx={cell * 2} fill={bgColor} />
<g transform={`translate(${offset} ${offset})`}>
<FinderPattern x={0} y={0} cell={cell} color={fgColor} />
<FinderPattern x={count - 7} y={0} cell={cell} color={fgColor} />
<FinderPattern x={0} y={count - 7} cell={cell} color={fgColor} />
</g>
{dots}
</svg>
)
}
export { QRCodeView as QRCode }
import パスをプロジェクト構成に合わせて更新します。
使い方#
import { QRCode } from "@/components/ui/qr-code"<QRCode value="https://example.com" />
<QRCode value="https://example.com" size={140} />サンプル#
カラー#
fgColor / bgColor には任意の CSS カラーや CSS 変数を渡せます。デフォルトは var(--foreground) / var(--background) で、テーマに追従します。
import { QRCode } from "@/components/ui/qr-code"
export function QRCodeColors() {Props#
<svg> の属性に加えて、以下を受け取ります。
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | - | 必須。エンコードする文字列または URL。 |
size | number | 268 | QR コードのサイズ(ピクセル)。 |
fgColor | string | var(--foreground) | 前景(暗い部分)の色。CSS 変数も指定できます。 |
bgColor | string | var(--background) | 背景(明るい部分)の色。CSS 変数も指定できます。 |
errorCorrectionLevel | "L" | "M" | "Q" | "H" | "M" | 誤り訂正レベル。L: 7%、M: 15%、Q: 25%、H: 30%。 |
className | string | - | 追加の CSS クラス。 |