diff --git a/.changeset/config.json b/.changeset/config.json index a471b4df..ab8933f0 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -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"] } diff --git a/.changeset/react-native-support.md b/.changeset/react-native-support.md new file mode 100644 index 00000000..02d40e95 --- /dev/null +++ b/.changeset/react-native-support.md @@ -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. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3972d37f..ead0916d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/CLAUDE.md b/CLAUDE.md index 4227cc94..c812553e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -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. diff --git a/README.md b/README.md index 20b3ea8b..a7141fa7 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/apps/docs/public/llms-full.txt b/apps/docs/public/llms-full.txt index b081531c..6fe3a89e 100644 --- a/apps/docs/public/llms-full.txt +++ b/apps/docs/public/llms-full.txt @@ -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` | Renders the QR code to a PNG and resolves with its data URL (`data:image/png;base64,...`). Web and React Native. | ### DownloadOptions @@ -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(null) +const png = await ref.current?.toDataURL({ size: 1024 }) // "data:image/png;base64,..." +``` + --- ## Examples diff --git a/apps/docs/public/llms.txt b/apps/docs/public/llms.txt index 09e77872..ad75b942 100644 --- a/apps/docs/public/llms.txt +++ b/apps/docs/public/llms.txt @@ -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) diff --git a/apps/docs/src/app/page.tsx b/apps/docs/src/app/page.tsx index 20424fb0..9403cdfc 100644 --- a/apps/docs/src/app/page.tsx +++ b/apps/docs/src/app/page.tsx @@ -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' @@ -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 = [ @@ -64,6 +64,27 @@ export default function Page() { ))} + + +
+

Works in React Native

+

+ The same component renders in React Native and Expo apps through{' '} + + react-native-svg + + , with every style, logo and frame option. +

+
+ + Learn more + + + + { + return ( + + ) +} +` + +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(null) + + const exportPng = async () => { + // Resolves with "data:image/png;base64,..." + const png = await ref.current?.toDataURL({ size: 1024 }) + // Show it with , or write the base64 + // part to a file with expo-file-system and share it. + } + + return ( + <> + +