自动深色模式

This commit is contained in:
tangly1024
2022-03-03 16:30:18 +08:00
parent e8a8ef3b89
commit e2be56051b
2 changed files with 12 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
import lang from './lang' import lang from './lang'
import { useContext, createContext, useState, useEffect } from 'react' import { useContext, createContext, useState } from 'react'
import Router from 'next/router' import Router from 'next/router'
import { initDarkMode } from './theme' import { initDarkMode } from './theme'
const GlobalContext = createContext() const GlobalContext = createContext()

View File

@@ -1,4 +1,5 @@
import cookie from 'react-cookies' import cookie from 'react-cookies'
import BLOG from '@/blog.config'
/** /**
* 初始化主题 * 初始化主题
@@ -23,10 +24,16 @@ export const initDarkMode = (isDarkMode, updateDarkMode) => {
* @returns {*} * @returns {*}
*/ */
export function isPreferDark () { export function isPreferDark () {
// 系统深色模式或时间是夜间时,强行置为夜间模式 if (BLOG.APPEARANCE === 'dark') {
const date = new Date() return true
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches }
return prefersDarkMode || (date.getHours() >= 18 || date.getHours() < 6) if (BLOG.APPEARANCE === 'auto') {
// 系统深色模式或时间是夜间时,强行置为夜间模式
const date = new Date()
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
return prefersDarkMode || (date.getHours() >= 18 || date.getHours() < 6)
}
return false
} }
/** /**