Command palette

@yasinsensoy/web

Web · Components

QRCode

Draws text or a URL as a QR code (SVG).

7 exports

Live example

Preview

Code

import { QRCode } from "@yasinsensoy/web";

<QRCode value="https://github.com/yasinsensoy/ui" size={192} />

Events

  1. Events appear here when you interact with the preview.

Import

import { QRCode, encodeQR } from "@yasinsensoy/web";

QRCode

componentServer component

Draws text or a URL as a QR code (SVG). The encoder is the library's own; no external package. It is a server component: the code is generated on the server and only a single SVG path goes to the browser.

  • Picks the smallest code for the text type (numeric, uppercase, UTF-8); non-ASCII characters are encoded correctly.
  • Square, rounded or dotted look with shape.
  • Logo in the center with image; the level becomes H automatically.
  • By default black modules on a white background; high contrast is kept whatever the theme (readers expect dark-on-light). On a transparent background modules take the text color. To download, serialize the SVG element to a string with XMLSerializer.
QRCode({ value, level, size, margin, shape, color, background, image, imageSize, label, minVersion, className, ...props }: QRCodeProps): JSX.Element

Examples

<QRCode value="https://example.com/menu" size={192} />
<QRCode value={wifi} shape="dots" color="#0f172a" image="/logo.svg" label="Connect to Wi-Fi" />

encodeQR

function

Converts text into a QR code: picks the smallest fitting version and (when omitted) the most readable mask. Digits-only text uses numeric mode, uppercase plus $%*+-./: uses alphanumeric mode (smaller code); other text is encoded as UTF-8 (including non-ASCII characters).

encodeQR(text: string, options?: EncodeQROptions): QRMatrix

Examples

const qr = encodeQR("https://example.com", { level: "Q" });
qr.size; // 29
qr.modules[0][0]; // true

Error correction level: how much of the code can be damaged and still be read.

  • L: ~7% (densest, smallest code)
  • M: ~15% (general use)
  • Q: ~25%
  • H: ~30% (when a logo goes in the middle)
type QRErrorLevel = "L" | "M" | "Q" | "H"

QRMatrix

interface

Encoded QR matrix.

NameTypeDescription
sizenumberSide length (modules).
versionnumberVersion (1–40).
levelQRErrorLevelError correction level used.
masknumberChosen mask (0–7).
modulesboolean[][]Modules row by row: modules[y][x] is true when dark.

encodeQR options.

NameTypeDefaultDescription
level?QRErrorLevel"M"Error correction level.
minVersion?number1Minimum version (1–40).
mask?numberMask (0–7). The most readable one is chosen when omitted.

Shape of the modules (squares).

  • square: classic squares; the most compatible with every reader.
  • rounded: squares with softened corners, finders with rounded corners.
  • dots: dots and round finders.
type QRCodeStyle = "square" | "rounded" | "dots"

QRCodeProps

interface

Props of the QRCode component.

Extends: Omit<ComponentProps<"svg">, "children" | "style" | "color">

NameTypeDefaultDescription
valuestringText or URL to encode.
level?QRErrorLevel"M"`, `"H"` if `image` is givenError correction level. See QRErrorLevel.
size?number160Width and height (px). Can also be given with a class (size-40).
margin?number2Quiet zone around the code (modules). At least 2–4 is recommended for readers.
shape?QRCodeStyle"square"Module shape. See QRCodeStyle.
color?string"#000"`, `"currentColor"` if `background="transparent"Color of dark modules (CSS). With a background the default is black; it isn't bound to the theme text color (light gray in dark theme), so the code reads in every theme. On a transparent background the text color (currentColor) is used.
background?string"#fff"Background color (CSS). "transparent" leaves it transparent.
image?stringLogo placed in the center (image URL). Modules behind it are cleared; the level becomes "H" automatically for readability.
imageSize?number0.22Side length of the logo as a fraction of the code (0–0.3).
label?stringvalueScreen reader name.
minVersion?EncodeQROptions["minVersion"]Minimum version (1–40); to fix the size of the code.