# QR Code Generator (Components)

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

A text input with a live QR code preview, style presets and download buttons, for shadcn/ui projects.

`QrCodeGenerator` is a small generator in one component: a text field, a preview that redraws as you type, a row of style presets and download buttons. Use it to give your own users a way to make codes inside your product, without sending them anywhere else.

## Installation [#installation]

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

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

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

## Usage [#usage]

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

```tsx title="components/example.tsx"
<QrCodeGenerator defaultValue="https://example.com" />
```

The preview updates as you type. Each preset button shows a small sample of its style, and downloads use the selected preset at 1024 pixels. With an empty field the preview shows "Type some text to generate a code" and the download buttons are disabled.

## Examples [#examples]

### Custom presets [#custom-presets]

Replace the built-in presets with your own. A preset is an `id`, a `name` and a `style` with any qrdx option, including gradients, logos and error correction `level`.

The built-in presets are exported as `QR_CODE_PRESETS`, so you can extend them:

```tsx title="components/brand-generator.tsx"
import {
  QR_CODE_PRESETS,
  QrCodeGenerator,
} from "@/components/qr-code-generator";

export function BrandGenerator() {
  return (
    <QrCodeGenerator
      presets={[
        ...QR_CODE_PRESETS,
        { id: "brand", name: "Brand", style: { fgColor: "#155DFC" } },
      ]}
    />
  );
}
```

### Saving the result [#saving-the-result]

`onChange` reports the text and the selected preset on every change, for example to store the user's choice:

```tsx title="components/draft-generator.tsx"
import { QrCodeGenerator } from "@/components/qr-code-generator";

export function DraftGenerator({
  saveDraft,
}: {
  saveDraft: (draft: { value: string; style: object }) => void;
}) {
  return (
    <QrCodeGenerator
      onChange={({ value, preset }) =>
        saveDraft({ value, style: preset.style })
      }
    />
  );
}
```

## Accessibility and rendering [#accessibility-and-rendering]

- The field has a visible "Text or URL" label, and the presets are a `<fieldset>` with the legend "Style". Each preset is a toggle button with `aria-pressed`, and its sample code is hidden from screen readers.
- The preview is a `role="img"` element labelled `QR code: <value>`, or "Empty QR code preview" with no text.
- A download error shows under the preview with `role="alert"`.
- The component is a Client Component with its own state. Render it from any page; pass `defaultValue` and `defaultPreset` to start it filled.

## API reference [#api-reference]

### $Fumadocs

| Prop             | Type     | Description                                                                           |
| ---------------- | -------- | ------------------------------------------------------------------------------------- |
| `presets?`       | `array`  | Style presets to pick from. Default: `QR_CODE_PRESETS`                                |
| `defaultPreset?` | `string` | Id of the preset selected first. Defaults to the first preset.                        |
| `size?`          | `number` | Width and height of the preview in pixels. Default: `200`                             |
| `formats?`       | `array`  | Download buttons under the preview. Pass `[]` to hide them. Default: `["png", "svg"]` |
| `filename?`      | `string` | Download file name without extension. Default: `"qr-code"`                            |

### QrCodePreset [#qrcodepreset]

### QrCodePreset

| Prop    | Type          | Description |
| ------- | ------------- | ----------- |
| `id`    | `string`      |             |
| `name`  | `string`      |             |
| `style` | `QrCodeStyle` |             |

## Related [#related]

- [QR Code](https://qrdx.dev/docs/components/qr-code): A single code with download buttons, when the value comes from your data.
- [QR Code Download](https://qrdx.dev/docs/components/qr-code-download): A menu with more formats and sizes than the generator's buttons.
- [Set colors and gradients](https://qrdx.dev/docs/features/colors-gradients): Every style option a preset can hold.
