You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
880 B
TypeScript
47 lines
880 B
TypeScript
import {
|
|
Component,
|
|
createEffect,
|
|
createResource,
|
|
JSX,
|
|
splitProps,
|
|
} from "solid-js";
|
|
import qrcode from "qrcode";
|
|
|
|
const QrCode: Component<
|
|
{
|
|
margin?: number;
|
|
value: string;
|
|
errorCorrectionLevel?: string;
|
|
} & JSX.HTMLAttributes<HTMLDivElement>
|
|
> = (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 as any,
|
|
});
|
|
}
|
|
);
|
|
|
|
createEffect(() => {
|
|
const html = svg() || "";
|
|
requestAnimationFrame(function () {
|
|
ref.innerHTML = html;
|
|
});
|
|
});
|
|
|
|
return <div ref={ref} {...rest}></div>;
|
|
};
|
|
|
|
export default QrCode;
|