Skip to main content

QRCode

A QR code for any text or URL, drawn with plain views: no react-native-svg, no native module. The encoder is written in TypeScript and follows ISO/IEC 18004: byte mode (UTF-8), the four error-correction levels and all 40 versions, picking the smallest that fits.

import { QRCode } from 'patine-ui';
Loading…
<QRCode value="https://github.com/patine-ui/patine" size={180} accessibilityLabel="Scan to open the Patine repository" />

Try the props​

Props
errorCorrection
size
quietZone
<QRCode value="https://github.com/patine-ui/patine" />

Example: a concert ticket​

A ticket in an events app: the code holds the ticket's ID for the door scanner, sits on a white plate so it scans in dark mode too, and is named for screen readers.

Loading…
<Box border radius="lg" padding="lg" gap="md" style={{ width: '100%', maxWidth: 340 }}>
  <VStack gap="xs">
    <HStack justify="space-between" align="center">
      <Text size="lg" weight="semibold">{TICKET.event}</Text>
      <Badge variant="success">Valid</Badge>
    </HStack>
    <Text color="textSecondary">{TICKET.venue}</Text>
    <Text color="textSecondary">{TICKET.date}</Text>
  </VStack>
  <Separator />
  <VStack align="center" gap="sm">
    <QRCode value={TICKET.id} size={180} backgroundColor="#FFFFFF" color="#111827" accessibilityLabel={`Ticket code for ${TICKET.event}, ${TICKET.holder}`} />
    <Text size="sm" style={{ fontFamily: 'monospace' }}>{TICKET.id}</Text>
  </VStack>
  <HStack justify="space-between">
    <Text size="sm" color="textSecondary">{TICKET.section}</Text>
    <Text size="sm" color="textSecondary">{TICKET.holder}</Text>
  </HStack>
  <Button variant="outline">Add to wallet</Button>
</Box>

Pass anything as logo: an Image, an emoji, your icon. The modules under it are cleared onto a plate in the background colour, and the level switches to H, which recovers about 30% of the code, so it still scans.

Loading…
<QRCode value="https://github.com/patine-ui/patine" size={200} logo={<Icon name="rocket" size={36} color="#1E1B4B" strokeWidth={2} />} color="#1E1B4B" />

Colours and dark mode​

Scanners expect dark modules on a light ground, so the code stays black on white in dark mode too. Choose any color and backgroundColor with strong contrast; inverted codes (light on dark) fail on many readers.

The matrix​

createQRCode(text, options) returns the encoded matrix, for drawing it yourself (an SVG, a canvas, a printed PDF) or testing:

import { createQRCode } from 'patine-ui';

const { version, size, modules } = createQRCode('WIFI:T:WPA;S:Home;P:secret;;', { errorCorrection: 'Q' });
// modules[y][x] is true for a dark module; size is 17 + 4 × version.

It throws a RangeError when the text is longer than version 40 holds (2,953 bytes at L, 1,273 at H); <QRCode> calls onError and draws nothing.

Accessibility​

The code is an image named "QR code"; pass an accessibilityLabel that says where it leads, such as "Scan to pay Ana €12".

Props​

PropTypeDefaultDescription
valuestring—Text or URL, encoded as UTF-8.
sizenumber200Width and height, quiet zone included. Modules snap to whole points; the rest widens the quiet zone.
errorCorrection'L' | 'M' | 'Q' | 'H''M', 'H' with a logoAbout 7%, 15%, 25% and 30% recovery.
colorstring'#000000'Dark modules.
backgroundColorstring'#FFFFFF'Ground and quiet zone.
quietZonenumber4Margin in modules.
logoReactNode—Shown in the centre on a cleared plate.
logoSizenumber0.22The plate's share of the width, up to 0.3.
accessibilityLabelstring'QR code'
onError(error: Error) => void—The value is too long.
styleStyleProp<ViewStyle>—
theme / designprovider'sRounds the corners like the design's cards.