DisplayReact
Draws a QR code for a text, such as a link, a ticket or a Wi-Fi setting, as an SVG image that scales without blurring.
Rendered by React 19.3.1
Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<QrCode.Root>
<QrCode.Frame>
<QrCode.Pattern />
</QrCode.Frame>
</QrCode.Root>
// Not in this example: QrCode.DownloadTrigger, QrCode.OverlayExamples
Logo
The same link at level H, with a logo in an Overlay over the middle.
Download
Two DownloadTriggers that save the code as a PNG and as an SVG file named ticket-42.
Overflow
A button that swaps the link for a 3000-character text, which marks every part and disables the download.
API reference
11 props across 5 parts, in React syntax. className, style, render are listed once, under Shared props.
DownloadTriggerQrCode.DownloadTrigger
A button that downloads the symbol as a PNG or SVG file, in the colours the code is shown in. Disabled while the value does not fit. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
nativeButtonWhether the component renders a native <button> element when replacing it | boolean | true |
fileNameThe name of the downloaded file; the extension of type is added when missing. | string | 'qr-code' |
typeThe file format. | QrCodeDownloadType | 'image/png' |
scaleThe CSS pixels per module of a PNG, an integer of at least 1. | number | 10 |
focusableWhenDisabledWhether the button stays focusable when disabled. | boolean | false |
| Data attribute | Description |
|---|---|
data-overflow | Present while the value does not fit a version 40 symbol. |
data-disabled | Present when the button is disabled, while the value does not fit. |
FrameQrCode.Frame
The image of the code, named "QR code: " and the value unless given a name. Holds the Pattern; CSS sizes it, and its background-color is the light colour. Renders a <svg>.
| Data attribute | Description |
|---|---|
data-overflow | Present while the value does not fit a version 40 symbol. |
OverlayQrCode.Overlay
A logo or other content over the code, hidden from assistive technology. The modules under it stay drawn, so use level Q or H with it. Renders a <div>.
| Data attribute | Description |
|---|---|
data-overflow | Present while the value does not fit a version 40 symbol. |
PatternQrCode.Pattern
The dark modules, filled with currentColor. Place it inside the Frame. Renders a <path>.
| Data attribute | Description |
|---|---|
data-overflow | Present while the value does not fit a version 40 symbol. |
RootQrCode.Root
Groups all parts of the QR code and encodes the value into its symbol. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
valuerequiredThe text encoded in the symbol. | string | — |
errorCorrectionThe error correction level requested: L, M, Q or H. | QrCodeErrorCorrection | 'M' |
boostErrorCorrectionWhether the level is raised to the highest one that fits the same version. | boolean | true |
quietZoneThe modules of light margin on each side, drawn inside the Frame. | number | 4 |
minVersionThe smallest version used, 1 to 40. | number | 1 |
labelsThe texts, in English by default: the Frame's name and the DownloadTrigger's. | Partial<QrCodeLabels> | — |
| Data attribute | Description |
|---|---|
data-overflow | Present while the value does not fit a version 40 symbol. |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State> |
Keyboard and accessibility
Keyboard
QrCode has no keys of its own. The image takes no focus; the DownloadTrigger is a native button, so Enter and Space press it, and Tab passes over it while the text does not fit.
Accessibility
| Part | Sets | So that |
|---|---|---|
| Frame | an svg with role="img", named "QR code: " and the text (labels), or by aria-label or aria-labelledby | a screen reader reads what the code holds, which a person who cannot scan it needs most |
| Frame | while the text does not fit: no role and no name, aria-hidden="true" | an empty image is not announced |
| Pattern | a path with no role | the modules are not read one by one |
| Overlay | aria-hidden="true" | the logo does not add a second name to the code |
| DownloadTrigger | a button of type button named by its text, or "Download QR code" (labels) when it has none; disabled while the text does not fit | the button says what it saves, and cannot save an empty file |
| Root | a div with no role | the parts are read in the order they are placed |
Focus order
- The image is not focusable; focus passes over it.
- Each DownloadTrigger is one Tab stop, in the order placed, and none while the text does not fit unless
focusableWhenDisabledis set.