# QR Code

URL: https://ark-ui.com/docs/components/qr-code
Source: https://raw.githubusercontent.com/chakra-ui/ark/refs/heads/main/website/src/content/pages/components/qr-code.mdx

A component that generates a QR code based on the provided data.

---

<ComponentPreview id="QrCode" />

## Anatomy

To set up the QR code correctly, you'll need to understand its anatomy and how we name its parts.

> Each part includes a `data-part` attribute to help identify them in the DOM.

<Anatomy id="qr-code" />

## Examples

Learn how to use the `QR Code` component in your project. Let's take a look at the most basic example:

**Example: basic**

#### React

```tsx
import { QrCode } from '@ark-ui/react/qr-code'

export const Basic = () => {
  return (
    <QrCode.Root defaultValue="http://ark-ui.com">
      <QrCode.Frame>
        <QrCode.Pattern />
      </QrCode.Frame>
      <QrCode.DownloadTrigger fileName="qr-code.png" mimeType="image/png">
        Download
      </QrCode.DownloadTrigger>
    </QrCode.Root>
  )
}

```

#### Solid

```tsx
import { QrCode } from '@ark-ui/solid/qr-code'

export const Basic = () => {
  return (
    <QrCode.Root value="http://ark-ui.com">
      <QrCode.Frame>
        <QrCode.Pattern />
      </QrCode.Frame>
      <QrCode.DownloadTrigger fileName="qr-code.png" mimeType="image/png">
        Download
      </QrCode.DownloadTrigger>
    </QrCode.Root>
  )
}

```

#### Vue

```vue
<script setup lang="ts">
import { QrCode } from '@ark-ui/vue/qr-code'
</script>

<template>
  <QrCode.Root defaultValue="http://ark-ui.com">
    <QrCode.Frame>
      <QrCode.Pattern />
    </QrCode.Frame>
    <QrCode.DownloadTrigger fileName="qr-code.png" mimeType="image/png">Download</QrCode.DownloadTrigger>
  </QrCode.Root>
</template>

```

#### Svelte

```svelte
<script lang="ts">
  import { QrCode } from '@ark-ui/svelte/qr-code'
</script>

<QrCode.Root defaultValue="http://ark-ui.com">
  <QrCode.Frame>
    <QrCode.Pattern />
  </QrCode.Frame>
  <QrCode.DownloadTrigger fileName="qr-code.png" mimeType="image/png">Download</QrCode.DownloadTrigger>
</QrCode.Root>

```

### With Overlay

You can also add a logo or overlay to the QR code. This is useful when you want to brand the QR code.

**Example: with-overlay**

#### React

```tsx
import { QrCode } from '@ark-ui/react/qr-code'

export const WithOverlay = () => {
  return (
    <QrCode.Root defaultValue="http://ark-ui.com" encoding={{ ecc: 'H' }}>
      <QrCode.Frame>
        <QrCode.Pattern />
      </QrCode.Frame>
      <QrCode.Overlay>
        <img src="https://ark-ui.com/icon-192.png" alt="Ark UI Logo" />
      </QrCode.Overlay>
    </QrCode.Root>
  )
}

```

#### Solid

```tsx
import { QrCode } from '@ark-ui/solid/qr-code'

export const WithOverlay = () => {
  return (
    <QrCode.Root defaultValue="http://ark-ui.com">
      <QrCode.Frame>
        <QrCode.Pattern />
      </QrCode.Frame>
      <QrCode.Overlay>
        <img src="https://ark-ui.com/icon-192.png" alt="Ark UI Logo" />
      </QrCode.Overlay>
    </QrCode.Root>
  )
}

```

#### Vue

```vue
<script setup lang="ts">
import { QrCode } from '@ark-ui/vue/qr-code'
</script>

<template>
  <QrCode.Root defaultValue="http://ark-ui.com">
    <QrCode.Frame>
      <QrCode.Pattern />
    </QrCode.Frame>
    <QrCode.Overlay>
      <img src="https://ark-ui.com/icon-192.png" alt="Ark UI Logo" />
    </QrCode.Overlay>
  </QrCode.Root>
</template>

```

#### Svelte

```svelte
<script lang="ts">
  import { QrCode } from '@ark-ui/svelte/qr-code'
</script>

<QrCode.Root defaultValue="http://ark-ui.com" encoding={{ ecc: 'H' }}>
  <QrCode.Frame>
    <QrCode.Pattern />
  </QrCode.Frame>
  <QrCode.Overlay>
    <img src="https://ark-ui.com/icon-192.png" alt="Ark UI Logo" />
  </QrCode.Overlay>
</QrCode.Root>

```

