QRCode
Every example on this page is live, drawn by the library's Storybook, where you can also change its props and see the result.
Generates a QR code image from a text string. Useful for encoding URLs, identifiers, or other data that can be scanned by a mobile device.
The code is always square. Size it in CSS (size="40%", or size="fill" for the room its flex
parent leaves) rather than in pixels worked out from your layout, so it shrinks with its box in a
saved render. Beside or before the code, the caption is what gives way when a row runs short; give
it a width of its own, by passing a node that has one, when the row also holds other flexible text.
Import
import { QRCode } from "~lib/qr-code";
Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text | string | Yes | N/A | The text or URL to encode |
size | number | string | "fill" | No | 256 | The code's width in pixels, any CSS width such as "40%", or "fill"; always square |
caption | ReactNode | No | N/A | Text shown with the code, such as "Scan to read", or a node drawn in its place |
captionPlacement | "below" | "beside" | "before" | No | "below" | Whether the caption sits under the code, to its right or its left |
captionSize | "xs" | "sm" | "base" | "lg" | "xl" | No | "sm" | Size of the caption text, on the same scale as Paragraph |
captionLines | number | No | 1 below, no limit otherwise | The most lines a string caption wraps to before it ends in "…" |
Examples
Default Size
A QR code at the default 256x256px size:
Small
A compact 100x100px QR code:
With a Caption
Add a caption to tell people what the code is for. It sits centered under the code on one line,
so the code and its caption always take the same height. A caption wider than the code stays on
one line and the code stays centered above it:
Under a small code, set captionSize="xs" to keep the caption close to the code's width:
Set captionPlacement="beside" to put it to the right of the code instead, centered on its height:
Set captionPlacement="before" to put it to the left of the code, aligned against it. Use it when
the code sits at the right-hand end of a row, where a caption after it would run off the edge. Like
a caption beside the code, it wraps to the room the row leaves it:
Sized to Its Box
A pixel size stays that many pixels. A saved render of your widget is laid out again in boxes a
little smaller than the one it was drawn in, so a code whose size you worked out from the layout
keeps its pixels and runs past the edge. Give it a CSS width instead, such as size="40%" or
size="100%": the code stays square, and shrinks with its box. With a caption, the code is that
share of the block, which spans its parent:
size="fill" lets the code take the room its flex parent leaves. Down a column it grows to the
column's width and is the first thing to give way when the column runs short, so a title above it
and a caption under it always fit; across a row it takes the row's height, which the row needs of
its own. The code is the shorter side of that room either way, centered:
<div className="flex min-h-0 flex-1 flex-col gap-2">
<Heading level={4}>Cinema listings</Heading>
<QRCode text={listingsUrl} size="fill" caption="Scan to read" />
</div>
Larger and Longer Captions
captionSize takes every size Paragraph does, so a caption beside a large code can be set as
large as the text around it. Under the code a caption stays on one line unless you set
captionLines; beside or before the code it wraps into the room the row leaves it, as far as
captionLines when you set it. Either way it ends in "…" when it has more to say than its lines
hold: