# QRCode

> A zero-dependency QR Code encoder and SVG component with automatic modes and versions 1 to 40, the four error correction levels, mask selection, GS1 element strings and a logo in a cleared center.

Canonical: https://salmonui.dev/docs/data-visualization/qr-code

```tsx
import { QRCode } from '@salmonui/charts/barcode';
import '@salmonui/charts/barcode/styles.css';

<QRCode value="https://salmonui.dev" errorCorrection="q" />;
```

`import { QRCode } from '@salmonui/charts/barcode'` alone stays under 6 KB
gzip: it imports only the QR encoder, not the 1D or Data Matrix code.

## Encoding

- `mode`: `'auto'` (default) picks numeric, alphanumeric or byte (UTF-8),
  whichever encodes the whole value; or force one.
- `version`: `'auto'` (default, the smallest of 1 to 40 that fits) or a fixed
  version; a value that does not fit renders the invalid state.
- `errorCorrection: 'l' | 'm' | 'q' | 'h'` (default `'m'`), Reed-Solomon over
  GF(256) with the ISO block structure; the mask with the lowest ISO penalty
  is chosen unless `mask` forces one. A version 40 symbol encodes in a few
  milliseconds.
- `gs1`: the value is a GS1 element string (`(01)…(10)…`), encoded with FNC1
  in first position.

## Logo

`logo` (any React content) is centered over a cleared area of about 22 % of
the symbol width, and the default error correction rises to `'h'` so the
covered modules are recoverable. An explicit `errorCorrection`
still wins.

## Shared options

`moduleSize` (default 4 px) or `width` (width and height, the symbol is
square), `quietZone`, `text` (a caption under the symbol, off by default),
`color`, `background`, `label`, `invalidLabel`, `onInvalid` and the `ref`
API (`QRCodeApi`, the same as `BarcodeApi`) work as on
[Barcode](https://salmonui.dev/docs/data-visualization/barcode). The symbol is one
`<svg role="img">` named by `label` (default the value), renders its final
markup on the server, and draws only as SVG (see
[Barcode](https://salmonui.dev/docs/data-visualization/barcode#svg-only)).

All props and types are in the
[API reference](https://salmonui.dev/docs/reference/charts/barcode).

### Live demo: QR codes

Live demo: https://salmonui.dev/demos/data-visualization/qr-code-basic

A QR code that follows a text field, the four error correction levels, a fixed version, a logo in a cleared center and PNG export through the ref.

`Demo.tsx`:

```tsx
'use client';

import { useRef, useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Radio, RadioGroup } from '@salmonui/inputs/radio-group';
import { TextField } from '@salmonui/inputs/text-field';
import { QRCode, type QRCodeApi, type QRErrorCorrection } from '@salmonui/charts/barcode';

// A live QR code with an error correction switch, a logo variant and export.
export default function QRCodeBasicDemo() {
  const [value, setValue] = useState('https://salmonui.dev/docs/data-visualization/qr-code');
  const [level, setLevel] = useState<QRErrorCorrection>('m');
  const ref = useRef<QRCodeApi>(null);
  return (
    <div style={{ display: 'grid', gap: 16 }}>
      <TextField label="Value" value={value} onValueChange={setValue} style={{ maxInlineSize: 480 }} />
      <RadioGroup label="Error correction" name="qr-level" orientation="horizontal" value={level} onValueChange={(next) => setLevel(next as QRErrorCorrection)}>
        {(['l', 'm', 'q', 'h'] as const).map((l) => (
          <Radio key={l} value={l}>
            {l.toUpperCase()}
          </Radio>
        ))}
      </RadioGroup>
      <div style={{ display: 'flex', flexWrap: 'wrap', gap: 24, alignItems: 'flex-start' }}>
        <QRCode ref={ref} value={value || ' '} errorCorrection={level} moduleSize={4} />
        <QRCode value={value || ' '} logo={<span style={{ background: '#fff', font: '700 14px system-ui', padding: 2 }}>S</span>} moduleSize={4} />
        <QRCode value="HELLO WORLD" version={2} errorCorrection="q" moduleSize={4} text />
      </div>
      <div>
        <Button variant="outline" type="button" onClick={() => void ref.current?.export({ format: 'png', fileName: 'qr-code.png' })}>
          Download PNG
        </Button>
      </div>
    </div>
  );
}
```
