From e2be56051b8e2f13c665fbabba5c41fa004b385f Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Thu, 3 Mar 2022 16:30:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B7=B1=E8=89=B2=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/global.js | 2 +- lib/theme.js | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/global.js b/lib/global.js index 860d37f1..5032f2cb 100644 --- a/lib/global.js +++ b/lib/global.js @@ -1,5 +1,5 @@ import lang from './lang' -import { useContext, createContext, useState, useEffect } from 'react' +import { useContext, createContext, useState } from 'react' import Router from 'next/router' import { initDarkMode } from './theme' const GlobalContext = createContext() diff --git a/lib/theme.js b/lib/theme.js index ba888dba..701554a8 100644 --- a/lib/theme.js +++ b/lib/theme.js @@ -1,4 +1,5 @@ import cookie from 'react-cookies' +import BLOG from '@/blog.config' /** * 初始化主题 @@ -23,10 +24,16 @@ export const initDarkMode = (isDarkMode, updateDarkMode) => { * @returns {*} */ export function isPreferDark () { - // 系统深色模式或时间是夜间时,强行置为夜间模式 - const date = new Date() - const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches - return prefersDarkMode || (date.getHours() >= 18 || date.getHours() < 6) + if (BLOG.APPEARANCE === 'dark') { + return true + } + 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 } /**