Files
orca/mobile/src/theme/mobile-appearance.ts
Kelvin Amoaba f87a746200 feat(mobile): light and dark themes with desktop's palettes (#34217)
* feat(mobile): light and dark themes with desktop's palettes

Settings → Appearance offers System, Light and Dark. The shell passes the
resolved scheme to over-the-air pages in its init frame.

Fixes #22184

* fix(mobile): draw your chat messages in desktop's user bubble

Desktop uses a faint surface with a hairline border in both themes; the inverted fill went black in light.

* test(mobile): re-measure the page script sweep after the shared theme hooks

Every route now imports the theme hooks while the entry does not, so they emit one shared script (96 for the tree). Re-swept every route prefix; the worst route step is now 15.

* test(mobile): read themed stylesheets in the text-input font-size census

The census only unwrapped StyleSheet.create({...}); inputs styled through themedStyleSheet(() => ({...})) came back unread.
2026-10-27 17:35:31 +00:00

77 lines
2.6 KiB
TypeScript

import AsyncStorage from '@react-native-async-storage/async-storage'
import { useSyncExternalStore } from 'react'
import { AppState, Appearance } from 'react-native'
import { applyMobileColorScheme } from './mobile-theme'
// Same choices as desktop's theme setting; System follows the phone.
export type MobileAppearancePreference = 'system' | 'light' | 'dark'
const APPEARANCE_KEY = 'orca:appearance'
let preference: MobileAppearancePreference = 'system'
// A choice made while the saved one is still loading wins over the stale read.
let chosenThisLaunch = false
let started: Promise<void> | null = null
const preferenceListeners = new Set<() => void>()
function isAppearancePreference(value: unknown): value is MobileAppearancePreference {
return value === 'system' || value === 'light' || value === 'dark'
}
// Appearance carries the override too, so native alerts, pickers, the keyboard and WebViews match the app.
function applyAppearance(): void {
// iOS flips the trait collection while snapshotting a backgrounded app; settle on return instead.
if (AppState.currentState === 'background') {
return
}
applyMobileColorScheme(Appearance.getColorScheme() === 'light' ? 'light' : 'dark')
}
function adoptPreference(next: MobileAppearancePreference): void {
preference = next
Appearance.setColorScheme(next === 'system' ? 'unspecified' : next)
applyAppearance()
for (const listener of preferenceListeners) {
listener()
}
}
// Resolves once the saved choice is applied, so the splash can cover the first themed frame.
export function startMobileAppearance(): Promise<void> {
if (started !== null) {
return started
}
applyAppearance()
Appearance.addChangeListener(applyAppearance)
AppState.addEventListener('change', (state) => {
if (state === 'active') {
applyAppearance()
}
})
started = AsyncStorage.getItem(APPEARANCE_KEY)
.then((stored) => {
if (!chosenThisLaunch && isAppearancePreference(stored) && stored !== preference) {
adoptPreference(stored)
}
})
.catch(() => undefined)
return started
}
export function setMobileAppearancePreference(next: MobileAppearancePreference): Promise<void> {
chosenThisLaunch = true
adoptPreference(next)
return AsyncStorage.setItem(APPEARANCE_KEY, next)
}
function subscribePreference(listener: () => void): () => void {
preferenceListeners.add(listener)
return () => {
preferenceListeners.delete(listener)
}
}
export function useMobileAppearancePreference(): MobileAppearancePreference {
return useSyncExternalStore(subscribePreference, () => preference)
}