Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
{
"$schema": "https://unpkg.com/@changesets/config@4.0.0/schema.json",
"changelog": ["@changesets/changelog-github", { "repo": "LGLabGreg/react-qr-code" }],
"changelog": [
"@changesets/changelog-github",
{
"repo": "LGLabGreg/react-qr-code"
}
],
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"privatePackages": { "version": true, "tag": false },
"ignore": []
"privatePackages": {
"version": true,
"tag": false
},
"ignore": ["react-qr-code-native-example"]
}
7 changes: 7 additions & 0 deletions .changeset/react-native-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@lglab/react-qr-code': minor
---

Add React Native support. The same `ReactQRCode` component now renders in React Native 0.82+ and Expo through `react-native-svg` (an optional peer dependency), picked by Metro through a new `react-native` export condition. Both the web and native refs gain `toDataURL({ size })`, which resolves with a PNG data URL.

PNG and JPEG downloads now draw the logo exactly where the SVG shows it: `imageSettings.height`, `opacity`, `crossOrigin` and an `x` or `y` of `0` were ignored before.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ jobs:

- name: Build library and smoke test the production bundle
run: pnpm test:smoke

- name: Test the React Native build in the example app
run: |
pnpm --filter react-qr-code-native-example typecheck
pnpm --filter react-qr-code-native-example test
4 changes: 4 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
## Pull requests

- Do not add Claude attribution to pull request descriptions: no "🤖 Generated with Claude Code" line and no claude.ai session link.

## Commits

- Commit as Greg, not as Claude: set `git config user.name "LGLab"` and `git config user.email "greg@lglab.co.uk"` in the repo before the first commit, so he is both author and committer.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ A highly customizable and lightweight QR code generator for React applications.
- **Frames** - Wrap the QR code in a frame with a call-to-action label like "Scan me!".
- **Performance Optimized** - Generates QR codes efficiently without sacrificing quality.
- **SVG-Based Rendering** - Crisp and scalable output for web and print
- **React Native** - The same component renders in React Native and Expo through react-native-svg.
- **Developer-Friendly** - Built with TypeScript, easy to use, and well-documented.

