@@ -7,8 +7,13 @@ import React, {
77 useEffect ,
88 useState
99} from 'react'
10- import { ImageStyle , StyleSheet , TextStyle , ViewStyle } from 'react-native'
11- import { Appearance , AppearanceProvider } from 'react-native-appearance'
10+ import {
11+ ImageStyle ,
12+ StyleSheet ,
13+ TextStyle ,
14+ useColorScheme ,
15+ ViewStyle
16+ } from 'react-native'
1217import merge from 'ts-deepmerge'
1318
1419export interface BaseTheme { }
@@ -67,7 +72,7 @@ type ThemeProviderProps = {
6772
6873const themeContext = createContext < ThemeContext > ( { } as ThemeContext )
6974
70- const generateTheme = ( mode : ThemeMode , themes : Themes ) => {
75+ const generateTheme = ( mode : Exclude < ThemeMode , 'auto' > , themes : Themes ) => {
7176 if (
7277 typeof themes !== 'object' ||
7378 ! themes ||
@@ -78,42 +83,27 @@ const generateTheme = (mode: ThemeMode, themes: Themes) => {
7883 ) {
7984 return { }
8085 }
81- const constants = 'constants' in themes ? themes [ 'constants' ] : { }
82- const systemColorScheme = Appearance . getColorScheme ( )
83- const currentMode =
84- mode !== 'auto'
85- ? mode
86- : systemColorScheme !== 'no-preference'
87- ? systemColorScheme
88- : 'light'
89- return merge ( constants , themes [ currentMode ] )
86+ const constants = ( 'constants' in themes ? themes [ 'constants' ] : { } ) || { }
87+ return merge ( constants , themes [ mode ] )
9088}
9189
92- const RawThemeProvider : React . FC < ThemeProviderProps > = ( {
90+ export const ThemeProvider : React . FC < ThemeProviderProps > = ( {
9391 children,
9492 mode : initialMode ,
9593 themes : initialThemes
9694} ) => {
95+ const colorScheme = useColorScheme ( )
9796 const [ mode , setMode ] = useState ( initialMode ?? 'auto' )
9897 const [ themes , setThemes ] = useState ( initialThemes )
99- const [ theme , setTheme ] = useState ( generateTheme ( mode , themes ) )
98+ const [ theme , setTheme ] = useState (
99+ generateTheme ( mode !== 'auto' ? mode : colorScheme || 'light' , themes )
100+ )
100101 useEffect ( ( ) => {
101- let subscription : any
102- setTheme ( generateTheme ( mode , themes ) )
103- if ( mode === 'auto' ) {
104- subscription = Appearance . addChangeListener ( ( { colorScheme } ) => {
105- setTheme (
106- generateTheme (
107- colorScheme !== 'no-preference' ? colorScheme : 'light' ,
108- themes
109- )
110- )
111- } )
112- }
113- return ( ) => {
114- subscription && subscription . remove ( )
115- }
102+ mode !== 'auto' && setTheme ( generateTheme ( mode , themes ) )
116103 } , [ mode , themes ] )
104+ useEffect ( ( ) => {
105+ mode === 'auto' && setTheme ( generateTheme ( colorScheme || 'light' , themes ) )
106+ } , [ colorScheme , mode , themes ] )
117107 return createElement (
118108 themeContext . Provider ,
119109 {
@@ -129,16 +119,6 @@ const RawThemeProvider: React.FC<ThemeProviderProps> = ({
129119 )
130120}
131121
132- export const ThemeProvider : typeof RawThemeProvider = ( {
133- children,
134- ...props
135- } ) =>
136- createElement (
137- AppearanceProvider ,
138- null ,
139- createElement ( RawThemeProvider , props , children )
140- )
141-
142122export const useMode : UseMode = ( ) => {
143123 const { mode, setMode } = useContext ( themeContext )
144124 return [ mode , setMode ]
0 commit comments