Skip to content

Commit ad095b2

Browse files
novlan1anlyyao
andauthored
feat: support uniapp token and mode (#802)
* feat: support uniapp token and mode * fix: fix cr --------- Co-authored-by: anlyyao <anly_yaw@163.com>
1 parent 43ab5e0 commit ad095b2

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

packages/theme-generator/src/common/themes/core.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export const CUSTOM_OPTIONS_ID = `${CUSTOM_THEME_ID}-options`;
2626
export const CUSTOM_TOKEN_ID = `${CUSTOM_THEME_ID}-tokens`;
2727

2828
export const isMiniProgram = (device) => device === 'mini-program';
29-
export const isMobile = (device) => device === 'mobile' || isMiniProgram(device);
29+
export const isUniApp = (device) => device === 'uni-app';
30+
export const isMobile = (device) => device === 'mobile' || isMiniProgram(device) || isUniApp(device);
3031

3132
export function normalizeDevice(device) {
3233
return isMobile(device) ? 'mobile' : 'web';
@@ -98,7 +99,29 @@ export function exportCustomStyleSheet(device) {
9899
const extraCssString = extraStyleSheet?.textContent || '';
99100

100101
let finalCssString;
101-
if (isMiniProgram(device)) {
102+
if (isUniApp(device)) {
103+
finalCssString = `
104+
@media (prefers-color-scheme: light) {
105+
/* #ifdef H5 */
106+
:root,
107+
/* #endif */
108+
page,
109+
.page {
110+
${cssString}
111+
}
112+
}
113+
@media (prefers-color-scheme: dark) {
114+
/* #ifdef H5 */
115+
:root,
116+
/* #endif */
117+
page,
118+
.page {
119+
${darkCssString}
120+
}
121+
}
122+
${extraCssString}
123+
`;
124+
} else if (isMiniProgram(device)) {
102125
finalCssString = `
103126
@media (prefers-color-scheme: light) {
104127
page, .page {

packages/theme-generator/src/common/themes/iframe.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { extractRootContent, getThemeMode, setUpModeObserver } from '../utils';
2-
import { CUSTOM_DARK_ID, CUSTOM_THEME_ID, isMiniProgram, isMobile } from './core';
2+
import { CUSTOM_DARK_ID, CUSTOM_THEME_ID, isMiniProgram, isMobile, isUniApp } from './core';
33

44
/* ----- 同步亮暗模式 ----- */
55
function handleMobileModeChange(iframe, mode) {
66
iframe.contentDocument.documentElement.setAttribute('theme-mode', mode);
77
}
88

9-
function handleMiniProgramModeChange(iframe, mode) {
9+
function handleMiniProgramModeChange(iframe, mode, uniapp = false) {
1010
const isDark = mode === 'dark';
1111

1212
const prevModeId = isDark ? CUSTOM_THEME_ID : CUSTOM_DARK_ID;
@@ -24,7 +24,8 @@ function handleMiniProgramModeChange(iframe, mode) {
2424
style.id = currentModeId;
2525

2626
const cssString = extractRootContent(themeStyle.innerText);
27-
style.textContent = `body {\n${cssString}\n}`;
27+
const selector = uniapp ? 'uni-page-body' : 'body';
28+
style.textContent = `${selector} {\n${cssString}\n}`;
2829

2930
iframeDom.head.appendChild(style);
3031
}
@@ -53,8 +54,9 @@ function handleMobileTokenChange(iframe, styleElement) {
5354
}
5455
}
5556

56-
function handleMiniProgramTokenChange(iframe, styleElement) {
57-
const updatedCss = `body {\n${extractRootContent(styleElement.innerText)}\n}`;
57+
function handleMiniProgramTokenChange(iframe, styleElement, uniapp = false) {
58+
const selector = uniapp ? 'uni-page-body' : 'body';
59+
const updatedCss = `${selector} {\n${extractRootContent(styleElement.innerText)}\n}`;
5860

5961
const updatedId = styleElement.id;
6062
const iframeStyleElement = iframe.contentDocument.getElementById(updatedId);
@@ -82,8 +84,8 @@ function watchThemeModeChange(iframe) {
8284

8385
const device = iframe.getAttribute('device');
8486
const handleModeChange = (mode) => {
85-
if (isMiniProgram(device)) {
86-
handleMiniProgramModeChange(iframe, mode);
87+
if (isMiniProgram(device) || isUniApp(device)) {
88+
handleMiniProgramModeChange(iframe, mode, isUniApp(device));
8789
} else {
8890
handleMobileModeChange(iframe, mode);
8991
}
@@ -110,8 +112,8 @@ function watchThemeTokenChange(iframe) {
110112

111113
const device = iframe.getAttribute('device');
112114
const handleTokenChange = (styleElement) => {
113-
if (isMiniProgram(device)) {
114-
handleMiniProgramTokenChange(iframe, styleElement);
115+
if (isMiniProgram(device) || isUniApp(device)) {
116+
handleMiniProgramTokenChange(iframe, styleElement, isUniApp(device));
115117
} else {
116118
handleMobileTokenChange(iframe, styleElement);
117119
}

0 commit comments

Comments
 (0)