Skip to main content
Default Gray Amethyst

QR Code

<mo-qr-code> | MOQrCode
Since 1.0 experimental

QR code is a machine-readable code consisting of an array of black and white squares.

QR codes are useful for providing small pieces of information to users who can quickly scan them with a smartphone. Most smartphones have built-in QR code scanners, so simply pointing the camera at a QR code will decode it and allow the user to visit a website, dial a phone number, read a message, etc.


<div class="qr-overview">
  <mo-qr-code value="https://metso.com/" label="Scan this code to visit Metso on the web!"></mo-qr-code>
  <br />

  <mo-input maxlength="255" clearable></mo-input>
</div>

<script>
  const container = document.querySelector('.qr-overview');
  const qrCode = container.querySelector('mo-qr-code');
  const input = container.querySelector('mo-input');

  input.value = qrCode.value;
  input.addEventListener('mo-input', () => (qrCode.value = input.value));
</script>

<style>
  .qr-overview {
    max-width: 256px;
  }

  .qr-overview mo-input {
    margin-top: 1rem;
  }
</style>
import { useState } from 'react';
import { MOQrCode, MOInput } from '@metsooutotec/modes-web-components/dist/react';

const css = `
  .qr-overview {
    max-width: 256px;
  }

  .qr-overview mo-input {
    margin-top: 1rem;
  }
`;

const App = () => {
  const [value, setValue] = useState('https://metso.com/');

  return (
    <>
      <div className="qr-overview">
        <MOQrCode value={value} label="Scan this code to visit Metso on the web!" />
        <br />

        <MOInput maxlength="255" clearable onInput={event => setValue(event.target.value)} />
      </div>

      <style>{css}</style>
    </>
  );
};

Examples

Colors

Use the fill and background attributes to modify the QR code’s colors. You should always ensure good contrast for optimal compatibility with QR code scanners.

<mo-qr-code value="https://metso.com/" fill="deeppink" background="white"></mo-qr-code>
import { MOQrCode } from '@metsooutotec/modes-web-components/dist/react';

const App = () => <MOQrCode value="https://metso.com/" fill="deeppink" background="white" />;

Size

Use the size attribute to change the size of the QR code.

<mo-qr-code value="https://metso.com/" size="64"></mo-qr-code>
import { MOQrCode } from '@metsooutotec/modes-web-components/dist/react';

const App = () => <MOQrCode value="https://metso.com/" size="64" />;

Radius

Create a rounded effect with the radius attribute.

<mo-qr-code value="https://metso.com/" radius="0.5"></mo-qr-code>
import { MOQrCode } from '@metsooutotec/modes-web-components/dist/react';

const App = () => <MOQrCode value="https://metso.com/" radius="0.5" />;

Error correction

QR codes can be rendered with various levels of error correction that can be set using the error-correction attribute. This example generates four codes with the same value using different error correction levels.

<div class="qr-error-correction">
  <mo-qr-code value="https://metso.com/" error-correction="L"></mo-qr-code>
  <mo-qr-code value="https://metso.com/" error-correction="M"></mo-qr-code>
  <mo-qr-code value="https://metso.com/" error-correction="Q"></mo-qr-code>
  <mo-qr-code value="https://metso.com/" error-correction="H"></mo-qr-code>
</div>

<style>
  .qr-error-correction {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
  }
</style>
import { MOQrCode } from '@metsooutotec/modes-web-components/dist/react';

const css = `
  .qr-error-correction {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
  }
`;

const App = () => {
  return (
    <>
      <div className="qr-error-correction">
        <MOQrCode value="https://metso.com/" error-correction="L" />
        <MOQrCode value="https://metso.com/" error-correction="M" />
        <MOQrCode value="https://metso.com/" error-correction="Q" />
        <MOQrCode value="https://metso.com/" error-correction="H" />
      </div>

      <style>{css}</style>
    </>
  );
};

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Bundler React Script

To import this component using a bundler:

import '@metsooutotec/modes-web-components/dist/components/qr-code/qr-code.js';

To import this component as a React component:

import MOQrCode from '@metsooutotec/modes-web-components/dist/react/qr-code/';

To import this component using a script tag:

<script type="module" src="https://modes-web.metso.com/dist/components/cdn/components/qr-code/qr-code.js"></script>

Properties

Name Description Reflects Type Default
value The QR code’s value. string ''
label The label used when screen readers announce the code. If unspecified, the value will be used. string ''
size The size of the code’s overall square in pixels. number 128
fill The fill color. This can be any valid CSS color, but not a CSS custom property. string '#000'
background The background color. This can be any valid CSS color or transparent, but not a CSS custom property. string '#fff'
radius The edge radius of each module. Must be between 0 and 0.5. number 0
errorCorrection
error-correction
The level of error correction to use. 'L' | 'M' | 'Q' | 'H' 'H'
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Parts

Name Description
base The component’s internal wrapper.

Learn more about customizing CSS parts.