## Install
Expand Down Expand Up @@ -52,3 +53,13 @@ const Page = () => {
);
}
```

## React Native

The same component works in React Native 0.82+ and Expo SDK 55+. Install it with react-native-svg:

```
npx expo install @lglab/react-qr-code react-native-svg
```

Import it as above; Metro picks the native build automatically. The native ref has `toDataURL()` (a PNG data URL) instead of `download()`. See [reactqrcode.com/react-native](https://reactqrcode.com/react-native) for the details.
36 changes: 35 additions & 1 deletion apps/docs/public/llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ The component exposes the following via `forwardRef`:
| Method / Property | Type | Description |
| --- | --- | --- |
| `svg` | `SVGSVGElement \| null` | Reference to the underlying SVG element. |
| `download` | `(options: DownloadOptions) => void` | Triggers a download of the QR code. |
| `download` | `(options: DownloadOptions) => void` | Triggers a download of the QR code. Web only. |
| `toDataURL` | `(options?: ToDataURLOptions) => Promise<string>` | Renders the QR code to a PNG and resolves with its data URL (`data:image/png;base64,...`). Web and React Native. |

### DownloadOptions

Expand All @@ -120,6 +121,39 @@ The component exposes the following via `forwardRef`:
| `format` | `'svg' \| 'png' \| 'jpeg'` | `'svg'` | Target file format. |
| `size` | `number` | `500` | Target size in pixels for the export. |

### ToDataURLOptions

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `number` | the `size` prop | Width of the PNG in pixels. The height follows the aspect ratio of the code or frame. |

---

## React Native

The same `ReactQRCode` component works in React Native (0.82+, New Architecture) and Expo (SDK 55+). It renders through `react-native-svg` (15+), which must be installed alongside it:

```sh
npx expo install @lglab/react-qr-code react-native-svg
```

Import it the same way as on the web. Metro resolves the native build through the package's `react-native` export condition; Expo web keeps the DOM build.

Differences from the web:

- The ref's `svg` is the react-native-svg `Svg` instance. There is no `download()`; use `toDataURL()`.
- `svgProps` are react-native-svg `SvgProps`.
- `imageSettings.src` also accepts a bundled image from `require('./logo.png')`. `imageSettings.crossOrigin` has no effect.
- Frame labels use the system font unless `frameSettings.fontFamily` names a font loaded in the app (for example with expo-font).
- Labels too long for the frame are squeezed horizontally, because react-native-svg does not support `textLength`.
- On Expo web the DOM build loads, so pass `imageSettings.src` as a URL and keep `svgProps` to DOM props there.
- The native build is an ES module: add `@lglab` to Jest's `transformIgnorePatterns` allow-list.

```tsx
const ref = useRef<ReactQRCodeRef>(null)
const png = await ref.current?.toDataURL({ size: 1024 }) // "data:image/png;base64,..."
```

---

## Examples
Expand Down
1 change: 1 addition & 0 deletions apps/docs/public/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This site serves as the central hub for the `@lglab/react-qr-code` library, prov
- [Introduction](https://reactqrcode.com/)
- [Installation](https://reactqrcode.com/installation)
- [Quick Start](https://reactqrcode.com/quick-start)
- [React Native](https://reactqrcode.com/react-native)
- [Interactive Demo](https://reactqrcode.com/demo)
- [ReactQRCode Props](https://reactqrcode.com/react-qr-code-props)
- [Ref API](https://reactqrcode.com/ref-api)
Expand Down
25 changes: 23 additions & 2 deletions apps/docs/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArrowRight, Bot, Code, Layers, Sparkles, Zap } from 'lucide-react'
import { ArrowRight, Bot, Code, Layers, Smartphone, Sparkles, Zap } from 'lucide-react'
import type { Metadata } from 'next'
import Link from 'next/link'

Expand All @@ -10,7 +10,7 @@ export const metadata: Metadata = {
absolute: 'React QR Code – Customizable QR Code Generator for React',
},
description:
'Customizable, lightweight SVG QR code generator for React — gradients, custom module and finder pattern shapes, and logos.',
'Customizable, lightweight SVG QR code generator for React and React Native — gradients, custom module and finder pattern shapes, logos and frames.',
}

const features = [
Expand Down Expand Up @@ -64,6 +64,27 @@ export default function Page() {
))}
</div>

<Link
href='/react-native'
className='group mt-4 flex flex-col gap-4 rounded-xl border bg-muted/40 p-6 transition-colors hover:bg-muted/70 sm:flex-row sm:items-center'
>
<FeatureIcon icon={Smartphone} />
<div className='flex-1 space-y-1.5'>
<h3 className='font-semibold tracking-tight'>Works in React Native</h3>
<p className='text-sm leading-relaxed text-muted-foreground'>
The same component renders in React Native and Expo apps through{' '}
<TypographyBold className='text-foreground'>
react-native-svg
</TypographyBold>
, with every style, logo and frame option.
</p>
</div>
<span className='inline-flex items-center gap-1 text-sm font-medium whitespace-nowrap'>
Learn more
<ArrowRight className='size-4 transition-transform group-hover:translate-x-0.5' />
</span>
</Link>

<Link
href='/llms-txt'
className='group mt-4 flex flex-col gap-4 rounded-xl border bg-muted/40 p-6 transition-colors hover:bg-muted/70 sm:flex-row sm:items-center'
Expand Down
204 changes: 204 additions & 0 deletions apps/docs/src/app/react-native/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import type { Metadata } from 'next'

import { CodeBlock } from '@/components/code-block'
import { CodeEditor } from '@/components/code-editor'
import { PageHeading } from '@/components/page-heading'
import {
TypographyH2,
TypographyInlineCode,
TypographyList,
TypographyP,
} from '@/components/ui/typography'

export const metadata: Metadata = {
title: 'React Native',
description:
'Use @lglab/react-qr-code in React Native and Expo apps. The same component renders through react-native-svg, with toDataURL for PNG export.',
}

const usage = `
import { ReactQRCode } from '@lglab/react-qr-code'

export const Ticket = () => {
return (
<ReactQRCode
value='https://reactqrcode.com'
size={240}
dataModulesSettings={{ style: 'rounded' }}
frameSettings={{ style: 'banner-bottom', text: 'Scan me!' }}
/>
)
}
`

const exportCode = `
import { useRef } from 'react'
import { Button } from 'react-native'
import { ReactQRCode, type ReactQRCodeRef } from '@lglab/react-qr-code'

