惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
V
V2EX
G
Google Developers Blog
F
Full Disclosure
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
NISL@THU
NISL@THU
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
A
About on SuperTechFans
The Cloudflare Blog
C
Cisco Blogs
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
Help Net Security
Help Net Security
Recorded Future
Recorded Future
PCI Perspectives
PCI Perspectives
S
Schneier on Security
AI
AI
N
News | PayPal Newsroom
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
S
Securelist
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
AWS News Blog
AWS News Blog
TaoSecurity Blog
TaoSecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 三生石上(FineUI控件)
C
CXSECURITY Database RSS Feed - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cloudbric
Cloudbric
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
Check Point Blog
S
Security Affairs

戴兜的小屋

Hi! 2024 - 戴兜的小屋 优雅地实现滚动容器遮罩 - 戴兜的小屋 [简中] Google Summer of Code & Chrome Extensions - 戴兜的小屋 Google Summer of Code & Chrome Extensions - 戴兜的小屋 SCSS+WindiCSS实现主题色切换 - 戴兜的小屋 Plaid CTF Writeup [Treasure Map/CSS] - 戴兜的小屋 写一个炫酷的个人名片页✨✨ - 戴兜的小屋 【戴兜的游戏安利】Hi-Fi RUSH,不可多得的动作爽游 - 戴兜的小屋 Manifest V3扩展Content Script绕过CSP限制点击页面内元素 - 戴兜的小屋 实现滚动时Header自动隐藏 - 戴兜的小屋 你好!2023 - 戴兜的小屋 !important导致TransitionGroup失效 - 戴兜的小屋 新年快乐! - 戴兜的小屋 CSS Layout API初探:瀑布流布局实现 - 戴兜的小屋 你好! 2022 - 戴兜的小屋 手撸一个前端天气卡片 - 戴兜的小屋 Hi!2021 - 戴兜的小屋 服务器又搬迁啦! - 戴兜的小屋 base64数据隐写实现原理分析 - 戴兜的小屋
在 Nuxt.js 中配合 windicss 实现暗黑模式适配 - 戴兜的小屋
戴兜文章作者 · 2023-02-09 · via 戴兜的小屋

在 windicss 中,提供了媒体查询和 class 两种方式实现暗黑模式适配。而且比较省心的是——其提供的 dark 变体会自动根据选择的适配模式,生成对应的代码,可以有效避免写出一堆没用的css,看起来也比较清晰。

为了方便控制,我们选择使用 class 的方式来切换暗黑模式(即给根元素赋予类名 dark 来切换到暗黑模式)

基础样式

首先,需要一些全局 css 来解决 windicss 无法覆盖的样式。

滚动条颜色改变

正常情况下,你可能会想用 -webkit-scrollbar 伪类,但是,其实有更优雅的写法。浏览器提供了一个 color-scheme css 属性,将其设置为dark,浏览器便会自动将页面内所有浏览器自带的元素渲染成暗色风格

html.dark {
    color-scheme: dark;
}

图片亮度降低

也很简单,应用一个 filter 就好了

html.dark img {
    filter: brightness(0.8);
}

自动检测

接下来就是重头戏了,如何判断并给html元素加上dark类名,毕竟windicss可不会帮我们自动处理。

我们会在前端为用户提供一个下拉框,用户可以选择自动适应、保持暗黑模式、保持明亮模式

为了避免页面初载入时样式切换导致的闪屏,最终决定将该配置储存到cookie而非localstorage中,这样能够发挥ssr的作用,当用户强制暗黑/明亮时,服务端就能将类名写入html标签中。

import { useCookie } from 'nuxt/app'
// ---cut---
function readDarkModeInStorage() {
  const darkMode = useCookie('darkMode')
  const possibleValues = ['auto', 'dark', 'light']
  if (darkMode.value && possibleValues.includes(darkMode.value)) {
    return darkMode.value
  } else {
    return 'auto'
  }
}

上面是一个辅助函数,用于从储存中读出暗黑模式配置(服务端/客户端均可用)

import { useHead } from '@unhead/vue'
// ---cut---
function setModeClass(isDark: boolean): void {
  if (isDark) {
    useHead({
      htmlAttrs: { class: 'dark' },
      meta: [{ name: 'theme-color', content: '#121212' }],
    })
  } else {
    useHead({
      htmlAttrs: { class: '' },
      meta: [{ name: 'theme-color', content: '#ffffff' }],
    })
  }
}

一个用于设置暗黑模式样式的工具函数,当传入布尔值时,会同时设置 html 的类名和 theme-color 的 meta 标签(ssr/csr均可用)

使用了来自 VueUse 的 useHead 方法

import { useCookie } from 'nuxt/app'
import { ref, watchEffect } from 'vue'
import { usePreferredDark } from '@vueuse/core'
import { useHead } from '@unhead/vue'
function readDarkModeInStorage() {
  const darkMode = useCookie('darkMode')
  const possibleValues = ['auto', 'dark', 'light']
  if (darkMode.value && possibleValues.includes(darkMode.value)) {
    return darkMode.value
  } else {
    return 'auto'
  }
}
function setModeClass(isDark: boolean): void {
  if (isDark) {
    useHead({
      htmlAttrs: { class: 'dark' },
      meta: [{ name: 'theme-color', content: '#121212' }],
    })
  } else {
    useHead({
      htmlAttrs: { class: '' },
      meta: [{ name: 'theme-color', content: '#ffffff' }],
    })
  }
}
// ---cut---
const currentMode = ref(readDarkModeInStorage())
const preferredDark = usePreferredDark()

watchEffect(() => {
  if (currentMode.value === 'auto') {
    if (preferredDark.value) {
      setModeClass(true)
    } else {
      setModeClass(false)
    }
  } else if (currentMode.value === 'dark') {
    setModeClass(true)
  } else if (currentMode.value === 'light') {
    setModeClass(false)
  }

  useCookie('darkMode').value = currentMode.value
})

这里便是关键,首先,读入配置并初始化到 currentMode 变量,接着,使用 VueUse 提供的 usePreferredDark 来获取当前浏览器的颜色模式。

使用一个监听副作用的函数,当上面两个值发生改变时,调用 setModeClass 工具函数去完成最终的类名修改,并将配置写入 cookie。