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 avaluethat exceeds the capacity of version 40 at the givenlevel, degrades gracefully — the root node stays mounted (with its accessible attributes) and the<svg>is not rendered.encodeQrcodethrows on over-capacity input, so the component layer guards withtry/catch. - Deterministic: the matrix is memoized on
[value, level, version, margin], so re-rendering with the same props always produces the identical path. versionaccepts 1-40;0(default) picks the smallest version that fits.marginis the quiet zone in modules (defaults to 4, the spec minimum).- The SVG
viewBoxis0 0 total totalwheretotal = size + 2 * margin(module count including the quiet zone), so the quiet zone is always rendered and the code stays scannable at anysize.
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
| Prop | Description | Type | Default |
|---|---|---|---|
| value | Text to encode (UTF-8, Chinese supported; empty = empty state) | string | '' |
| level | Error correction level | 'L' | 'M' | 'Q' | 'H' | 'M' |
| version | QR version 1-40 (0 = auto, smallest that fits) | number | 0 |
| margin | Quiet zone in modules | number | 4 |
| color | Module (foreground) color | string | theme token |
| bgColor | Background color (omitted = no background rect) | string | — |
| size | Rendered size in px (svg width / height) | number | 120 |
| sx | Quick layout props | LhSxProps | — |
Accessibility
- The root carries
role="img"witharia-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.