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 opposite. So, we need a component which 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. 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
import React from 'react'const DarkTheme = (props) => {return (<div></div>)}export default DarkTheme
import React, {useState} from 'react'const DarkTheme = (props) => {const [dark, setDark] = useState(false)return (<div></div>)}export default DarkTheme
This will be used for checking if the current mode is in dark or not.
import React, {useState} from 'react'import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'const DarkTheme = (props) => {const [dark, setDark] = useState(false)const theme = createMuiTheme({palette: {type: dark ? 'dark' : 'light',},})return (<div></div>)}export 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.
import React, { useState } from 'react'import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'import Typography from '@material-ui/core/Typography'import Paper from '@material-ui/core/Paper'const DarkTheme = () => {const [dark, setDark] = useState(false)const theme = createMuiTheme({palette: {type: dark ? 'dark' : 'light',},})return (<Paper><Typography variant='h1'>This is a h1 text</Typography><Typography variant='body2'>This is a body2 text</Typography></Paper>)}export default DarkTheme
import React, { useState } from 'react'import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'import Typography from '@material-ui/core/Typography'import Paper from '@material-ui/core/Paper'const DarkTheme = () => {const [dark, setDark] = useState(false)const theme = createMuiTheme({palette: {type: dark ? 'dark' : 'light',},})return (<ThemeProvider theme={theme}><Paper><Typography variant='h1'>This is a h1 text</Typography><Typography variant='body2'>This is a body2 text</Typography></Paper></ThemeProvider>)}export default DarkTheme
If you have no idea about theming in material-ui, then please check this video out.
ThemeProvider basically 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
import React, { useState } from 'react'import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'import Typography from '@material-ui/core/Typography'import Paper from '@material-ui/core/Paper'import Switch from '@material-ui/core/Switch'const DarkTheme = () => {const [dark, setDark] = useState(false)const theme = createMuiTheme({palette: {type: dark ? 'dark' : 'light',},})return (<ThemeProvider theme={theme}><Switch checked={dark} onChange={() => setDark(!dark)} /><Paper><Typography variant='h1'>This is a h1 text</Typography><Typography variant='body2'>This is a body2 text</Typography></Paper></ThemeProvider>)}export default DarkTheme
Everytime 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.
Paper component has white Background color and dark font color. And Typography component by default inherit 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 contrast between foreground and background color.
So, This is how it works. Isn't it so easy? Subscribe to my channel. Until then, stay safe and good bye.
My name is Anjan. I am a full stack web developer from Dhaka, Bangladesh.
I can create complex full stack web applications like social media application, blogging, e-commerce website and many more.
I love to solve problems and develop new ideas. I also enjoy sharing my knowledge to other people who are wiling to learn. That's why I write blog posts and run a youtube channel called Cules Coding
Email: anjancules@gmail.com
linkedin: @thatanjan
portofolio: anjan
Github: @thatanjan
Instagram (personal): @thatanjan
Instagram (youtube channel): @thatanjan
twitter: @thatanjan
Cules Coding will teach you full stack development. I will teach you not only the basic concepts but also the advanced concepts that other youtube channels don't cover. I will also teach you Data Structures and Algorithms with abstraction and without Math. You will also find many tutorials about developer tools and technologies. I also explain advanced concepts and technologies with simplicity.
Subscribe to Cules Coding so that my friend you don't miss any of these cool stuffs.