Files
wx_pbzc/utils/theme.js
T
lc 5d155e41fe fix: tab bar icons invisible due to CSS variable isolation
Root cause: WeChat miniprogram components have style isolation by default.
Changing tab bar hardcoded #888/#666 to var(--text-secondary) broke
because --text-secondary was only defined on page{} in app.wxss and
couldn't cascade into the custom-tab-bar component.

Fix:
1. theme.js: inject BASE_VARS (--text, --text-secondary, --border, etc.)
   into themeStyle string so they're available via inline style everywhere
2. Add styleIsolation: apply-shared to all 3 component JSONs so page-level
   CSS variables cascade into components
2026-06-04 16:39:17 +08:00

103 lines
2.4 KiB
JavaScript

const THEMES = [
{
id: 'orange',
name: '活力橙',
primary: '#FF6B35',
primaryLight: '#FF8C5A',
primaryBg: '#FFF3ED',
primaryRgb: '255,107,53'
},
{
id: 'blue',
name: '深海蓝',
primary: '#2979FF',
primaryLight: '#5C9CFF',
primaryBg: '#EDF4FF',
primaryRgb: '41,121,255'
},
{
id: 'green',
name: '森林绿',
primary: '#43A047',
primaryLight: '#6EC072',
primaryBg: '#EDF7EE',
primaryRgb: '67,160,71'
},
{
id: 'purple',
name: '优雅紫',
primary: '#7E57C2',
primaryLight: '#A08AD6',
primaryBg: '#F5F0FA',
primaryRgb: '126,87,194'
},
{
id: 'pink',
name: '樱花粉',
primary: '#EC407A',
primaryLight: '#F06A9A',
primaryBg: '#FDEEF3',
primaryRgb: '236,64,122'
}
]
const BASE_VARS = '--text:#333;--text-secondary:#999;--bg:#F5F5F5;--card-bg:#FFFFFF;--border:#EEE;--success:#4CAF50;'
const DEFAULT_THEME_ID = 'orange'
const THEME_KEY = 'app_theme'
let _lastNavColor = ''
let _navColorTimer = null
const getThemeById = (id) => THEMES.find(t => t.id === id) || THEMES[0]
const getCurrentTheme = () => {
const id = wx.getStorageSync(THEME_KEY) || DEFAULT_THEME_ID
return getThemeById(id)
}
const _setNavBarColor = (primary) => {
if (_lastNavColor === primary) return
_lastNavColor = primary
if (_navColorTimer) clearTimeout(_navColorTimer)
_navColorTimer = setTimeout(() => {
try {
wx.setNavigationBarColor({
frontColor: '#ffffff',
backgroundColor: primary
})
} catch (e) {}
}, 50)
}
const setTheme = (id) => {
wx.setStorageSync(THEME_KEY, id)
const theme = getThemeById(id)
_setNavBarColor(theme.primary)
const app = getApp()
if (app && app.globalData) app.globalData.theme = theme
return theme
}
const applyThemeToPage = (self) => {
const theme = getCurrentTheme()
self.setData({
theme,
themeStyle: `${BASE_VARS}--primary:${theme.primary};--primary-light:${theme.primaryLight};--primary-bg:${theme.primaryBg};--primary-rgb:${theme.primaryRgb};`
})
_setNavBarColor(theme.primary)
}
const getThemeStyle = (theme) => {
const t = theme || getCurrentTheme()
return `${BASE_VARS}--primary:${t.primary};--primary-light:${t.primaryLight};--primary-bg:${t.primaryBg};--primary-rgb:${t.primaryRgb};`
}
module.exports = {
THEMES,
DEFAULT_THEME_ID,
getThemeById,
getCurrentTheme,
setTheme,
applyThemeToPage,
getThemeStyle
}