跳到主要内容

Qrcode

QR code renderer: encodes value via the self-contained pure-JS encoder in @lh-ui/share/core/qrcode (full ISO/IEC 18004 pipeline: byte-mode UTF-8, Reed-Solomon error correction, 8-mask penalty selection, version 1-40) and renders the matrix as an inline SVG (span.lh-qrcode > svg.__svg), where each dark module becomes a 1×1 rectangle in a single <path> (qrcodeToSvgPath).

  • Empty state: an empty value, or a value that exceeds the capacity of version 40 at the given level, degrades gracefully — the root node stays mounted (with its accessible attributes) and the <svg> is not rendered. encodeQrcode throws on over-capacity input, so the component layer guards with try/catch.
  • Deterministic: the matrix is memoized on [value, level, version, margin], so re-rendering with the same props always produces the identical path.
  • version accepts 1-40; 0 (default) picks the smallest version that fits. margin is the quiet zone in modules (defaults to 4, the spec minimum).
  • The SVG viewBox is 0 0 total total where total = size + 2 * margin (module count including the quiet zone), so the quiet zone is always rendered and the code stays scannable at any size.

Basic usage​

Qrcode (default / high error correction / Chinese / locked version)
import { LhQrcode } from '@lh-ui/react'

export default function Demo() {
return (
<>
<LhQrcode value="https://lh-ui.lhgb.top" />
<LhQrcode value="https://lh-ui.lhgb.top" level="H" color="#E4007E" />
<LhQrcode value="lh-ui 跨端组件库家族" size={160} margin={2} />
<LhQrcode value="version 锁定 v5" version={5} bgColor="#F2F3F5" />
</>
)
}

Error correction level​

Higher levels sacrifice capacity for resilience: L recovers ~7% of codewords, M ~15%, Q ~25%, H ~30%. Choose H when the code may be partially occluded (e.g. a logo overlay or printed wear); choose L for dense content. The level changes the interleaved block layout, so the rendered path differs even for the same value.

Qrcode API​

PropDescriptionTypeDefault
valueText to encode (UTF-8, Chinese supported; empty = empty state)string''
levelError correction level'L' | 'M' | 'Q' | 'H''M'
versionQR version 1-40 (0 = auto, smallest that fits)number0
marginQuiet zone in modulesnumber4
colorModule (foreground) colorstringtheme token
bgColorBackground color (omitted = no background rect)string—
sizeRendered size in px (svg width / height)number120
sxQuick layout propsLhSxProps—

Accessibility​

  • The root carries role="img" with aria-label: the encoded text itself when non-empty, so assistive technology announces what the code contains (a URL, for example). The empty state falls back to the localized generic label.
  • No interactive behavior: the component is presentational only.