Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/59 create with html element instead of id only #697

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c667224
Send HTML Element instead of Id to scanner
bilal-elchami Feb 8, 2023
dcabcdc
Add backward compatibility
bilal-elchami Feb 8, 2023
6bb601f
Rename variables & format documents
bilal-elchami Feb 8, 2023
006e728
Remove unused var & format documents
bilal-elchami Feb 13, 2023
73d0ebf
Merge branch 'master' into feature/59_Create_With_HTML_Element_Instea…
bilal-elchami Feb 13, 2023
2d937ea
Update package-lock
bilal-elchami Feb 13, 2023
891ce00
Add shadow dom example
bilal-elchami Feb 13, 2023
240dcc8
Update Example & Documentation
bilal-elchami Feb 13, 2023
ddea928
Format example
bilal-elchami Feb 13, 2023
735ef23
Revert 3rd party fix
bilal-elchami Feb 13, 2023
a70cd3a
Reduce constructor complexity & fix back quotes
bilal-elchami Feb 13, 2023
133a752
Initialize the container in the constructor
bilal-elchami Feb 13, 2023
2db7242
Replace single quotes with double quotes
bilal-elchami Feb 13, 2023
d987f39
Revert html5 example & upload new build
bilal-elchami Feb 13, 2023
2c875e8
Merge branch 'master' into feature/59_Create_With_HTML_Element_Instea…
bilal-elchami Feb 25, 2023
88b5a26
Remove manual scan call
bilal-elchami Feb 27, 2023
1bad070
Merge branch 'master' into feature/59_Create_With_HTML_Element_Instea…
bilal-elchami Mar 3, 2023
313e4cb
Revert minified and package lock files
bilal-elchami Mar 3, 2023
33c3428
Update readmes
bilal-elchami Mar 3, 2023
df96228
Revert package lock and minified files
bilal-elchami Mar 3, 2023
77c78b8
Fix Code Analyzer warning
bilal-elchami Mar 3, 2023
bcc6017
Merge branch 'master' into feature/59_Create_With_HTML_Element_Instea…
bilal-elchami Jun 29, 2023
f67598c
Merge latest master and fix test errors
bilal-elchami Jun 29, 2023
5d80902
Fix readme table
bilal-elchami Jun 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ We are working continuously on adding support for more and more platforms. If yo
### Framework support
The library can be easily used with several other frameworks, I have been adding examples for a few of them and would continue to add more.

