| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 1 | import { css, html, LitElement } from "lit"; |
| 2 | import { customElement, property } from "lit/decorators.js"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 3 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 4 | @customElement("sketch-network-status") |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 5 | export class SketchNetworkStatus extends LitElement { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 6 | @property() |
| 7 | connection: string; |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 8 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 9 | @property() |
| 10 | message: string; |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 11 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 12 | @property() |
| 13 | error: string; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 14 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 15 | // See https://lit.dev/docs/components/styles/ for how lit-element handles CSS. |
| 16 | // Note that these styles only apply to the scope of this web component's |
| 17 | // shadow DOM node, so they won't leak out or collide with CSS declared in |
| 18 | // other components or the containing web page (...unless you want it to do that). |
| 19 | |
| 20 | static styles = css` |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 21 | .status-container { |
| 22 | display: flex; |
| 23 | align-items: center; |
| 24 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 25 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 26 | .polling-indicator { |
| 27 | display: inline-block; |
| 28 | width: 8px; |
| 29 | height: 8px; |
| 30 | border-radius: 50%; |
| 31 | margin-right: 4px; |
| 32 | background-color: #ccc; |
| 33 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 34 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 35 | .polling-indicator.active { |
| 36 | background-color: #4caf50; |
| 37 | animation: pulse 1.5s infinite; |
| 38 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 39 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 40 | .polling-indicator.error { |
| 41 | background-color: #f44336; |
| 42 | animation: pulse 1.5s infinite; |
| 43 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 44 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 45 | @keyframes pulse { |
| 46 | 0% { |
| 47 | opacity: 1; |
| 48 | } |
| 49 | 50% { |
| 50 | opacity: 0.5; |
| 51 | } |
| 52 | 100% { |
| 53 | opacity: 1; |
| 54 | } |
| 55 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 56 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 57 | .status-text { |
| 58 | font-size: 11px; |
| 59 | color: #666; |
| 60 | } |
| 61 | `; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 62 | |
| 63 | constructor() { |
| 64 | super(); |
| 65 | } |
| 66 | |
| 67 | // See https://lit.dev/docs/components/lifecycle/ |
| 68 | connectedCallback() { |
| 69 | super.connectedCallback(); |
| 70 | } |
| 71 | |
| 72 | // See https://lit.dev/docs/components/lifecycle/ |
| 73 | disconnectedCallback() { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 74 | super.disconnectedCallback(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | indicator() { |
| 78 | if (this.connection === "disabled") { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 79 | return ""; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 80 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 81 | return this.connection === "connected" ? "active" : "error"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | render() { |
| 85 | return html` |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 86 | <div class="status-container"> |
| 87 | <span |
| 88 | id="pollingIndicator" |
| 89 | class="polling-indicator ${this.indicator()}" |
| 90 | ></span> |
| 91 | <span id="statusText" class="status-text" |
| 92 | >${this.error || this.message}</span |
| 93 | > |
| 94 | </div> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 95 | `; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | declare global { |
| 100 | interface HTMLElementTagNameMap { |
| 101 | "sketch-network-status": SketchNetworkStatus; |
| 102 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 103 | } |