### 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

**Example: error-correction**

#### React

```tsx
import { QrCode } from '@ark-ui/react/qr-code'

export const ErrorCorrection = () => {
  return (
    <QrCode.Root defaultValue="http://ark-ui.com" encoding={{ ecc: 'H' }}>
      <QrCode.Frame>
        <QrCode.Pattern />
      </QrCode.Frame>
    </QrCode.Root>
  )
}

```

#### Solid

```tsx
import { QrCode } from '@ark-ui/solid/qr-code'

export const ErrorCorrection = () => {
  return (
    <QrCode.Root defaultValue="http://ark-ui.com" encoding={{ ecc: 'H' }}>
      <QrCode.Frame>
        <QrCode.Pattern />
      </QrCode.Frame>
    </QrCode.Root>
  )
}

```

#### Vue

```vue
<script setup lang="ts">
import { QrCode, type QrCodeGenerateOptions } from '@ark-ui/vue/qr-code'
import { ref } from 'vue'

const encoding = ref<QrCodeGenerateOptions>({ ecc: 'H' })
</script>

<template>
  <QrCode.Root defaultValue="http://ark-ui.com" :encoding="encoding">
    <QrCode.Frame>
      <QrCode.Pattern />
    </QrCode.Frame>
  </QrCode.Root>
</template>

```

#### Svelte

```svelte
<script lang="ts">
  import { QrCode } from '@ark-ui/svelte/qr-code'
</script>

<QrCode.Root defaultValue="http://ark-ui.com" encoding={{ ecc: 'H' }}>
  <QrCode.Frame>
    <QrCode.Pattern />
  </QrCode.Frame>
</QrCode.Root>

```

### Using the Root Provider

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.

**Example: root-provider**

#### React

```tsx
import { QrCode, useQrCode } from '@ark-ui/react/qr-code'
import { useState } from 'react'

export const RootProvider = () => {
  const [value, setValue] = useState('http://ark-ui.com')
  const qrCode = useQrCode({ value })

  return (
    <>
      <button
        onClick={() => {
          setValue('https://chakra-ui.com')
        }}
      >
        Set Value
      </button>
      <QrCode.RootProvider value={qrCode}>
        <QrCode.Frame>
          <QrCode.Pattern />
        </QrCode.Frame>
      </QrCode.RootProvider>
    </>
  )
}

```

#### Solid

```tsx
import { QrCode, useQrCode } from '@ark-ui/solid/qr-code'

export const RootProvider = () => {
  const qrCode = useQrCode({ defaultValue: 'http://ark-ui.com' })

  return (
    <>
      <button onClick={() => qrCode().setValue('https://ark-ui().com')}>Set Value</button>

      <QrCode.RootProvider value={qrCode}>
        <QrCode.Frame>
          <QrCode.Pattern />
        </QrCode.Frame>
      </QrCode.RootProvider>
    </>
  )
}

```

#### Vue

```vue
<script setup lang="ts">
import { QrCode, useQrCode } from '@ark-ui/vue/qr-code'

const qrCode = useQrCode({ defaultValue: 'http://ark-ui.com' })
</script>

<template>
  <button @click="qrCode.setValue('https://ark-ui.com')">Set Value</button>

  <QrCode.RootProvider :value="qrCode">
    <QrCode.Frame>
      <QrCode.Pattern />
    </QrCode.Frame>
  </QrCode.RootProvider>
</template>

```

#### Svelte

```svelte
<script lang="ts">
  import { QrCode, useQrCode } from '@ark-ui/svelte/qr-code'

  const id = $props.id()
  const qrCode = useQrCode({ id, value: 'http://ark-ui.com' })
  const setValue = () => qrCode().setValue('https://ark-ui.com')
</script>

<button onclick={setValue}>Set Value</button>

<QrCode.RootProvider value={qrCode}>
  <QrCode.Frame>
    <QrCode.Pattern />
  </QrCode.Frame>
</QrCode.RootProvider>

```

> If you're using the `RootProvider` component, you don't need to use the `Root` component.

## Guides

### Download a QR Code

You can download the QR code by using the `QrCode.DownloadTrigger`. You will have to provide the `fileName` and the
`mimeType` of the image.

```tsx
<QrCode.DownloadTrigger fileName="qr-code.png" mimeType="image/png">
  Download
</QrCode.DownloadTrigger>
```

## API Reference

### Props

**Component API Reference**

#### React

