

使用 React hooks 监听系统的暗黑模式
source link: https://www.fly63.com/article/detial/11130
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

苹果的“暗黑模式”带来了全然一新的外观,它能使您的眼睛放松,并有助于您专心工作。暗黑模式使用一种较深的配色方案,这种配色作用于整个系统,现在大部分网站也加入了暗黑模式,包括 Tailwindcss、antd design 等都支持了暗黑模式,因此我们的网站也要适配系统皮肤。
css 实现
暗模式传统上是通过使用 prefers-color-scheme 媒体查询来实现的,当暗黑模式被激活时,它可以重新应用一套样式。
body {
color: black;
background: white;
}
@media (prefers-color-scheme: dark) {
body {
color: white;
background: black;
}
}
react hooks 实现
前端页面中除了使用 css 实现外,还有很大部分是使用 JavaScript 实现的,比如 echarts 图表等,这时就需要使用 JavaScript, 可以使用window.matchMedia[1] 来获取皮肤颜色。我们可以把这个逻辑写成一个自定义 hooks
import { useEffect, useState } from "react"
export type ThemeName = "light" | "dark"
function useTheme() {
const [themeName, setThemeName] = useState<ThemeName>("light")
useEffect(() => {
// 设置初始皮肤
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
setThemeName("dark")
} else {
setThemeName("light")
}
// 监听系统颜色切换
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (event) => {
if (event.matches) {
setThemeName("dark")
} else {
setThemeName("light")
}
})
}, [])
return {
themeName,
isDarkMode: themeName === "dark",
isLightMode: themeName === "light",
}
}
export default useTheme
下面代码是配合 echarts 使用
import './styles.css'
import React, { useRef, useEffect } from 'react'
import * as echarts from 'echarts'
import useTheme from './hooks/useTheme'
export default function App() {
const domRef = useRef(null)
const { isDarkMode } = useTheme()
useEffect(() => {
var myChart = echarts.init(domRef.current, isDarkMode ? 'dark' : 'light')
var option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
},
yAxis: {
type: 'value',
},
series: [
{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line',
smooth: true,
},
],
}
myChart.setOption(option)
return () => {
myChart.dispose()
}
}, [isDarkMode])
return (
<div className='App'>
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<div ref={domRef} style={{ height: 500 }}></div>
</div>
)
}
Recommend
-
32
在上篇文章我们讲了如何使用React的Suspense组件和lazy方法来实现模块的懒加载,后面还讲了如何使用 React的useState方法来实现自定义的Hooks,从而达到复用的目的。 我们知道,不管在做什么样的前端项目,列表页肯...
-
11
在小程序中使用 React with Hooks原文地址:在小程序中使用 React with Hooks · Issue #1 · CodeFalling/remax介绍一下 Remax
-
4
vue迁移react使用useReducer hooks还想支持回调?如果您发现本文排版有问题,可以先点击下面...
-
9
[译]使用React Hooks请求数据[译]使用React Hooks请求数据原文:How to fetch data with React Hooks?在这篇文章里,我将演示...
-
10
react hooks: 像组件state一样使用redux字节跳动 前端工程师redux与react结合使用一直是:利用HOC通过props向组件注入数据。这种包装模式达到了很好的逻辑复用与解耦。但随着react...
-
12
Mac系统开启Chrome 跟 Edge的网页强制暗黑模式2020-09-17 mac开启强制暗黑模式Chrome进入浏览器,地址栏输入:chrome://flags/#ena...
-
7
使用 React Hooks 构建一个音频播放器 分类: React, 前端, JavaScript作者:
-
6
在Canvas中使用React Hooks 在本文中,我将使用React Hooks创建一个html canvas 画图网站,我将使用create-react-app脚手架从零开始构建项目。最后这个应用程序有诸如清除、撤销和使用localStorage...
-
10
IDE的暗黑模式(dark mode)真的护眼吗?我为什么一直使用浅色背景(light mode) 2021-06-03 日记
-
6
如何使用 React Hooks 重构类组件?-51CTO.COM 如何使用 React Hooks 重构类组件? 作者:GUOZE 2022-07-18 09:01:58 通过这篇文章,相信你对使用Hooks(函数组件)来重构类组件有了...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK