diff --git a/src/components/QrCode.tsx b/src/components/QrCode.tsx new file mode 100644 index 0000000..a85716a --- /dev/null +++ b/src/components/QrCode.tsx @@ -0,0 +1,46 @@ +import { + Component, + createEffect, + createResource, + JSX, + splitProps, +} from "solid-js"; +import qrcode from "qrcode"; + +const QrCode: Component< + { + margin?: number; + value: string; + errorCorrectionLevel?: string; + } & JSX.HTMLAttributes +> = (p) => { + const [props, rest] = splitProps(p, [ + "margin", + "value", + "errorCorrectionLevel", + ]); + + let ref: HTMLDivElement = undefined!; + + const [svg] = createResource( + () => props.value, + (value) => { + return qrcode.toString(value, { + type: "svg", + margin: props.margin, + errorCorrectionLevel: props.errorCorrectionLevel, + }); + } + ); + + createEffect(() => { + const html = svg(); + requestAnimationFrame(function () { + ref.innerHTML = html; + }); + }); + + return
; +}; + +export default QrCode;