Feat: Use system default color mode, but allow user override

zio/stable
Jaz 2023-05-16 21:36:43 -07:00
parent 404b2f043c
commit da0ed7e002
6 changed files with 59 additions and 35 deletions

View File

@ -51,7 +51,7 @@ const App = observer(() => {
return null return null
} }
return ( return (
<ThemeProvider theme={rootStore.shell.darkMode ? 'dark' : 'light'}> <ThemeProvider theme={rootStore.shell.colorMode}>
<RootSiblingParent> <RootSiblingParent>
<analytics.Provider> <analytics.Provider>
<RootStoreProvider value={rootStore}> <RootStoreProvider value={rootStore}>

View File

@ -30,7 +30,7 @@ const App = observer(() => {
} }
return ( return (
<ThemeProvider theme={rootStore.shell.darkMode ? 'dark' : 'light'}> <ThemeProvider theme={rootStore.shell.colorMode}>
<RootSiblingParent> <RootSiblingParent>
<analytics.Provider> <analytics.Provider>
<RootStoreProvider value={rootStore}> <RootStoreProvider value={rootStore}>

View File

@ -89,10 +89,13 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({
theme, theme,
children, children,
}) => { }) => {
const colorScheme = useColorScheme() const colorSchemeFromRN = useColorScheme()
// if theme is 'system', use the device's configured color scheme
let colorScheme = theme === 'system' ? colorSchemeFromRN : theme
const value = useMemo( const value = useMemo(
() => ((theme || colorScheme) === 'dark' ? darkTheme : defaultTheme), () => (colorScheme === 'dark' ? darkTheme : defaultTheme),
[colorScheme, theme], [colorScheme, theme],
) )

View File

@ -189,7 +189,7 @@ export interface ComposerOpts {
} }
export class ShellUiModel { export class ShellUiModel {
darkMode = false colorMode = 'system'
minimalShellMode = false minimalShellMode = false
isDrawerOpen = false isDrawerOpen = false
isDrawerSwipeDisabled = false isDrawerSwipeDisabled = false
@ -210,20 +210,20 @@ export class ShellUiModel {
serialize(): unknown { serialize(): unknown {
return { return {
darkMode: this.darkMode, colorMode: this.colorMode,
} }
} }
hydrate(v: unknown) { hydrate(v: unknown) {
if (isObj(v)) { if (isObj(v)) {
if (hasProp(v, 'darkMode') && typeof v.darkMode === 'boolean') { if (hasProp(v, 'colorMode') && typeof v.colorMode === 'string') {
this.darkMode = v.darkMode this.colorMode = v.colorMode
} }
} }
} }
setDarkMode(v: boolean) { setColorMode(mode: string) {
this.darkMode = v this.colorMode = mode
} }
setMinimalShellMode(v: boolean) { setMinimalShellMode(v: boolean) {

View File

@ -53,6 +53,19 @@ export const DrawerContent = observer(() => {
const {notifications} = store.me const {notifications} = store.me
const colorModes = ['light', 'dark', 'system']
const modeAccessibilityText = {
light: 'Sets display to light mode',
dark: 'Sets display to dark mode',
system: 'Sets display to system default',
}
const nextColorMode = () => {
return colorModes[
(colorModes.indexOf(store.shell.colorMode) + 1) % colorModes.length
]
}
// events // events
// = // =
@ -112,9 +125,9 @@ export const DrawerContent = observer(() => {
Linking.openURL(FEEDBACK_FORM_URL) Linking.openURL(FEEDBACK_FORM_URL)
}, [track]) }, [track])
const onDarkmodePress = React.useCallback(() => { const onColorModePress = React.useCallback(() => {
track('Menu:ItemClicked', {url: '#darkmode'}) track('Menu:ItemClicked', {url: '#cycleColorMode'})
store.shell.setDarkMode(!store.shell.darkMode) store.shell.setColorMode(nextColorMode())
}, [track, store]) }, [track, store])
// rendering // rendering
@ -280,13 +293,9 @@ export const DrawerContent = observer(() => {
{!isWeb && ( {!isWeb && (
<TouchableOpacity <TouchableOpacity
accessibilityRole="button" accessibilityRole="button"
accessibilityLabel="Toggle dark mode" accessibilityLabel="Cycle color mode"
accessibilityHint={ accessibilityHint={modeAccessibilityText[store.shell.colorMode]}
theme.colorScheme === 'dark' onPress={onColorModePress}
? 'Sets display to light mode'
: 'Sets display to dark mode'
}
onPress={onDarkmodePress}
style={[ style={[
styles.footerBtn, styles.footerBtn,
theme.colorScheme === 'light' theme.colorScheme === 'light'

View File

@ -17,10 +17,26 @@ import {formatCount} from 'view/com/util/numeric/format'
export const DesktopRightNav = observer(function DesktopRightNav() { export const DesktopRightNav = observer(function DesktopRightNav() {
const store = useStores() const store = useStores()
const pal = usePalette('default') const pal = usePalette('default')
const mode = useColorSchemeStyle('Light', 'Dark') const colorModes = ['light', 'dark', 'system']
const modeAccessibilityText = {
light: 'Sets display to light mode',
dark: 'Sets display to dark mode',
system: 'Sets display to system default',
}
const modeHelpText = {
light: 'Light Theme',
dark: 'Dark Theme',
system: 'System Default Theme',
}
const onDarkmodePress = React.useCallback(() => { const nextColorMode = () => {
store.shell.setDarkMode(!store.shell.darkMode) return colorModes[
(colorModes.indexOf(store.shell.colorMode) + 1) % colorModes.length
]
}
const onModePress = React.useCallback(() => {
store.shell.setColorMode(nextColorMode())
}, [store]) }, [store])
return ( return (
@ -61,20 +77,16 @@ export const DesktopRightNav = observer(function DesktopRightNav() {
<InviteCodes /> <InviteCodes />
<View> <View>
<TouchableOpacity <TouchableOpacity
style={[styles.darkModeToggle]} style={[styles.cycleColorModeToggle]}
onPress={onDarkmodePress} onPress={onModePress}
accessibilityRole="button" accessibilityRole="button"
accessibilityLabel="Toggle dark mode" accessibilityLabel="Cycle color mode"
accessibilityHint={ accessibilityHint={modeAccessibilityText[nextColorMode()]}>
mode === 'Dark' <View style={[pal.viewLight, styles.cycleColorModeToggleIcon]}>
? 'Sets display to light mode'
: 'Sets display to dark mode'
}>
<View style={[pal.viewLight, styles.darkModeToggleIcon]}>
<MoonIcon size={18} style={pal.textLight} /> <MoonIcon size={18} style={pal.textLight} />
</View> </View>
<Text type="sm" style={pal.textLight}> <Text type="sm" style={pal.textLight}>
{mode} mode {modeHelpText[store.shell.colorMode]}
</Text> </Text>
</TouchableOpacity> </TouchableOpacity>
</View> </View>
@ -148,13 +160,13 @@ const styles = StyleSheet.create({
marginRight: 6, marginRight: 6,
}, },
darkModeToggle: { cycleColorModeToggle: {
flexDirection: 'row', flexDirection: 'row',
alignItems: 'center', alignItems: 'center',
gap: 8, gap: 8,
marginHorizontal: 12, marginHorizontal: 12,
}, },
darkModeToggleIcon: { cycleColorModeToggleIcon: {
flexDirection: 'row', flexDirection: 'row',
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',