1 | import { connect } from "./connect";
|
2 | export class InstallButton extends HTMLElement {
|
3 | connectedCallback() {
|
4 | if (this.renderRoot) {
|
5 | return;
|
6 | }
|
7 | this.renderRoot = this.attachShadow({ mode: "open" });
|
8 | if (!InstallButton.isSupported || !InstallButton.isAllowed) {
|
9 | this.toggleAttribute("install-unsupported", true);
|
10 | this.renderRoot.innerHTML = !InstallButton.isAllowed
|
11 | ? "<slot name='not-allowed'>You can only install ESP devices on HTTPS websites or on the localhost.</slot>"
|
12 | : "<slot name='unsupported'>Your browser does not support installing things on ESP devices. Use Google Chrome or Microsoft Edge.</slot>";
|
13 | return;
|
14 | }
|
15 | this.toggleAttribute("install-supported", true);
|
16 | const slot = document.createElement("slot");
|
17 | slot.addEventListener("click", async (ev) => {
|
18 | ev.preventDefault();
|
19 | connect(this);
|
20 | });
|
21 | slot.name = "activate";
|
22 | const button = document.createElement("button");
|
23 | button.innerText = "Connect";
|
24 | slot.append(button);
|
25 | if ("adoptedStyleSheets" in Document.prototype &&
|
26 | "replaceSync" in CSSStyleSheet.prototype) {
|
27 | const sheet = new CSSStyleSheet();
|
28 | sheet.replaceSync(InstallButton.style);
|
29 | this.renderRoot.adoptedStyleSheets = [sheet];
|
30 | }
|
31 | else {
|
32 | const styleSheet = document.createElement("style");
|
33 | styleSheet.innerText = InstallButton.style;
|
34 | this.renderRoot.append(styleSheet);
|
35 | }
|
36 | this.renderRoot.append(slot);
|
37 | }
|
38 | }
|
39 | InstallButton.isSupported = "serial" in navigator;
|
40 | InstallButton.isAllowed = window.isSecureContext;
|
41 | InstallButton.style = `
|
42 | button {
|
43 | position: relative;
|
44 | cursor: pointer;
|
45 | font-size: 14px;
|
46 | font-weight: 500;
|
47 | padding: 10px 24px;
|
48 | color: var(--esp-tools-button-text-color, #fff);
|
49 | background-color: var(--esp-tools-button-color, #03a9f4);
|
50 | border: none;
|
51 | border-radius: var(--esp-tools-button-border-radius, 9999px);
|
52 | }
|
53 | button::before {
|
54 | content: " ";
|
55 | position: absolute;
|
56 | top: 0;
|
57 | bottom: 0;
|
58 | left: 0;
|
59 | right: 0;
|
60 | opacity: 0.2;
|
61 | border-radius: var(--esp-tools-button-border-radius, 9999px);
|
62 | }
|
63 | button:hover::before {
|
64 | background-color: rgba(255,255,255,.8);
|
65 | }
|
66 | button:focus {
|
67 | outline: none;
|
68 | }
|
69 | button:focus::before {
|
70 | background-color: white;
|
71 | }
|
72 | button:active::before {
|
73 | background-color: grey;
|
74 | }
|
75 | :host([active]) button {
|
76 | color: rgba(0, 0, 0, 0.38);
|
77 | background-color: rgba(0, 0, 0, 0.12);
|
78 | box-shadow: none;
|
79 | cursor: unset;
|
80 | pointer-events: none;
|
81 | }
|
82 | .hidden {
|
83 | display: none;
|
84 | }`;
|
85 | customElements.define("esp-web-install-button", InstallButton);
|