**create a fresh folder
run the commands npm create vite@latest (in the folder directory)
also cd to the directory and run the command npm install**
step 1
`code in app.jsx`
import react, { usestate } from 'react';
import { fasun, famoon } from 'react-icons/fa'; // import sun and moon icons
import './app.css';
function app() {
const [darkmode, setdarkmode] = usestate(false);
// toggle dark mode
const toggletheme = () => {
setdarkmode(!darkmode);
};
return (
<div classname={darkmode ? 'dark' : 'light'}>
<div classname="container">
<h1>{darkmode ? 'dark mode' : 'light mode'}</h1>
<button classname="toggle-button" onclick={toggletheme}>
{darkmode ? <famoon classname="icon" /> : <fasun classname="icon" />}
{darkmode ? ' dark mode' : ' light mode'}
</button>
</div>
</div>
);
}
export default app;
Code in App.css
`body {
margin: 0;
font-family: Arial, sans-serif;
}
.container {
text-align: center;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
/* Light Mode Styles */
.light {
background-color: #f9f9f9;
color: #333;
}
/* Dark Mode Styles */
.dark {
background-color: #333;
color: #f9f9f9;
}
/* Toggle Button Styles */
.toggle-button {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 20px;
border: none;
border-radius: 30px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
background-color: #007bff;
color: white;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
transition: all 0.3s ease;
}
.toggle-button:hover {
background-color: #0056b3;
transform: scale(1.05);
}
.icon {
font-size: 20px;
}`
最终输出
以上就是React 中的深色模式 (vite)的详细内容,更多请关注知识资源分享宝库其它相关文章!
版权声明
本站内容来源于互联网搬运,
仅限用于小范围内传播学习,请在下载后24小时内删除,
如果有侵权内容、不妥之处,请第一时间联系我们删除。敬请谅解!
E-mail:dpw1001@163.com
发表评论