import { QrCode } from "@/components/ui/qr-code"
const Demo = () => {
return <QrCode value="https://www.google.com" />
}
Setup
If you don't already have the snippet, run the following command to add the
qr-code
snippet
npx @chakra-ui/cli snippet add qr-code
The snippet includes a closed component composition for the QR Code
component.
Usage
Learn how to use the QR Code
component in your project. Let's take a look at
the most basic example:
import { QrCode } from "@/components/ui/qr-code"
<QrCode value="..." />
Examples
Sizes
Use the size
prop to set the size of the QR code.
import { For, Stack } from "@chakra-ui/react"
import { QrCode } from "@/components/ui/qr-code"
const Demo = () => {
return (
<Stack>
<For each={["2xs", "xs", "sm", "md", "lg", "xl", "2xl"]}>
{(size) => (
<QrCode size={size} value="https://www.google.com" key={size} />
)}
</For>
</Stack>
)
}
Logo Overlay
Pass the children prop to the QrCode.Overlay
component to add a logo or
overlay to the QR code.
import { QrCode } from "@/components/ui/qr-code"
const Demo = () => {
return (
<QrCode value="https://www.google.com">
<Logo />
</QrCode>
)
}
const Logo = () => {
return (
<svg
width="40"
height="40"
viewBox="0 0 40 40"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M10 0C15.5228 0 20 4.47715 20 10V0H30C35.5228 0 40 4.47715 40 10C40 15.5228 35.5228 20 30 20C35.5228 20 40 24.4772 40 30C40 32.7423 38.8961 35.2268 37.1085 37.0334L37.0711 37.0711L37.0379 37.1041C35.2309 38.8943 32.7446 40 30 40C27.2741 40 24.8029 38.9093 22.999 37.1405C22.9756 37.1175 22.9522 37.0943 22.9289 37.0711C22.907 37.0492 22.8852 37.0272 22.8635 37.0051C21.0924 35.2009 20 32.728 20 30C20 35.5228 15.5228 40 10 40C4.47715 40 0 35.5228 0 30V20H10C4.47715 20 0 15.5228 0 10C0 4.47715 4.47715 0 10 0ZM18 10C18 14.4183 14.4183 18 10 18V2C14.4183 2 18 5.58172 18 10ZM38 30C38 25.5817 34.4183 22 30 22C25.5817 22 22 25.5817 22 30H38ZM2 22V30C2 34.4183 5.58172 38 10 38C14.4183 38 18 34.4183 18 30V22H2ZM22 18V2L30 2C34.4183 2 38 5.58172 38 10C38 14.4183 34.4183 18 30 18H22Z"
fill="#5417D7"
></path>
</svg>
)
}
Fill
Use the fill
prop to set the fill color of the QR code.
import { Flex, For } from "@chakra-ui/react"
import { QrCode } from "@/components/ui/qr-code"
const Demo = () => {
return (
<Flex gap="4">
<For each={["#5417D7", "#FF0000"]}>
{(fill) => (
<QrCode key={fill} fill={fill} value="https://www.google.com" />
)}
</For>
</Flex>
)
}
Download
Use the QrCode.DownloadTrigger
to download the QR code.
import { Button, QrCode } from "@chakra-ui/react"
const Demo = () => {
return (
<QrCode.Root value="https://www.google.com">
<QrCode.Frame>
<QrCode.Pattern />
</QrCode.Frame>
<QrCode.DownloadTrigger
asChild
fileName="qr-code.png"
mimeType="image/png"
>
<Button variant="outline" size="xs" mt="3">
Download
</Button>
</QrCode.DownloadTrigger>
</QrCode.Root>
)
}
Error Correction
In cases where the link is too long or the logo overlay covers a significant area, the error correction level can be increased.
Use the encoding.ecc
or encoding.boostEcc
property to set the error
correction level:
L
: Allows recovery of up to 7% data loss (default)M
: Allows recovery of up to 15% data lossQ
: Allows recovery of up to 25% data lossH
: Allows recovery of up to 30% data loss
"use client"
import { Stack } from "@chakra-ui/react"
import { QrCode } from "@/components/ui/qr-code"
import { SegmentedControl } from "@/components/ui/segmented-control"
import { useState } from "react"
type ErrorLevel = "L" | "M" | "Q" | "H"
const Demo = () => {
const [errorLevel, setErrorLevel] = useState<ErrorLevel>("L")
return (
<Stack align="flex-start">
<QrCode
size="xl"
value="https://www.google.com"
encoding={{ ecc: errorLevel }}
/>
<SegmentedControl
size="sm"
defaultValue={"L"}
items={["L", "M", "Q", "H"]}
onValueChange={(e) => setErrorLevel(e.value as ErrorLevel)}
/>
</Stack>
)
}
Store
The RootProvider
component provides a context for the QR code.
It accepts the value of the useQrCode
hook. You can leverage it to access the
component state and methods from outside the QR code.
"use client"
import { Button, QrCode, Stack, useQrCode } from "@chakra-ui/react"
const Demo = () => {
const qrCode = useQrCode({ defaultValue: "https://www.google.com" })
return (
<Stack align="flex-start">
<Button onClick={() => qrCode.setValue("https://www.x.com")}>
Update to x.com
</Button>
<QrCode.RootProvider value={qrCode}>
<QrCode.Frame>
<QrCode.Pattern />
</QrCode.Frame>
</QrCode.RootProvider>
</Stack>
)
}
Input
Here's an example of how to use the QrCode
component with an Input
component.
"use client"
import { Input, Stack } from "@chakra-ui/react"
import { QrCode } from "@/components/ui/qr-code"
import { useState } from "react"
const Demo = () => {
const [value, setValue] = useState("https://www.google.com")
return (
<Stack maxW="240px" gap="4">
<QrCode value={value} />
<Input value={value} onChange={(e) => setValue(e.target.value)} />
</Stack>
)
}
Spinner
Here's an example of how to use the QrCode
component with a Spinner
component.
import { AbsoluteCenter, Box, Spinner } from "@chakra-ui/react"
import { QrCode } from "@/components/ui/qr-code"
const Demo = () => {
return (
<Box position="relative">
<QrCode
value="https://www.google.com"
overlay={
<AbsoluteCenter bg="bg/80" boxSize="100%">
<Spinner color="red" />
</AbsoluteCenter>
}
/>
</Box>
)
}
Without Snippet
Here's how to setup the QR code without using the snippet.
import { QrCode } from "@chakra-ui/react"
const Demo = () => {
return (
<QrCode.Root value="..." size="md">
<QrCode.Frame>
<QrCode.Pattern />
</QrCode.Frame>
</QrCode.Root>
)
}
Props
Root
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
defaultValue | string The initial value of the qr code when it is first rendered. Use when you do not need to control the state of the qr code. | |
encoding | QrCodeGenerateOptions The qr code encoding options. | |
id | string The unique identifier of the machine. | |
ids | Partial<{ root: string; frame: string }> The element ids. | |
onValueChange | (details: ValueChangeDetails) => void Callback fired when the value changes. | |
value | string The value to encode. |
DownloadTrigger
Prop | Default | Type |
---|---|---|
fileName * | string The name of the file. | |
mimeType * | DataUrlType The mime type of the image. | |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
quality | number The quality of the image. |