UNPKG

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