|
| 1 | +import { LitElement } from 'lit' |
| 2 | +import { addScriptBreadcrumb } from '@components/scripts/errors' |
| 3 | +import { handleScriptError } from '@components/scripts/errors/handler' |
| 4 | +import { |
| 5 | + createAnimationController, |
| 6 | + type AnimationControllerHandle, |
| 7 | + type AnimationPlayState, |
| 8 | +} from '@components/scripts/store' |
| 9 | +import { defineCustomElement } from '@components/scripts/utils' |
| 10 | +import type { WebComponentModule } from '@components/scripts/@types/webComponentModule' |
| 11 | +import { queryHeroReadyTextElement } from './selectors' |
| 12 | + |
| 13 | +const SCRIPT_NAME = 'HomeHeroElement' |
| 14 | + |
| 15 | +const READY_TEXT = 'ready...' |
| 16 | +const READY_DELAY_MS = 500 |
| 17 | + |
| 18 | +export class HomeHeroElement extends LitElement { |
| 19 | + static registeredName = 'home-hero' |
| 20 | + |
| 21 | + private initialized = false |
| 22 | + private readyTextElement: HTMLSpanElement | null = null |
| 23 | + private animationController: AnimationControllerHandle | undefined |
| 24 | + private timeoutId: number | null = null |
| 25 | + private currentIndex = 0 |
| 26 | + private completed = false |
| 27 | + private renderContainer: HTMLDivElement | undefined |
| 28 | + |
| 29 | + protected override createRenderRoot() { |
| 30 | + // Preserve server-rendered HTML inside the element. |
| 31 | + // Lit needs a render root, so we render into a hidden container that does not affect layout. |
| 32 | + if (!this.renderContainer) { |
| 33 | + this.renderContainer = document.createElement('div') |
| 34 | + this.renderContainer.setAttribute('data-lit-root', '') |
| 35 | + this.renderContainer.style.display = 'none' |
| 36 | + this.appendChild(this.renderContainer) |
| 37 | + } |
| 38 | + |
| 39 | + return this.renderContainer |
| 40 | + } |
| 41 | + |
| 42 | + override connectedCallback(): void { |
| 43 | + super.connectedCallback() |
| 44 | + this.initialize() |
| 45 | + } |
| 46 | + |
| 47 | + override disconnectedCallback(): void { |
| 48 | + this.teardown() |
| 49 | + super.disconnectedCallback() |
| 50 | + } |
| 51 | + |
| 52 | + private initialize(): void { |
| 53 | + if (this.initialized) return |
| 54 | + |
| 55 | + const context = { scriptName: SCRIPT_NAME, operation: 'initialize' } |
| 56 | + addScriptBreadcrumb(context) |
| 57 | + |
| 58 | + try { |
| 59 | + this.readyTextElement = queryHeroReadyTextElement(this) |
| 60 | + |
| 61 | + if (!this.readyTextElement) { |
| 62 | + this.initialized = true |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // Baseline state: show prompt only until we decide to animate. |
| 67 | + this.readyTextElement.textContent = '' |
| 68 | + |
| 69 | + this.animationController = createAnimationController({ |
| 70 | + animationId: 'home-hero-ready', |
| 71 | + debugLabel: SCRIPT_NAME, |
| 72 | + defaultState: 'playing', |
| 73 | + onPlay: () => { |
| 74 | + this.handlePlay() |
| 75 | + }, |
| 76 | + onPause: () => { |
| 77 | + this.handlePause() |
| 78 | + }, |
| 79 | + }) |
| 80 | + |
| 81 | + this.initialized = true |
| 82 | + } catch (error) { |
| 83 | + handleScriptError(error, context) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private teardown(): void { |
| 88 | + this.clearTimeout() |
| 89 | + this.animationController?.destroy() |
| 90 | + this.animationController = undefined |
| 91 | + this.initialized = false |
| 92 | + this.readyTextElement = null |
| 93 | + this.currentIndex = 0 |
| 94 | + this.completed = false |
| 95 | + } |
| 96 | + |
| 97 | + private handlePlay(): void { |
| 98 | + if (this.completed) return |
| 99 | + if (!this.readyTextElement) return |
| 100 | + if (this.timeoutId !== null) return |
| 101 | + |
| 102 | + this.currentIndex = 0 |
| 103 | + this.readyTextElement.textContent = '' |
| 104 | + this.scheduleNextTick() |
| 105 | + } |
| 106 | + |
| 107 | + private handlePause(): void { |
| 108 | + if (this.completed) return |
| 109 | + if (!this.readyTextElement) return |
| 110 | + |
| 111 | + this.clearTimeout() |
| 112 | + this.readyTextElement.textContent = READY_TEXT |
| 113 | + this.completed = true |
| 114 | + } |
| 115 | + |
| 116 | + private scheduleNextTick(): void { |
| 117 | + if (!this.readyTextElement) return |
| 118 | + |
| 119 | + this.timeoutId = window.setTimeout(() => { |
| 120 | + this.timeoutId = null |
| 121 | + this.currentIndex += 1 |
| 122 | + |
| 123 | + this.readyTextElement!.textContent = READY_TEXT.slice(0, this.currentIndex) |
| 124 | + |
| 125 | + if (this.currentIndex >= READY_TEXT.length) { |
| 126 | + this.completed = true |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + this.scheduleNextTick() |
| 131 | + }, READY_DELAY_MS) |
| 132 | + } |
| 133 | + |
| 134 | + private clearTimeout(): void { |
| 135 | + if (this.timeoutId === null) return |
| 136 | + window.clearTimeout(this.timeoutId) |
| 137 | + this.timeoutId = null |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +export const registerWebComponent = async (tagName = HomeHeroElement.registeredName) => { |
| 142 | + if (typeof window === 'undefined') return |
| 143 | + defineCustomElement(tagName, HomeHeroElement) |
| 144 | +} |
| 145 | + |
| 146 | +export const registerHomeHeroWebComponent = registerWebComponent |
| 147 | + |
| 148 | +export const webComponentModule: WebComponentModule<HomeHeroElement> = { |
| 149 | + registeredName: HomeHeroElement.registeredName, |
| 150 | + componentCtor: HomeHeroElement, |
| 151 | + registerWebComponent, |
| 152 | +} |
| 153 | + |
| 154 | +export type { AnimationPlayState } |
0 commit comments