Easily toggle between light and dark theme with Material-UI
In this blog you are going to learn how to create a toggle button that switches between light and dark theme.
I have already created a video about it on my youtube channel. Check that out.
We know that in dark mode, the background stays dark and the foreground is white to keep the contrast. In light mode, it is just the opposite. So, we need a component that automatically changes its background color based on the theme. Also for the text.
In Material-ui, we have a component like that. That is the Paper component. The Paper component is intelligent enough to automatically detect the theme and it will change its background color. For more information, please check their docs out. Paper
- Let's create a boilerplate component.
1import React from 'react'23const DarkTheme = (props) => {4 return (5 <div>67 </div>8 )9}1011export default DarkTheme
- Create a new state with useState hook.
1import React, {useState} from 'react'23const DarkTheme = (props) => {4 const [dark, setDark] = useState(false)5 return (6 <div>78 </div>9 )10}1112export default DarkTheme
This will be used for checking if the current mode is in dark or not.
- Create a new theme.
1import React, {useState} from 'react'2import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'34const DarkTheme = (props) => {5 const [dark, setDark] = useState(false)67 const theme = createMuiTheme({8 palette: {9 type: dark ? 'dark' : 'light',10 },11 })1213 return (14 <div>1516 </div>17 )18}1920export default DarkTheme
Simply we specify the theme mode by the type property. In this case, it will be updated based on the dark
state value. We will use a button to manipulate the state.
- Insert a paper with the Typography component.
1import React, { useState } from 'react'2import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'3import Typography from '@material-ui/core/Typography'4import Paper from '@material-ui/core/Paper'56const DarkTheme = () => {7 const [dark, setDark] = useState(false)89 const theme = createMuiTheme({10 palette: {11 type: dark ? 'dark' : 'light',12 },13 })1415 return (16 <Paper>17 <Typography variant='h1'>This is a h1 text</Typography>1819 <Typography variant='body2'>This is a body2 text</Typography>20 </Paper>21 )22}2324export default DarkTheme
- Wrap all components by the ThemeProvider and pass the theme object as a theme prop.
1import React, { useState } from 'react'2import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'3import Typography from '@material-ui/core/Typography'4import Paper from '@material-ui/core/Paper'56const DarkTheme = () => {7 const [dark, setDark] = useState(false)89 const theme = createMuiTheme({10 palette: {11 type: dark ? 'dark' : 'light',12 },13 })1415 return (16 <ThemeProvider theme={theme}>17 <Paper>18 <Typography variant='h1'>This is a h1 text</Typography>1920 <Typography variant='body2'>This is a body2 text</Typography>21 </Paper>22 </ThemeProvider>23 )24}2526export default DarkTheme
If you have no idea about theming in material-ui, then please check this video out.
ThemeProvider is making our custom theme available to all the children. It is like redux and context API.
But we still don't have the toggle functionality. We need a switch component
- Insert a switch component or any other button-type component that can be togglable.
1import React, { useState } from 'react'2import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'3import Typography from '@material-ui/core/Typography'4import Paper from '@material-ui/core/Paper'5import Switch from '@material-ui/core/Switch'67const DarkTheme = () => {8 const [dark, setDark] = useState(false)910 const theme = createMuiTheme({11 palette: {12 type: dark ? 'dark': 'light',13 },14 })1516 return (17 <ThemeProvider theme={theme}>18 <Switch checked={dark} onChange={() => setDark(!dark)} />19 <Paper>20 <Typography variant='h1'>This is a h1 text</Typography>2122 <Typography variant='body2'>This is a body2 text</Typography>23 </Paper>24 </ThemeProvider>25 )26}2728export default DarkTheme
Every time you click on the switch button, the dark state will be changed to its opposite value. This will toggle the theme type.
Switch off.
Switch on.
How is it working?
The Paper component has a white Background color and dark font color. And Typography component by default inherits the font color from its parent. In this case, it is the Paper component. When the theme type is changed, Paper is intelligent enough to detect the change. It will invert the color to keep the contrast between foreground and background color.
So, This is how it works. Isn't it so easy?
Shameless Plug
I have made a video about how to build a carousel postcard with React, Material-UI, and Swiper.js. If you are interested you can check the video.
You can also demo the application form here
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
If you have any questions, feel free to contact me on any social media as @thatanjan. Stay safe. Goodbye.
About me
Why do I do what I do?
The Internet has revolutionized our life. I want to make the internet more beautiful and useful.
What do I do?
I ended up being a full-stack software engineer.
What can I do?
I can develop complex full-stack web applications like social media applications or e-commerce sites.
What have I done?
I have developed a social media application called Confession. The goal of this application is to help people overcome their imposter syndrome by sharing our failure stories. I also love to share my knowledge. So, I run a youtube channel called Cules Coding where I teach people full-stack web development, data structure algorithms, and many more. So, Subscribe to Cules Coding so that you don't miss the cool stuff.
Want to work with me?
I am looking for a team where I can show my ambition and passion and produce great value for them. Contact me through my email or any social media as @thatanjan. I would be happy to have a touch with you.
Contacts
- Email: thatanjan@gmail.com
- linkedin: @thatanjan
- portfolio: anjan
- Github: @thatanjan
- Instagram (personal): @thatanjan
- Instagram (youtube channel): @thatanjan
- twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
Videos might you might want to watch:
Shameless Plug
I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram : @thatanjan
- Twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: