Skip to content

QR Code Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Show a text as a QR code that updates as the user types. Setting the value property redraws the code, and :state(--empty) hides it while the input is empty.

--empty
Encoding
4
Style
10rem
Sets inline-size .
fill color
Sets --sv-qr-code-fill-clr .
finder color
Sets --sv-qr-code-finder-clr .
background
Sets --sv-qr-code-bg .
<div class="qr-code-basic">
<sv-qr-code
aria-label="QR code of the text below"
value="https://example.com/sign-up"
></sv-qr-code>
<input
type="text"
class="qr-code-basic-field"
aria-label="Text to encode"
name="qr-code-text"
value="https://example.com/sign-up"
/>
</div>
<script type="application/x-typescript" module>
const input = document.querySelector<HTMLInputElement>(".qr-code-basic-field")!;
const qrCode = document.querySelector<QrCode>(".qr-code-basic sv-qr-code")!;
input.addEventListener("input", () => {
qrCode.value = input.value;
});
</script>
<style>
.qr-code-basic {
display: grid;
gap: 1em;
place-content: center;
justify-items: center;
sv-qr-code {
inline-size: 10rem;
}
sv-qr-code:state(--empty) {
visibility: hidden;
}
input {
inline-size: 16rem;
padding: 0.4em 0.75em;
font: inherit;
font-size: 0.875rem;
color: inherit;
background-color: var(--sv-gray-7);
border: solid var(--sv-bdr-sz-sm) var(--sv-gray-5);
border-radius: var(--sv-bdr-rad-md);
}
}
</style>

Put a brand logo in the center of the code. The logo slot holds the logo and clears the modules under it, and error-correction="H" keeps the code readable.

0.2
<div class="qr-code-logo">
<sv-qr-code
aria-label="QR code to download the app"
error-correction="H"
value="https://example.com/app"
>
<!-- Logo -->
<svg aria-hidden="true" slot="logo" viewBox="0 0 24 24">
<rect fill="#4f46e5" height="24" rx="6" width="24" />
<path
d="M7 12.5 10.5 16 17 8.5"
fill="none"
stroke="#fff"
stroke-linecap="round"
stroke-width="2.5"
/>
</svg>
</sv-qr-code>
</div>
<style>
.qr-code-logo {
display: grid;
place-content: center;
sv-qr-code {
inline-size: 10rem;
}
svg {
display: block;
inline-size: 80%;
}
}
</style>

Since a large logo-size at a low error-correction level can make the code unreadable, test each setting with a scanner.

Download

Let the user save the code as an SVG file. getSvgMarkup() returns a standalone SVG with the current colors, and a Blob link downloads it.

<div class="qr-code-download">
<sv-qr-code aria-label="QR code to the menu" value="https://example.com/menu"></sv-qr-code>
<button type="button" class="btn btn-primary">Download SVG</button>
</div>
<script type="application/x-typescript" module>
const qrCode = document.querySelector<QrCode>(".qr-code-download sv-qr-code")!;
const button = document.querySelector<HTMLButtonElement>(".qr-code-download button")!;
button.addEventListener("click", () => {
const svgBlob = new Blob([qrCode.getSvgMarkup()], { type: "image/svg+xml" });
const link = document.createElement("a");
link.href = URL.createObjectURL(svgBlob);
link.download = "qr-code.svg";
link.click();
setTimeout(() => URL.revokeObjectURL(link.href));
});
</script>
<style>
.qr-code-download {
display: grid;
gap: 1em;
place-content: center;
justify-items: center;
sv-qr-code {
inline-size: 10rem;
}
}
</style>