|<img src="https://scanapp.org/assets/github_assets/html5.png" width="30px" />| <img src="https://scanapp.org/assets/github_assets/vuejs.png" width="30px" />|<img src="https://scanapp.org/assets/github_assets/electron.png" width="30px" /> | <img src="https://scanapp.org/assets/github_assets/react.svg" width="30px" /> | <img src="https://seeklogo.com/images/L/lit-logo-6B43868CDC-seeklogo.com.png" width="30px" />
| -------- | -------- | -------- | -------- | -------- |
| [Html5](./examples/html5) | [VueJs](./examples/vuejs) | [ElectronJs](./examples/electron) | [React](https://github.com/scanapp-org/html5-qrcode-react) | [Lit](./examples/lit)
|<img src="https://scanapp.org/assets/github_assets/html5.png" width="30px" />| <img src="https://scanapp.org/assets/github_assets/vuejs.png" width="30px" />|<img src="https://scanapp.org/assets/github_assets/electron.png" width="30px" /> | <img src="https://scanapp.org/assets/github_assets/react.svg" width="30px" /> | <img src="https://seeklogo.com/images/L/lit-logo-6B43868CDC-seeklogo.com.png" width="30px" /> | <img src="https://pbs.twimg.com/profile_images/1511434207079407618/AwzUxnVf_400x400.png" height="30px" />
| -------- | -------- | -------- | -------- | -------- | -------- |
| [Html5](./examples/html5) | [VueJs](./examples/vuejs) | [ElectronJs](./examples/electron) | [React](https://github.com/scanapp-org/html5-qrcode-react) | [Lit](./examples/lit) | [Shadow DOM](./examples/html5-shadow-dom)

### Supported Code formats
Code scanning is dependent on [Zxing-js](https://github.com/zxing-js/library) library. We will be working on top of it to add support for more types of code scanning. If you feel a certain type of code would be helpful to have, please file a feature request.
Expand Down
51 changes: 51 additions & 0 deletions examples/html5-shadow-dom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# html5-qrcode with HTML Element

## Include the js library in your project
```html
<script src="https://unpkg.com/html5-qrcode"></script>
```

## Create the component in JavaScript
```js
class CustomComponent extends HTMLElement {
constructor() {
// Always call super first in constructor
super();

// Create a shadow root
const shadow = this.attachShadow({ mode: 'open' });

// Create elements
const wrapper = document.createElement('div');
const title = document.createElement('h2');
title.innerText = 'HTML5 qr-code Scanner with shadow dom components';
const idReader = document.createElement('div');
idReader.className = 'qr-reader';
idReader.style.width = '500px';
wrapper.appendChild(title);
wrapper.appendChild(idReader);
// Attach the created elements to the shadow dom
shadow.appendChild(wrapper);
}
}

// Define the new element
customElements.define('custom-component', CustomComponent);
```

## Query an HTML element from DOM or Shadow DOM and pass it to the library

```js
const myCustomComponent = document.querySelector("custom-component");
const childComponentQrReaderElement = myCustomComponent.shadowRoot.querySelector("div.qr-reader");
const html5QrCodeScanner = new Html5QrcodeScanner(childComponentQrReaderElement, {
fps: 10,
qrbox: 250,
});
html5QrCodeScanner.render(onScanSuccess);
```
bilal-elchami marked this conversation as resolved.
Show resolved Hide resolved

### Contributors
| Name | Profile|
| ----- | ------ |
| Bilal EL CHAMI | [@bilal-elchami](https://github.com/bilal-elchami) |
67 changes: 67 additions & 0 deletions examples/html5-shadow-dom/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<html>

<head>
<title>Html-Qrcode Demo</title>

<body>
<h1>This is a Shadow Dom example</h1>
<custom-component></custom-component>
</body>
<script src="/html5-qrcode.min.js"></script>
<script>
// Create a class for the element
class CustomComponent extends HTMLElement {
constructor() {
// Always call super first in constructor
super();

// Create a shadow root
const shadow = this.attachShadow({ mode: 'open' });

// Create elements
const wrapper = document.createElement('div');
const title = document.createElement('h2');
title.innerText = 'HTML5 qr-code Scanner with shadow dom components';
const idReader = document.createElement('div');
idReader.className = 'qr-reader';
idReader.style.width = '500px';
wrapper.appendChild(title);
wrapper.appendChild(idReader);
// Attach the created elements to the shadow dom
shadow.appendChild(wrapper);
}
}

// Define the new element
customElements.define('custom-component', CustomComponent);

function docReady(fn) {
// see if DOM is already available
if (document.readyState === "complete"
|| document.readyState === "interactive") {
// call on next available tick
setTimeout(fn, 1);
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}

docReady(function () {
var lastResult, countResults = 0;
function onScanSuccess(decodedText, decodedResult) {
if (decodedText !== lastResult) {
++countResults;
lastResult = decodedText;
// Handle on success condition with the decoded message.
console.log(`Scan result ${decodedText}`, decodedResult);
}
}
const myCustomComponent = document.querySelector('custom-component');
const childComponentQrReaderElement = myCustomComponent.shadowRoot.querySelector('div.qr-reader')
var html5QrCodeScanner = new Html5QrcodeScanner(childComponentQrReaderElement, { fps: 10, qrbox: 250 });
html5QrCodeScanner.render(onScanSuccess);
});
</script>
</head>

</html>
Loading