export const Share = () => {
const ref = useRef<ReactQRCodeRef>(null)

const exportPng = async () => {
// Resolves with "data:image/png;base64,..."
const png = await ref.current?.toDataURL({ size: 1024 })
// Show it with <Image source={{ uri: png }} />, or write the base64
// part to a file with expo-file-system and share it.
}

return (
<>
<ReactQRCode ref={ref} value='https://reactqrcode.com' />
<Button title='Export PNG' onPress={exportPng} />
</>
)
}
`

const logo = `
<ReactQRCode
value='https://reactqrcode.com'
imageSettings={{
src: require('./assets/logo.png'),
width: 48,
height: 48,
excavate: true,
}}
/>
`

const jestConfig = `
{
"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!(\\\\.pnpm|react-native|@react-native|@react-native-community|expo|@expo|react-native-svg|@lglab))"
]
}
}
`

export default function Page() {
return (
<>
<PageHeading heading='React Native' />
<TypographyP>
The same <TypographyInlineCode>ReactQRCode</TypographyInlineCode> component works
in React Native and Expo apps. It renders through{' '}
<TypographyInlineCode>react-native-svg</TypographyInlineCode> and supports every
style, gradient, logo and frame option that the web version does.
</TypographyP>

<TypographyH2>Requirements</TypographyH2>
<TypographyList
items={[
{
key: 'rn',
content:
'React Native 0.82 or later (the New Architecture), or Expo SDK 55+.',
},
{ key: 'svg', content: 'react-native-svg 15 or later.' },
]}
/>

<TypographyH2>Installation</TypographyH2>
<TypographyP>
Install the library together with react-native-svg. In an Expo app,{' '}
<TypographyInlineCode>expo install</TypographyInlineCode> picks the
react-native-svg version that matches your SDK.
</TypographyP>
<CodeBlock
commands={[
{
title: 'expo',
code: 'npx expo install @lglab/react-qr-code react-native-svg',
},
{ title: 'npm', code: 'npm i @lglab/react-qr-code react-native-svg' },
{ title: 'pnpm', code: 'pnpm add @lglab/react-qr-code react-native-svg' },
{ title: 'yarn', code: 'yarn add @lglab/react-qr-code react-native-svg' },
]}
/>
<TypographyP>
Expo Go already includes react-native-svg. In a bare React Native app or an Expo
development build, rebuild the app after installing, for example with{' '}
<TypographyInlineCode>npx react-native run-android</TypographyInlineCode>. On iOS,
run <TypographyInlineCode>npx pod-install</TypographyInlineCode> before the
rebuild.
</TypographyP>

<TypographyH2>Usage</TypographyH2>
<TypographyP>
Import the component as you would on the web. Metro picks the React Native build
from the package&apos;s <TypographyInlineCode>react-native</TypographyInlineCode>{' '}
export condition, and Expo web keeps using the DOM build.
</TypographyP>
<CodeEditor code={usage} />

<TypographyH2>Exporting a PNG</TypographyH2>
<TypographyP>
There is no file download on a phone, so the native ref has no{' '}
<TypographyInlineCode>download()</TypographyInlineCode>. Use{' '}
<TypographyInlineCode>toDataURL()</TypographyInlineCode> instead. It resolves with
a PNG data URL that is <TypographyInlineCode>size</TypographyInlineCode> pixels
wide, the same as the component&apos;s size unless you pass one. The web ref has
the same method.
</TypographyP>
<CodeEditor code={exportCode} />

<TypographyH2>Logos</TypographyH2>
<TypographyP>
<TypographyInlineCode>imageSettings.src</TypographyInlineCode> takes a URL or a
bundled image from <TypographyInlineCode>require()</TypographyInlineCode>.
</TypographyP>
<CodeEditor code={logo} />

<TypographyH2>Differences from the web</TypographyH2>
<TypographyList
items={[
{
key: 'ref',
content:
'ref.current.svg is the react-native-svg Svg instance, and the ref has toDataURL() but no download().',
},
{
key: 'props',
content:
'svgProps are react-native-svg SvgProps, so use testID, style and accessibility props there.',
},
{
key: 'font',
content:
'Frame labels use the system font by default. To use another, load it in your app (for example with expo-font) and pass its name as frameSettings.fontFamily.',
},
{
key: 'label',
content:
'Labels too long for the frame are squeezed horizontally to fit, because react-native-svg does not support textLength.',
},
{
key: 'cross',
content: 'imageSettings.crossOrigin has no effect.',
},
]}
/>

<TypographyH2>Expo web</TypographyH2>
<TypographyP>
On Expo web the package loads its DOM build, so web rules apply there: pass{' '}
<TypographyInlineCode>imageSettings.src</TypographyInlineCode> as a URL rather
than a <TypographyInlineCode>require()</TypographyInlineCode>, and keep{' '}
<TypographyInlineCode>svgProps</TypographyInlineCode> to props a DOM svg accepts.
TypeScript in an Expo project uses the React Native types, so it will not flag
these.
</TypographyP>

<TypographyH2>Testing with Jest</TypographyH2>
<TypographyP>
The React Native build is an ES module, so add{' '}
<TypographyInlineCode>@lglab</TypographyInlineCode> to the packages Jest
transforms. With the jest-expo preset:
</TypographyP>
<CodeEditor code={jestConfig} language='json' />
</>
)
}
Loading
Loading