react-native-qrcode-styled
Version:
A fully customizable QR Code generator for React Native based on react-native-svg and javascript-qrcode.
67 lines (65 loc) • 1.58 kB
JavaScript
;
import React, { useEffect, useMemo } from 'react';
import { Image as SVGImage } from 'react-native-svg';
import useQRCodeLogoSize from "../hooks/useQRCodeLogoSize.js";
import { jsx as _jsx } from "react/jsx-runtime";
export default function SVGQRLogo({
errorCorrectionLevel,
pieceSize,
qrCodeSize,
padding = 0,
scale = 1,
href,
x,
y,
onChange,
...props
}) {
const {
width,
height
} = useQRCodeLogoSize({
errorCorrectionLevel,
logoHref: href,
logoScale: scale,
pieceSize,
qrCodeSize
});
const svgSize = pieceSize * qrCodeSize;
const hasSize = width > 0 && height > 0;
const logoX = Number(x ?? (width ? svgSize / 2 - width / 2 : 0));
const logoY = Number(y ?? (height ? svgSize / 2 - height / 2 : 0));
const logoArea = useMemo(() => hasSize ? {
x: logoX,
y: logoY,
width,
height
} : undefined, [hasSize, logoX, logoY, width, height]);
useEffect(() => {
if (hasSize) {
onChange?.(logoArea);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasSize, logoArea]);
// padding limitations
let _padding = padding;
if (_padding * 2 > width) {
_padding = width / 2;
}
if (_padding * 2 > height) {
_padding = height / 2;
}
if (!hasSize) {
return null;
}
return /*#__PURE__*/_jsx(SVGImage, {
href: href,
x: logoX + _padding,
y: logoY + _padding,
preserveAspectRatio: "xMidYMid meet",
...props,
width: width - _padding * 2,
height: height - _padding * 2
});
}
//# sourceMappingURL=SVGQRLogo.js.map