QR Code

Draws a QR code for a link or other text.

Usage

Code
import { QrCode } from '@leverui/components';
Code
<QrCode.Root value="...">
  <QrCode.Frame>
    <QrCode.Pattern />
  </QrCode.Frame>
</QrCode.Root>

For one component instead of parts, see the snippet below.

Examples

Sizes

Use the size prop to set the size of the QR code.

Logo Overlay

Pass the children prop to the QrCode.Overlay component to add a logo or overlay to the QR code.

Fill

Use the fill prop to set the fill color of the QR code.

Download

Use the QrCode.DownloadTrigger to download the QR code.

The fileName and the mimeType props are required.

Error Correction

In cases where the link is too long or the logo overlay covers a significant area, the error correction level can be increased.

Use the encoding.ecc or encoding.boostEcc property to set the error correction level:

  • L: Allows recovery of up to 7% data loss (default)
  • M: Allows recovery of up to 15% data loss
  • Q: Allows recovery of up to 25% data loss
  • H: Allows recovery of up to 30% data loss

Store

The RootProvider component provides a context for the QR code.

It accepts the value of the useQrCode hook. You can leverage it to access the component state and methods from outside the QR code.

Input

Here's an example of how to use the QrCode component with an Input component.

Spinner

Here's an example of how to use the QrCode component with a Spinner component.

Closed Component

Code
import { Show, omit } from 'solid-js';
import type { JSX } from '@solidjs/web';
import { QrCode, type QrCodeRootProps } from '@leverui/components';

export type ClosedQrCodeProps = QrCodeRootProps & {
  fill?: string | undefined;
  overlay?: JSX.Element;
};

export function ClosedQrCode(props: ClosedQrCodeProps) {
  const root = omit(props, 'children', 'fill', 'overlay');
  return (
    <QrCode.Root {...root}>
      <QrCode.Frame style={props.fill ? { fill: props.fill } : {}}>
        <QrCode.Pattern />
      </QrCode.Frame>
      {props.overlay}
      <Show when={props.children}>
        <QrCode.Overlay>{props.children}</QrCode.Overlay>
      </Show>
    </QrCode.Root>
  );
}

Guides

Generated pattern

QrCode.Frame draws the pattern when it has no children. Add QrCode.Pattern yourself to put other SVG children next to it.

Give the frame an aria-label when the code carries information the nearby text does not. When an overlay covers more of the pattern, raise encoding.ecc to M, Q, or H.

External machine

useQrCode creates the state machine outside the component and returns an accessor. Pass it to QrCode.RootProvider. Call qrCode().setValue(nextValue) to change the value. The hook is also QrCode.useQrCode. QrCode.Context and useQrCodeContext() read the nearest machine from inside a QR code. QrCode.PropsProvider sets default props. They stay reactive.

Props

Root

PropDefaultTypeDescription
sizemd'2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'full'QR code size. Responsive.
asdivNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the root's props.
unstyledfalsebooleanTurn off the Lever UI styles.
defaultValue—stringInitial value to encode. Uncontrolled.
encoding—QrCodeGenerateOptionsEncoding options.
id—stringUnique id of the state machine.
ids—Element ID mapYour own ids for the elements.
onValueChange—(details) => voidCalled when the value changes.
pixelSize—numberPixel size of the QR code.
value—stringControlled value to encode.

DownloadTrigger

PropDefaultTypeDescription
fileName (required)—stringName of the downloaded file.
mimeType (required)—DataUrlTypeMIME type of the downloaded image.
asChild—Solid render functionRender your own element with the trigger's props.
quality—numberQuality of the downloaded image.

Customize with CSS

Import @leverui/components/styles.css before your own stylesheet. Classes: .leverui-qr-code__*. Set --leverui-qr-code-size or --leverui-qr-code-overlay-size on a root to resize the code or its overlay. Restyle a part with your own class. The SVG fill takes a CSS color or a --leverui-* color variable.

unstyled turns off the default styles of the root, frame, and overlay. The Pattern and DownloadTrigger have classes but no default styles.

Explorer

Click a part name to highlight it in the preview.

Component Anatomy

Hover to highlight, click to select parts