next-qrcode

repository·master·Indexed 41 days ago

https://github.com/bunlong/next-qrcode

React hooks for generating QR codes in Next.js and React applications. Version 2.5.1 provides the useQRCode hook, which offers Canvas, SVG, and Image rendering components. It supports multiple encoding modes, character sets (Kanji, Chinese, Emojis), and custom configurations for error correction, margins, and colors. The Canvas component specifically supports adding a centered or manually positioned logo overlay.

Tokens
2.6K
Snippets
10
Records
13
Agent score
49%

What's inside next-qrcode

  1. How useQRCode works

    master

    The useQRCode hook is the primary entry point for the library. It returns an object containing different rendering components: Canvas, SVG, and Image. You can choose the component based on your rendering needs (e.g., Canvas for pixel-based rendering, SVG for vector graphics, or Image for standard image formats).

    import { useQRCode } from 'next-qrcode';
    
    function App() {
      const { Canvas, SVG, Image } = useQRCode();
      // ... use components
    }
  2. Use the Canvas component

    master

    The Canvas component renders the QR code using an HTML5 Canvas. It is useful for pixel-based rendering and supports adding a logo in the center.

    import React from 'react';
    import { useQRCode } from 'next-qrcode';
    
    function App() {
      const { Canvas } = useQRCode();
    
      return (
        <Canvas
          text={'https://github.com/bunlong/next-qrcode'}
          options={{
            errorCorrectionLevel: 'M',
            margin: 3,
            scale: 4,
            width: 200,
            color: {
              dark: '#010599FF',
              light: '#FFBF60FF',
            },
          }}
        />
      );
    }
  3. Use the SVG component

    master

    The SVG component renders the QR code as a scalable vector graphic. This is ideal for high-quality displays and print media.

    import React from 'react';
    import { useQRCode } from 'next-qrcode';
    
    function App() {
      const { SVG } = useQRCode();
    
      return (
        <SVG
          text={'https://github.com/bunlong/next-qrcode'}
          options={{
            margin: 2,
            width: 200,
            color: {
              dark: '#010599FF',
              light: '#FFBF60FF',
            },
          }}
        />
      );
    }
  4. Use the Image component

    master

    The Image component renders the QR code as a standard image element (e.g., <img>). This is useful when you need to provide a specific image format like PNG, JPEG, or WebP.

    import React from 'react';
    import { useQRCode } from 'next-qrcode';
    
    function App() {
      const { Image } = useQRCode();
    
      return (
        <Image
          text={'https://github.com/bunlong/next-qrcode'}
          options={{
            type: 'image/jpeg',
            quality: 0.3,
            errorCorrectionLevel: 'M',
            margin: 3,
            scale: 4,
            width: 200,
            color: {
              dark: '#010599FF',
              light: '#FFBF60FF',
            },
          }}
        />
      );
    }
  5. SVG props and options

    master

    SVG Props

    PropTypeRequiredDescription
    textstring✔️Text/URL to encode.
    optionsoptions❌QR code options.

    SVG Options

    PropTypeDefaultRequiredDescription
    marginnumber4❌Width of the quiet zone.
    widthnumber4❌Forces specific width. Takes precedence over scale.
    color.darkstring#000000ff❌Color of dark module (Hex RGBA).
    color.lightstring#ffffffff❌Color of light module (Hex RGBA).
  6. Canvas props and options

    master

    Canvas Props

    PropTypeRequiredDescription
    textstring✔️Text/URL to encode.
    optionsoptions❌QR code options.
    logologo❌QR code logo.

    QR Code Options

    PropTypeDefaultRequiredDescription
    errorCorrectionLevelstringM❌Correction level. Values: low, medium, quartile, high or L, M, Q, H.
    marginnumber4❌Width of the quiet zone.
    scalenumber4❌Scale factor (1 = 1px per module).
    widthnumber4❌Forces specific width. Takes precedence over scale.
    color.darkstring#000000ff❌Color of dark module (Hex RGBA). Must be darker than color.light.
    color.lightstring#ffffffff❌Color of light module (Hex RGBA).

    Logo Options

    PropTypeRequiredDescription
    srcstring✔️The path to the image.
    optionsoptions❌Logo options.

    Logo Options Details:

    PropTypeRequiredDescription
    widthnumber❌Logo dimension.
    xnumber❌X position (defaults to center).
    ynumber❌Y position (defaults to center).
  7. Image props and options

    master

    Image Props

    PropTypeRequiredDescription
    textstring✔️Text/URL to encode.
    optionsobject❌QR code options.

    Image Options

    PropTypeDefaultRequiredDescription
    typestringimage/png❌Image format (image/png, image/jpeg, image/webp).
    qualitynumber0.92❌Quality between 0 and 1 (for jpeg or webp).
    errorCorrectionLevelstringM❌Correction level. Values: low, medium, quartile, high or L, M, Q, H.
    marginnumber4❌Width of the quiet zone.
    scalenumber4❌Scale factor (1 = 1px per module).
    widthnumber4❌Forces specific width. Takes precedence over scale.
    color.darkstring#000000ff❌Color of dark module (Hex RGBA).
    color.lightstring#ffffffff❌Color of light module (Hex RGBA).
  8. Use the useQRCode hook to generate QR codes

    master

    The useQRCode hook is the primary entry point for the library. It returns three specialized React components: Image, Canvas, and SVG. Each component accepts a text prop (the content of the QR code) and optional options or logo configurations.

    Components

    • Image: Renders an <img> element with the QR code as a Data URL.
    • Canvas: Renders a <canvas> element. This is the only component that supports a logo overlay.
    • SVG: Renders a <div> containing the QR code as an inline SVG string.
    import { useQRCode } from 'next-qrcode';
    
    const { Canvas, Image, SVG } = useQRCode();
    
    function MyComponent() {
      return (
        <Canvas 
          text="https://example.com" 
          options={{ margin: 2 }} 
        />
      );
    }
  9. Add a logo to a Canvas QR code

    master

    To overlay a logo on a QR code, you must use the Canvas component. The logo prop accepts an object containing the image source and optional positioning options.

    Logo Positioning

    • Manual Positioning: Provide x and y coordinates within logo.options to place the logo at a specific location.
    • Automatic Centering: If x and y are omitted, the component automatically calculates the center position based on the QR code's width and the logo's width.

    Logo Options

    • width: The size of the logo (defaults to 30).
    • x: The horizontal coordinate.
    • y: The vertical coordinate.
    const { Canvas } = useQRCode();
    
    <Canvas
      text="https://example.com"
      logo={{
        src: "/path/to/logo.png",
        options: {
          width: 40,
          x: 10,
          y: 10
        }
      }}
    />