mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
自动深色模式
This commit is contained in:
@@ -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()
|
||||
|
||||
15
lib/theme.js
15
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
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user