mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-20 07:26:46 +00:00
DarkMode 模块重命名
This commit is contained in:
20
lib/theme.js
20
lib/theme.js
@@ -3,27 +3,27 @@ import cookie from 'react-cookies'
|
||||
/**
|
||||
* 初始化主题
|
||||
* @param theme 用户默认主题state
|
||||
* @param changeTheme 更改主题ChangeState函数
|
||||
* @param updateDarkMode 更改主题ChangeState函数
|
||||
* @description 读取cookie中存的用户主题
|
||||
*/
|
||||
export const initTheme = (theme, changeTheme) => {
|
||||
export const initTheme = (isDarkMode, updateDarkMode) => {
|
||||
// 若未指定主题,则从时间和浏览器偏好中决定初始主题
|
||||
if (!theme) {
|
||||
if (!isDarkMode) {
|
||||
const date = new Date()
|
||||
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||
const useDark = prefersDarkMode || (date.getHours() >= 18 || date.getHours() < 6)
|
||||
if (useDark) {
|
||||
theme = 'dark'
|
||||
isDarkMode = true
|
||||
} else {
|
||||
theme = 'light'
|
||||
isDarkMode = false
|
||||
}
|
||||
}
|
||||
if (typeof window !== 'undefined') {
|
||||
const htmlElement = document.getElementsByTagName('html')
|
||||
htmlElement.className = ''
|
||||
changeTheme(theme)
|
||||
saveTheme(theme)
|
||||
htmlElement.classList?.add(theme)
|
||||
updateDarkMode(isDarkMode)
|
||||
saveDarkModeToCookies(isDarkMode)
|
||||
htmlElement.classList?.add(isDarkMode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ export const initTheme = (theme, changeTheme) => {
|
||||
* 读取默认主题
|
||||
* @returns {*}
|
||||
*/
|
||||
export const loadUserThemeFromCookies = () => {
|
||||
export const loadDarkModeFromCookies = () => {
|
||||
return cookie.load('theme')
|
||||
}
|
||||
|
||||
@@ -39,6 +39,6 @@ export const loadUserThemeFromCookies = () => {
|
||||
* 保存默认主题
|
||||
* @param newTheme
|
||||
*/
|
||||
export const saveTheme = (newTheme) => {
|
||||
export const saveDarkModeToCookies = (newTheme) => {
|
||||
cookie.save('theme', newTheme, { path: '/' })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user