snort/src/element/QrCode.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-01-07 20:54:12 +00:00
import QRCodeStyling from "qr-code-styling";
2023-01-08 20:36:36 +00:00
import { useEffect, useRef } from "react";
2023-01-07 20:54:12 +00:00
export default function QrCode(props) {
const qrRef = useRef();
2023-01-08 20:36:36 +00:00
2023-01-07 20:54:12 +00:00
useEffect(() => {
2023-01-08 20:36:36 +00:00
if (props.data?.length > 0) {
2023-01-07 20:54:12 +00:00
let qr = new QRCodeStyling({
width: props.width || 256,
height: props.height || 256,
2023-01-08 20:36:36 +00:00
data: props.data,
2023-01-07 20:54:12 +00:00
margin: 5,
type: 'canvas',
image: props.avatar,
dotsOptions: {
type: 'rounded'
},
cornersSquareOptions: {
type: 'extra-rounded'
},
imageOptions: {
crossOrigin: "anonymous"
}
});
qrRef.current.innerHTML = "";
qr.append(qrRef.current);
2023-01-08 20:36:36 +00:00
if (props.link) {
qrRef.current.onclick = function (e) {
let elm = document.createElement("a");
elm.href = props.link;
elm.click();
}
2023-01-07 20:54:12 +00:00
}
2023-01-07 23:01:32 +00:00
} else {
qrRef.current.innerHTML = "";
2023-01-07 20:54:12 +00:00
}
2023-01-08 20:36:36 +00:00
}, [props.data, props.link]);
2023-01-07 20:54:12 +00:00
return (
<div className="qr" ref={qrRef}></div>
);
}