Ant Design Svelte

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

String with address
Segments, without frame

Basic example

Address line and individual segments. Canvas with border and SVG without border.

QR-Code

Icon in code

Local icon, square and rectangular iconSize, H correction.

Active
Loading

QR code expired

Expired

Scanned

Scanned
Updated: 0

Four states

active, loading, expired and scanned. The update removes the cover of the expired code.

QR code expired

Own status cover

statusRender receives status, locale and callback updates.

Canvas or SVG

Switches the output method for one value.

160px
QR-Code

Code size

From 48 to 300 pixels; the icon takes up a quarter of the size.

Color and background

Own colors of modules and background with a reserve of contrast.

Download QR code

PNG from Canvas and SVG via nativeElement without temporary object URLs.

Error correction

Levels L/M/Q/H and automatic increase boostLevel.

Code inside Popover

The button opens a code for sending a link to your phone.

QR code expired

Scanned

Semantic DOM

The styles/classNames object and function style root and cover.

QR code expired

Code has expired

General settings and locale

ConfigProvider.qrcode, local styles and custom state labels.

Scanned

Theme of the picture and cover

General color of modules, local aliases of frame and status text.

Editor from review

Text, format, cleaning and updating of expired code.

QR code expired

Local CSS prefix

SVG, states and code update with own prefixCls; standard neighbor

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.

Topics of official examples
ThemeExample
baseEnter text and update a picture.
With IconLocal icon in Canvas and SVG.
other statusFour states and onRefresh.
custom status renderSnippet with locale, status and onRefresh.
Custom Render TypeCanvas/SVG switching.
Custom SizeCode size and proportional icon.
Custom ColorColor of modules and opaque background.
Download QRCodeDownload PNG and SVG via nativeElement.
Error LevelL/M/Q/H and separate boostLevel.
Advanced UsageQRCode inside Popover.
Custom semantic dom stylingstyles/classNames object and function for root and cover.
Local CSS prefixChanging CSS classes while preserving SVG, cover and onRefresh.
Main parameters
PropertyDescriptionTypeDefault
prefixClsFull 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.stringConfigProvider + -qrcode
valueText or individual segments. An empty line hides the component.string | string[]is required
typeCanvas is drawn after mount; SVG is available with SSR.'canvas' | 'svg'canvas
sizeSize in CSS pixels; bitmap Canvas takes into account devicePixelRatio.number160
iconicon URL; for Canvas crossOrigin=anonymous is used.string—
iconSizeIcon size in pixels.number | { width: number; height: number }40
colorColor of modules. style.color and component alias colorText do not replace it.stringgeneral colorText
bgColorBackground of the picture; In the example, download is set to white.stringtransparent
marginSizeFree zone around modules; in the main examples it is set to 4.number0
borderedfalse removes the visible frame, padding and fillet.booleantrue
errorLevelError correction level.'L' | 'M' | 'Q' | 'H'M
boostLevelIncreases the correction if a larger version of the QR code is not needed.booleantrue
statusControlled cover state.'active' | 'expired' | 'loading' | 'scanned'active
onRefreshRequest for update. The new value/status is set by the owner.() => void—
statusRenderGets inactive status, locale and onRefresh.Snippet<[StatusRenderInfo]>built-in cover
classNamesAn object or function of the root/cover classes.QRCodeSemantic—
stylesroot/cover CSS string object or function.QRCodeSemantic—
nativeElementbind: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.

SlotPurpose
rootContainer, frame, background, padding and dimensions.
coverInactive 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.

Common tokens of the current topic
TokenMeaning
colorSplitrgba(5,5,5,0.06)
colorTextrgba(0,0,0,0.88)
colorWhite#fff
borderRadiusLG8
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'
fontSize14
lineHeight1.5714285714285714
lineTypesolid
lineWidth1
paddingSM12

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.