# QR Code (Components)

URL: https://qrdx.dev/docs/components/qr-code
Source: https://github.com/qrdxinc/qrdx/blob/main/apps/docs/content/docs/components/qr-code.mdx

A styled, accessible QR code with download buttons for shadcn/ui projects. Colors, gradients, patterns, logos and frame templates come from the qrdx library.

`QrCode` renders a QR code as an SVG with an accessible name, a caption and PNG and SVG download buttons, in one component you own. Use it wherever a page shows a code someone may want to save: an order confirmation, a ticket, a menu, a Wi-Fi card.

## Installation [#installation]

```bash
npx shadcn@latest add @qrdx/qr-code
```

Or by URL, which works with older versions of the CLI too:

```bash
npx shadcn@latest add https://qrdx.dev/r/qr-code.json
```

## Usage [#usage]

```tsx title="components/example.tsx"
import { QrCode } from "@/components/qr-code";
```

```tsx title="components/example.tsx"
<QrCode value="https://qrdx.dev" />
```

The code renders as an SVG inside a `role="img"` box, so it stays sharp at any size and screen readers announce what it links to. Two buttons under it save a 1024 px PNG and an SVG.

## Examples [#examples]

### Sizes [#sizes]

`size` sets the on-screen width and height in pixels. Downloads use `downloadSize`, so a small code on the page still exports a print-ready file.

### Colors [#colors]

`fgColor` and `bgColor` take any CSS color. `eyeColor` and `dotColor` color the three corner eyes separately. Keep the modules darker than the background so every scanner can read the code.

### Gradients [#gradients]

Pass a qrdx gradient instead of a color: `linear` with an `angle`, or `radial`. Gradients carry over into PNG, SVG and PDF downloads.

### Patterns [#patterns]

