Vitral 0.2.18
Media

QRCode

A QR code drawn as one SVG path, encoded in the browser with no service and no dependency: sharp at any size, printable, and named for a screen reader.

Import

main.ts
import { QRCode } from '@vitral/vue';

Basic

Type and the code follows. It picks the smallest version that holds the text, and the densest mode — digits, capitals or bytes — that the text allows.

<script setup lang="ts">import { InputText, QRCode } from '@vitral/vue';import { ref } from 'vue'; const text = ref('https://vitral.dev');</script> <template>    <div style="display: flex; flex-wrap: wrap; gap: 1.5rem; align-items: center">        <QRCode :value="text" />        <InputText v-model="text" aria-label="Text to encode" style="flex: 1 1 16rem" />    </div></template>

Size and colour

`size` is any CSS length. Keep `color` darker than `background`: most phone cameras will not read a light code on a dark ground.

<script setup lang="ts">import { QRCode } from '@vitral/vue';</script> <template>    <div style="display: flex; flex-wrap: wrap; gap: 1.5rem; align-items: end">        <QRCode value="WIFI:T:WPA;S:Vitral Guest;P:stained-glass;;" size="6rem" label="Wi-Fi: Vitral Guest" />        <QRCode value="WIFI:T:WPA;S:Vitral Guest;P:stained-glass;;" size="8rem" color="#1e3a8a" background="#eff6ff" label="Wi-Fi: Vitral Guest" />        <QRCode value="WIFI:T:WPA;S:Vitral Guest;P:stained-glass;;" size="10rem" color="#14532d" :margin="2" label="Wi-Fi: Vitral Guest" />    </div></template>

A logo in the middle

With an `image` the code is encoded at level H, which survives losing 30% of it, so the logo hides nothing a reader needs. `imageSize` is its width against the code's.

<script setup lang="ts">import { QRCode } from '@vitral/vue'; // The Vitral mark, as the site draws it; any image the page can load works.const logo =    'data:image/svg+xml;utf8,' +    encodeURIComponent(        '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#2563eb"/><stop offset=".5" stop-color="#3b82f6"/><stop offset="1" stop-color="#06b6d4"/></linearGradient></defs><rect width="32" height="32" rx="8" fill="url(#g)"/><path d="M9 9h4l3 9 3-9h4l-5.5 14h-3z" fill="#fff"/></svg>'    );</script> <template>    <div style="display: flex; flex-wrap: wrap; gap: 1.5rem; align-items: end">        <QRCode value="https://vitral.dev/components/qrcode" :image="logo" size="11rem" />        <QRCode value="https://vitral.dev/components/qrcode" :image="logo" :image-size="0.3" size="11rem" color="#1e3a8a" />    </div></template>

API

Read from packages/vue/src/components/QRCode/types.ts, so it says what the component actually accepts.

Props

NameTypeDescription
value * stringWhat the code says: a link, a Wi-Fi setting, any text.
ecc 'L' | 'M' | 'Q' | 'H'How much of the code can be lost and still read: `L` 7%, `M` 15%, `Q` 25%, `H` 30%. Defaults to `M`, and to `H` with an `image` over it.
margin numberLight squares around the code, which a reader needs to find it. Defaults to 4.
size stringAny CSS length; the theme's (`10rem`) otherwise.
color stringThe dark squares' colour. Keep it darker than `background`, or phones will not read it.
background string—
image stringA logo in the middle.
imageSize numberThe logo's width, as a share of the code's. Defaults to 0.22.
label stringNames the code for a screen reader; the value it holds otherwise.

Plus pt, dt and unstyled from BaseProps, see pass-through and unstyled mode.