|
| 1 | +# html5-qrcode with lit |
| 2 | + |
| 3 | +<img src="https://lit.dev/images/logo.svg" height="80px"><br> |
| 4 | +[lit.dev](https://lit.dev/) |
| 5 | + |
| 6 | +## How to create a `lit` component for `html5-qrcode` |
| 7 | + |
| 8 | +### Create the component in JavaScript |
| 9 | + |
| 10 | +```js |
| 11 | +import { html, css, LitElement } from "https://unpkg.com/lit?module"; |
| 12 | +import { Html5QrcodeScanner } from "https://unpkg.com/html5-qrcode?module"; |
| 13 | + |
| 14 | +export class QRCodeScanner extends LitElement { |
| 15 | + render() { |
| 16 | + return html` |
| 17 | + <div id="reader"></div> |
| 18 | + <div>${this.decodedText}</div> |
| 19 | + <div>${this.errorMessage}</div> |
| 20 | + `; |
| 21 | + } |
| 22 | + |
| 23 | + firstUpdated() { |
| 24 | + const onScanSuccess = (decodedText, decodedResult) => { |
| 25 | + // handle the scanned code as you like |
| 26 | + this.decodedText = decodedText; |
| 27 | + }; |
| 28 | + |
| 29 | + const onScanFailure = (errorMessage, error) => { |
| 30 | + // handle scan failure, usually better to ignore and keep scanning |
| 31 | + this.errorMessage = errorMessage; |
| 32 | + }; |
| 33 | + |
| 34 | + const config = { |
| 35 | + fps: 10, |
| 36 | + qrbox: { |
| 37 | + width: 350, |
| 38 | + height: 250, |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + const reader = this.shadowRoot.querySelector("#reader"); |
| 43 | + const scanner = new Html5QrcodeScanner(reader, config); |
| 44 | + |
| 45 | + scanner.render(onScanSuccess, onScanFailure); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +customElements.define("qrcode-scanner", QRCodeScanner); |
| 50 | +``` |
| 51 | + |
| 52 | +### Create the component in TypeScript |
| 53 | + |
| 54 | +```ts |
| 55 | +import { css, html, LitElement, TemplateResult } from 'lit'; |
| 56 | +import { customElement } from 'lit/decorators/custom-element.js'; |
| 57 | +import { query } from 'lit-element'; |
| 58 | +import { Html5QrcodeError, Html5QrcodeResult } from 'html5-qrcode/esm/core'; |
| 59 | +import { Html5QrcodeScanner } from 'html5-qrcode'; |
| 60 | + |
| 61 | +@customElement('qrcode-scanner') |
| 62 | +export class QRCodeScanner extends LitElement { |
| 63 | + @query('#reader') |
| 64 | + reader: HTMLElement; |
| 65 | + |
| 66 | + protected render(): TemplateResult { |
| 67 | + return html` <div id="reader"></div> `; |
| 68 | + } |
| 69 | + |
| 70 | + protected firstUpdated(): void { |
| 71 | + const onScanSuccess = ( |
| 72 | + decodedText: string, |
| 73 | + decodedResult: Html5QrcodeResult |
| 74 | + ) => { |
| 75 | + // handle the scanned code as you like |
| 76 | + console.log(`Code matched = ${decodedText}`, decodedResult); |
| 77 | + }; |
| 78 | + |
| 79 | + const onScanFailure = (errorMessage: string, error: Html5QrcodeError) => { |
| 80 | + // handle scan failure, usually better to ignore and keep scanning |
| 81 | + console.warn(`Code scan error = ${errorMessage}`, error); |
| 82 | + }; |
| 83 | + |
| 84 | + const config = { |
| 85 | + fps: 10, |
| 86 | + qrbox: { |
| 87 | + width: 350, |
| 88 | + height: 250, |
| 89 | + }, |
| 90 | + }; |
| 91 | + |
| 92 | + const scanner = new Html5QrcodeScanner( |
| 93 | + this.reader, |
| 94 | + config, |
| 95 | + false |
| 96 | + ); |
| 97 | + |
| 98 | + scanner.render(onScanSuccess, onScanFailure); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +declare global { |
| 103 | + interface HTMLElementTagNameMap { |
| 104 | + 'qrcode-scanner': QRCodeScanner; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +### Include the component in your HTML page |
| 111 | + |
| 112 | +```html |
| 113 | +<!DOCTYPE html> |
| 114 | +<head> |
| 115 | + <script type="module" src="./qrcode-scanner.js"></script> |
| 116 | +</head> |
| 117 | +<body> |
| 118 | + <qrcode-scanner></qrcode-scanner> |
| 119 | +</body> |
| 120 | +``` |
| 121 | + |
| 122 | +### Contributors |
| 123 | +| Name | Profile| |
| 124 | +| ----- | ------ | |
| 125 | +| Markus Fürer | [@kusigit](https://github.com/kusigit) | |
0 commit comments