QRCode
QR codes with states, icons, Canvas/SVG and result download.
When to use
QRCode converts text into code to send a link or other value to a scanner device. Generating a picture does not control link expiration, payment, or authorization: the application stores these states.
Meaning and method of drawing
value accepts a string or an array of segments. type selects canvas or svg, size specifies the size in pixels. An empty string does not create a DOM, so the ref disappears when cleared.
| Problem | Setting | Example |
|---|---|---|
| Create code | value, type |
Basic option |
| Add a small logo | icon, iconSize |
Icon |
| Show expiration date | status, onRefresh |
States |
| Replace cover | statusRender |
Your own cover |
| Save file | bind:nativeElement |
Download |
| Compare correction | errorLevel, boostLevel |
Error Correction |
SVG is completely generated on the server. Canvas is drawn after mount and remains empty until hydration. If you need an image without JavaScript, choose SVG. Switches and downloads still require client code.
Readability, colors and correction
color sets the color of the modules, bgColor sets the background. Provide contrast and a clear marginSize around the image; it is measured in modules. Changing style.color does not recolor the modules.
errorLevel supports L/M/Q/H. The default boostLevel can increase the correction if the image version remains the same, so changing the errorLevel does not always change the image. For a visual comparison, the example disables boostLevel.
The logo covers part of the design. Keep it small, especially with long text. iconSize can be a number or a width/height object. Higher density requires increasing the size or reducing the value. DOM and image checking does not replace scanning the target device's camera.
Status, export and registration
Active displays the usual code, loading/expired/scanned adds a cover image. onRefresh reports a refresh request; the new value and status are set by the application. statusRender-snippet gets inactive status, locale and callback. It replaces the default cover content without executing the request itself.
To export, get root via bind:nativeElement. Canvas gives toDataURL, SVG is serialized by XMLSerializer. The example uses data URLs, so it does not create temporary object URLs. External icon for Canvas requires a CORS enabled source.
ConfigProvider.qrcode sets the class/style and semantic defaults, locale.qrcode — state signatures. Root/cover are configured by an object or callback; styles contains CSS strings. There are no separate public QRCode component design tokens. The general theme affects the frame and text, and set the color of the modules using color or the general colorText.
Leave a text link or description of the purpose of the code nearby: not every user can scan the image. Convey an accessible name to the root without putting service instructions in just the cover color.
Local prefixCls changes the CSS classes of the root, cover and QRCode states; a non-empty value takes precedence. The empty string and undefined inherit ConfigProvider.prefixCls with the -qrcode suffix, defaulting to ant-qrcode. Data, canvas/SVG, onRefresh, nativeElement and semantic slots retain their contracts. theme.cssVar.prefix specifies the names of the CSS variables separately. Prefix and states show the local namespace next to the standard QRCode; ConfigProvider is a common prefix. When wrapping your CSS, use your chosen prefix or semantic classNames.
Spin in the loading state separately inherits the general prefix of the provider, and not the local QRCode.prefixCls. Changing the namespace QRCode does not rename nested components by itself.
Examples
Basic example
Address line and individual segments. Canvas with border and SVG without border.
<script lang="ts">
import { QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
figure {
display: grid;
gap: 12px;
margin: 0;
}
figcaption { color: var(--ant-color-text-secondary); }
</style>
<div class="examples">
<figure>
<QRCode value="https://woodger.org/ant-design" marginSize={4}
aria-label={("Library home page QR code")} />
<figcaption>{("String with address")}</figcaption>
</figure>
<figure>
<QRCode value={['https://woodger.org/', 'ant-design']} type="svg" bordered={false}
marginSize={4} aria-label={("QR code from separate segments")} />
<figcaption>{("Segments, without frame")}</figcaption>
</figure>
</div>
Icon in code
Local icon, square and rectangular iconSize, H correction.
<script lang="ts">
import { QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="examples">
<QRCode value="https://woodger.org/ant-design" icon="/favicon.png" iconSize={28}
errorLevel="H" marginSize={4} aria-label={("Canvas QR code with icon")} />
<QRCode value="https://woodger.org/ant-design" type="svg" icon="/favicon.png"
iconSize={{ width: 32, height: 24 }} errorLevel="H" marginSize={4}
aria-label={("SVG QR code with rectangular icon")} />
</div>
QR code expired
Scanned
Four states
active, loading, expired and scanned. The update removes the cover of the expired code.
<script lang="ts">
import { QRCode, type QRStatus } from '@woodger/antd';
const states = [
{ value: 'active', label: "Active" },
{ value: 'loading', label: "Loading" },
{ value: 'expired', label: "Expired" },
{ value: 'scanned', label: "Scanned" }
] as const satisfies readonly { value: QRStatus; label: string }[];
let expired = $state(true);
let refreshed = $state(0);
function refresh() {
expired = false;
refreshed++;
}
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
figure {
display: grid;
gap: 12px;
margin: 0;
}
figcaption, output { color: var(--ant-color-text-secondary); }
output {
display: block;
margin-top: 20px;
}
</style>
<div class="examples">
{#each states as state (state.value)}
<figure>
<QRCode value="https://woodger.org/ant-design" type="svg" marginSize={4}
status={state.value === 'expired' && !expired ? 'active' : state.value}
onRefresh={refresh} aria-label={("QR code: " + String((state.label)) + "")} />
<figcaption>{(state.label)}</figcaption>
</figure>
{/each}
</div>
<output
aria-label={("QR Code Updates")}
>{("Updated: ")}{refreshed}</output>
QR code expired
Own status cover
statusRender receives status, locale and callback updates.
<script lang="ts">
import { Button, QRCode, RadioGroup, type QRStatus, type StatusRenderInfo } from '@woodger/antd';
let status = $state<QRStatus>('expired');
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
.cover {
display: grid;
justify-items: center;
gap: 8px;
text-align: center;
}
p { margin: 0; }
</style>
{#snippet renderStatus(info: StatusRenderInfo)}
<div class="cover">
{#if info.status === 'expired'}
<p>{(info.locale.expired)}</p>
<Button size="small" onclick={info.onRefresh}>{("Get new code")}</Button>
{:else if info.status === 'loading'}
<p role="status">{("We are preparing a new code...")}</p>
{:else}
<p role="status">{("Login confirmed")}</p>
{/if}
</div>
{/snippet}
<div class="example">
<RadioGroup aria-label={("Own cover condition")} value={status}
options={(['active', 'expired', 'loading', 'scanned'])}
onValueChange={next => { status = next; }} />
<QRCode value="https://woodger.org/ant-design" type="svg" {status} marginSize={4}
statusRender={renderStatus} onRefresh={() => { status = 'active'; }}
aria-label={("QR code with its own cover")} />
</div>
Canvas or SVG
Switches the output method for one value.
<script lang="ts">
import { QRCode, RadioGroup } from '@woodger/antd';
let type = $state<'canvas' | 'svg'>('canvas');
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
</style>
<div class="example">
<RadioGroup aria-label={("QR code output method")} value={type} options={(['canvas', 'svg'])}
optionType="button" onValueChange={next => { type = next; }} />
<QRCode value="https://woodger.org/ant-design" {type} marginSize={4}
aria-label={("QR code of the selected format")} />
</div>
Code size
From 48 to 300 pixels; the icon takes up a quarter of the size.
<script lang="ts">
import { Button, QRCode } from '@woodger/antd';
let size = $state(160);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.drawing {
max-width: 100%;
overflow-x: auto;
}
.drawing:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<div class="controls">
<Button aria-label={("Shrink QR code")} disabled={size <= 48}
onclick={() => { size = Math.max(48, size - 10); }}>−</Button>
<Button aria-label={("Enlarge QR code")} disabled={size >= 300}
onclick={() => { size = Math.min(300, size + 10); }}>+</Button>
<output aria-label={("QR code size")}>{size}px</output>
</div>
<div class="drawing" role="region" aria-label={("QR code area")} tabindex="0">
<QRCode value="https://woodger.org/ant-design" {size} icon="/favicon.png"
iconSize={size / 4} errorLevel="H" marginSize={4} aria-label={("Variable Size QR Code")} />
</div>
</div>
Color and background
Own colors of modules and background with a reserve of contrast.
<script lang="ts">
import { QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="examples">
<QRCode value="https://woodger.org/ant-design" type="svg" color="#237804" bgColor="#f6ffed"
marginSize={4} aria-label={("Green QR code")} />
<QRCode value="https://woodger.org/ant-design" type="svg" color="#9e1068" bgColor="#fff0f6"
marginSize={4} aria-label={("Pink QR code")} />
</div>
Download QR code
PNG from Canvas and SVG via nativeElement without temporary object URLs.
<script lang="ts">
import { Button, QRCode, RadioGroup } from '@woodger/antd';
let type = $state<'canvas' | 'svg'>('canvas');
let nativeElement = $state<HTMLDivElement>();
function download() {
const drawing = nativeElement?.querySelector('canvas, svg');
if (!drawing) {
return;
}
const link = document.createElement('a');
link.download = `woodger-qrcode.${type === 'canvas' ? 'png' : 'svg'}`;
link.href = drawing instanceof HTMLCanvasElement
? drawing.toDataURL('image/png')
: `data:image/svg+xml;charset=utf-8,${encodeURIComponent(
new XMLSerializer().serializeToString(drawing)
)}`;
link.click();
}
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
</style>
<div class="example">
<RadioGroup aria-label={("QR code download format")} value={type} options={(['canvas', 'svg'])}
optionType="button" onValueChange={next => { type = next; }} />
<QRCode value="https://woodger.org/ant-design" {type} bind:nativeElement
color="#141414" bgColor="#ffffff" marginSize={4} aria-label={("QR code for download")} />
<Button type="primary" onclick={download}>{("Download ")}{(type === 'canvas' ? 'PNG' : 'SVG')}</Button>
</div>
Error correction
Levels L/M/Q/H and automatic increase boostLevel.
<script lang="ts">
import { Checkbox, QRCode, RadioGroup, type QRCodeErrorLevel } from '@woodger/antd';
let errorLevel = $state<QRCodeErrorLevel>('M');
let boostLevel = $state(false);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
</style>
<div class="example">
<RadioGroup aria-label={("QR code correction level")} value={errorLevel}
options={(['L', 'M', 'Q', 'H'])} optionType="button"
onValueChange={next => { errorLevel = next; }} />
<Checkbox bind:checked={boostLevel}>{("Automatically increase correction")}</Checkbox>
<QRCode
value="https://woodger.org/ant-design/components/qr-code"
type="svg"
{errorLevel}
{boostLevel}
marginSize={4}
aria-label={("QR code with error correction")}
/>
</div>
Code inside Popover
The button opens a code for sending a link to your phone.
<script lang="ts">
import { Button, Popover, QRCode } from '@woodger/antd';
</script>
{#snippet code()}
<QRCode value="https://woodger.org/ant-design" type="svg" color="#141414"
bgColor="#ffffff" marginSize={4} bordered={false} aria-label={("QR code for sending a link")} />
{/snippet}
<Popover
title={("Open page on phone")}
content={(code)}
trigger="click"
>
<Button>{("Share link")}</Button>
</Popover>
QR code expired
Scanned
Semantic DOM
The styles/classNames object and function style root and cover.
<script lang="ts">
import { QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="examples">
<QRCode value="https://woodger.org/ant-design" type="svg" status="expired"
classNames={{ root: 'qrcode-semantic-object', cover: 'qrcode-object-cover' }}
styles={{
root: 'border-radius: 16px',
cover: 'backdrop-filter: blur(2px)'
}}
aria-label={("QR code with object styles")} />
<QRCode value="https://woodger.org/ant-design" type="svg" status="scanned"
classNames={() => ({ root: 'qrcode-semantic-function' })}
styles={({ props }) => ({
root: 'border: 2px solid var(--ant-color-success)',
cover: `font-weight: ${props.status === 'scanned' ? 700 : 400}`
})} aria-label={("QR code with styles function")} />
</div>
QR code expired
Code has expired
General settings and locale
ConfigProvider.qrcode, local styles and custom state labels.
<script lang="ts">
import { Button, ConfigProvider, QRCode, enUS, ruRU } from '@woodger/antd';
let englishExpired = $state(true);
let customExpired = $state(true);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
.codes {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="example">
<div class="codes">
<ConfigProvider locale={enUS} qrcode={{ styles: { root: 'border-radius: 12px' } }}>
<QRCode value="https://woodger.org/ant-design" type="svg"
status={englishExpired ? 'expired' : 'active'}
onRefresh={() => { englishExpired = false; }} aria-label={("QR code with English locale")} />
</ConfigProvider>
<ConfigProvider locale={({
...ruRU, qrcode: {
refresh: "Get new",
expired: "Code has expired"
}
})}
qrcode={{ styles: { root: 'border-radius: 12px' } }}>
<QRCode value="https://woodger.org/ant-design" type="svg"
status={customExpired ? 'expired' : 'active'} styles={{ root: 'border-radius: 20px' }}
onRefresh={() => { customExpired = false; }} aria-label={("QR code with your labels")} />
</ConfigProvider>
</div>
<Button onclick={() => {
englishExpired = true;
customExpired = true;
}}>{("Show expired codes again")}</Button>
</div>
Scanned
Theme of the picture and cover
General color of modules, local aliases of frame and status text.
<script lang="ts">
import { ConfigProvider, QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<ConfigProvider theme={{
token: { colorText: '#135200' },
components: {
QRCode: {
colorBgContainer: '#fff7e6',
colorSplit: '#d3adf7',
colorText: '#722ed1',
borderRadiusLG: 16
}
}
}}>
<div class="examples">
<QRCode value="https://woodger.org/ant-design" type="svg" marginSize={4}
aria-label={("QR code with local theme")} />
<QRCode value="https://woodger.org/ant-design" type="svg" status="scanned" marginSize={4}
aria-label={("Scanned QR code with local theme")} />
</div>
</ConfigProvider>
Editor from review
Text, format, cleaning and updating of expired code.
<script lang="ts">
import { Button, Input, QRCode, RadioGroup, type RadioOption } from '@woodger/antd';
const formats: RadioOption<'svg' | 'canvas'>[] = [
{ value: 'svg', label: 'SVG' },
{ value: 'canvas', label: 'Canvas' }
];
let value = $state('https://woodger.org/ant-design');
let type = $state<'svg' | 'canvas'>('svg');
let expired = $state(false);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 16px;
max-width: 520px;
}
.field {
display: grid;
gap: 8px;
width: 100%;
}
.controls {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
}
p {
margin: 0;
color: var(--ant-color-text-secondary);
}
</style>
<div class="example">
<div class="field">
<label for="showcase-qrcode-value">{("QR code text")}</label>
<Input id="showcase-qrcode-value" bind:value maxLength={200} allowClear />
</div>
<div class="controls">
<RadioGroup
aria-label={("QR code format")}
options={(formats)}
value={type}
optionType="button"
onValueChange={next => { type = next; }}
/>
<Button disabled={!value} onclick={() => { expired = !expired; }}>{(expired ? "Activate code" : "Expired")}</Button>
</div>
{#if value}
<QRCode
{value}
{type}
size={192}
status={expired ? 'expired' : 'active'}
onRefresh={() => { expired = false; }}
aria-label={("QR code page")}
/>
{:else}
<p>{("Enter text to create a QR code.")}</p>
{/if}
</div>
QR code expired
Local CSS prefix
SVG, states and code update with own prefixCls; standard neighbor
<script lang="ts">
import { Checkbox, QRCode } from '@woodger/antd';
let alternate = $state(false);
let expired = $state(true);
</script>
<style>
.example {
display: grid;
gap: 24px;
}
.codes {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="example">
<Checkbox bind:checked={alternate}>{("Another QRCode prefix")}</Checkbox>
<Checkbox bind:checked={expired}>{("Expired code with prefix")}</Checkbox>
<div class="codes">
<QRCode prefixCls={alternate ? 'woodger-code' : 'woodger-qrcode'} type="svg"
value="https://woodger.org/ant-design" status={expired ? 'expired' : 'active'}
onRefresh={() => { expired = false; }} aria-label={("QR code with prefix")} />
<QRCode type="svg" value="https://woodger.org/ant-design" aria-label={("Standard QR code")} />
</div>
</div>
API
Verified against 11 ant.design 6.7.0 themes and public Svelte API based on 6.6.8. statusRender accepts Snippet, styles - CSS strings. React ref is replaced by bind:nativeElement. Full definitions of Props are in the package.
| Theme | Example |
|---|---|
| base | Enter text and update a picture. |
| With Icon | Local icon in Canvas and SVG. |
| other status | Four states and onRefresh. |
| custom status render | Snippet with locale, status and onRefresh. |
| Custom Render Type | Canvas/SVG switching. |
| Custom Size | Code size and proportional icon. |
| Custom Color | Color of modules and opaque background. |
| Download QRCode | Download PNG and SVG via nativeElement. |
| Error Level | L/M/Q/H and separate boostLevel. |
| Advanced Usage | QRCode inside Popover. |
| Custom semantic dom styling | styles/classNames object and function for root and cover. |
| Local CSS prefix | Changing CSS classes while preserving SVG, cover and onRefresh. |
| Property | Description | Type | Default |
|---|---|---|---|
prefixCls | Full local CSS prefix for root, cover and states. The empty string inherits the common prefix; ant-qrcode is used without configuration. Data, Canvas/SVG and semantic slots are preserved. | string | ConfigProvider + -qrcode |
value | Text or individual segments. An empty line hides the component. | string | string[] | is required |
type | Canvas is drawn after mount; SVG is available with SSR. | 'canvas' | 'svg' | canvas |
size | Size in CSS pixels; bitmap Canvas takes into account devicePixelRatio. | number | 160 |
icon | icon URL; for Canvas crossOrigin=anonymous is used. | string | — |
iconSize | Icon size in pixels. | number | { width: number; height: number } | 40 |
color | Color of modules. style.color and component alias colorText do not replace it. | string | general colorText |
bgColor | Background of the picture; In the example, download is set to white. | string | transparent |
marginSize | Free zone around modules; in the main examples it is set to 4. | number | 0 |
bordered | false removes the visible frame, padding and fillet. | boolean | true |
errorLevel | Error correction level. | 'L' | 'M' | 'Q' | 'H' | M |
boostLevel | Increases the correction if a larger version of the QR code is not needed. | boolean | true |
status | Controlled cover state. | 'active' | 'expired' | 'loading' | 'scanned' | active |
onRefresh | Request for update. The new value/status is set by the owner. | () => void | — |
statusRender | Gets inactive status, locale and onRefresh. | Snippet<[StatusRenderInfo]> | built-in cover |
classNames | An object or function of the root/cover classes. | QRCodeSemantic | — |
styles | root/cover CSS string object or function. | QRCodeSemantic | — |
nativeElement | bind:nativeElement provides root after mount; cleared when unmounted. | HTMLDivElement | undefined | — |
ConfigProvider and locale
ConfigProvider.qrcode specifies class, style and semantic styles. Local styles specify general settings; normal style is applied after semantic root. The nested provider replaces the entire qrcode object. locale.qrcode combines expired, refresh and scanned by fields.
Semantic DOM
The container contains a canvas or svg with role=img. aria-label refers to a drawing; give it a name that explains the purpose of the code. The picture itself is not a link. To open the address, leave an accessible text link nearby in the application.
| Slot | Purpose |
|---|---|
root | Container, frame, background, padding and dimensions. |
cover | Inactive state cover. |
statusRender replaces the contents of cover for expired, loading and scanned. An empty Snippet leaves an empty cover. When active, Snippet is not called. Changing the status and updating data after onRefresh belongs to the example.
Registration tokens
There are no separate public QRCode component tokens. Common aliases can be overridden in theme.components.QRCode: border follows colorSplit, fillet follows borderRadiusLG, cover background follows colorBgContainer. The picture uses color or a generic theme.token.colorText; component alias colorText changes the status text.
| Token | Meaning |
|---|---|
colorSplit | rgba(5,5,5,0.06) |
colorText | rgba(0,0,0,0.88) |
colorWhite | #fff |
borderRadiusLG | 8 |
fontFamily | -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji' |
fontSize | 14 |
lineHeight | 1.5714285714285714 |
lineType | solid |
lineWidth | 1 |
paddingSM | 12 |
FAQ
Why did changing errorLevel not change the picture?
boostLevel can increase the correction without increasing the version. For comparison, turn it off.
How to download PNG or SVG?
Get canvas/svg from nativeElement, use toDataURL or XMLSerializer. For external icon, check out CORS.
What works without JavaScript?
SVG is generated with SSR; Canvas is drawn after mount. Management and downloading require hydration.
Why is the code unreadable?
Check contrast, white space, logo size and pattern density. Then check the target device's camera.