Mantine Qr Code

Logo

@gfazioli/mantine-qr-code

A highly customizable QR Code component for React applications built with Mantine.

Installation

yarn add @gfazioli/mantine-qr-code

After installation import package styles at the root of your application:

import '@gfazioli/mantine-qr-code/styles.css';

You can import styles within a layer @layer mantine-qr-code by importing @gfazioli/mantine-qr-code/styles.layer.css file.

import '@gfazioli/mantine-qr-code/styles.layer.css';

Usage

import { QRCode } from '@gfazioli/mantine-qr-code';

function Demo() {
  return (
    <QRCode value="https://mantine.dev" />
  );
}

Configurator

Explore all available props and see the component in action:

Color
Background
Size
Radius
Dot style
Corner style
Quiet zone
Image size
Image padding
import { QRCode } from '@gfazioli/mantine-qr-code';

function Demo() {
  return (
    <QRCode value="https://mantine.dev" radius="sm" errorCorrectionLevel="H" image="https://raw.githubusercontent.com/mantinedev/mantine/master/.demo/avatars/avatar-1.png"/>
  );
}

Colors

The QRCode component supports all Mantine theme colors. Use the color and background props to customize:

red

blue

violet

teal

orange

cyan

import { Group, Stack, Text } from '@mantine/core';
import { QRCode } from '@gfazioli/mantine-qr-code';

function Demo() {
  return (
    <Group>
      {['red', 'blue', 'violet', 'teal', 'orange', 'cyan'].map((color) => (
        <Stack key={color} align="center" gap="xs">
          <QRCode value="https://mantine.dev" color={color} />
          <Text size="xs" tt="capitalize">{color}</Text>
        </Stack>
      ))}
    </Group>
  );
}

Dot styles

Control the appearance of data modules with the dotStyle prop:

Square

Rounded

Dots

import { Group, Stack, Text } from '@mantine/core';
import { QRCode } from '@gfazioli/mantine-qr-code';

function Demo() {
  return (
    <Group>
      <Stack align="center" gap="xs">
        <QRCode value="https://mantine.dev" dotStyle="square" size="lg" />
        <Text size="xs">Square</Text>
      </Stack>
      <Stack align="center" gap="xs">
        <QRCode value="https://mantine.dev" dotStyle="rounded" size="lg" />
        <Text size="xs">Rounded</Text>
      </Stack>
      <Stack align="center" gap="xs">
        <QRCode value="https://mantine.dev" dotStyle="dots" size="lg" />
        <Text size="xs">Dots</Text>
      </Stack>
    </Group>
  );
}

Corner styles

Customize the three finder patterns (corners) with the cornerStyle prop:

Square

Rounded

Dots

import { Group, Stack, Text } from '@mantine/core';
import { QRCode } from '@gfazioli/mantine-qr-code';

function Demo() {
  return (
    <Group>
      <Stack align="center" gap="xs">
        <QRCode value="https://mantine.dev" cornerStyle="square" size="lg" />
        <Text size="xs">Square</Text>
      </Stack>
      <Stack align="center" gap="xs">
        <QRCode value="https://mantine.dev" cornerStyle="rounded" size="lg" />
        <Text size="xs">Rounded</Text>
      </Stack>
      <Stack align="center" gap="xs">
        <QRCode value="https://mantine.dev" cornerStyle="dots" size="lg" />
        <Text size="xs">Dots</Text>
      </Stack>
    </Group>
  );
}

Error correction

QR codes support four error correction levels. Higher levels allow more damage tolerance but produce denser codes:

L (7%)

M (15%)

Q (25%)

H (30%)

import { Group, Stack, Text } from '@mantine/core';
import { QRCode } from '@gfazioli/mantine-qr-code';

function Demo() {
  return (
    <Group>
      {(['L', 'M', 'Q', 'H'] as const).map((level) => (
        <Stack key={level} align="center" gap="xs">
          <QRCode value="https://mantine.dev" errorCorrectionLevel={level} size="lg" />
          <Text size="xs">{level} ({level === 'L' ? '7%' : level === 'M' ? '15%' : level === 'Q' ? '25%' : '30%'})</Text>
        </Stack>
      ))}
    </Group>
  );
}

Image overlay

Add a logo or image at the center of the QR code. Use errorCorrectionLevel="H" for best results when overlaying images:

