All files / qr-code/src QRCode.tsx

97.5% Statements 39/40
89.74% Branches 35/39
100% Functions 8/8
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267                        1x                                                                                                                     1x                                                     63x 63x 63x       179x   63x                                                                     64x   64x   64x   4x 4x               64x         64x 64x   64x     46x 43x 42x         64x 6x 4x     57x 25x                         32x 32x   32x                               2x 4x   2x 1x 1x           1x 2x                       32x                                                            
"use client";
 
import {
  type CSSProperties,
  type HTMLAttributes,
  type ReactNode,
  type Ref,
  useImperativeHandle,
  useMemo,
} from "react";
import type { QRMatrix, QROptions } from "./types";
import { type UseQRCodeOptions, useQRCode } from "./useQRCode";
import { linearEndpoints, radialGeometry } from "./render/gradient";
import type { PNGExport, PNGExportOptions } from "./render/png";
import type { RenderOptions } from "./render/svg";
 
/** Imperative handle exposed through {@link QRCodeProps.controllerRef}. */
export interface QRCodeController {
  /** The encoded symbol, or `null` when encoding failed. */
  getMatrix(): QRMatrix | null;
  /** Serialize as a standalone SVG document. */
  toSVG(options?: RenderOptions): string;
  /** Rasterize to a PNG. */
  toPNG(options?: PNGExportOptions): Promise<PNGExport>;
  /** Trigger a browser download. */
  download(format?: "png" | "svg", filename?: string): Promise<void>;
}
 
/**
 * Attributes forwarded to the rendered element. `className`/`style` are listed
 * explicitly below; everything else — `id`, `data-*`, event handlers — passes
 * straight through, which is what lets a consumer target the element from a
 * test or a stylesheet.
 */
type PassthroughAttributes = Omit<
  HTMLAttributes<SVGSVGElement & HTMLCanvasElement>,
  // `color` and `onError` collide with this component's own props; children
  // and raw HTML have no meaning inside a generated symbol.
  "color" | "onError" | "children" | "dangerouslySetInnerHTML"
>;
 
export interface QRCodeProps extends QROptions, PassthroughAttributes {
  /** The data to encode. */
  value: string | Uint8Array;
  /** Rendered width and height in px. @default 160 */
  size?: number;
  /**
   * `"svg"` renders scalable markup and server-renders with no client work.
   * `"canvas"` is cheaper when many codes are on screen at once.
   * @default "svg"
   */
  render?: "svg" | "canvas";
  controllerRef?: Ref<QRCodeController>;
  /**
   * Accessible name. Without one the code is `aria-hidden`, on the assumption
   * that nearby text already says where it leads — reading a raw URL aloud is
   * poor screen-reader UX. Pass `aria-label` or `aria-hidden` explicitly to
   * override that decision either way.
   */
  title?: string;
  /** Called when encoding fails. The component renders nothing in that case. */
  onError?: (error: Error) => void;
  /** Throw the encoding failure during render, for an error boundary to catch. @default false */
  throwOnError?: boolean;
  /** Backing-store scale for `render="canvas"`. @default `min(devicePixelRatio, 2)` */
  dpr?: number;
  className?: string;
  style?: CSSProperties;
}
 
/** Props consumed by this component rather than forwarded to the DOM. */
const OWN_PROPS = new Set([
  "value",
  "size",
  "render",
  "controllerRef",
  "throwOnError",
  "dpr",
  "onError",
  "level",
  "version",
  "minVersion",
  "mask",
  "eci",
  "margin",
  "fg",
  "bg",
  "moduleShape",
  "eyeShape",
  "eyeColor",
  "moduleGap",
  "logo",
  "className",
  "style",
  "title",
]);
 
function passthrough(props: QRCodeProps): Record<string, unknown> {
  const source = props as unknown as Record<string, unknown>;
  const rest: Record<string, unknown> = {};
  for (const key of Object.keys(source)) {
    // `undefined` means the caller said nothing. Carrying the key through
    // would let a spread of `aria-label={undefined}` erase the accessible name
    // the component just derived from `title`.
    if (!OWN_PROPS.has(key) && source[key] !== undefined) rest[key] = source[key];
  }
  return rest;
}
 
/**
 * A QR code.
 *
 * @example
 * ```tsx
 * <QRCode value="https://usefy.dev" size={200} level="Q" title="Open usefy.dev" />
 * ```
 *
 * @example Branded, with a logo
 * ```tsx
 * <QRCode
 *   value={ticketUrl}
 *   level="H"
 *   moduleShape="rounded"
 *   eyeShape="circle"
 *   fg={{ type: "linear", rotation: 45, stops: [
 *     { offset: 0, color: "#6366f1" },
 *     { offset: 1, color: "#ec4899" },
 *   ] }}
 *   logo={{ src: logoDataUri, size: 0.2 }}
 * />
 * ```
 */
