Compare commits
2
Commits
No files matched your search
@@ -7,6 +7,15 @@ import {
|
|||||||
View,
|
View,
|
||||||
type ScrollViewInstance,
|
type ScrollViewInstance,
|
||||||
} from "react-native"
|
} from "react-native"
|
||||||
|
import Animated, {
|
||||||
|
cancelAnimation,
|
||||||
|
Easing,
|
||||||
|
useAnimatedStyle,
|
||||||
|
useSharedValue,
|
||||||
|
withSequence,
|
||||||
|
withTiming,
|
||||||
|
type SharedValue,
|
||||||
|
} from "react-native-reanimated"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
usePlaybackActions,
|
usePlaybackActions,
|
||||||
@@ -18,6 +27,7 @@ import type { NowPlayingColors } from "@/lib/now-playing-colors"
|
|||||||
import {
|
import {
|
||||||
activeLineIndex,
|
activeLineIndex,
|
||||||
type LyricLine,
|
type LyricLine,
|
||||||
|
type LyricWord,
|
||||||
type Lyrics,
|
type Lyrics,
|
||||||
} from "@otunes/convex/client/lyrics"
|
} from "@otunes/convex/client/lyrics"
|
||||||
|
|
||||||
@@ -25,6 +35,8 @@ import {
|
|||||||
const MANUAL_SCROLL_HOLD_MS = 3000
|
const MANUAL_SCROLL_HOLD_MS = 3000
|
||||||
/** Where the playing line sits, as a share of the view's height from the top. */
|
/** Where the playing line sits, as a share of the view's height from the top. */
|
||||||
const ACTIVE_LINE_POSITION = 0.12
|
const ACTIVE_LINE_POSITION = 0.12
|
||||||
|
/** Blur radius by distance from the playing line; farther lines share the last. */
|
||||||
|
const LINE_BLUR = [0, 1, 2, 3]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lyrics in the player's own colors. Synced lines follow playback and seek
|
* Lyrics in the player's own colors. Synced lines follow playback and seek
|
||||||
@@ -70,19 +82,22 @@ function LyricsLines({
|
|||||||
const scrollRef = useRef<ScrollViewInstance>(null)
|
const scrollRef = useRef<ScrollViewInstance>(null)
|
||||||
const lineOffsets = useRef<number[]>([])
|
const lineOffsets = useRef<number[]>([])
|
||||||
const [height, setHeight] = useState(0)
|
const [height, setHeight] = useState(0)
|
||||||
const manualScrollAt = useRef(0)
|
// While the listener scrolls, lines stay sharp and the view stays put.
|
||||||
|
const [browsing, setBrowsing] = useState(false)
|
||||||
|
const browseTimer = useRef<ReturnType<typeof setTimeout>>(undefined)
|
||||||
|
|
||||||
|
useEffect(() => () => clearTimeout(browseTimer.current), [])
|
||||||
|
|
||||||
const scrollToLine = useCallback(
|
const scrollToLine = useCallback(
|
||||||
(index: number) => {
|
(index: number) => {
|
||||||
const y = lineOffsets.current[Math.max(index, 0)]
|
const y = lineOffsets.current[Math.max(index, 0)]
|
||||||
if (y === undefined || !synced || height === 0) return
|
if (y === undefined || !synced || height === 0 || browsing) return
|
||||||
if (Date.now() - manualScrollAt.current < MANUAL_SCROLL_HOLD_MS) return
|
|
||||||
scrollRef.current?.scrollTo({
|
scrollRef.current?.scrollTo({
|
||||||
y: Math.max(0, y - height * ACTIVE_LINE_POSITION),
|
y: Math.max(0, y - height * ACTIVE_LINE_POSITION),
|
||||||
animated: true,
|
animated: true,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
[height, synced]
|
[height, synced, browsing]
|
||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -106,11 +121,20 @@ function LyricsLines({
|
|||||||
(seconds: number) => {
|
(seconds: number) => {
|
||||||
seekTo(seconds)
|
seekTo(seconds)
|
||||||
setIsPlaying(true)
|
setIsPlaying(true)
|
||||||
manualScrollAt.current = 0
|
clearTimeout(browseTimer.current)
|
||||||
|
setBrowsing(false)
|
||||||
},
|
},
|
||||||
[seekTo, setIsPlaying]
|
[seekTo, setIsPlaying]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const releaseScroll = () => {
|
||||||
|
clearTimeout(browseTimer.current)
|
||||||
|
browseTimer.current = setTimeout(
|
||||||
|
() => setBrowsing(false),
|
||||||
|
MANUAL_SCROLL_HOLD_MS
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const lineStyle = useMemo(
|
const lineStyle = useMemo(
|
||||||
() => ({ fontSize, lineHeight: Math.round(fontSize * 1.18) }),
|
() => ({ fontSize, lineHeight: Math.round(fontSize * 1.18) }),
|
||||||
[fontSize]
|
[fontSize]
|
||||||
@@ -127,12 +151,12 @@ function LyricsLines({
|
|||||||
showsVerticalScrollIndicator={false}
|
showsVerticalScrollIndicator={false}
|
||||||
onLayout={(event) => setHeight(event.nativeEvent.layout.height)}
|
onLayout={(event) => setHeight(event.nativeEvent.layout.height)}
|
||||||
onScrollBeginDrag={() => {
|
onScrollBeginDrag={() => {
|
||||||
manualScrollAt.current = Date.now()
|
clearTimeout(browseTimer.current)
|
||||||
}}
|
setBrowsing(true)
|
||||||
onMomentumScrollEnd={() => {
|
|
||||||
// Hold from when the scroll settles, not when the finger lifted.
|
|
||||||
if (manualScrollAt.current > 0) manualScrollAt.current = Date.now()
|
|
||||||
}}
|
}}
|
||||||
|
// Hold from when the scroll settles, not when the finger lifted.
|
||||||
|
onScrollEndDrag={releaseScroll}
|
||||||
|
onMomentumScrollEnd={releaseScroll}
|
||||||
>
|
>
|
||||||
{lyrics.lines.map((line, index) =>
|
{lyrics.lines.map((line, index) =>
|
||||||
synced ? (
|
synced ? (
|
||||||
@@ -140,10 +164,14 @@ function LyricsLines({
|
|||||||
key={index}
|
key={index}
|
||||||
index={index}
|
index={index}
|
||||||
line={line}
|
line={line}
|
||||||
state={
|
distance={
|
||||||
index === active ? "active" : index < active ? "past" : "next"
|
browsing
|
||||||
|
? 0
|
||||||
|
: Math.min(Math.abs(index - active), LINE_BLUR.length - 1)
|
||||||
}
|
}
|
||||||
|
active={index === active}
|
||||||
time={index === active ? time : null}
|
time={index === active ? time : null}
|
||||||
|
isPlaying={isPlaying}
|
||||||
colors={colors}
|
colors={colors}
|
||||||
textStyle={lineStyle}
|
textStyle={lineStyle}
|
||||||
onSelect={playFrom}
|
onSelect={playFrom}
|
||||||
@@ -171,14 +199,17 @@ function LyricsLines({
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* One timed line. Only the playing line gets the time, so the others skip
|
* One timed line, blurred by its `distance` from the playing line. Only the
|
||||||
* re-rendering as the song plays.
|
* playing line gets the time, so the others skip re-rendering as the song
|
||||||
|
* plays.
|
||||||
*/
|
*/
|
||||||
const SyncedLine = memo(function SyncedLine({
|
const SyncedLine = memo(function SyncedLine({
|
||||||
index,
|
index,
|
||||||
line,
|
line,
|
||||||
state,
|
distance,
|
||||||
|
active,
|
||||||
time,
|
time,
|
||||||
|
isPlaying,
|
||||||
colors,
|
colors,
|
||||||
textStyle,
|
textStyle,
|
||||||
onSelect,
|
onSelect,
|
||||||
@@ -186,58 +217,159 @@ const SyncedLine = memo(function SyncedLine({
|
|||||||
}: {
|
}: {
|
||||||
index: number
|
index: number
|
||||||
line: LyricLine
|
line: LyricLine
|
||||||
state: "past" | "active" | "next"
|
distance: number
|
||||||
|
active: boolean
|
||||||
time: number | null
|
time: number | null
|
||||||
|
isPlaying: boolean
|
||||||
colors: NowPlayingColors
|
colors: NowPlayingColors
|
||||||
textStyle: { fontSize: number; lineHeight: number }
|
textStyle: { fontSize: number; lineHeight: number }
|
||||||
onSelect: (seconds: number) => void
|
onSelect: (seconds: number) => void
|
||||||
onLayoutY: (index: number, y: number) => void
|
onLayoutY: (index: number, y: number) => void
|
||||||
}) {
|
}) {
|
||||||
const active = state === "active"
|
const blur = LINE_BLUR[distance]
|
||||||
return (
|
return (
|
||||||
<Pressable
|
<Pressable
|
||||||
onPress={() => onSelect(line.start ?? 0)}
|
onPress={() => onSelect(line.start ?? 0)}
|
||||||
onLayout={(event) => onLayoutY(index, event.nativeEvent.layout.y)}
|
onLayout={(event) => onLayoutY(index, event.nativeEvent.layout.y)}
|
||||||
accessibilityRole="button"
|
accessibilityRole="button"
|
||||||
accessibilityHint="Plays from this line"
|
accessibilityHint="Plays from this line"
|
||||||
style={styles.line}
|
style={[styles.line, blur > 0 && { filter: [{ blur }] }]}
|
||||||
>
|
>
|
||||||
<Text
|
{active && line.words && time !== null ? (
|
||||||
style={[
|
<FilledWords
|
||||||
styles.lineText,
|
words={line.words}
|
||||||
textStyle,
|
time={time}
|
||||||
{
|
isPlaying={isPlaying}
|
||||||
color:
|
colors={colors}
|
||||||
active && !line.words ? colors.foreground : colors.lyricsInactive,
|
textStyle={textStyle}
|
||||||
},
|
/>
|
||||||
]}
|
) : (
|
||||||
>
|
<Text
|
||||||
{active && line.words
|
style={[
|
||||||
? line.words.map((word, wordIndex) => (
|
styles.lineText,
|
||||||
<Text
|
textStyle,
|
||||||
key={wordIndex}
|
{
|
||||||
style={{
|
color:
|
||||||
color:
|
active && !line.words
|
||||||
time !== null && time >= word.start
|
? colors.foreground
|
||||||
? colors.foreground
|
: colors.lyricsInactive,
|
||||||
: colors.lyricsUnsung,
|
},
|
||||||
}}
|
]}
|
||||||
>
|
>
|
||||||
{word.text}
|
{line.text}
|
||||||
</Text>
|
</Text>
|
||||||
))
|
)}
|
||||||
: line.text}
|
|
||||||
</Text>
|
|
||||||
</Pressable>
|
</Pressable>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The playing line's words, each wiping from dim to bright as it is sung.
|
||||||
|
* Reanimated runs the wipe; playback updates only re-anchor it.
|
||||||
|
*/
|
||||||
|
function FilledWords({
|
||||||
|
words,
|
||||||
|
time,
|
||||||
|
isPlaying,
|
||||||
|
colors,
|
||||||
|
textStyle,
|
||||||
|
}: {
|
||||||
|
words: LyricWord[]
|
||||||
|
time: number
|
||||||
|
isPlaying: boolean
|
||||||
|
colors: NowPlayingColors
|
||||||
|
textStyle: { fontSize: number; lineHeight: number }
|
||||||
|
}) {
|
||||||
|
const sung = useSharedValue(time)
|
||||||
|
const end = words.at(-1)?.end ?? time
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
cancelAnimation(sung)
|
||||||
|
// Jump to the reported time first, so the wipe never runs from a stale one.
|
||||||
|
sung.value =
|
||||||
|
isPlaying && end > time
|
||||||
|
? withSequence(
|
||||||
|
withTiming(time, { duration: 0 }),
|
||||||
|
withTiming(end, {
|
||||||
|
duration: (end - time) * 1000,
|
||||||
|
easing: Easing.linear,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
: time
|
||||||
|
}, [sung, time, isPlaying, end])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View style={[styles.words, { columnGap: textStyle.fontSize * 0.25 }]}>
|
||||||
|
{words.map((word, index) => (
|
||||||
|
<FilledWord
|
||||||
|
key={index}
|
||||||
|
word={word}
|
||||||
|
sung={sung}
|
||||||
|
colors={colors}
|
||||||
|
textStyle={textStyle}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function FilledWord({
|
||||||
|
word,
|
||||||
|
sung,
|
||||||
|
colors,
|
||||||
|
textStyle,
|
||||||
|
}: {
|
||||||
|
word: LyricWord
|
||||||
|
sung: SharedValue<number>
|
||||||
|
colors: NowPlayingColors
|
||||||
|
textStyle: { fontSize: number; lineHeight: number }
|
||||||
|
}) {
|
||||||
|
const [width, setWidth] = useState(0)
|
||||||
|
const text = word.text.trim()
|
||||||
|
const wipe = useAnimatedStyle(() => {
|
||||||
|
const progress =
|
||||||
|
(sung.value - word.start) / Math.max(word.end - word.start, 0.05)
|
||||||
|
return { width: width * Math.min(Math.max(progress, 0), 1) }
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View>
|
||||||
|
<Text
|
||||||
|
style={[styles.lineText, textStyle, { color: colors.lyricsUnsung }]}
|
||||||
|
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
|
||||||
|
>
|
||||||
|
{text}
|
||||||
|
</Text>
|
||||||
|
<Animated.View style={[styles.wipe, wipe]}>
|
||||||
|
<Text
|
||||||
|
numberOfLines={1}
|
||||||
|
style={[
|
||||||
|
styles.lineText,
|
||||||
|
textStyle,
|
||||||
|
{ color: colors.foreground, width },
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
{text}
|
||||||
|
</Text>
|
||||||
|
</Animated.View>
|
||||||
|
</View>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
fill: { flex: 1 },
|
fill: { flex: 1 },
|
||||||
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
|
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
|
||||||
emptyText: { fontSize: 17, fontWeight: "600" },
|
emptyText: { fontSize: 17, fontWeight: "600" },
|
||||||
line: { paddingVertical: 9 },
|
line: { paddingVertical: 9 },
|
||||||
lineText: { fontWeight: "800" },
|
lineText: { fontWeight: "800" },
|
||||||
|
words: { flexDirection: "row", flexWrap: "wrap" },
|
||||||
|
wipe: {
|
||||||
|
position: "absolute",
|
||||||
|
top: 0,
|
||||||
|
bottom: 0,
|
||||||
|
left: 0,
|
||||||
|
overflow: "hidden",
|
||||||
|
},
|
||||||
plainLine: { fontWeight: "700", paddingVertical: 2 },
|
plainLine: { fontWeight: "700", paddingVertical: 2 },
|
||||||
stanzaGap: { height: 18 },
|
stanzaGap: { height: 18 },
|
||||||
})
|
})
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"platforms": ["apple"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
// React Native draws `filter: [{ blur }]` on iOS only with its SwiftUI
|
||||||
|
// filters flag, which the stable release leaves off. This keeps the stable
|
||||||
|
// flags and turns that one on.
|
||||||
|
|
||||||
|
#import <UIKit/UIKit.h>
|
||||||
|
#import <react/featureflags/ReactNativeFeatureFlags.h>
|
||||||
|
#import <react/featureflags/ReactNativeFeatureFlagsOverridesOSSStable.h>
|
||||||
|
|
||||||
|
using namespace facebook::react;
|
||||||
|
|
||||||
|
class SwiftUIFilterFlags : public ReactNativeFeatureFlagsOverridesOSSStable {
|
||||||
|
public:
|
||||||
|
bool enableSwiftUIBasedFilters() override {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@interface SwiftUIFilters : NSObject
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation SwiftUIFilters
|
||||||
|
|
||||||
|
+ (void)load
|
||||||
|
{
|
||||||
|
// React Native sets its flags once while launching and throws if they were
|
||||||
|
// set first, so this waits for launch to finish. No views exist yet then.
|
||||||
|
[NSNotificationCenter.defaultCenter
|
||||||
|
addObserverForName:UIApplicationDidFinishLaunchingNotification
|
||||||
|
object:nil
|
||||||
|
queue:nil
|
||||||
|
usingBlock:^(NSNotification *notification) {
|
||||||
|
ReactNativeFeatureFlags::dangerouslyForceOverride(
|
||||||
|
std::make_unique<SwiftUIFilterFlags>());
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
Pod::Spec.new do |s|
|
||||||
|
s.name = 'SwiftUIFilters'
|
||||||
|
s.version = '1.0.0'
|
||||||
|
s.summary = 'Turns on React Native view filters like blur on iOS'
|
||||||
|
s.description = s.summary
|
||||||
|
s.license = { :type => 'MIT' }
|
||||||
|
s.author = 'oTunes'
|
||||||
|
s.homepage = 'https://otunes.abunchofknowitalls.com'
|
||||||
|
s.platforms = { :ios => '16.4' }
|
||||||
|
s.source = { :git => '' }
|
||||||
|
s.static_framework = true
|
||||||
|
install_modules_dependencies(s)
|
||||||
|
s.source_files = '**/*.{h,mm}'
|
||||||
|
end
|
||||||
@@ -12,11 +12,26 @@ import { cn } from "@/lib/utils"
|
|||||||
import {
|
import {
|
||||||
activeLineIndex,
|
activeLineIndex,
|
||||||
type LyricLine,
|
type LyricLine,
|
||||||
|
type LyricWord,
|
||||||
type Lyrics,
|
type Lyrics,
|
||||||
} from "@otunes/convex/client/lyrics"
|
} from "@otunes/convex/client/lyrics"
|
||||||
|
|
||||||
/** How long a manual scroll keeps the panel from following the song. */
|
/** How long a manual scroll keeps the panel from following the song. */
|
||||||
const MANUAL_SCROLL_HOLD_MS = 3000
|
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. */
|
/** Lyrics beside the page, in the queue's place. Synced lines follow playback. */
|
||||||
export function LyricsPanel() {
|
export function LyricsPanel() {
|
||||||
@@ -47,38 +62,42 @@ function LyricsLines({ lyrics }: { lyrics: Lyrics }) {
|
|||||||
const time = useLyricsTime(lyrics, currentTime, isPlaying)
|
const time = useLyricsTime(lyrics, currentTime, isPlaying)
|
||||||
const active = activeLineIndex(lyrics, time)
|
const active = activeLineIndex(lyrics, time)
|
||||||
const scrollRef = React.useRef<HTMLDivElement>(null)
|
const scrollRef = React.useRef<HTMLDivElement>(null)
|
||||||
const manualScrollAt = React.useRef(0)
|
|
||||||
const synced = lyrics.synced !== "none"
|
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(() => {
|
React.useEffect(() => {
|
||||||
const container = scrollRef.current
|
const container = scrollRef.current
|
||||||
const line = container?.querySelector<HTMLElement>(
|
const line = container?.querySelector<HTMLElement>(
|
||||||
`[data-line="${Math.max(active, 0)}"]`
|
`[data-line="${Math.max(active, 0)}"]`
|
||||||
)
|
)
|
||||||
if (
|
if (!container || !line || !synced || browsing) return
|
||||||
!container ||
|
|
||||||
!line ||
|
|
||||||
!synced ||
|
|
||||||
Date.now() - manualScrollAt.current < MANUAL_SCROLL_HOLD_MS
|
|
||||||
) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
container.scrollTo({
|
container.scrollTo({
|
||||||
top: line.offsetTop - container.clientHeight * 0.25,
|
top: line.offsetTop - container.clientHeight * 0.25,
|
||||||
behavior: "smooth",
|
behavior: "smooth",
|
||||||
})
|
})
|
||||||
}, [active, synced])
|
}, [active, synced, browsing])
|
||||||
|
|
||||||
const playFrom = React.useCallback(
|
const playFrom = React.useCallback(
|
||||||
(seconds: number) => {
|
(seconds: number) => {
|
||||||
seekTo(seconds)
|
seekTo(seconds)
|
||||||
setIsPlaying(true)
|
setIsPlaying(true)
|
||||||
|
clearTimeout(browseTimer.current)
|
||||||
|
setBrowsing(false)
|
||||||
},
|
},
|
||||||
[seekTo, setIsPlaying]
|
[seekTo, setIsPlaying]
|
||||||
)
|
)
|
||||||
|
|
||||||
const holdScroll = () => {
|
const holdScroll = () => {
|
||||||
manualScrollAt.current = Date.now()
|
setBrowsing(true)
|
||||||
|
clearTimeout(browseTimer.current)
|
||||||
|
browseTimer.current = setTimeout(
|
||||||
|
() => setBrowsing(false),
|
||||||
|
MANUAL_SCROLL_HOLD_MS
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -86,7 +105,7 @@ function LyricsLines({ lyrics }: { lyrics: Lyrics }) {
|
|||||||
ref={scrollRef}
|
ref={scrollRef}
|
||||||
onWheel={holdScroll}
|
onWheel={holdScroll}
|
||||||
onTouchMove={holdScroll}
|
onTouchMove={holdScroll}
|
||||||
className="relative min-h-0 flex-1 overflow-y-auto px-6 pt-[25svh] pb-[60svh]"
|
className="group relative min-h-0 flex-1 overflow-y-auto px-6 pt-[25svh] pb-[60svh]"
|
||||||
>
|
>
|
||||||
{lyrics.lines.map((line, index) =>
|
{lyrics.lines.map((line, index) =>
|
||||||
synced ? (
|
synced ? (
|
||||||
@@ -94,10 +113,14 @@ function LyricsLines({ lyrics }: { lyrics: Lyrics }) {
|
|||||||
key={index}
|
key={index}
|
||||||
index={index}
|
index={index}
|
||||||
line={line}
|
line={line}
|
||||||
state={
|
distance={
|
||||||
index === active ? "active" : index < active ? "past" : "next"
|
browsing
|
||||||
|
? 0
|
||||||
|
: Math.min(Math.abs(index - active), LINE_BLUR.length - 1)
|
||||||
}
|
}
|
||||||
|
active={index === active}
|
||||||
time={index === active ? time : null}
|
time={index === active ? time : null}
|
||||||
|
isPlaying={isPlaying}
|
||||||
onSelect={playFrom}
|
onSelect={playFrom}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
@@ -117,48 +140,104 @@ function LyricsLines({ lyrics }: { lyrics: Lyrics }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* One timed line. Only the active line gets the playback time, so memoized
|
* One timed line, blurred by its `distance` from the playing line. Only the
|
||||||
* lines elsewhere skip re-rendering as the song plays.
|
* active line gets the playback time, so memoized lines elsewhere skip
|
||||||
|
* re-rendering as the song plays.
|
||||||
*/
|
*/
|
||||||
const SyncedLine = React.memo(function SyncedLine({
|
const SyncedLine = React.memo(function SyncedLine({
|
||||||
index,
|
index,
|
||||||
line,
|
line,
|
||||||
state,
|
distance,
|
||||||
|
active,
|
||||||
time,
|
time,
|
||||||
|
isPlaying,
|
||||||
onSelect,
|
onSelect,
|
||||||
}: {
|
}: {
|
||||||
index: number
|
index: number
|
||||||
line: LyricLine
|
line: LyricLine
|
||||||
state: "past" | "active" | "next"
|
distance: number
|
||||||
|
active: boolean
|
||||||
time: number | null
|
time: number | null
|
||||||
|
isPlaying: boolean
|
||||||
onSelect: (seconds: number) => void
|
onSelect: (seconds: number) => void
|
||||||
}) {
|
}) {
|
||||||
const active = state === "active"
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
data-line={index}
|
data-line={index}
|
||||||
onClick={() => onSelect(line.start ?? 0)}
|
onClick={() => onSelect(line.start ?? 0)}
|
||||||
className={cn(
|
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-colors duration-300 hover:bg-foreground/5",
|
"-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 ? "text-foreground" : "text-foreground/30"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{active && line.words
|
{active && line.words && time !== null ? (
|
||||||
? line.words.map((word, wordIndex) => (
|
<FilledWords words={line.words} time={time} isPlaying={isPlaying} />
|
||||||
<span
|
) : (
|
||||||
key={wordIndex}
|
line.text
|
||||||
className={cn(
|
)}
|
||||||
"transition-colors duration-200",
|
|
||||||
time !== null && time >= word.start
|
|
||||||
? "text-foreground"
|
|
||||||
: "text-foreground/45"
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{word.text}
|
|
||||||
</span>
|
|
||||||
))
|
|
||||||
: line.text}
|
|
||||||
</button>
|
</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>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in new issue
Block a user