feat: add icon toggle and color picker to QR code component

This commit is contained in:
MinerAle 2025-02-27 08:21:51 +01:00
parent 3d5c4b0dd4
commit 1d04e199ce

View file

@ -1,5 +1,5 @@
<script setup> <script setup>
import { Button } from '#components' import { Button, Switch } from '#components'
import { Download, Palette } from 'lucide-vue-next' import { Download, Palette } from 'lucide-vue-next'
import QRCodeStyling from 'qr-code-styling' import QRCodeStyling from 'qr-code-styling'
import { ref, watch } from 'vue' import { ref, watch } from 'vue'
@ -14,6 +14,8 @@ const props = defineProps({
default: '', default: '',
}, },
}) })
const showIcon = ref(true)
const color = ref('#000000') const color = ref('#000000')
const options = { const options = {
width: 256, width: 256,
@ -21,7 +23,12 @@ const options = {
data: props.data, data: props.data,
margin: 10, margin: 10,
qrOptions: { typeNumber: '0', mode: 'Byte', errorCorrectionLevel: 'Q' }, qrOptions: { typeNumber: '0', mode: 'Byte', errorCorrectionLevel: 'Q' },
imageOptions: { hideBackgroundDots: true, imageSize: 0.4, margin: 2 }, imageOptions: {
hideBackgroundDots: true,
imageSize: 0.3,
margin: 0,
crossOrigin: 'anonymous',
},
dotsOptions: { type: 'dots', color: '#000000', gradient: null }, dotsOptions: { type: 'dots', color: '#000000', gradient: null },
backgroundOptions: { color: '#ffffff', gradient: null }, backgroundOptions: { color: '#ffffff', gradient: null },
image: props.image, image: props.image,
@ -72,6 +79,16 @@ const options = {
const qrCode = new QRCodeStyling(options) const qrCode = new QRCodeStyling(options)
const qrCodeEl = ref(null) const qrCodeEl = ref(null)
function updateIcon(show) {
qrCode.update({
image: show ? props.image : '',
})
}
watch(showIcon, (show) => {
updateIcon(show)
})
function updateColor(newColor) { function updateColor(newColor) {
qrCode.update({ qrCode.update({
dotsOptions: { type: 'dots', color: newColor, gradient: null }, dotsOptions: { type: 'dots', color: newColor, gradient: null },
@ -94,6 +111,7 @@ function downloadQRCode() {
onMounted(() => { onMounted(() => {
qrCode.append(qrCodeEl.value) qrCode.append(qrCodeEl.value)
updateIcon(showIcon.value)
}) })
</script> </script>
@ -103,20 +121,26 @@ onMounted(() => {
ref="qrCodeEl" ref="qrCodeEl"
:data-text="data" :data-text="data"
/> />
<div class="flex items-center gap-2"> <div class="flex items-center gap-4">
<div class="flex items-center gap-2 px-3 py-2 border rounded-md"> <div class="flex items-center gap-2">
<Palette class="w-4 h-4" /> <div class="flex items-center gap-2 px-3 py-2 border rounded-md">
<input <Palette class="w-4 h-4" />
v-model="color" <input
type="color" v-model="color"
class="w-20 h-8 rounded cursor-pointer" type="color"
title="Change QR code color" class="w-20 h-8 rounded cursor-pointer"
> title="Change QR code color"
>
</div>
<Button variant="outline" @click="downloadQRCode">
<Download class="w-4 h-4 mr-2" />
Download QR Code
</Button>
</div>
<div class="flex items-center gap-2">
<span class="text-sm text-gray-500">Icon</span>
<Switch v-model="showIcon" />
</div> </div>
<Button variant="outline" @click="downloadQRCode">
<Download class="w-4 h-4 mr-2" />
Download QR Code
</Button>
</div> </div>
</div> </div>
</template> </template>