|
| 1 | +<script setup> |
| 2 | +import { QRCode, QRCodeCorrectLevel } from '@stacksjs/qrx' |
| 3 | +import { onMounted, ref, watch } from 'vue' |
| 4 | +// import { QRCode, QRCodeCorrectLevel } from './qrcode' |
| 5 | +
|
| 6 | +const text = ref('Hello VitePress!') |
| 7 | +const errorLevel = ref('2') // H level by default |
| 8 | +const useSVG = ref(true) |
| 9 | +const qrContainer = ref(null) |
| 10 | +let qrCode = null |
| 11 | +
|
| 12 | +function updateQRCode() { |
| 13 | + if (!qrContainer.value) |
| 14 | + return |
| 15 | +
|
| 16 | + if (qrCode) { |
| 17 | + qrCode.clear() |
| 18 | + } |
| 19 | +
|
| 20 | + qrCode = new QRCode(qrContainer.value, { |
| 21 | + text: text.value, |
| 22 | + width: 256, |
| 23 | + height: 256, |
| 24 | + colorDark: '#000000', |
| 25 | + colorLight: '#ffffff', |
| 26 | + correctLevel: Number.parseInt(errorLevel.value), |
| 27 | + useSVG: useSVG.value, |
| 28 | + }) |
| 29 | +} |
| 30 | +
|
| 31 | +onMounted(() => { |
| 32 | + updateQRCode() |
| 33 | +}) |
| 34 | +
|
| 35 | +watch([text, errorLevel, useSVG], () => { |
| 36 | + updateQRCode() |
| 37 | +}) |
| 38 | +</script> |
| 39 | + |
| 40 | +<template> |
| 41 | + <div class="qr-demo"> |
| 42 | + <div class="controls"> |
| 43 | + <div class="input-group"> |
| 44 | + <label for="qr-text">Text to encode:</label> |
| 45 | + <input |
| 46 | + id="qr-text" |
| 47 | + v-model="text" |
| 48 | + type="text" |
| 49 | + placeholder="Enter text to encode" |
| 50 | + > |
| 51 | + </div> |
| 52 | + |
| 53 | + <div class="input-group"> |
| 54 | + <label for="error-level">Error Correction:</label> |
| 55 | + <select id="error-level" v-model="errorLevel"> |
| 56 | + <option value="1"> |
| 57 | + Level L (7%) |
| 58 | + </option> |
| 59 | + <option value="0"> |
| 60 | + Level M (15%) |
| 61 | + </option> |
| 62 | + <option value="3"> |
| 63 | + Level Q (25%) |
| 64 | + </option> |
| 65 | + <option value="2"> |
| 66 | + Level H (30%) |
| 67 | + </option> |
| 68 | + </select> |
| 69 | + </div> |
| 70 | + |
| 71 | + <div class="input-group"> |
| 72 | + <label> |
| 73 | + <input v-model="useSVG" type="checkbox"> |
| 74 | + Use SVG Renderer |
| 75 | + </label> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + |
| 79 | + <div ref="qrContainer" class="qr-container" /> |
| 80 | + </div> |
| 81 | +</template> |
| 82 | + |
| 83 | +<style scoped> |
| 84 | +.qr-demo { |
| 85 | + padding: 20px; |
| 86 | + max-width: 600px; |
| 87 | + margin: 0 auto; |
| 88 | +} |
| 89 | +
|
| 90 | +.controls { |
| 91 | + margin-bottom: 20px; |
| 92 | +} |
| 93 | +
|
| 94 | +.input-group { |
| 95 | + margin-bottom: 15px; |
| 96 | +} |
| 97 | +
|
| 98 | +.input-group label { |
| 99 | + display: block; |
| 100 | + margin-bottom: 5px; |
| 101 | + font-weight: 500; |
| 102 | +} |
| 103 | +
|
| 104 | +input[type="text"] { |
| 105 | + width: 100%; |
| 106 | + padding: 8px; |
| 107 | + border: 1px solid #ddd; |
| 108 | + border-radius: 4px; |
| 109 | +} |
| 110 | +
|
| 111 | +select { |
| 112 | + padding: 8px; |
| 113 | + border: 1px solid #ddd; |
| 114 | + border-radius: 4px; |
| 115 | + background-color: white; |
| 116 | +} |
| 117 | +
|
| 118 | +.qr-container { |
| 119 | + display: flex; |
| 120 | + justify-content: center; |
| 121 | + align-items: center; |
| 122 | + min-height: 256px; |
| 123 | + padding: 20px; |
| 124 | + border: 1px solid #ddd; |
| 125 | + border-radius: 4px; |
| 126 | +} |
| 127 | +</style> |
0 commit comments