File size: 2,152 Bytes
2068834
 
 
 
 
 
 
 
 
5eac338
2068834
 
5eac338
2068834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5eac338
2068834
5eac338
2068834
5eac338
2068834
5eac338
2068834
 
 
 
 
 
 
5eac338
 
2068834
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { clock } from "@/app/fonts"
import { cn } from "@/lib/utils"

import { splitElapsedTime } from "../utils/splitElapsedTime"
import { zeroPad } from "../utils/zeroPad"
import { Separator } from "../Separator"

export function Counter({
  valueInMs,
  color
}: {
  valueInMs: number
  color: string
}) {

  const { hours, minutes, seconds, milliseconds } = splitElapsedTime(valueInMs)

  return (
    <div className={cn(
      `flex flex-row items-center justify-center`,
      `flex-grow`,
      `transition-all duration-200 ease-out`,
      clock.className,
      `text-2xl @md:text-2xl @lg:text-2xl @xl:text-3xl @2xl:text-4xl`
    )}>
      <div
        // we use a div with a fixed width to avoid glitchy recentering movement when counting
        // this is also why we need a MONOSPACE font!
        className={cn(
          `transition-all duration-200 ease-out`,
          
          // the purpose of the fixed height and w is to avoid any unwanted line return,
          // especially during fast resize (when the animation is playing slower than container resize)
          `pl-1.5 @lg:w-36 @xl:w-44 @2xl:w-52 h-8 @2xl:h-10 overflow-hidden`,

          // sometimes we can accidentally select text when double clicking on the play button,
          // so we want to avoid that 
          `select-none`
        )}>
        <span style={{ color }}>{
          zeroPad(hours || 0)
        }</span><Separator color={color} /><span  style={{ color }}>{
          zeroPad(minutes || 0)
        }</span><Separator color={color} /><span  style={{ color }}>{
          zeroPad(seconds || 0)
        }</span><Separator color={color} /><span className={cn(

          // to add some flair we reduce the opacity of the last milliseconds,
          // since this element is less important (it moves too fast to be really meaningful)
          "opacity-50",

          // the milliseconds are also reduced of 1 degree in size
          `text-lg @md:text-xl @lg:text-xl @xl:text-2xl @2xl:text-3xl`
          )}
          style={{ color }}>{
          zeroPad(Math.min(999, milliseconds || 0)).slice(0, 2)
        }</span>
      </div>
    </div>
  )
}