11import { 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/* ----- 同步亮暗模式 ----- */
55function 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