提交 4ab5b33f 编写于 作者: D DCloud_LXH

fix(h5): darkmode body dark-default background-color

上级 98df378a
......@@ -23,6 +23,13 @@ body {
background-color: white;
}
@media (prefers-color-scheme: dark) {
body {
background-color: #191919;
color: hsla(0, 0%, 100%, 0.8);
}
}
input[type='search']::-webkit-search-cancel-button {
display: none;
}
......
import { extend } from '@vue/shared'
import type { Rule, Declaration, Plugin, Root } from 'postcss'
import postcss from 'postcss'
import selectorParser from 'postcss-selector-parser'
import {
createRpx2Unit,
defaultRpx2Unit,
isBuiltInComponent,
COMPONENT_SELECTOR_PREFIX,
normalizeStyles,
} from '@dcloudio/uni-shared'
import {
parsePagesJsonOnce,
normalizeThemeConfigOnce,
getPlatformManifestJsonOnce,
} from '../../json'
export interface UniAppCssProcessorOptions {
unit?: string // 目标单位,默认rem
unitRatio?: number // 单位转换比例,默认10/320
......@@ -91,33 +83,13 @@ function walkDecls(rpx2unit: ReturnType<typeof createRpx2Unit>) {
}
}
function darkmodeAtRule(root: Root, platform: UniApp.PLATFORM) {
const pageJson = parsePagesJsonOnce(process.env.UNI_PLATFORM, platform)
const filePath = root.source?.input.file || ''
if (
process.env.VUE_APP_DARK_MODE === 'true' &&
filePath.indexOf('App.vue') !== -1
) {
const pageBGC = (pageJson.globalStyle || {}).backgroundColor || ''
if (pageBGC.indexOf('@') === 0) {
;['dark', 'light'].forEach((theme) => {
const { backgroundColor } = normalizeStyles(
{ backgroundColor: pageBGC },
normalizeThemeConfigOnce(getPlatformManifestJsonOnce()),
theme as UniApp.ThemeMode
)
if (backgroundColor !== 'undefined') {
const mediaRoot = postcss.parse(`
/* #ifndef APP-NVUE*/
@media (prefers-color-scheme: ${theme}) {
body,
uni-page-body {
background-color: ${backgroundColor};
}
}
/* #endif */
`)
root.nodes = [...mediaRoot.nodes, ...root.nodes]
function filterPrefersColorScheme(root: Root) {
if (process.env.VUE_APP_DARK_MODE === 'true') {
const filePath = root.source?.input.file
if (filePath && filePath.includes('@dcloudio')) {
root.walkAtRules((rule) => {
if (rule.params.includes('prefers-color-scheme')) {
rule.remove()
}
})
}
......@@ -173,9 +145,7 @@ const uniapp = (opts?: UniAppCssProcessorOptions) => {
OnceExit(root) {
root.walkDecls(walkDecls(rpx2unit))
const rewriteTag = transforms[platform]
if (['h5', 'app'].includes(platform)) {
darkmodeAtRule(root, platform)
}
filterPrefersColorScheme(root)
if (rewriteTag) {
root.walkRules(
walkRules({
......
......@@ -13,7 +13,8 @@ body {
@media (prefers-color-scheme: dark) {
body {
color-scheme: dark;
background-color: #191919;
color: hsla(0, 0%, 100%, 0.8);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册