125 lines
4.2 KiB
TypeScript
125 lines
4.2 KiB
TypeScript
import { useSyncExternalStore } from "react"
|
|
import { WallpaperImage } from "@/components/wallpaper"
|
|
import type { ClockFont, ClockStyle } from "@/lib/clock"
|
|
import { cn } from "@/vendor/collabry/lib/cn"
|
|
|
|
const subscribe = (tick: () => void) => {
|
|
const timer = setInterval(tick, 1000)
|
|
return () => clearInterval(timer)
|
|
}
|
|
// changes once a minute, so the clock re-renders once a minute
|
|
const currentMinute = () => Math.floor(Date.now() / 60_000)
|
|
|
|
const FONTS: Record<ClockFont, string> = {
|
|
serif: "font-serif tracking-wide",
|
|
sans: "font-sans font-bold tracking-tight",
|
|
light: "font-sans font-extralight tracking-tight",
|
|
rounded: "font-rounded font-extrabold",
|
|
slab: "font-slab font-semibold",
|
|
}
|
|
|
|
// What the wallpaper looks like through the digits, and how much white is in the glass.
|
|
const GLASS: Record<Exclude<ClockStyle, "solid">, [string, string]> = {
|
|
glass: ["blur-xl saturate-150 brightness-150", "bg-white/45"],
|
|
tinted: ["blur-3xl saturate-150 brightness-150", "bg-white/20"],
|
|
}
|
|
|
|
export function Clock({
|
|
font,
|
|
look,
|
|
wallpaper,
|
|
}: {
|
|
font: ClockFont
|
|
look: ClockStyle
|
|
wallpaper: string | null
|
|
}) {
|
|
// null on the server: its clock and locale are not the visitor's
|
|
const minute = useSyncExternalStore<number | null>(
|
|
subscribe,
|
|
currentMinute,
|
|
() => null
|
|
)
|
|
const now = minute === null ? null : new Date(minute * 60_000)
|
|
const parts = now
|
|
? new Intl.DateTimeFormat(undefined, {
|
|
hour: "numeric",
|
|
minute: "2-digit",
|
|
}).formatToParts(now)
|
|
: []
|
|
const time = parts
|
|
.filter((p) => p.type !== "dayPeriod")
|
|
.map((p) => p.value)
|
|
.join("")
|
|
.trim()
|
|
const period = parts.find((p) => p.type === "dayPeriod")?.value
|
|
|
|
// Each piece fades in by itself. Opacity on a common parent would cut the
|
|
// glass off from the wallpaper it blends with.
|
|
const fade = cn("transition-opacity duration-500", !now && "opacity-0")
|
|
const digits = (
|
|
<>
|
|
{time || "0:00"}
|
|
{period && (
|
|
<span className="absolute top-1 left-full ml-2 text-xl tracking-normal sm:top-3 sm:ml-4 sm:text-3xl">
|
|
{period}
|
|
</span>
|
|
)}
|
|
</>
|
|
)
|
|
|
|
return (
|
|
<div className="flex flex-col items-center text-white [text-shadow:0_2px_24px_rgb(0_0_0/0.45)]">
|
|
<p className={cn("text-lg font-medium sm:text-xl", fade)}>
|
|
{now
|
|
? now.toLocaleDateString(undefined, {
|
|
weekday: "long",
|
|
month: "long",
|
|
day: "numeric",
|
|
})
|
|
: "\u00a0"}
|
|
</p>
|
|
<time
|
|
dateTime={now?.toISOString()}
|
|
className={cn(
|
|
"relative mt-2 text-7xl leading-none sm:text-[9.5rem]",
|
|
FONTS[font]
|
|
)}
|
|
>
|
|
{look === "solid" ? (
|
|
<span className={cn("block", fade)}>{digits}</span>
|
|
) : (
|
|
<>
|
|
{/* the real text: it sets the size and casts the shadow */}
|
|
<span className={cn("block text-transparent", fade)}>{digits}</span>
|
|
{/* The glass. A blurred copy of the wallpaper lies exactly on the real
|
|
one. Multiplying it with white digits on black leaves it only inside
|
|
the digits, and screen drops the black. The clip keeps the copy
|
|
inside the clock, with room for AM/PM on the right. */}
|
|
<span
|
|
aria-hidden
|
|
className="absolute inset-0 isolate mix-blend-screen [clip-path:inset(-0.1em_-1em_-0.1em_-0.1em)] [text-shadow:none]"
|
|
>
|
|
<span className={cn("fixed inset-0", fade)}>
|
|
<WallpaperImage src={wallpaper} className={GLASS[look][0]} />
|
|
<span className={cn("fixed inset-0", GLASS[look][1])} />
|
|
</span>
|
|
<span className="absolute inset-0 bg-black mix-blend-multiply shadow-[0_0_0_100vmax_#000]">
|
|
{digits}
|
|
</span>
|
|
{/* light on the edges: only the shadows show, the black digits drop out */}
|
|
<span
|
|
className={cn(
|
|
"absolute inset-0 text-black mix-blend-screen [text-shadow:0_-1px_0.5px_#aaa,0_1px_0.5px_#444]",
|
|
fade
|
|
)}
|
|
>
|
|
{digits}
|
|
</span>
|
|
</span>
|
|
</>
|
|
)}
|
|
</time>
|
|
</div>
|
|
)
|
|
}
|