1
0

install-button.ts 3.1 KB

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