Web fills rewound to the start whenever played before their word, so every upcoming word filled at once. iOS ignored the blur filter because React Native draws it only with its SwiftUI filters flag, now turned on at launch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
244 lines
6.8 KiB
TypeScript
244 lines
6.8 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
|
|
import {
|
|
useIsPlaying,
|
|
usePlayback,
|
|
usePlaybackProgress,
|
|
} from "@/components/playback-provider"
|
|
import { useLyrics, useLyricsTime } from "@/hooks/use-lyrics"
|
|
import { cn } from "@/lib/utils"
|
|
import {
|
|
activeLineIndex,
|
|
type LyricLine,
|
|
type LyricWord,
|
|
type Lyrics,
|
|
} from "@otunes/convex/client/lyrics"
|
|
|
|
/** How long a manual scroll keeps the panel from following the song. */
|
|
const MANUAL_SCROLL_HOLD_MS = 3000
|
|
/** Blur by distance from the playing line; farther lines share the last. */
|
|
const LINE_BLUR = ["blur-none", "blur-[1px]", "blur-[2px]", "blur-[3px]"]
|
|
/** Unsung words wipe from dim to bright, soft edge first, as they are sung. */
|
|
const WORD_FILL = {
|
|
backgroundImage:
|
|
"linear-gradient(90deg, var(--foreground) 40%, color-mix(in oklab, var(--foreground) 45%, transparent) 60%)",
|
|
backgroundSize: "250% 100%",
|
|
}
|
|
const WORD_FILL_FRAMES = [
|
|
{ backgroundPosition: "100% 0" },
|
|
{ backgroundPosition: "0% 0" },
|
|
]
|
|
/** How far a word's fill may drift from playback before it is reset. */
|
|
const FILL_DRIFT_MS = 80
|
|
|
|
/** Lyrics beside the page, in the queue's place. Synced lines follow playback. */
|
|
export function LyricsPanel() {
|
|
const { currentSong } = usePlayback()
|
|
const lyrics = useLyrics(currentSong?.id ?? null)
|
|
|
|
return (
|
|
<aside
|
|
aria-label="Lyrics"
|
|
className={cn(
|
|
"flex h-svh w-72 shrink-0 flex-col border-l bg-background",
|
|
"max-md:fixed max-md:inset-y-0 max-md:right-0 max-md:z-20 max-md:shadow-xl"
|
|
)}
|
|
>
|
|
{lyrics === null ? (
|
|
<p className="m-auto text-sm text-muted-foreground">No lyrics</p>
|
|
) : lyrics ? (
|
|
<LyricsLines key={currentSong?.id} lyrics={lyrics} />
|
|
) : null}
|
|
</aside>
|
|
)
|
|
}
|
|
|
|
function LyricsLines({ lyrics }: { lyrics: Lyrics }) {
|
|
const { seekTo, currentTime } = usePlaybackProgress()
|
|
const isPlaying = useIsPlaying()
|
|
const { setIsPlaying } = usePlayback()
|
|
const time = useLyricsTime(lyrics, currentTime, isPlaying)
|
|
const active = activeLineIndex(lyrics, time)
|
|
const scrollRef = React.useRef<HTMLDivElement>(null)
|
|
const synced = lyrics.synced !== "none"
|
|
// While the listener scrolls, lines stay sharp and the panel stays put.
|
|
const [browsing, setBrowsing] = React.useState(false)
|
|
const browseTimer = React.useRef<ReturnType<typeof setTimeout>>(undefined)
|
|
|
|
React.useEffect(() => () => clearTimeout(browseTimer.current), [])
|
|
|
|
React.useEffect(() => {
|
|
const container = scrollRef.current
|
|
const line = container?.querySelector<HTMLElement>(
|
|
`[data-line="${Math.max(active, 0)}"]`
|
|
)
|
|
if (!container || !line || !synced || browsing) return
|
|
container.scrollTo({
|
|
top: line.offsetTop - container.clientHeight * 0.25,
|
|
behavior: "smooth",
|
|
})
|
|
}, [active, synced, browsing])
|
|
|
|
const playFrom = React.useCallback(
|
|
(seconds: number) => {
|
|
seekTo(seconds)
|
|
setIsPlaying(true)
|
|
clearTimeout(browseTimer.current)
|
|
setBrowsing(false)
|
|
},
|
|
[seekTo, setIsPlaying]
|
|
)
|
|
|
|
const holdScroll = () => {
|
|
setBrowsing(true)
|
|
clearTimeout(browseTimer.current)
|
|
browseTimer.current = setTimeout(
|
|
() => setBrowsing(false),
|
|
MANUAL_SCROLL_HOLD_MS
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div
|
|
ref={scrollRef}
|
|
onWheel={holdScroll}
|
|
onTouchMove={holdScroll}
|
|
className="group relative min-h-0 flex-1 overflow-y-auto px-6 pt-[25svh] pb-[60svh]"
|
|
>
|
|
{lyrics.lines.map((line, index) =>
|
|
synced ? (
|
|
<SyncedLine
|
|
key={index}
|
|
index={index}
|
|
line={line}
|
|
distance={
|
|
browsing
|
|
? 0
|
|
: Math.min(Math.abs(index - active), LINE_BLUR.length - 1)
|
|
}
|
|
active={index === active}
|
|
time={index === active ? time : null}
|
|
isPlaying={isPlaying}
|
|
onSelect={playFrom}
|
|
/>
|
|
) : (
|
|
<p
|
|
key={index}
|
|
className={cn(
|
|
"text-lg leading-snug font-semibold text-foreground/85",
|
|
line.text === "" && "h-4"
|
|
)}
|
|
>
|
|
{line.text}
|
|
</p>
|
|
)
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* One timed line, blurred by its `distance` from the playing line. Only the
|
|
* active line gets the playback time, so memoized lines elsewhere skip
|
|
* re-rendering as the song plays.
|
|
*/
|
|
const SyncedLine = React.memo(function SyncedLine({
|
|
index,
|
|
line,
|
|
distance,
|
|
active,
|
|
time,
|
|
isPlaying,
|
|
onSelect,
|
|
}: {
|
|
index: number
|
|
line: LyricLine
|
|
distance: number
|
|
active: boolean
|
|
time: number | null
|
|
isPlaying: boolean
|
|
onSelect: (seconds: number) => void
|
|
}) {
|
|
return (
|
|
<button
|
|
type="button"
|
|
data-line={index}
|
|
onClick={() => onSelect(line.start ?? 0)}
|
|
className={cn(
|
|
"-mx-2 my-1 block w-[calc(100%+1rem)] rounded-md px-2 py-1.5 text-left text-[22px] leading-tight font-bold transition-[color,filter] duration-300 group-hover:blur-none hover:bg-foreground/5",
|
|
LINE_BLUR[distance],
|
|
active && !line.words ? "text-foreground" : "text-foreground/30"
|
|
)}
|
|
>
|
|
{active && line.words && time !== null ? (
|
|
<FilledWords words={line.words} time={time} isPlaying={isPlaying} />
|
|
) : (
|
|
line.text
|
|
)}
|
|
</button>
|
|
)
|
|
})
|
|
|
|
/**
|
|
* The playing line's words, each filled as far as it has been sung. The
|
|
* browser runs the fills on the song's timeline (a word's start is its
|
|
* delay); playback updates only correct their drift.
|
|
*/
|
|
function FilledWords({
|
|
words,
|
|
time,
|
|
isPlaying,
|
|
}: {
|
|
words: LyricWord[]
|
|
time: number
|
|
isPlaying: boolean
|
|
}) {
|
|
const spans = React.useRef<(HTMLSpanElement | null)[]>([])
|
|
const fills = React.useRef<(Animation | undefined)[]>([])
|
|
|
|
React.useEffect(() => {
|
|
fills.current = words.map((word, index) =>
|
|
spans.current[index]?.animate(WORD_FILL_FRAMES, {
|
|
delay: word.start * 1000,
|
|
duration: Math.max(word.end - word.start, 0.05) * 1000,
|
|
easing: "linear",
|
|
fill: "both",
|
|
})
|
|
)
|
|
return () => fills.current.forEach((fill) => fill?.cancel())
|
|
}, [words])
|
|
|
|
React.useEffect(() => {
|
|
const at = time * 1000
|
|
fills.current.forEach((fill, index) => {
|
|
if (!fill) return
|
|
if (Math.abs(Number(fill.currentTime ?? 0) - at) > FILL_DRIFT_MS) {
|
|
fill.currentTime = at
|
|
}
|
|
// `play` on a finished fill would rewind it to the start.
|
|
if (isPlaying && time < words[index].end) fill.play()
|
|
else fill.pause()
|
|
})
|
|
}, [words, time, isPlaying])
|
|
|
|
return words.map((word, index) => {
|
|
const text = word.text.trimEnd()
|
|
return (
|
|
<React.Fragment key={index}>
|
|
<span
|
|
ref={(span) => {
|
|
spans.current[index] = span
|
|
}}
|
|
className="bg-clip-text text-transparent"
|
|
style={WORD_FILL}
|
|
>
|
|
{text}
|
|
</span>
|
|
{word.text.slice(text.length)}
|
|
</React.Fragment>
|
|
)
|
|
})
|
|
}
|