@@ -59,15 +59,15 @@ const STYLE = `
5959 }
6060 .${ CONTAINER_CLS } :hover .${ TOOLTIP_CLS } { opacity: 1; visibility: visible; }
6161 .tooltip-left {
62- left: auto ;
63- right: calc(100% + 8px) ;
62+ left: calc(100% + 8px) ;
63+ right: auto ;
6464 top: 50%;
6565 bottom: auto;
6666 transform: translateY(-50%);
6767 }
6868 .tooltip-right {
69- left: calc(100% + 8px) ;
70- right: auto ;
69+ left: auto ;
70+ right: calc(100% + 8px) ;
7171 top: 50%;
7272 bottom: auto;
7373 transform: translateY(-50%);
@@ -92,13 +92,6 @@ function createSvgEl<K extends keyof SVGElementTagNameMap>(tag: K, attributes: R
9292 return el
9393}
9494
95- function getStage ( remaining : number , total : number ) : Stage {
96- const progress = remaining / total
97- if ( progress < 0.2 || remaining < 30 * MILL_PER_SECOND ) return 'short'
98- if ( progress < 0.5 || remaining < 2 * MILL_PER_MINUTE ) return 'warning'
99- return 'enough'
100- }
101-
10295type RingState = { progress : number , stage : Stage }
10396
10497class ProgressRing extends StatefulNode < RingState , SVGSVGElement > {
@@ -284,16 +277,6 @@ class Tooltip extends StatefulNode<RemainingData[], HTMLDivElement> {
284277 }
285278}
286279
287- function getTooltipEdge ( position : Position ) : Edge {
288- const { w } = getViewportSize ( )
289- const { x } = clampPosition ( position )
290- const spaceRight = w - x - HALF_SIZE
291- const spaceLeft = x - HALF_SIZE
292- if ( spaceRight >= TOOLTIP_MAX_WIDTH ) return 'right'
293- if ( spaceLeft >= TOOLTIP_MAX_WIDTH ) return 'left'
294- return spaceRight >= spaceLeft ? 'right' : 'left'
295- }
296-
297280class Container extends StatefulNode < Edge , HTMLElement > {
298281 protected init ( ) : HTMLElement {
299282 const el = document . createElement ( 'div' )
@@ -352,15 +335,16 @@ export class Icon {
352335 }
353336
354337 set position ( position : Position ) {
355- this . #container. render ( getEdge ( position ) )
356- this . #tooltip. edge = getTooltipEdge ( position )
338+ const edge = getEdge ( position )
339+ this . #container. render ( edge )
340+ this . #tooltip. edge = edge
357341 this . #tooltip. y = position . y
358342 }
359343
360344 render ( position : Position , { target, all } : CountdownData ) {
361345 const { remaining, total } = target
362346 this . #text. render ( this . #formatRemaining( remaining ) )
363- const stage = getStage ( remaining , total )
347+ const stage = this . #calcStage ( remaining , total )
364348 const lastStage = this . #ring. state ?. stage
365349 const progress = Math . ceil ( remaining / total * 100 )
366350 this . #ring. render ( { stage, progress } )
@@ -381,6 +365,13 @@ export class Icon {
381365 return `${ seconds } s`
382366 }
383367
368+ #calcStage( remaining : number , total : number ) : Stage {
369+ const progress = remaining / total
370+ if ( progress < 0.2 || remaining < 30 * MILL_PER_SECOND ) return 'short'
371+ if ( progress < 0.5 || remaining < 2 * MILL_PER_MINUTE ) return 'warning'
372+ return 'enough'
373+ }
374+
384375 destroy ( ) {
385376 this . el . remove ( )
386377 }
0 commit comments