# QRCode

> 将文本或链接生成可访问的二维码，支持纠错、尺寸和无损图片下载。

网页来源：/components/qr-code

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

`value` 提供原始文本，`label` 说明扫码对象。组件只编码内容，链接创建、访问权限和有效期由调用方处理。

## 基本用法与下载

更新 `value` 后图案同步变化，空格和 Unicode 字符原样保留。`actions` 插槽提供 `status` 和 `download`，
可组合标准按钮；`download({ format, fileName })` 支持 PNG / SVG，失败时拒绝 Promise。
扫码区域始终保留黑色模块、白色背景和完整留白，明暗主题下均保持一致。

### 示例：qr-code/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { ref, watch } from 'vue';
import { QuireButton, QuireCard, QuireClipboard, QuireField, QuireQRCode, QuireTextarea } from '@quirekit/ui/vue';
import type { QuireKitQRCodeActionsSlotProps, QuireKitQRCodeFormat } from '@quirekit/ui/vue';

const value = ref('https://example.com/documents/project-review');
const feedback = ref('');
watch(value, () => { feedback.value = ''; });

/**
 * Reports completion of a local image download without creating a share link.
 * @param download - The mounted QR code's public export action.
 * @param format - The requested lossless image format.
 * @returns Completion of the example feedback update.
 */
async function save(download: QuireKitQRCodeActionsSlotProps['download'], format: QuireKitQRCodeFormat): Promise<void> {
  try {
    await download({ format, fileName: `document-link.${format}` });
    feedback.value = `已请求下载 ${format.toUpperCase()} 图片`;
  } catch {
    feedback.value = '下载失败，请生成二维码后重试';
  }
}
</script>

<template>
  <div class="p-qk-surface" data-qr-code-basic>
    <QuireCard title="文档分享链接" description="编辑本地示例链接，生成可保存的二维码。">
      <div class="grid min-w-0 gap-qk-group sm:grid-cols-[minmax(0,1fr)_auto]">
        <div class="grid min-w-0 content-start gap-qk-surface-compact">
          <QuireField label="二维码内容" width="100%" hint="此示例仅转换文本，不创建链接或变更文档权限。">
            <template #default="{ controlProps }">
              <QuireTextarea v-bind="controlProps" v-model="value" width="100%" :rows="3" resize="none" />
            </template>
          </QuireField>
          <div class="flex items-center gap-qk-inline">
            <QuireClipboard :value="value" mode="icon" />
            <span class="text-qk-helper text-qk-text-secondary">也可以直接复制文本</span>
          </div>
        </div>
        <QuireQRCode :value="value" label="文档分享链接二维码">
          <template #actions="{ status, download }">
            <QuireButton variant="outline" :disabled="status !== 'ready'" @click="save(download, 'png')">下载 PNG</QuireButton>
            <QuireButton variant="outline" :disabled="status !== 'ready'" @click="save(download, 'svg')">下载 SVG</QuireButton>
          </template>
        </QuireQRCode>
      </div>
      <p v-if="feedback" class="m-0 mt-qk-surface-compact text-qk-helper text-qk-text-secondary" role="status">{{ feedback }}</p>
    </QuireCard>
  </div>
</template>
```

## 图形尺寸

`size` 设置包含留白的正方形尺寸，默认 `160` CSS 像素，窄容器内等比收缩。
长内容会增加图案密度，应相应增大尺寸并检查实际扫码效果。PNG 按显示尺寸和设备像素比导出，SVG 保留矢量。

### 示例：qr-code/sizes

文件：`sizes.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireQRCode, QuireSegmented } from '@quirekit/ui/vue';
const size = ref('160');
const sizes = [{ value: '128', label: '128 px' }, { value: '160', label: '160 px' }, { value: '224', label: '224 px' }];
</script>

<template>
  <div class="grid justify-items-start gap-qk-surface p-qk-surface" data-qr-code-sizes>
    <QuireSegmented v-model="size" :items="sizes" label="二维码尺寸" />
    <QuireQRCode value="https://example.com/documents/project-review" label="项目评审链接二维码" :size="Number(size)" />
    <span class="text-qk-helper text-qk-text-secondary">{{ size }} × {{ size }} CSS 像素，包含四周留白</span>
  </div>
</template>
```

## 纠错等级

`errorCorrection` 支持 `L`、`M`、`Q`、`H`，默认 `M`。更高等级通常需要更密集的图案；
纠错不替代完整留白，也不保证内容被遮盖后仍能识别。

### 示例：qr-code/correction

文件：`correction.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireQRCode, QuireSelect } from '@quirekit/ui/vue';
import type { QuireKitQRCodeErrorCorrection } from '@quirekit/ui/vue';
const level = ref<QuireKitQRCodeErrorCorrection>('M');
const levels = ['L', 'M', 'Q', 'H'].map(value => ({ value, label: value }));

