Skip to main content

theme-switcher

To implement a theme switcher in React with TypeScript, you can follow these steps:

  1. Define Theme Interface: Define a TypeScript interface for your theme, specifying the properties you want to control (e.g., background color, text color).
interface Theme {
background: string;
color: string;
}

const lightTheme: Theme = {
background: '#fff',
color: '#000',
};

const darkTheme: Theme = {
background: '#000',
color: '#fff',
};
  1. Create a Theme Context: Create a React context to manage and provide access to the theme throughout your application.
import React, { createContext, useContext, useState } from 'react';

interface ThemeContextProps {
theme: Theme;
toggleTheme: () => void;
}

const ThemeContext = createContext<ThemeContextProps | undefined>(undefined);

export const useTheme = () => useContext(ThemeContext);
  1. Create a Theme Provider: Create a provider component that holds the current theme state and a function to toggle between themes.
export const ThemeProvider: React.FC = ({ children }) => {
const [theme, setTheme] = useState<Theme>(lightTheme);

const toggleTheme = () => {
setTheme(theme === lightTheme ? darkTheme : lightTheme);
};

return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
};
  1. Create a Theme Switcher Button: Create a button component that, when clicked, toggles the theme using the toggleTheme function provided by the ThemeContext.
const ThemeSwitcher: React.FC = () => {
const { toggleTheme } = useTheme();

return (
<button onClick={toggleTheme}>Switch Theme</button>
);
};
  1. Wrap Your Application with the Theme Provider: In your main application file, wrap your application with the ThemeProvider to provide theme context to all components.
import { ThemeProvider } from './ThemeContext';

const App: React.FC = () => {
return (
<ThemeProvider>
<ThemeSwitcher />
{/* rest of your app */}
</ThemeProvider>
);
};

export default App;
  1. Use Theme in Components: Use the useTheme hook in your components to access the current theme and apply it to your component styles.
const StyledComponent: React.FC = () => {
const { theme } = useTheme();

const style = {
backgroundColor: theme.background,
color: theme.color,
// ...other styles
};

return (
<div style={style}>Themed content</div>
);
};

This basic setup allows you to toggle between a light and dark theme across your application. Each component that requires theming can access the theme context and apply the relevant styles accordingly.

To provide a step-by-step explanation of the implementation, here's how the theme switcher feature is implemented using a functional React component with TypeScript:

Step-by-Step Implementation of Theme Switcher

  1. Define the Theme Interface and Themes:

    • Create interfaces for the light and dark themes specifying the CSS properties that will change.
    • Define the actual theme objects with their respective styles.
  2. Create the Theme Context:

    • Use the createContext function from React to create a new context for the theme.
    • The context will hold the current theme object and a function to toggle the theme.
  3. Create the Theme Provider Component:

    • Define a ThemeProvider component that uses the useState hook to manage the current theme.
    • The component includes a toggleTheme function that switches the theme state between light and dark.
    • The ThemeProvider renders a ThemeContext.Provider with the theme and toggleTheme function as its value, wrapping its children.
  4. Create a Custom Hook for Using the Theme:

    • Define a custom hook useTheme that wraps the useContext hook for consuming the ThemeContext.
    • This hook will be used by components to access the theme and toggle functionality.
  5. Create the Theme Switcher Button Component:

    • Make a ThemeSwitcher button component that uses the useTheme hook to get access to the toggleTheme function.
    • When the button is clicked, it calls toggleTheme to switch the current theme.
  6. Wrap the Application with the Theme Provider:

    • In the top-level component (such as App), wrap the entire application with the ThemeProvider.
    • This ensures that any component in the application can access the theme context.
  7. Apply the Theme to Components:

    • In individual components, use the useTheme hook to access the current theme's styles.
    • Apply these styles to the components' elements directly through the style attribute or by assigning them to className if using CSS classes.
  8. Ensure Reusability:

    • Make sure that all components use the theme context in a way that they remain reusable.
    • The components should not have direct references to the theme objects but should always access them through the context.
  9. Testing:

    • Test the ThemeSwitcher by clicking the switch button and observing the changes in the UI.
    • Ensure that the theme persists across different components and that the switcher updates the theme in real-time.

By following these steps, you have created a theme switcher feature in your React application that allows users to switch between light and dark modes. The use of context and hooks makes the feature highly reusable and easy to integrate into different parts of your application.