fix(mobile): fade artwork text colors on the UI thread

This commit is contained in:
joshuafhiggins committed 2026-11-05 21:30:23 -05:00 Verified
1 parent 7d4dc9a4de
commit c91bf600cb
10 files changed
+337 -166

No files matched your search

+20 -6
View File
@@ -4,7 +4,7 @@ import {
AndroidTabThemeProvider,
useAndroidTabPalette,
useAndroidTabBackgroundStyle,
useAndroidTabSelectedColor,
useAndroidNativeTabColors,
} from "@/contexts/android-tab-theme"
import { DarkTheme, DefaultTheme, ThemeProvider } from "expo-router"
import { NativeTabs } from "expo-router/native-tabs"
@@ -43,10 +43,12 @@ function useRouterTheme() {
}
function useNativeTabColors() {
const selectedColor = useAndroidTabSelectedColor()
// These props initialize native tabs; the worklet owns subsequent colors.
const selectedColor = colors.dark.primary
const themeIsDark = useIsDark()
const androidPalette = useAndroidTabPalette()
const isDark = Platform.OS === "android" ? androidPalette.isDark : themeIsDark
const androidPalette = colors.dark
const artworkPalette = useAndroidTabPalette()
const isDark = Platform.OS === "android" ? artworkPalette.isDark : themeIsDark
const inactive = appleMusic.tabInactive
const iconColor =
@@ -56,14 +58,14 @@ function useNativeTabColors() {
selected: appleMusic.pink,
}
: {
default: androidPalette.secondaryForeground,
default: androidPalette.mutedForeground,
selected: selectedColor,
}
const labelColor =
Platform.OS === "ios"
? DynamicColorIOS({ dark: inactive, light: "#636366" })
: androidPalette.secondaryForeground
: androidPalette.mutedForeground
return {
isDark,
@@ -86,6 +88,9 @@ function useNativeTabColors() {
}
function TabsContent() {
const homeColors = useAndroidNativeTabColors()
const libraryColors = useAndroidNativeTabColors()
const searchColors = useAndroidNativeTabColors()
const keyboardVisible = useAndroidKeyboardVisible()
const routerTheme = useRouterTheme()
const tabColors = useNativeTabColors()
@@ -135,6 +140,9 @@ function TabsContent() {
<NativeTabs.Trigger
name="home"
unstable_nativeProps={
Platform.OS === "android" ? homeColors : undefined
}
disableTransparentOnScrollEdge
contentStyle={androidContentStyle}
>
@@ -144,6 +152,9 @@ function TabsContent() {
<NativeTabs.Trigger
name="library"
unstable_nativeProps={
Platform.OS === "android" ? libraryColors : undefined
}
disableTransparentOnScrollEdge
contentStyle={androidContentStyle}
>
@@ -156,6 +167,9 @@ function TabsContent() {
<NativeTabs.Trigger
name="search"
unstable_nativeProps={
Platform.OS === "android" ? searchColors : undefined
}
disableTransparentOnScrollEdge
contentStyle={androidContentStyle}
>
@@ -1,3 +1,8 @@
import {
PaletteText,
PaletteIcon,
renderArtworkBackButton,
} from "@/components/palette-text"
import Animated from "react-native-reanimated"
import {
renderAndroidArtworkBackground,
@@ -7,8 +12,7 @@ import { MusicEntityView } from "@/components/music-entity-view"
import { Stack, useIsFocused, useLocalSearchParams } from "expo-router"
import { StatusBar } from "expo-status-bar"
import { memo, useCallback, useEffect, useMemo } from "react"
import { Platform, Pressable, StyleSheet, Text, View } from "react-native"
import { Ionicons } from "@expo/vector-icons"
import { Platform, Pressable, StyleSheet, View } from "react-native"
import {
CollectionDivider,
@@ -137,6 +141,8 @@ export default function AlbumScreen() {
? {
headerStyle: { backgroundColor: "transparent" },
headerBackground: renderAndroidArtworkBackground,
headerBackVisible: false,
headerLeft: renderArtworkBackButton,
headerTintColor: palette.foreground,
}
: {}),
@@ -163,9 +169,9 @@ export default function AlbumScreen() {
}}
/>
<View style={[styles.empty, { backgroundColor: colors.background }]}>
<Text style={{ color: colors.mutedForeground }}>
<PaletteText style={{ color: colors.mutedForeground }}>
Album not found.
</Text>
</PaletteText>
</View>
</>
)
@@ -188,6 +194,8 @@ export default function AlbumScreen() {
? {
headerStyle: { backgroundColor: "transparent" },
headerBackground: renderAndroidArtworkBackground,
headerBackVisible: false,
headerLeft: renderArtworkBackButton,
statusBarStyle: palette.isDark
? ("light" as const)
: ("dark" as const),
@@ -209,7 +217,8 @@ export default function AlbumScreen() {
}
>
<Pressable hitSlop={8}>
<Ionicons
<PaletteIcon
tone="foreground"
name="ellipsis-horizontal"
size={22}
color={palette.isDark ? "#fff" : "#000"}
@@ -239,16 +248,22 @@ export default function AlbumScreen() {
hasCover={album.cover != null}
style={styles.cover}
/>
<Text style={[styles.title, { color: palette.foreground }]}>
<PaletteText
tone="foreground"
style={[styles.title, { color: palette.foreground }]}
>
{album.title}
</Text>
</PaletteText>
<Pressable
onPress={() => push(`/artist/${album.artist.id}`)}
style={styles.artistPressable}
>
<Text style={[styles.artist, { color: palette.accentText }]}>
<PaletteText
tone="accentText"
style={[styles.artist, { color: palette.accentText }]}
>
{album.artist.name}
</Text>
</PaletteText>
</Pressable>
<View
style={{
@@ -260,18 +275,25 @@ export default function AlbumScreen() {
}}
>
{metadata ? (
<Text style={{ color: palette.secondaryForeground }}>
<PaletteText
tone="secondaryForeground"
style={{ color: palette.secondaryForeground }}
>
{metadata}
</Text>
</PaletteText>
) : null}
{hasLossless ? (
<>
{metadata ? (
<Text style={{ color: palette.secondaryForeground }}>
<PaletteText
tone="secondaryForeground"
style={{ color: palette.secondaryForeground }}
>
·
</Text>
</PaletteText>
) : null}
<AudioFormatBadge
artwork
format="lossless"
color={palette.secondaryForeground}
backgroundColor={palette.background}
@@ -281,11 +303,15 @@ export default function AlbumScreen() {
{hasAtmos ? (
<>
{metadata || hasLossless ? (
<Text style={{ color: palette.secondaryForeground }}>
<PaletteText
tone="secondaryForeground"
style={{ color: palette.secondaryForeground }}
>
·
</Text>
</PaletteText>
) : null}
<AudioFormatBadge
artwork
format="atmos"
color={palette.secondaryForeground}
backgroundColor={palette.background}
@@ -302,7 +328,8 @@ export default function AlbumScreen() {
{ backgroundColor: palette.surface },
]}
>
<Ionicons
<PaletteIcon
tone={favorited ? "accent" : "foreground"}
name={favorited ? "star" : "star-outline"}
size={24}
color={favorited ? palette.accent : palette.foreground}
@@ -328,7 +355,8 @@ export default function AlbumScreen() {
{ backgroundColor: palette.surface },
]}
>
<Ionicons
<PaletteIcon
tone="foreground"
name="shuffle"
size={24}
color={palette.foreground}
@@ -430,11 +458,12 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
style={styles.trackRow}
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
>
<Text
<PaletteText
tone="secondaryForeground"
style={[styles.trackNumber, { color: palette.secondaryForeground }]}
>
{number}
</Text>
</PaletteText>
<View
style={[
styles.trackContent,
@@ -442,24 +471,29 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
]}
>
<View style={styles.trackMeta}>
<Text
<PaletteText
tone="foreground"
style={[styles.trackTitle, { color: palette.foreground }]}
numberOfLines={1}
>
{track.title}
</Text>
</PaletteText>
{track.artists ? (
<Text
<PaletteText
tone="secondaryForeground"
style={{ color: palette.secondaryForeground }}
numberOfLines={1}
>
{track.artists.join(", ")}
</Text>
</PaletteText>
) : null}
</View>
<Text style={{ color: palette.secondaryForeground }}>
<PaletteText
tone="secondaryForeground"
style={{ color: palette.secondaryForeground }}
>
{formatDuration(track.durationSeconds)}
</Text>
</PaletteText>
</View>
</Pressable>
</MusicEntityView>
@@ -1,3 +1,8 @@
import {
PaletteText,
PaletteIcon,
renderArtworkBackButton,
} from "@/components/palette-text"
import Animated from "react-native-reanimated"
import {
PlaylistSearchBar,
@@ -21,12 +26,10 @@ import {
Platform,
Pressable,
StyleSheet,
Text,
View,
type ListRenderItemInfo,
} from "react-native"
import ReanimatedSwipeable from "react-native-gesture-handler/ReanimatedSwipeable"
import { Ionicons } from "@expo/vector-icons"
import { CollectionDivider } from "@/components/collection-list"
import { CoverImage } from "@/components/cover-image"
@@ -106,6 +109,8 @@ export default function PlaylistScreen() {
? {
headerStyle: { backgroundColor: "transparent" },
headerBackground: renderAndroidArtworkBackground,
headerBackVisible: false,
headerLeft: renderArtworkBackButton,
headerTintColor: palette.foreground,
}
: {}),
@@ -132,9 +137,9 @@ export default function PlaylistScreen() {
}}
/>
<View style={[styles.empty, { backgroundColor: colors.background }]}>
<Text style={{ color: colors.mutedForeground }}>
<PaletteText style={{ color: colors.mutedForeground }}>
Playlist not found.
</Text>
</PaletteText>
</View>
</>
)
@@ -269,6 +274,8 @@ function PlaylistContent({
? {
headerStyle: { backgroundColor: "transparent" },
headerBackground: renderAndroidArtworkBackground,
headerBackVisible: false,
headerLeft: renderArtworkBackButton,
statusBarStyle: palette.isDark
? ("light" as const)
: ("dark" as const),
@@ -287,7 +294,8 @@ function PlaylistContent({
onAction={handlePlaylistMenuAction}
>
<Pressable hitSlop={8}>
<Ionicons
<PaletteIcon
tone="foreground"
name="ellipsis-horizontal"
size={22}
color={palette.isDark ? "#fff" : "#000"}
@@ -358,14 +366,15 @@ function PlaylistContent({
contentInsetAdjustmentBehavior="automatic"
onScrollBeginDrag={swipeGroup.closeOpenRow}
ListEmptyComponent={
<Text
<PaletteText
tone="secondaryForeground"
style={[
styles.emptyResults,
{ color: palette.secondaryForeground },
]}
>
{songs.length ? "No songs found." : "No songs in this playlist."}
</Text>
</PaletteText>
}
ListHeaderComponent={
<PlaylistHero
@@ -459,15 +468,19 @@ const PlaylistSongRow = memo(function PlaylistSongRow({
}}
style={[styles.removeAction, { backgroundColor: colors.destructive }]}
>
<Ionicons name="trash" size={22} color={colors.primaryForeground} />
<Text
<PaletteIcon
name="trash"
size={22}
color={colors.primaryForeground}
/>
<PaletteText
style={[
styles.removeActionText,
{ color: colors.primaryForeground },
]}
>
Remove
</Text>
</PaletteText>
</Pressable>
)}
>
@@ -505,10 +518,18 @@ const PlaylistHero = memo(function PlaylistHero({
<MusicEntityView kind="playlist" id={entityId}>
<View style={styles.hero}>
<CoverImage uri={coverUri} hasCover={hasCover} style={styles.cover} />
<Text style={[styles.title, { color: palette.foreground }]}>
<PaletteText
tone="foreground"
style={[styles.title, { color: palette.foreground }]}
>
{title}
</Text>
<Text style={{ color: palette.secondaryForeground }}>{ownerName}</Text>
</PaletteText>
<PaletteText
tone="secondaryForeground"
style={{ color: palette.secondaryForeground }}
>
{ownerName}
</PaletteText>
<View style={styles.actions}>
{showSubscribe ? (
<Pressable
@@ -526,7 +547,8 @@ const PlaylistHero = memo(function PlaylistHero({
},
]}
>
<Ionicons
<PaletteIcon
tone={isSubscribed ? "foreground" : "accentForeground"}
name={isSubscribed ? "checkmark" : "add"}
size={24}
color={
@@ -552,7 +574,12 @@ const PlaylistHero = memo(function PlaylistHero({
onPress={onShuffle}
style={[styles.iconAction, { backgroundColor: palette.surface }]}
>
<Ionicons name="shuffle" size={24} color={palette.foreground} />
<PaletteIcon
tone="foreground"
name="shuffle"
size={24}
color={palette.foreground}
/>
</Pressable>
</View>
</View>
+10 -2
View File
@@ -1,4 +1,5 @@
import { StyleSheet, Text, View } from "react-native"
import { PaletteText } from "@/components/palette-text"
import { StyleSheet, View } from "react-native"
import Svg, { G, Path } from "react-native-svg"
import type { PlaybackFormat } from "@otunes/convex/playback-stream"
@@ -74,10 +75,12 @@ function LosslessMark({
/** The Dolby Atmos or Lossless label shown under the progress bar. */
export function AudioFormatBadge({
artwork = false,
format,
color,
backgroundColor,
}: {
artwork?: boolean
format: PlaybackFormat
color: string
backgroundColor: string
@@ -90,7 +93,12 @@ export function AudioFormatBadge({
) : (
<LosslessMark color={color} backgroundColor={backgroundColor} />
)}
<Text style={[styles.label, { color }]}>{label}</Text>
<PaletteText
tone={artwork ? "secondaryForeground" : undefined}
style={[styles.label, { color }]}
>
{label}
</PaletteText>
</View>
)
}
+7 -3
View File
@@ -1,5 +1,6 @@
import { PaletteText } from "@/components/palette-text"
import { memo } from "react"
import { StyleSheet, Text, View } from "react-native"
import { StyleSheet, View } from "react-native"
import { useThemeColors } from "@/lib/theme"
@@ -25,9 +26,12 @@ export const DiscHeader = memo(function DiscHeader({
}) {
const colors = useThemeColors()
return (
<Text style={[styles.discHeader, { color: color ?? colors.foreground }]}>
<PaletteText
tone={color ? "foreground" : undefined}
style={[styles.discHeader, { color: color ?? colors.foreground }]}
>
{label}
</Text>
</PaletteText>
)
})
+27 -12
View File
@@ -1,13 +1,13 @@
import { PaletteText, PaletteIcon } from "@/components/palette-text"
import Animated from "react-native-reanimated"
import {
useAndroidTabPalette,
useAndroidTabBackgroundStyle,
useAndroidTabHasArtworkPalette,
} from "@/contexts/android-tab-theme"
import { Ionicons } from "@expo/vector-icons"
import { NativeTabs } from "expo-router/native-tabs"
import { useRouter } from "expo-router"
import { Platform, Pressable, StyleSheet, Text, View } from "react-native"
import { Platform, Pressable, StyleSheet, View } from "react-native"
import { useSafeAreaInsets } from "react-native-safe-area-context"
@@ -67,7 +67,8 @@ function MiniPlayerContent({
<View
style={isAccessory ? styles.accessoryInlineMeta : styles.inlineMeta}
>
<Text
<PaletteText
tone="foreground"
style={[
isAccessory ? styles.accessoryTitle : styles.title,
{ color: colors.foreground },
@@ -75,7 +76,7 @@ function MiniPlayerContent({
numberOfLines={1}
>
{currentSong.title}
</Text>
</PaletteText>
</View>
<Pressable
style={styles.controlButton}
@@ -85,7 +86,8 @@ function MiniPlayerContent({
}}
hitSlop={6}
>
<Ionicons
<PaletteIcon
tone="foreground"
name={isPlaying ? "pause" : "play"}
size={20}
color={colors.foreground}
@@ -103,7 +105,8 @@ function MiniPlayerContent({
style={artworkStyle}
/>
<View style={isAccessory ? styles.accessoryMeta : styles.meta}>
<Text
<PaletteText
tone="foreground"
style={[
isAccessory ? styles.accessoryTitle : styles.title,
{ color: colors.foreground },
@@ -111,8 +114,9 @@ function MiniPlayerContent({
numberOfLines={1}
>
{currentSong.title}
</Text>
<Text
</PaletteText>
<PaletteText
tone="secondaryForeground"
style={[
isAccessory ? styles.accessoryArtist : styles.artist,
{ color: colors.mutedForeground },
@@ -120,7 +124,7 @@ function MiniPlayerContent({
numberOfLines={1}
>
{currentSong.artist.name}
</Text>
</PaletteText>
</View>
<View style={styles.controls}>
{Platform.OS === "android" && !isAccessory ? (
@@ -134,7 +138,12 @@ function MiniPlayerContent({
}}
hitSlop={6}
>
<Ionicons name="play-back" size={24} color={colors.foreground} />
<PaletteIcon
tone="foreground"
name="play-back"
size={24}
color={colors.foreground}
/>
</Pressable>
) : null}
<Pressable
@@ -145,7 +154,8 @@ function MiniPlayerContent({
}}
hitSlop={6}
>
<Ionicons
<PaletteIcon
tone="foreground"
name={isPlaying ? "pause" : "play"}
size={24}
color={colors.foreground}
@@ -159,7 +169,12 @@ function MiniPlayerContent({
}}
hitSlop={6}
>
<Ionicons name="play-forward" size={24} color={colors.foreground} />
<PaletteIcon
tone="foreground"
name="play-forward"
size={24}
color={colors.foreground}
/>
</Pressable>
</View>
</Pressable>
+49
View File
@@ -0,0 +1,49 @@
import { Ionicons } from "@expo/vector-icons"
import type { ComponentProps } from "react"
import { Pressable, type TextProps } from "react-native"
import { useRouter } from "expo-router"
import Animated from "react-native-reanimated"
import {
useAnimatedPaletteColor,
type PaletteColor,
} from "@/contexts/android-tab-theme"
const AnimatedIcon = Animated.createAnimatedComponent(Ionicons)
/** Keep artwork text colors on the same UI-thread animation as the background. */
export function PaletteText({
tone,
style,
...props
}: TextProps & { tone?: PaletteColor }) {
const colorStyle = useAnimatedPaletteColor(tone)
return <Animated.Text {...props} style={[style, colorStyle]} />
}
export function PaletteIcon({
tone,
style,
...props
}: ComponentProps<typeof Ionicons> & { tone?: PaletteColor }) {
const colorStyle = useAnimatedPaletteColor(tone)
return <AnimatedIcon {...props} style={[style, colorStyle]} />
}
function ArtworkBackButton() {
const router = useRouter()
return (
<Pressable
accessibilityRole="button"
accessibilityLabel="Navigate up"
hitSlop={8}
onPress={() => router.back()}
>
<PaletteIcon tone="foreground" name="arrow-back" size={24} />
</Pressable>
)
}
export function renderArtworkBackButton() {
return <ArtworkBackButton />
}
+13 -7
View File
@@ -1,3 +1,4 @@
import { PaletteText } from "@/components/palette-text"
import { MusicEntityView } from "@/components/music-entity-view"
import { collectionInset } from "@/components/collection-list"
import { CoverImage } from "@/components/cover-image"
@@ -8,7 +9,7 @@ import { formatDuration } from "@/lib/format"
import { albumCoverUrl } from "@/lib/urls"
import { miniPlayerHeight, spacing, useThemeColors } from "@/lib/theme"
import { memo, useCallback } from "react"
import { Platform, Pressable, StyleSheet, Text, View } from "react-native"
import { Platform, Pressable, StyleSheet, View } from "react-native"
import {
MediaContextMenu,
@@ -46,22 +47,27 @@ export const SongRow = memo(function SongRow({
const content = (
<>
<View style={styles.rowMeta}>
<Text
<PaletteText
tone={palette ? "foreground" : undefined}
style={[styles.rowTitle, { color: foreground }]}
numberOfLines={1}
>
{song.title}
</Text>
<Text
</PaletteText>
<PaletteText
tone={palette ? "secondaryForeground" : undefined}
style={[styles.rowSubtitle, { color: secondaryForeground }]}
numberOfLines={1}
>
{song.artist.name} · {song.album.title}
</Text>
</PaletteText>
</View>
<Text style={[styles.duration, { color: secondaryForeground }]}>
<PaletteText
tone={palette ? "secondaryForeground" : undefined}
style={[styles.duration, { color: secondaryForeground }]}
>
{formatDuration(song.durationSeconds)}
</Text>
</PaletteText>
{trailing}
</>
)
+15 -2
View File
@@ -1,3 +1,4 @@
import { PaletteText, PaletteIcon } from "@/components/palette-text"
import {
ActivityIndicator,
Platform,
@@ -73,8 +74,20 @@ export function Button({
<ActivityIndicator color={textColor} />
) : (
<View style={styles.buttonContent}>
{icon ? <Ionicons name={icon} size={18} color={textColor} /> : null}
<Text style={[styles.buttonText, { color: textColor }]}>{title}</Text>
{icon ? (
<PaletteIcon
tone={tint ? "accentForeground" : undefined}
name={icon}
size={18}
color={textColor}
/>
) : null}
<PaletteText
tone={tint ? "accentForeground" : undefined}
style={[styles.buttonText, { color: textColor }]}
>
{title}
</PaletteText>
</View>
)}
</Pressable>
+95 -94
View File
@@ -11,15 +11,16 @@ import {
useLayoutEffect,
useMemo,
useState,
useRef,
type PropsWithChildren,
} from "react"
import { Animated, Easing, Platform, StyleSheet } from "react-native"
import { Platform, StyleSheet, type HostInstance } from "react-native"
import Reanimated, {
interpolateColor,
useReducedMotion,
useSharedValue,
useAnimatedStyle,
useAnimatedReaction,
useAnimatedRef,
setNativeProps,
processColor,
withTiming,
type SharedValue,
} from "react-native-reanimated"
@@ -27,10 +28,14 @@ import Reanimated, {
import { themeCoverPalette, type CoverPalette } from "@/lib/cover-palette"
import { useIsDark, useThemeColors } from "@/lib/theme"
export type PaletteColor = Exclude<keyof CoverPalette, "isDark">
type PaletteColors = Pick<CoverPalette, PaletteColor> & {
selectedColor: string
}
type TabTheme = {
palette: CoverPalette
background: SharedValue<string>
selectedColor: string
colors: SharedValue<PaletteColors>
hasArtworkPalette: boolean
setPalette: (palette: CoverPalette | null, owner: string) => void
}
@@ -48,14 +53,30 @@ export function AndroidTabThemeProvider({ children }: PropsWithChildren) {
palette: CoverPalette
owner: string
} | null>(null)
const background = useFadingBackground(
screen?.palette.background ?? defaultPalette.background,
defaultPalette.background
)
const palette = useFadingPalette(
screen?.palette ?? defaultPalette,
defaultPalette
)
const palette = screen?.palette ?? defaultPalette
const hasArtwork =
palette.background !== defaultPalette.background ||
palette.foreground !== defaultPalette.foreground ||
palette.secondaryForeground !== defaultPalette.secondaryForeground ||
palette.accent !== defaultPalette.accent
const { isDark: _defaultIsDark, ...defaultColors } = defaultPalette
const animatedColors = useSharedValue<PaletteColors>({
...defaultColors,
selectedColor: defaultPalette.accentText,
})
useLayoutEffect(() => {
if (Platform.OS !== "android") return
const { isDark: _isDark, ...colors } = palette
animatedColors.value = withTiming(
{
...colors,
selectedColor: hasArtwork
? palette.foreground
: defaultPalette.accentText,
},
{ duration: PALETTE_FADE_MS }
)
}, [animatedColors, palette, defaultPalette, hasArtwork])
const setPalette = useCallback(
(palette: CoverPalette | null, owner: string) => {
setScreen((current) =>
@@ -64,23 +85,15 @@ export function AndroidTabThemeProvider({ children }: PropsWithChildren) {
},
[]
)
const artworkPalette =
palette &&
(palette.background !== defaultPalette.background ||
palette.foreground !== defaultPalette.foreground ||
palette.secondaryForeground !== defaultPalette.secondaryForeground ||
palette.accent !== defaultPalette.accent)
? palette
: null
const artworkPalette = hasArtwork ? palette : null
const value = useMemo(
() => ({
palette: artworkPalette ?? defaultPalette,
background,
selectedColor: artworkPalette?.foreground ?? defaultPalette.accentText,
colors: animatedColors,
hasArtworkPalette: artworkPalette !== null,
setPalette,
}),
[defaultPalette, artworkPalette, setPalette, background]
[defaultPalette, artworkPalette, setPalette, animatedColors]
)
return (
<AndroidTabTheme.Provider value={value}>
@@ -89,6 +102,50 @@ export function AndroidTabThemeProvider({ children }: PropsWithChildren) {
)
}
/**
* NativeTabs forwards raw screen props to its Fabric screen. Bind that host
* directly so appearance updates stay on the UI thread with the page colors.
*/
export function useAndroidNativeTabColors() {
const colors = useContext(AndroidTabTheme)?.colors
const ref = useAnimatedRef<HostInstance>()
const mounted = useSharedValue(false)
const attach = useCallback(
(host: HostInstance | null) => {
if (host) ref(host)
mounted.value = host !== null
},
[ref, mounted]
)
useAnimatedReaction(
() => ({ colors: colors?.value, mounted: mounted.value }),
({ colors: palette, mounted: ready }) => {
if (Platform.OS !== "android" || !ready || !palette) return
const normal = processColor(palette.secondaryForeground)
const selected = processColor(palette.selectedColor)
setNativeProps(ref, {
standardAppearance: {
tabBarBackgroundColor: processColor("transparent"),
tabBarItemRippleColor: selected,
tabBarItemActiveIndicatorEnabled: false,
tabBarItemTitleSmallLabelFontSize: 10,
tabBarItemTitleLargeLabelFontSize: 10,
normal: {
tabBarItemTitleFontColor: normal,
tabBarItemIconColor: normal,
},
selected: {
tabBarItemTitleFontColor: selected,
tabBarItemIconColor: selected,
},
},
})
}
)
return { ref: attach, style: { backgroundColor: "transparent" } }
}
export function useAndroidTabPalette() {
const theme = useContext(AndroidTabTheme)
const defaultPalette = useDefaultPalette()
@@ -99,18 +156,23 @@ export function useAndroidTabHasArtworkPalette() {
return useContext(AndroidTabTheme)?.hasArtworkPalette ?? false
}
export function useAndroidTabSelectedColor() {
const theme = useContext(AndroidTabTheme)
const defaultPalette = useDefaultPalette()
return theme?.selectedColor ?? defaultPalette.accentText
}
export function useAndroidTabBackgroundStyle() {
const theme = useContext(AndroidTabTheme)
const fallback = useDefaultPalette().background
const colors = theme?.colors
return useAnimatedStyle(() =>
Platform.OS === "android"
? { backgroundColor: theme?.background.value ?? fallback }
? { backgroundColor: colors?.value.background ?? fallback }
: {}
)
}
/** Color tokens share the background's UI-thread timing. */
export function useAnimatedPaletteColor(tone: PaletteColor | undefined) {
const colors = useContext(AndroidTabTheme)?.colors
return useAnimatedStyle(() =>
Platform.OS === "android" && tone && colors
? { color: colors.value[tone] }
: {}
)
}
@@ -125,68 +187,8 @@ export function renderAndroidArtworkBackground() {
return <AndroidArtworkBackground />
}
/** Run backgrounds on the UI thread even while React is rendering track rows. */
function useFadingBackground(target: string, initial: string) {
const background = useSharedValue(initial)
useLayoutEffect(() => {
if (Platform.OS === "android") {
background.value = withTiming(target, { duration: PALETTE_FADE_MS })
}
}, [background, target])
return background
}
// Match the default Android medium animation time used by slide_from_right.
// Continue from displayed colors when artwork arrives late or focus changes.
const PALETTE_FADE_MS = 400
function useFadingPalette(target: CoverPalette, initial: CoverPalette) {
const [displayed, setDisplayed] = useState(initial)
const current = useRef(initial)
const reducedMotion = useReducedMotion()
useLayoutEffect(() => {
if (Platform.OS !== "android") return
if (reducedMotion || current.current === target) {
current.current = target
setDisplayed(target)
return
}
const from = current.current
const progress = new Animated.Value(0)
const listener = progress.addListener(({ value }) => {
const color = (key: Exclude<keyof CoverPalette, "isDark">) =>
interpolateColor(value, [0, 1], [from[key], target[key]], "RGB")
const next: CoverPalette = {
background: color("background"),
foreground: color("foreground"),
secondaryForeground: color("secondaryForeground"),
accent: color("accent"),
accentForeground: color("accentForeground"),
accentText: color("accentText"),
surface: color("surface"),
divider: color("divider"),
isDark: value < 0.5 ? from.isDark : target.isDark,
}
current.current = value === 1 ? target : next
setDisplayed(current.current)
})
const animation = Animated.timing(progress, {
toValue: 1,
duration: PALETTE_FADE_MS,
easing: Easing.out(Easing.cubic),
useNativeDriver: false,
})
animation.start()
return () => {
animation.stop()
progress.removeListener(listener)
}
}, [target, reducedMotion])
return Platform.OS === "android" ? displayed : target
}
/** The focused artwork screen selects the shared background for page and bars. */
export function useAndroidScreenPalette(palette: CoverPalette) {
const isFocused = useIsFocused()
@@ -207,7 +209,6 @@ export function useAndroidScreenPalette(palette: CoverPalette) {
const theme = useContext(AndroidTabTheme)
const setPalette = theme?.setPalette
const target = isFocused && laidOut && !closing ? palette : defaultPalette
const displayed = useFadingPalette(target, defaultPalette)
const backgroundStyle = useAndroidTabBackgroundStyle()
useLayoutEffect(() => {
@@ -224,7 +225,7 @@ export function useAndroidScreenPalette(palette: CoverPalette) {
}, [closing, laidOut, isFocused, owner, palette, setPalette])
return {
palette: displayed,
palette: Platform.OS === "android" ? target : palette,
backgroundStyle,
onLayout: Platform.OS === "android" ? onLayout : undefined,
}