react-native-qrcode-styled

repository·main·Indexed 42 days ago

https://github.com/tokkozhin/react-native-qrcode-styled

A fully customizable QR Code generator for React Native version 0.4.0, built on react-native-svg and javascript-qrcode. It supports advanced styling including linear and radial gradients, custom piece shapes (rounded or cut), logo overlays with piece hiding, and independent styling for inner and outer eye patterns.

Tokens
4.9K
Snippets
17
Records
23
Agent score
51%

What's inside react-native-qrcode-styled

  1. Install react-native-qrcode-styled

    main

    Install the library along with its required dependency react-native-svg using npm or yarn. If you are using TypeScript, you should also install the @types/qrcode package as a development dependency.

    # Using npm
    npm i react-native-svg react-native-qrcode-styled
    
    # Using yarn
    yarn add react-native-svg react-native-qrcode-styled
    
    # For TypeScript users
    yarn add -D @types/qrcode
  2. Customize QR Code eyes with outerEyesOptions and innerEyesOptions

    main

    You can style the eyes (the three large squares in the corners) independently. You can provide a single configuration that applies to all eyes, or use AllEyesOptions to provide different styles for topLeft, topRight, and bottomLeft.

    This applies to both outerEyesOptions (the large squares) and innerEyesOptions (the small dots inside those squares).

    <SVGQRCodeStyled
      data="Hello"
      outerEyesOptions={{
        topLeft: { gradient: { colors: ['red', 'blue'] } },
        topRight: { color: 'green' },
        bottomLeft: { color: 'yellow' }
      }}
      innerEyesOptions={{ color: 'white' }}
    />
  3. Basic usage of QRCodeStyled

    main

    Import QRCodeStyled and provide the data prop to encode a message. You can also customize the background color, padding, and the size of each QR code piece.

    import QRCodeStyled from 'react-native-qrcode-styled';
    
    <QRCodeStyled
      data={'Simple QR Code'}
      style={{backgroundColor: 'white'}}
      padding={20}
      pieceSize={8}
    />
  4. Customize QR code eyes with EyeOptions

    main

    Use outerEyesOptions and innerEyesOptions to style the eye patterns of the QR code. You can provide a single EyeOptions object to apply to all eyes, or use AllEyesOptions to specify different configurations for topLeft, topRight, and bottomLeft positions.

    type EyeOptions = {
      scale?: PathProps['scale']; // scaleXY | [scaleX, scaleY]
      rotation?: string | number;
      borderRadius?: number | number[];
      color?: ColorValue;
      gradient?: GradientProps;
      stroke?: ColorValue;
      strokeWidth?: number;
    }
    
    type EyePosition = 'topLeft' | 'topRight' | 'bottomLeft';
    
    type AllEyesOptions = { [K in EyePosition]?: EyeOptions }
  5. Configure QRCodeStyled props

    main

    The QRCodeStyled component accepts several props to customize the appearance of the QR code. Key props include:

    • data: The string or array to encode.
    • size: The size of each individual piece.
    • pieceScale: Scale of each piece (default 1.03 to avoid gaps).
    • pieceCornerType: 'rounded' or 'cut'.
    • pieceBorderRadius: Border radius for corners.
    • color: The primary color of the QR code.
    • gradient: Configuration for linear or radial gradients.
    • logo: Configuration for an overlay logo.
    • errorCorrectionLevel: 'L', 'M', 'Q', or 'H'.
  6. Render custom QR code pieces with RenderCustomPieceItem

    main

    If you want complete control over the rendering of each individual piece, use the renderCustomPieceItem prop. This function receives the coordinates, size, and the bit matrix, and must return a React element (an SVG component) or null.

    type RenderCustomPieceItem = ({x, y, pieceSize, qrSize, bitMatrix}: {
      x: number;
      y: number;
      pieceSize: number;
      qrSize: number;
      bitMatrix: number[][];
    }) => React.ReactElement | null;
  7. Configure logo overlay with LogoOptions

    main

    The logo prop allows you to add an image to the center of the QR code. It supports standard SVG <Image/> props. You can also use hidePieces to remove the QR pieces behind the logo and onChange to receive the LogoArea (x, y, width, height) of the logo.

    export type LogoArea = {
      x: number;
      y: number;
      width: number;
      height: number;
    };
    
    export type LogoOptions = {
      hidePieces?: boolean;
      padding?: number;
      scale?: number;
      onChange?: (logoArea?: LogoArea) => void;
    } & SVGImageProps;
  8. Configure Metro for SVG support in the example project

    main

    The example project uses react-native-svg-transformer to handle SVG files. To replicate this setup in your own project, you must configure Metro to treat .svg files as source extensions rather than assets, and specify the transformer path.

    Key configuration steps:

    1. Enable unstable_enablePackageExports in the resolver.
    2. Add svg to resolver.sourceExts.
    3. Remove svg from resolver.assetExts to prevent it from being treated as a static asset.
    4. Set transformer.babelTransformerPath to react-native-svg-transformer.
    const path = require('path');
    const { getDefaultConfig } = require('@expo/metro-config');
    const { withMetroConfig } = require('react-native-monorepo-config');
    
    const root = path.resolve(__dirname, '..');
    
    const config = withMetroConfig(getDefaultConfig(__dirname), {
      root,
      dirname: __dirname,
    });
    
    config.resolver.unstable_enablePackageExports = true;
    config.resolver.sourceExts.push('svg');
    config.resolver.assetExts = config.resolver.assetExts.filter((ext) => ext !== 'svg');
    config.transformer = {
      ...config.transformer,
      babelTransformerPath: require.resolve('react-native-svg-transformer'),
    };
    
    module.exports = config;
  9. Configure gradients with GradientProps

    main

    You can apply gradients to the QR code using the gradient prop. The type can be either 'linear' or 'radial'.

    For 'linear', you can specify colors, start [x, y], end [x, y], and locations. For 'radial', you can specify colors, center [x, y], radius [x, y], and locations.

    type GradientType = 'linear' | 'radial';
    
    type LinearGradientProps = {
      colors?: ColorValue[];
      start?: [number, number]; // start point [x, y] (0 -> 0%, 1 -> 100%)
      end?: [number, number]; // end point [x, y] (0 -> 0%, 1 -> 100%)
      locations?: number[]; // list of colors positions (0 -> 0%, 1 -> 100%)
    };
    
    type RadialGradientProps = {
      colors?: ColorValue[];
      center?: [number, number]; // center point [x, y] (0 -> 0%, 1 -> 100%)
      radius?: [number, number]; // radiusXY [x, y] (0 -> 0%, 1 -> 100%)
      locations?: number[]; // list of colors positions (0 -> 0%, 1 -> 100%)
    };
    
    type GradientProps = {
      type?: GradientType;
      options?: LinearGradientProps | RadialGradientProps;
    };
  10. Use SVGQRCodeStyled to render a styled QR Code

    main

    The SVGQRCodeStyled component is the primary entry point for rendering highly customizable QR codes in React Native using SVG. It allows you to control the appearance of individual QR pieces, eyes (corners), logos, gradients, and backgrounds.

    Key capabilities include:

    • Styling Pieces: Control pieceCornerType ('rounded' or 'square'), pieceBorderRadius, pieceRotation, and pieceScale.
    • Eye Customization: Configure outerEyesOptions and innerEyesOptions separately to style the large corner squares and the small inner dots.
    • Gradients: Apply color gradients to the QR pieces or specifically to the eyes.
    • Logo Integration: Embed a logo in the center with options to hide pieces underneath the logo area.
    • Custom Rendering: Use renderCustomPieceItem to define how individual bits are drawn, or use children and renderBackground functions to inject custom SVG elements based on the pieceSize and bitMatrix.
    import SVGQRCodeStyled from 'react-native-qrcode-styled';
    
    <SVGQRCodeStyled
      data="https://example.com"
      size={200}
      color="black"
      pieceScale={1.1}
      pieceCornerType="rounded"
      logo={{ uri: 'https://example.com/logo.png' }}
    />