|
112 | 112 | <script lang="ts"> |
113 | 113 | import { tick } from 'svelte'; |
114 | 114 |
|
115 | | - let state = $state<TooltipState>({ text: '', x: 0, y: 0, visible: false, position: 'bottom' }); |
| 115 | + let tooltipState: TooltipState = $state({ text: '', x: 0, y: 0, visible: false, position: 'bottom' }); |
116 | 116 | let tooltipEl: HTMLDivElement | undefined = $state(); |
117 | | - let adjustment = $state({ x: 0, y: 0 }); |
| 117 | + let adjustment: { x: number; y: number } = $state({ x: 0, y: 0 }); |
118 | 118 |
|
119 | 119 | tooltipStore.subscribe(async (s) => { |
120 | | - state = s; |
| 120 | + tooltipState = s; |
121 | 121 | adjustment = { x: 0, y: 0 }; // Reset adjustment |
122 | 122 | if (s.visible) { |
123 | 123 | // Wait for DOM update, then check viewport collision |
|
155 | 155 | let transform = $derived.by(() => { |
156 | 156 | const { x, y } = adjustment; |
157 | 157 | const adj = (x !== 0 || y !== 0) ? ` translate(${x}px, ${y}px)` : ''; |
158 | | - switch (state.position) { |
| 158 | + switch (tooltipState.position) { |
159 | 159 | case 'top': |
160 | 160 | return `translateX(-50%) translateY(-100%)${adj}`; |
161 | 161 | case 'left': |
|
169 | 169 | }); |
170 | 170 | </script> |
171 | 171 |
|
172 | | -{#if state.visible} |
| 172 | +{#if tooltipState.visible} |
173 | 173 | <div |
174 | 174 | bind:this={tooltipEl} |
175 | 175 | class="tooltip" |
176 | | - style="left: {state.x}px; top: {state.y}px; transform: {transform};{state.maxWidth ? ` max-width: ${state.maxWidth}px;` : ''}" |
| 176 | + style="left: {tooltipState.x}px; top: {tooltipState.y}px; transform: {transform};{tooltipState.maxWidth ? ` max-width: ${tooltipState.maxWidth}px;` : ''}" |
177 | 177 | > |
178 | | - <span class="text">{state.text}</span> |
179 | | - {#if state.shortcut} |
180 | | - <span class="shortcut">{state.shortcut}</span> |
| 178 | + <span class="text">{tooltipState.text}</span> |
| 179 | + {#if tooltipState.shortcut} |
| 180 | + <span class="shortcut">{tooltipState.shortcut}</span> |
181 | 181 | {/if} |
182 | 182 | </div> |
183 | 183 | {/if} |
|
0 commit comments