Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ColorSchame): 情景模式跟随系统 #88

Merged
merged 1 commit into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion hooks/useConfig/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createContext, useContext, useEffect } from 'react'
import { useReactive, useCookieState } from 'ahooks'
import * as hljs from '@/lib/highlight'

type languages = 'zh-cn' | 'en' | null
type themes = 'light' | 'dark' | null
Expand Down
27 changes: 27 additions & 0 deletions hooks/useSystemColorSchema/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @NOTE 获取浏览器的当前的情景模式
*
* * 通过prefers-color-schema: dark 来判断用户浏览器是否是暗黑模式
*/
interface IOptions {
change: (e: any) => void
}

const options = {
change () {}
}

const useSystemColorSchema = (ops: IOptions = options): Boolean => {
const prefersSchema = window.matchMedia('(prefers-color-scheme: dark)')

if (ops && ops.change) {
prefersSchema.addEventListener('change', function (e) {
ops.change(e)
})
}

return prefersSchema.matches
}


export default typeof window !== 'undefined' ? useSystemColorSchema : () => {}
12 changes: 10 additions & 2 deletions layout/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
LinkButtonGroup,
GalleryButton,
} from '@/components/button'
import useSystemColorSchema from '@/hooks/useSystemColorSchema'

import Response from './response'

Expand All @@ -22,8 +23,15 @@ const ConfigWrapper: React.FC = () => (
</div>
)

function Layout<T>(Component: React.ComponentType<T>) {
const inner = (props: T) => {
function Layout<T>(Component: React.FC<T>) {
useSystemColorSchema({
change(e) {
const themeMode = e.matches ? 'dark' : 'light'
const parentEl = document.querySelector('html')
parentEl?.setAttribute('data-theme-mode', themeMode)
}
})
const inner: React.FC<T> = (props) => {
return (
<Response>
<ConfigWrapper />
Expand Down
7 changes: 0 additions & 7 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ function MyApp({ Component, pageProps, router }: NextPropsWithLayout) {
*/
const [bubblePath, setBubblePath] = React.useState('')

/**
* @NOTE web worker 引入的脚本链接
*/
const [themePath, setThemePath] = React.useState('')

/**
* @NOTE Banner 区域的样式表
*/
Expand Down Expand Up @@ -82,7 +77,6 @@ function MyApp({ Component, pageProps, router }: NextPropsWithLayout) {
useExternal(iconColorPath, { type: 'css' })

useExternal(bubblePath, { type: 'js' })
useExternal(themePath, { type: 'js' })
useExternal(googleFontPath, { type: 'css' })
useExternal(googleFontNormalPath, { type: 'css' })

Expand All @@ -93,7 +87,6 @@ function MyApp({ Component, pageProps, router }: NextPropsWithLayout) {
setIconNormalPath('//at.alicdn.com/t/font_1587964_ap06iu717e5.css')
setBubblePath('/scripts/bubble.js')

setThemePath('/scripts/theme-main.js')

setIconColorPath('//at.alicdn.com/t/font_2595178_wa25xow6jmp.css')
setSlidePath('https://src.wuh.site/stylesheet/slick.min.css')
Expand Down