본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

QR Code (QR 코드)

원문 보기 위키 갱신

제공된 데이터를 바탕으로 QR 코드를 생성하는 컴포넌트예요. URL이나 텍스트 같은 값을 인코딩해서 화면에 표시하고, 필요하면 이미지로 다운로드할 수도 있어요.

출처: 문서

본문

구조 (Anatomy)

QR 코드 컴포넌트의 기본 구조는 다음과 같아요. Root 안에 이미지를 그리는 Frame과 Pattern, 그리고 선택적으로 오버레이나 다운로드 트리거를 배치해요.

<QrCode.Root>
  <QrCode.Frame>
    <QrCode.Pattern />
  </QrCode.Frame>
  <QrCode.Overlay />
  <QrCode.DownloadTrigger />
</QrCode.Root>

예제 (Examples)

기본 (Basic)

가장 기본적인 사용 사례예요. defaultValue로 인코딩할 값을 지정해요.

import { QrCode } from '@ark-ui/react/qr-code'
import styles from 'styles/qr-code.module.css'

export const Basic = () => {
  return (
    <QrCode.Root className={styles.Root} defaultValue="http://ark-ui.com">
      <QrCode.Frame className={styles.Frame}>
        <QrCode.Pattern className={styles.Pattern} />
      </QrCode.Frame>
    </QrCode.Root>
  )
}

오버레이 사용하기 (With Overlay)

QR 코드에 로고나 오버레이를 추가할 수도 있어요. QR 코드를 브랜딩하고 싶을 때 유용해요.

import { QrCode } from '@ark-ui/react/qr-code'
import styles from 'styles/qr-code.module.css'

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

오류 보정 (Error Correction)

링크가 너무 길거나 로고 오버레이가 넓은 영역을 덮는 경우에는 오류 보정 수준을 높일 수 있어요.

encoding.ecc 또는 encoding.boostEcc 속성으로 오류 보정 수준을 설정해요.

  • L: 데이터 손실 최대 7%까지 복구 가능 (기본값)
  • M: 데이터 손실 최대 15%까지 복구 가능
  • Q: 데이터 손실 최대 25%까지 복구 가능
  • H: 데이터 손실 최대 30%까지 복구 가능
import { QrCode } from '@ark-ui/react/qr-code'
import { RadioGroup } from '@ark-ui/react/radio-group'
import { useState } from 'react'
import styles from 'styles/qr-code.module.css'
import radio from 'styles/radio-group.module.css'

type ErrorLevel = 'L' | 'M' | 'Q' | 'H'

export const ErrorCorrection = () => {
  const [errorLevel, setErrorLevel] = useState<ErrorLevel>('L')

  return (
    <div className="stack">
      <QrCode.Root className={styles.Root} defaultValue="http://ark-ui.com" encoding={{ ecc: errorLevel }}>
        <QrCode.Frame className={styles.Frame}>
          <QrCode.Pattern className={styles.Pattern} />
        </QrCode.Frame>
      </QrCode.Root>
      <RadioGroup.Root
        className={radio.Root}
        defaultValue="L"
        orientation="horizontal"
        onValueChange={(e) => setErrorLevel(e.value as ErrorLevel)}
      >
        <div className="hstack">
          {['L', 'M', 'Q', 'H'].map((level) => (
            <RadioGroup.Item className={radio.Item} key={level} value={level}>
              <RadioGroup.ItemControl className={radio.ItemControl} />
              <RadioGroup.ItemText className={radio.ItemText}>{level}</RadioGroup.ItemText>
              <RadioGroup.ItemHiddenInput />
            </RadioGroup.Item>
          ))}
        </div>
      </RadioGroup.Root>
    </div>
  )
}

루트 프로바이더 (Root Provider)

QR 코드를 제어하는 또 다른 방법은 RootProvider 컴포넌트와 useQrCode 훅을 사용하는 거예요. 이렇게 하면 컴포넌트 밖에서 상태와 메서드에 접근할 수 있어요.

import { QrCode, useQrCode } from '@ark-ui/react/qr-code'
import styles from 'styles/qr-code.module.css'

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

  return (
    <div className="stack">
      <QrCode.RootProvider className={styles.Root} value={qrCode}>
        <QrCode.Frame className={styles.Frame}>
          <QrCode.Pattern className={styles.Pattern} />
        </QrCode.Frame>
      </QrCode.RootProvider>
      <output>{qrCode.value}</output>
    </div>
  )
}

다운로드 (Download)

QrCode.DownloadTrigger 컴포넌트를 사용해서 사용자가 QR 코드를 이미지로 다운로드할 수 있게 해요. 다운로드 파일의 fileName과 mimeType 속성을 지정해요.

import { QrCode } from '@ark-ui/react/qr-code'
import button from 'styles/button.module.css'
import styles from 'styles/qr-code.module.css'

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

API 레퍼런스 (API Reference)

속성 (Props)

Root

Props

asChild Type: boolean Required: false Default Value: undefined Description: Use the provided child element as the default rendered element, combining their props and behavior.

defaultValue Type: string Required: false Default Value: undefined Description: The initial value to encode when rendered. Use when you don't need to control the value of the qr code.

encoding Type: QrCodeGenerateOptions Required: false Default Value: undefined Description: The qr code encoding options.

id Type: string Required: false Default Value: undefined Description: The unique identifier of the machine.

ids Type: Partial<{ root: string; frame: string; overlay: string }> Required: false Default Value: undefined Description: The element ids.

onValueChange Type: (details: ValueChangeDetails) => void Required: false Default Value: undefined Description: Callback fired when the value changes.

pixelSize Type: number Required: false Default Value: undefined Description: The pixel size of the qr code.

value Type: string Required: false Default Value: undefined Description: The controlled value to encode.

DownloadTrigger

Props

fileName Type: string Required: true Default Value: undefined Description: The name of the file.

mimeType Type: DataUrlType Required: true Default Value: undefined Description: The mime type of the image.

asChild Type: boolean Required: false Default Value: undefined Description: Use the provided child element as the default rendered element, combining their props and behavior.

quality Type: number Required: false Default Value: undefined Description: The quality of the image.

Frame

Props

asChild Type: boolean Required: false Default Value: undefined Description: Use the provided child element as the default rendered element, combining their props and behavior.

Overlay

Props

asChild Type: boolean Required: false Default Value: undefined Description: Use the provided child element as the default rendered element, combining their props and behavior.

Pattern

Props

asChild Type: boolean Required: false Default Value: undefined Description: Use the provided child element as the default rendered element, combining their props and behavior.

RootProvider

Props

value Type: UseQrCodeReturn Required: true Default Value: undefined Description: undefined

asChild Type: boolean Required: false Default Value: undefined Description: Use the provided child element as the default rendered element, combining their props and behavior.

컨텍스트 (Context)

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. Includes the overlay when present.

더 알아보기 (Learn more)