export function QRCode(props: QRCodeProps): ReactNode {
  const {
    size = 160,
    render = "svg",
    controllerRef,
    throwOnError = false,
    className,
    style,
    title,
  } = props;
 
  const qr = useQRCode(props as UseQRCodeOptions);
 
  useImperativeHandle(
    controllerRef,
    () => ({
      getMatrix: () => qr.matrix,
      toSVG: qr.toSVG,
      toPNG: qr.toPNG,
      download: qr.download,
    }),
    [qr.matrix, qr.toSVG, qr.toPNG, qr.download],
  );
 
  const rest = passthrough(props);
 
  // A consumer's explicit aria-* always wins over the title-derived default.
  // `passthrough` has already dropped `undefined` values, so a key being
  // present here means the caller really did say something.
  const namedByConsumer = "aria-label" in rest || "aria-labelledby" in rest;
  const hiddenByConsumer = "aria-hidden" in rest;
 
  const accessibility = useMemo(() => {
    // `role="img"` accompanies any accessible name, whichever side supplied it:
    // a bare labelled <svg> is announced inconsistently across screen readers.
    if (namedByConsumer) return { role: "img" };
    if (hiddenByConsumer) return {};
    return title ? { role: "img", "aria-label": title } : { "aria-hidden": true as const };
  }, [title, namedByConsumer, hiddenByConsumer]);
 
  // Every hook has run by this point, so throwing here is safe for an error
  // boundary and cannot desynchronize the hook order.
  if (qr.error) {
    if (throwOnError) throw qr.error;
    return null;
  }
 
  if (render === "canvas") {
    return (
      <canvas
        ref={qr.canvasRef}
        className={className}
        // The JSX prop is the single owner of the CSS size; the draw effect
        // only sizes the backing store, so `style={{ width: "100%" }}` sticks.
        style={{ width: size, height: size, ...style }}
        {...accessibility}
        {...rest}
      />
    );
  }
 
  const svgProps = qr.svgProps;
  Iif (!svgProps) return null;
 
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      viewBox={svgProps.viewBox}
      width={size}
      height={size}
      shapeRendering={svgProps.shapeRendering}
      className={className}
      style={style}
      {...accessibility}
      {...rest}
    >
      {title ? <title>{title}</title> : null}
      {svgProps.defs.length > 0 ? (
        <defs>
          {svgProps.defs.map(({ id, gradient, bounds }) => {
            const stops = gradient.stops.map((stop, index) => (
              <stop key={index} offset={stop.offset} stopColor={stop.color} />
            ));
            if (gradient.type === "radial") {
              const { cx, cy, r } = radialGeometry(bounds);
              return (
                <radialGradient key={id} id={id} gradientUnits="userSpaceOnUse" cx={cx} cy={cy} r={r}>
                  {stops}
                </radialGradient>
              );
            }
            const { x1, y1, x2, y2 } = linearEndpoints(bounds, gradient.rotation ?? 0);
            return (
              <linearGradient key={id} id={id} gradientUnits="userSpaceOnUse" x1={x1} y1={y1} x2={x2} y2={y2}>
                {stops}
              </linearGradient>
            );
          })}
        </defs>
      ) : null}
      {svgProps.background ? (
        <rect width={svgProps.side} height={svgProps.side} fill={svgProps.background} />
      ) : null}
      {svgProps.paths.map((path, index) => (
        <path key={index} d={path.d} fill={path.fill} />
      ))}
      {svgProps.image ? (
        <>
          {svgProps.image.clipCircle ? (
            // The id is derived from the logo's own geometry (see render/ids.ts),
            // so two codes on one page can never clip each other's logo.
            <clipPath id={svgProps.image.clipId}>
              <circle
                cx={svgProps.image.x + svgProps.image.width / 2}
                cy={svgProps.image.y + svgProps.image.height / 2}
                r={svgProps.image.width / 2}
              />
            </clipPath>
          ) : null}
          <image
            href={svgProps.image.href}
            x={svgProps.image.x}
            y={svgProps.image.y}
            width={svgProps.image.width}
            height={svgProps.image.height}
            preserveAspectRatio="xMidYMid meet"
            crossOrigin={svgProps.image.crossOrigin}
            clipPath={svgProps.image.clipCircle ? `url(#${svgProps.image.clipId})` : undefined}
          />
        </>
      ) : null}
    </svg>
  );
}