feat : add theme context for dark and light mode
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
b46f72d984
commit
f24803afae
@ -0,0 +1,42 @@
|
||||
import React, {createContext, useState, useEffect} from 'react';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
|
||||
|
||||
interface ThemeContextType {
|
||||
theme: string,
|
||||
toggleTheme: (any)=> void
|
||||
};
|
||||
|
||||
const ThemeContext = createContext<ThemeContextType | null>(null);
|
||||
|
||||
export const ThemeProvider = ({children}) => {
|
||||
const [theme, setTheme] = useState('light');
|
||||
|
||||
useEffect(() => {
|
||||
// Load saved theme from storage
|
||||
const getTheme = async () => {
|
||||
try {
|
||||
const savedTheme = await AsyncStorage.getItem('theme');
|
||||
if (savedTheme) {
|
||||
setTheme(savedTheme);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('Error loading theme:', error);
|
||||
}
|
||||
};
|
||||
getTheme();
|
||||
}, []);
|
||||
|
||||
const toggleTheme = newTheme => {
|
||||
setTheme(newTheme);
|
||||
AsyncStorage.setItem('theme', newTheme)
|
||||
};
|
||||
|
||||
return (
|
||||
<ThemeContext.Provider value={{theme, toggleTheme}}>
|
||||
{children}
|
||||
</ThemeContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default ThemeContext;
|
Loading…
Reference in new issue