Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

QR Code Generator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource https://esm.sh/react */
import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function App() {
const [text, setText] = useState("");
const [qrCode, setQrCode] = useState("");
const generateQR = async () => {
if (text) {
const qr = await import("https://esm.sh/qrcode-generator");
const qrCode = qr.default(0, "L");
qrCode.addData(text);
qrCode.make();
const svgString = qrCode.createSvgTag({ cellSize: 4, margin: 4 });
setQrCode(svgString);
}
};
return (
<div className="container">
<h1>QR Code Generator</h1>
<input
type="text"
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="Enter text for QR code"
/>
<button onClick={generateQR}>Generate QR Code</button>
{qrCode && <div dangerouslySetInnerHTML={{ __html: qrCode }} />}
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") {
client();
}
export default async function server(request: Request): Promise<Response> {
return new Response(
`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
<title>QR Code Generator</title>
<style>${css}</style>
</head>
<body>
<div id="root"></div>
<script type="module" src="${import.meta.url}"></script>
</body>
</html>
`,
{
headers: { "Content-Type": "text/html" },
},
);
}
const css = `
body {
font-family: 'Inter', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f7f7f7;
}
.container {
background-color: white;
padding: 2rem;
border-radius: 12px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 400px;
width: 100%;
box-sizing: border-box;
}
h1 {
font-size: 2rem;
margin-bottom: 1.5rem;
color: #333;
}
input {
width: 100%;
padding: 0.75rem;
margin: 1rem 0;
border: 1px solid #ddd;
muhammad_owais_warsi-qr_generator.web.val.run
September 11, 2024