import { Group } from '@mantine/core';
import { QRCode } from '@gfazioli/mantine-qr-code';

function Demo() {
  return (
    <Group>
      <QRCode
        value="https://mantine.dev"
        size="xl"
        image="https://raw.githubusercontent.com/mantinedev/mantine/master/.demo/avatars/avatar-1.png"
        errorCorrectionLevel="H"
      />
      <QRCode
        value="https://mantine.dev"
        size="xl"
        image="https://raw.githubusercontent.com/mantinedev/mantine/master/.demo/avatars/avatar-1.png"
        errorCorrectionLevel="H"
        dotStyle="dots"
        cornerStyle="dots"
        imageRadius="md"
      />
    </Group>
  );
}
  • imageSize (default 0.2): the image side as a fraction of the QR code, quiet zone excluded. Keep it small enough for the error correction level to recover the modules it hides.
  • imagePadding (default 1): the clearance around the image, in modules. The cleared area spans an odd number of whole modules so it stays centered, so the clearance can differ from imagePadding by up to half a module.
  • imageExcavate (default true): removes the modules behind the image and its padding. Set it to false to draw the image over the modules instead.

Download

Use the useQRCodeDownload hook to download the QR code as SVG, PNG, JPEG or WebP:

  • its options set the defaults for every call: format (png), fileName (qrcode) and scale for raster formats (4);
  • download() takes the same options to override them for one download;
  • getDataUrl() returns the image as a data URL instead of downloading it.

An image overlay is fetched and embedded in the export, so it must come from the same origin or a server that allows cross-origin requests (CORS), or be passed as a data URL. When it cannot be fetched, PNG, JPEG and WebP leave the image out and SVG keeps its URL. With background="transparent", PNG, WebP and SVG stay transparent, while JPEG, which has no transparency, gets a white background:

import { useState } from 'react';
import { Button, Group, Stack, Switch } from '@mantine/core';
import { QRCode, useQRCodeDownload } from '@gfazioli/mantine-qr-code';

function Demo() {
  const [transparent, setTransparent] = useState(false);
  const { ref, download } = useQRCodeDownload({ fileName: 'my-qr-code' });

  return (
    <Stack align="center">
      <QRCode
        ref={ref}
        value="https://mantine.dev"
        size="xl"
        image="https://raw.githubusercontent.com/mantinedev/mantine/master/.demo/avatars/avatar-1.png"
        errorCorrectionLevel="H"
        background={transparent ? 'transparent' : 'white'}
      />
      <Switch
        label="Transparent background (PNG, WebP and SVG)"
        checked={transparent}
        onChange={(event) => setTransparent(event.currentTarget.checked)}
      />
      <Group>
        {(['svg', 'png', 'jpeg', 'webp'] as const).map((format) => (
          <Button key={format} onClick={() => download({ format })} variant="light">
            {format.toUpperCase()}
          </Button>
        ))}
      </Group>
    </Stack>
  );
}

Styles API

QRCode supports Styles API, you can add styles to any inner element of the component with classNames prop. Follow Styles API documentation to learn more.

Component Styles API

Hover over selectors to highlight corresponding elements

/*
 * Hover over selectors to apply outline styles
 *
 */

Use cases

QR codes can encode various data types including URLs, WiFi credentials, and contact information:

URL

WiFi

vCard

import { Group, Stack, Text } from '@mantine/core';
import { QRCode } from '@gfazioli/mantine-qr-code';

function Demo() {
  return (
    <Group>
      <Stack align="center" gap="xs">
        <QRCode value="https://mantine.dev" size="lg" dotStyle="rounded" cornerStyle="rounded" />
        <Text size="xs">URL</Text>
      </Stack>
      <Stack align="center" gap="xs">
        <QRCode
          value="WIFI:T:WPA;S:MyNetwork;P:MyPassword;;"
          size="lg"
          dotStyle="dots"
          cornerStyle="dots"
          color="blue"
        />
        <Text size="xs">WiFi</Text>
      </Stack>
      <Stack align="center" gap="xs">
        <QRCode
          value="BEGIN:VCARD\nVERSION:3.0\nFN:John Doe\nTEL:+1234567890\nEMAIL:john@example.com\nEND:VCARD"
          size="lg"
          color="teal"
          errorCorrectionLevel="H"
        />
        <Text size="xs">vCard</Text>
      </Stack>
    </Group>
  );
}