`bodyPattern` shapes the data modules, `cornerEyePattern` the corner frames and `cornerEyeDotPattern` the dots inside them. [Choose module and eye shapes](https://qrdx.dev/docs/features/patterns) lists every shape.

### Logo [#logo]

`logo` places an image in the center and clears the modules behind it. Use error correction level `H` so the code still scans with part of it covered. A data URL keeps the logo in downloads; a remote image needs CORS headers.

### Frame templates [#frame-templates]

`templateId` draws one of qrdx's [frame templates](https://qrdx.dev/docs/features/templates) around the code. Pair it with `caption` for a call to action under the frame.

### Download formats [#download-formats]

`downloadFormats` picks the buttons: `png`, `jpg`, `svg` and `pdf`. PDF export needs the optional `jspdf` package (`npm install jspdf`). Pass `[]` to hide the buttons.

For a single menu with formats and sizes, use [QR Code Download](https://qrdx.dev/docs/components/qr-code-download).

### Dark mode [#dark-mode]

The default white background is the safest choice in dark themes: dark modules on a light tile scan with every reader. To follow the theme instead, set `fgColor="currentColor"` and a transparent background. The code then inverts in dark mode, which phone cameras read but some older scanners do not.

## In a Next.js page [#in-a-nextjs-page]

`QrCode` is a Client Component (its file starts with `"use client"` because of the download buttons), so a Server Component page can import it and pass server data as props:

```tsx title="app/menu/page.tsx"
import { QrCode } from "@/components/qr-code";

export default function MenuPage() {
  return (
    <QrCode
      value="https://example.com/menu"
      caption="Scan to see the menu"
      filename="menu-qr"
    />
  );
}
```

The code itself is still rendered on the server, so it is in the page's HTML before the buttons become interactive. For more on rendering codes in the App Router, see [QR codes in Next.js](https://qrdx.dev/docs/nextjs).

## Any content, not only links [#any-content-not-only-links]

`value` is encoded exactly as you pass it, so any QR payload works: a Wi-Fi network, a contact card, `mailto:`, `tel:` or plain text. Set `label` so screen readers announce what the code does rather than the raw payload, which for Wi-Fi includes the password.

```tsx title="components/guest-wifi.tsx"
import { QrCode } from "@/components/qr-code";

export function GuestWifi() {
  return (
    <QrCode
      value="WIFI:T:WPA;S:Cafe Guest;P:sunshine123;;"
      label="QR code to join the Cafe Guest Wi-Fi"
      caption="Cafe Guest Wi-Fi"
    />
  );
}
```

To build the payload from a form, use [Wi-Fi QR Code](https://qrdx.dev/docs/components/wifi-qr-code) or [vCard QR Code](https://qrdx.dev/docs/components/vcard-qr-code), which escape special characters for you.

## A value from an input [#a-value-from-an-input]

```tsx title="components/link-qr-code.tsx"
"use client";

import { useState } from "react";
import { QrCode } from "@/components/qr-code";

export function LinkQrCode() {
  const [value, setValue] = useState("https://example.com");
  return (
    <div className="grid justify-items-center gap-4">
      <input
        aria-label="Link"
        className="w-full rounded-md border px-3 py-2"
        value={value}
        onChange={(event) => setValue(event.target.value)}
      />
      {value ? <QrCode value={value} downloadFormats={[]} /> : null}
    </div>
  );
}
```

`downloadFormats={[]}` hides the buttons while someone is still typing; show them once the value is final.

## Print-ready downloads [#print-ready-downloads]

```tsx title="components/table-code.tsx"
import { QrCode } from "@/components/qr-code";

export function TableCode() {
  return (
    <QrCode
      value="https://example.com/order?table=12"
      level="H"
      margin={4}
      downloadFormats={["svg", "png", "pdf"]}
      downloadSize={2048}
      filename="table-12"
    />
  );
}
```

- **SVG** scales to any size without blurring, so it is the best file for a print shop or a design tool.
- **PNG** at `downloadSize={2048}` prints sharply up to about 17 cm (6.8 inches) wide at 300 pixels per inch. `downloadSize` takes 50 to 5000 pixels.
- **PDF** needs the optional `jspdf` package (`npm install jspdf`).
- `filename` names the file without its extension: `table-12.svg`, `table-12.png`.

## Accessibility [#accessibility]

- The code sits in a `role="img"` element whose `aria-label` is `label`, or `QR code: <value>` without one. Set `label` whenever the value is not a public link.
- The download buttons are a `<fieldset>` with a visually hidden legend, "Download QR code". A download's result is announced through a polite live region, and an error shows under the buttons with `role="alert"`.
- `caption` renders as the figure's `<figcaption>`.

## Make sure it scans [#make-sure-it-scans]

- **Contrast.** Dark modules on a light background scan with every reader. A light code on a dark background works on most phone cameras but not on some older scanners.
- **Quiet zone.** `margin` defaults to 2 modules. Use 4 when the code sits on a photo, a pattern or a dark page.
- **Logos.** Use `level="H"` with a `logo`, so the code survives the modules the logo covers.
- **Print size.** A short link scanned from about 25 cm needs a code about 2.5 cm wide; the [print size calculator](https://qrdx.dev/tools/print-size) works it out for other distances.
- **Test it.** Scan the finished code with a few phones, or upload an image to the [scan checker](https://qrdx.dev/tools/scan-checker).

## API reference [#api-reference]

### $Fumadocs

| Prop                   | Type        | Description                                                                                                           |
| ---------------------- | ----------- | --------------------------------------------------------------------------------------------------------------------- |
| `value`                | `string`    | Text or URL to encode.                                                                                                |
| `size?`                | `number`    | Rendered width and height in pixels. Default: `200`                                                                   |
| `fgColor?`             | `union`     | Module color: any CSS color or a qrdx gradient. Default: `"#000000"`                                                  |
| `bgColor?`             | `union`     | Background color: any CSS color or a qrdx gradient. Default: `"#FFFFFF"`                                              |
| `eyeColor?`            | `union`     | Corner eye color. Defaults to `fgColor`.                                                                              |
| `dotColor?`            | `union`     | Color of the dot inside each corner eye. Defaults to `fgColor`.                                                       |
| `bodyPattern?`         | `union`     | Shape of the data modules. Default: `"circle"`                                                                        |
| `cornerEyePattern?`    | `union`     | Shape of the corner eye frames. Default: `"gear"`                                                                     |
| `cornerEyeDotPattern?` | `union`     | Shape of the dot inside each corner eye. Default: `"circle"`                                                          |
| `level?`               | `union`     | Error correction level. Use "H" with a logo. Default: `"Q"`                                                           |
| `margin?`              | `number`    | Quiet zone around the code, in modules. Default: `2`                                                                  |
| `templateId?`          | `string`    | qrdx frame template id, e.g. "Arrow" or "StandardBox".                                                                |
| `logo?`                | `string`    | Center logo: a data URL or an image URL served with CORS headers.                                                     |
| `label?`               | `string`    | Accessible name. Defaults to "QR code: <value>".                                                                      |
| `caption?`             | `ReactNode` | Visible caption under the code.                                                                                       |
| `downloadFormats?`     | `array`     | Download buttons to show. Pass `[]` to hide them. "pdf" needs the optional `jspdf` package. Default: `["png", "svg"]` |
| `downloadSize?`        | `number`    | Width and height of downloaded files in pixels (50 to 5000). Default: `1024`                                          |
| `filename?`            | `string`    | Download file name without extension. Default: `"qr-code"`                                                            |

Any other prop, such as `className`, is passed to the wrapping `<figure>`.

## Related [#related]

- [QR Code Download](https://qrdx.dev/docs/components/qr-code-download): One menu with every format and size, instead of a row of buttons.
- [QR Code Card](https://qrdx.dev/docs/components/qr-code-card): The code in a card with a title, a description and actions.
- [Choose module and eye shapes](https://qrdx.dev/docs/features/patterns): Every value bodyPattern, cornerEyePattern and cornerEyeDotPattern take.
- [shadcn/ui registry](https://qrdx.dev/docs/developers/shadcn): All eight components and how the registry installs them.
