|
| 1 | +import { useId, useMemo, type ReactNode, type SVGProps } from "react"; |
| 2 | +import { create } from "qrcode"; |
| 3 | + |
| 4 | +const CELL_SPACING_RATIO = 0.2; |
| 5 | +const FINDER_PATTERN_SIZE = 7; |
| 6 | +const QUIET_ZONE_SIZE = 4; |
| 7 | +const MAX_CENTER_OBSCURED_RATIO = 0.1; |
| 8 | +const CENTER_ICON_SIZE_RATIO = 0.8; |
| 9 | + |
| 10 | +type StyledQrCodeProps = Omit< |
| 11 | + SVGProps<SVGSVGElement>, |
| 12 | + "children" | "height" | "title" | "width" |
| 13 | +> & { |
| 14 | + backgroundColor?: string; |
| 15 | + centerImageSrc?: string; |
| 16 | + foregroundColor?: string; |
| 17 | + size?: number; |
| 18 | + title?: string; |
| 19 | + value: string; |
| 20 | +}; |
| 21 | + |
| 22 | +function isFinderCell(row: number, column: number, matrixSize: number) { |
| 23 | + const isTop = row < FINDER_PATTERN_SIZE; |
| 24 | + const isLeft = column < FINDER_PATTERN_SIZE; |
| 25 | + const isRight = column >= matrixSize - FINDER_PATTERN_SIZE; |
| 26 | + const isBottom = row >= matrixSize - FINDER_PATTERN_SIZE; |
| 27 | + |
| 28 | + return (isTop && isLeft) || (isTop && isRight) || (isBottom && isLeft); |
| 29 | +} |
| 30 | + |
| 31 | +function getCenterAreaSize(matrixSize: number) { |
| 32 | + const maxArea = matrixSize * matrixSize * MAX_CENTER_OBSCURED_RATIO; |
| 33 | + const maxSide = Math.floor(Math.sqrt(maxArea)); |
| 34 | + return maxSide % 2 === 0 ? maxSide - 1 : maxSide; |
| 35 | +} |
| 36 | + |
| 37 | +function FinderPattern({ |
| 38 | + backgroundColor, |
| 39 | + foregroundColor, |
| 40 | + x, |
| 41 | + y, |
| 42 | +}: { |
| 43 | + backgroundColor: string; |
| 44 | + foregroundColor: string; |
| 45 | + x: number; |
| 46 | + y: number; |
| 47 | +}) { |
| 48 | + return ( |
| 49 | + <g data-qr-finder-pattern=""> |
| 50 | + <rect |
| 51 | + fill={foregroundColor} |
| 52 | + height={FINDER_PATTERN_SIZE} |
| 53 | + rx={2} |
| 54 | + width={FINDER_PATTERN_SIZE} |
| 55 | + x={x} |
| 56 | + y={y} |
| 57 | + /> |
| 58 | + <rect |
| 59 | + fill={backgroundColor} |
| 60 | + height={FINDER_PATTERN_SIZE - 2} |
| 61 | + rx={0.8} |
| 62 | + width={FINDER_PATTERN_SIZE - 2} |
| 63 | + x={x + 1} |
| 64 | + y={y + 1} |
| 65 | + /> |
| 66 | + <rect |
| 67 | + fill={foregroundColor} |
| 68 | + height={FINDER_PATTERN_SIZE - 4} |
| 69 | + rx={0.4} |
| 70 | + width={FINDER_PATTERN_SIZE - 4} |
| 71 | + x={x + 2} |
| 72 | + y={y + 2} |
| 73 | + /> |
| 74 | + </g> |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +export function StyledQrCode({ |
| 79 | + backgroundColor = "#ffffff", |
| 80 | + centerImageSrc, |
| 81 | + foregroundColor = "#000000", |
| 82 | + size = 240, |
| 83 | + title = "QR code", |
| 84 | + value, |
| 85 | + ...svgProps |
| 86 | +}: StyledQrCodeProps) { |
| 87 | + const clipId = `styled-qr-logo-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; |
| 88 | + const matrix = useMemo( |
| 89 | + () => create(value, { errorCorrectionLevel: "M" }).modules, |
| 90 | + [value], |
| 91 | + ); |
| 92 | + const viewBoxSize = matrix.size + QUIET_ZONE_SIZE * 2; |
| 93 | + const dataCellSize = 1 - CELL_SPACING_RATIO; |
| 94 | + const dataCellRadius = dataCellSize / 2; |
| 95 | + const centerAreaSize = centerImageSrc ? getCenterAreaSize(matrix.size) : 0; |
| 96 | + const centerAreaStart = (matrix.size - centerAreaSize) / 2; |
| 97 | + const centerAreaEnd = centerAreaStart + centerAreaSize; |
| 98 | + const centerImageSize = centerAreaSize * CENTER_ICON_SIZE_RATIO; |
| 99 | + const centerImageStart = (matrix.size - centerImageSize) / 2; |
| 100 | + const cells: ReactNode[] = []; |
| 101 | + |
| 102 | + for (let row = 0; row < matrix.size; row += 1) { |
| 103 | + for (let column = 0; column < matrix.size; column += 1) { |
| 104 | + const isCenterCell = |
| 105 | + centerImageSrc && |
| 106 | + row >= centerAreaStart && |
| 107 | + row < centerAreaEnd && |
| 108 | + column >= centerAreaStart && |
| 109 | + column < centerAreaEnd; |
| 110 | + |
| 111 | + if ( |
| 112 | + !matrix.get(row, column) || |
| 113 | + isFinderCell(row, column, matrix.size) || |
| 114 | + isCenterCell |
| 115 | + ) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + cells.push( |
| 120 | + <circle |
| 121 | + cx={column + 0.5} |
| 122 | + cy={row + 0.5} |
| 123 | + fill={foregroundColor} |
| 124 | + key={`${row}-${column}`} |
| 125 | + r={dataCellRadius} |
| 126 | + />, |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return ( |
| 132 | + <svg |
| 133 | + {...svgProps} |
| 134 | + aria-label={title} |
| 135 | + data-qr-matrix-size={matrix.size} |
| 136 | + height={size} |
| 137 | + role="img" |
| 138 | + viewBox={`${-QUIET_ZONE_SIZE} ${-QUIET_ZONE_SIZE} ${viewBoxSize} ${viewBoxSize}`} |
| 139 | + width={size} |
| 140 | + xmlns="http://www.w3.org/2000/svg" |
| 141 | + > |
| 142 | + <title>{title}</title> |
| 143 | + <rect |
| 144 | + fill={backgroundColor} |
| 145 | + height={viewBoxSize} |
| 146 | + width={viewBoxSize} |
| 147 | + x={-QUIET_ZONE_SIZE} |
| 148 | + y={-QUIET_ZONE_SIZE} |
| 149 | + /> |
| 150 | + <g data-qr-data-cells="">{cells}</g> |
| 151 | + <FinderPattern |
| 152 | + backgroundColor={backgroundColor} |
| 153 | + foregroundColor={foregroundColor} |
| 154 | + x={0} |
| 155 | + y={0} |
| 156 | + /> |
| 157 | + <FinderPattern |
| 158 | + backgroundColor={backgroundColor} |
| 159 | + foregroundColor={foregroundColor} |
| 160 | + x={matrix.size - FINDER_PATTERN_SIZE} |
| 161 | + y={0} |
| 162 | + /> |
| 163 | + <FinderPattern |
| 164 | + backgroundColor={backgroundColor} |
| 165 | + foregroundColor={foregroundColor} |
| 166 | + x={0} |
| 167 | + y={matrix.size - FINDER_PATTERN_SIZE} |
| 168 | + /> |
| 169 | + {centerImageSrc ? ( |
| 170 | + <> |
| 171 | + <defs> |
| 172 | + <clipPath id={clipId}> |
| 173 | + <rect |
| 174 | + height={centerImageSize} |
| 175 | + rx={2} |
| 176 | + width={centerImageSize} |
| 177 | + x={centerImageStart} |
| 178 | + y={centerImageStart} |
| 179 | + /> |
| 180 | + </clipPath> |
| 181 | + </defs> |
| 182 | + <rect |
| 183 | + fill={foregroundColor} |
| 184 | + height={centerImageSize} |
| 185 | + rx={2} |
| 186 | + width={centerImageSize} |
| 187 | + x={centerImageStart} |
| 188 | + y={centerImageStart} |
| 189 | + /> |
| 190 | + <image |
| 191 | + clipPath={`url(#${clipId})`} |
| 192 | + height={centerImageSize} |
| 193 | + href={centerImageSrc} |
| 194 | + preserveAspectRatio="xMidYMid meet" |
| 195 | + width={centerImageSize} |
| 196 | + x={centerImageStart} |
| 197 | + y={centerImageStart} |
| 198 | + /> |
| 199 | + </> |
| 200 | + ) : null} |
| 201 | + </svg> |
| 202 | + ); |
| 203 | +} |
0 commit comments