Skip to content

Commit

Permalink
Merge pull request #88 from stack-wuh/feat-performance
Browse files Browse the repository at this point in the history
feat(ColorSchame): 情景模式跟随系统
  • Loading branch information
stack-wuh committed Oct 22, 2022
2 parents c86f016 + a29c571 commit ee519ad
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
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

1 comment on commit ee519ad

@vercel
Copy link

@vercel vercel bot commented on ee519ad Oct 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wuh-site – ./

wuh-site-git-main-163m.vercel.app
wuh-site-163m.vercel.app
wuh-site-kappa.vercel.app

Please sign in to comment.