**Root Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `defaultValue` | `string` | No | The initial value to encode when rendered.
Use when you don't need to control the value of the qr code. |
| `encoding` | `QrCodeGenerateOptions` | No | The qr code encoding options. |
| `id` | `string` | No | The unique identifier of the machine. |
| `ids` | `Partial<{ root: string; frame: string }>` | No | The element ids. |
| `onValueChange` | `(details: ValueChangeDetails) => void` | No | Callback fired when the value changes. |
| `pixelSize` | `number` | No | The pixel size of the qr code. |
| `value` | `string` | No | The controlled value to encode. |


**DownloadTrigger Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `fileName` | `string` | Yes | The name of the file. |
| `mimeType` | `DataUrlType` | Yes | The mime type of the image. |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `quality` | `number` | No | The quality of the image. |


**Frame Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


**Overlay Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


**Pattern Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


**RootProvider Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `UseQrCodeReturn` | Yes |  |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


#### Solid

**Root Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `defaultValue` | `string` | No | The initial value to encode when rendered.
Use when you don't need to control the value of the qr code. |
| `encoding` | `QrCodeGenerateOptions` | No | The qr code encoding options. |
| `ids` | `Partial<{ root: string; frame: string }>` | No | The element ids. |
| `onValueChange` | `(details: ValueChangeDetails) => void` | No | Callback fired when the value changes. |
| `pixelSize` | `number` | No | The pixel size of the qr code. |
| `value` | `string` | No | The controlled value to encode. |


**DownloadTrigger Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `fileName` | `string` | Yes | The name of the file. |
| `mimeType` | `DataUrlType` | Yes | The mime type of the image. |
| `asChild` | `(props: ParentProps<'button'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `quality` | `number` | No | The quality of the image. |


**Frame Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `(props: ParentProps<'svg'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


**Overlay Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


**Pattern Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `(props: ParentProps<'path'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


**RootProvider Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `UseQrCodeReturn` | Yes |  |
| `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


#### Vue

**Root Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `defaultValue` | `string` | No | The initial value to encode when rendered.
Use when you don't need to control the value of the qr code. |
| `encoding` | `QrCodeGenerateOptions` | No | The qr code encoding options. |
| `id` | `string` | No | The unique identifier of the machine. |
| `ids` | `Partial<{ root: string; frame: string }>` | No | The element ids. |
| `modelValue` | `string` | No | The v-model value of the qr code |
| `pixelSize` | `number` | No | The pixel size of the qr code. |


**DownloadTrigger Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `fileName` | `string` | Yes | The name of the file. |
| `mimeType` | `DataUrlType` | Yes | The mime type of the image. |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `quality` | `number` | No | The quality of the image. |


**Frame Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


**Overlay Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


**Pattern Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


**RootProvider Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `QrCodeApi<PropTypes>` | Yes |  |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


#### Svelte

**Root Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `defaultValue` | `string` | No | The initial value to encode when rendered.
Use when you don't need to control the value of the qr code. |
| `encoding` | `QrCodeGenerateOptions` | No | The qr code encoding options. |
| `id` | `string` | No | The unique identifier of the machine. |
| `ids` | `Partial<{ root: string; frame: string }>` | No | The element ids. |
| `onValueChange` | `(details: ValueChangeDetails) => void` | No | Callback fired when the value changes. |
| `pixelSize` | `number` | No | The pixel size of the qr code. |
| `ref` | `Element` | No |  |
| `value` | `string` | No | The controlled value to encode. |


**Context Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `api` | `Snippet<[UseQrCodeContext]>` | No |  |


**DownloadTrigger Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `fileName` | `string` | Yes | The name of the file. |
| `mimeType` | `DataUrlType` | Yes | The mime type of the image. |
| `asChild` | `Snippet<[PropsFn<'button'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `quality` | `number` | No | The quality of the image. |
| `ref` | `Element` | No |  |


**Frame Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `Snippet<[PropsFn<'svg'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `ref` | `Element` | No |  |


**Overlay Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `ref` | `Element` | No |  |


**Pattern Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `Snippet<[PropsFn<'path'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `ref` | `Element` | No |  |


**RootProvider Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `UseQrCodeReturn` | Yes |  |
| `ref` | `Element` | No |  |


### Context

These are the properties available when using `UqrUcode.Context`, `useUqrUcodeContext` hook or `useUqrUcode` hook.

**API:**

| Property | Type | Description |
|----------|------|-------------|
| `value` | `string` | The value to encode. |
| `setValue` | `(value: string) => void` | Set the value to encode. |
| `getDataUrl` | `(type: DataUrlType, quality?: number) => Promise<string>` | Returns the data URL of the qr code. |