/**
 * Keeps the required correction level when the generic Select has no value.
 * @param value - Selected public option value.
 */
function updateLevel(value: string | undefined): void {
  if (value === 'L' || value === 'M' || value === 'Q' || value === 'H') level.value = value;
}
</script>

<template>
  <div class="grid justify-items-start gap-qk-surface p-qk-surface" data-qr-code-correction>
    <QuireSelect :model-value="level" :items="levels" label="二维码纠错等级" width="160px" @update:model-value="updateLevel" />
    <QuireQRCode value="QuireKit 文档审阅 📝" label="文档审阅文字二维码" :error-correction="level" :size="192" />
    <span class="text-qk-helper text-qk-text-secondary">编码内容：QuireKit 文档审阅 📝</span>
  </div>
</template>
```

## 空内容与生成失败

空字符串和内容无法编码时显示提示，文案跟随 [Locale Provider](/raw/components/locale-provider.md)。这两种状态不保留旧二维码，
重新传入有效内容即可恢复。组件不创建自动播报，页面可按任务组合反馈。

### 示例：qr-code/fallback

文件：`fallback.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireButton, QuireQRCode } from '@quirekit/ui/vue';
const value = ref('');
</script>

<template>
  <div class="grid justify-items-start gap-qk-surface p-qk-surface" data-qr-code-fallback>
    <div class="flex flex-wrap gap-qk-inline">
      <QuireButton variant="outline" @click="value = ''">清空内容</QuireButton>
      <QuireButton variant="outline" @click="value = '文'.repeat(5000)">设置超长内容</QuireButton>
      <QuireButton variant="outline" @click="value = 'https://example.com/documents/project-review'">恢复示例链接</QuireButton>
    </div>
    <QuireQRCode :value="value" label="待生成的文档二维码" />
  </div>
</template>
```

## 实例方法与可访问性

模板引用提供只读 `status`、`download(options)` 和 `getDataUrl(format)`；图片生成方法须在挂载后调用，
没有可用图案时拒绝 Promise。`label` 是图片的可访问名称，另提供可读文本或 [Clipboard](/raw/components/clipboard.md)，
让无法扫码的用户完成同一任务。

## API

### QuireQRCode

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| value | 是 | — | Exact text to encode; an empty string displays feedback instead of a symbol. |
| label | 是 | — | Accessible image name describing the content or intended scan task. |
| size | 否 | 160 | Square image size in CSS pixels, including the quiet zone; defaults to 160. |
| errorCorrection | 否 | "M" | Error correction level; defaults to M. |

**value**

```ts
string
```

**label**

```ts
string
```

**size**

```ts
number | undefined
```

```ts
undefined | number
```

**errorCorrection**

```ts
QuireKitQRCodeErrorCorrection | undefined
```

```ts
undefined | "M" | "L" | "Q" | "H"
```

#### Events

无。

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| actions | 否 | — | Optional actions outside the scan area, such as public Button downloads. |

**actions**

```ts
QuireKitQRCodeActionsSlotProps
```

```ts
{
  /** Current content availability; disable export actions unless ready. */
  status: "empty" | "ready" | "error";
  /** Exports the mounted symbol using Ark's SVG and raster conversion. */
  getDataUrl: { (format?: QuireKitQRCodeFormat | undefined): Promise<string>; };
  /** Starts a browser download of the mounted symbol. */
  download: { (options?: QuireKitQRCodeDownloadOptions | undefined): Promise<void>; };
}
```

#### 公开实例成员

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| status | 否 | — |  |
| getDataUrl | 否 | — |  |
| download | 否 | — |  |

**status**

```ts
QuireKitQRCodeStatus
```

```ts
"empty" | "ready" | "error"
```

**getDataUrl**

```ts
(format?: QuireKitQRCodeFormat) => Promise<string>
```

```ts
{ (format?: QuireKitQRCodeFormat): Promise<string>; }
```

**download**

```ts
(options?: QuireKitQRCodeDownloadOptions) => Promise<void>
```

```ts
{ (options?: QuireKitQRCodeDownloadOptions): Promise<void>; }
```

#### 相关类型

```ts
type QuireKitQRCodeActionsSlotProps = {
  /** Current content availability; disable export actions unless ready. */
  status: "empty" | "ready" | "error";
  /** Exports the mounted symbol using Ark's SVG and raster conversion. */
  getDataUrl: { (format?: QuireKitQRCodeFormat | undefined): Promise<string>; };
  /** Starts a browser download of the mounted symbol. */
  download: { (options?: QuireKitQRCodeDownloadOptions | undefined): Promise<void>; };
};
```

```ts
type QuireKitQRCodeStatus = "empty" | "ready" | "error";
```
