THEME_SYSTEM_USAGE.md
14.5 KB
主题系统使用指南
一、快速开始
1.0 为什么不能直接用类名?(重要说明)
Tailwind 的工作原理:
- Tailwind 在编译时生成所有类名
- 每个类名对应固定的值(如
p-4=padding: 16px) - 类名和值都是静态的
我们的需求:
- 同一个类名(如
p-4)在不同主题下需要不同的值 -
p-4在 mobile-small 主题下是12px,在 tablet 主题下是20px - 这是运行时动态的,需要根据屏幕尺寸自动切换
解决方案:
- 为每个主题生成一套类名(如
.theme-mobile-medium .p-4) - 根据当前主题动态添加主题前缀类名
- 这样就能像 Tailwind 那样直接用类名了!
1.1 基础使用(动态样式绑定)
<template>
<view class="container">
<view :style="{ padding: padding + 'px', borderRadius: radius + 'px' }">
<text :style="{ fontSize: textSize + 'px' }">
响应式文本
</text>
</view>
</view>
</template>
<script setup lang="uts">
import { ref, onMounted } from 'vue'
import { useTheme } from '@/composables/useTheme.uts'
const { spacing, borderRadius, fontSize } = useTheme()
const padding = ref<number>(16)
const radius = ref<number>(8)
const textSize = ref<number>(14)
onMounted(() => {
// 获取当前主题的间距值
padding.value = spacing.value('base') // 16px (mobile-medium)
// 获取当前主题的圆角值
radius.value = borderRadius.value('lg') // 8px (mobile-medium)
// 获取当前主题的字体大小
textSize.value = fontSize.value('base') // 14px (mobile-medium)
})
</script>
1.2 使用 Tailwind 风格类名(推荐方案)
为什么不能直接用类名?
Tailwind 的类名(如 p-4)是固定的值(padding: 16px),但我们的需求是:
- 同一个类名(如
p-4)在不同主题下应该有不同的值 -
p-4在 mobile-small 主题下是12px,在 tablet 主题下是20px - 这是运行时动态的,不是编译时静态的
解决方案:为主题化类名 + 动态切换
为每个主题生成一套类名,然后根据当前主题动态切换:
<template>
<!-- 使用主题化类名,根据当前主题自动切换 -->
<view :class="['p-4', 'rounded-lg', themeClass]">
<text :class="['text-base', themeClass]">
响应式文本
</text>
</view>
</template>
<script setup lang="uts">
import { computed } from 'vue'
import { useTheme } from '@/composables/useTheme.uts'
const { currentThemeName } = useTheme()
// 根据当前主题生成类名前缀
const themeClass = computed(() => {
return `theme-${currentThemeName.value}`
})
</script>
CSS 定义(在 common.css 中):
/* mobile-small 主题 */
.theme-mobile-small .p-4 { padding: 12px; }
.theme-mobile-small .rounded-lg { border-radius: 8px; }
.theme-mobile-small .text-base { font-size: 14px; }
/* mobile-medium 主题 */
.theme-mobile-medium .p-4 { padding: 16px; }
.theme-mobile-medium .rounded-lg { border-radius: 8px; }
.theme-mobile-medium .text-base { font-size: 14px; }
/* tablet 主题 */
.theme-tablet .p-4 { padding: 20px; }
.theme-tablet .rounded-lg { border-radius: 12px; }
.theme-tablet .text-base { font-size: 16px; }
更优雅的方案:使用工具函数生成类名
<template>
<view :class="tw('p-4', 'rounded-lg')">
<text :class="tw('text-base')">
响应式文本
</text>
</view>
</template>
<script setup lang="uts">
import { computed } from 'vue'
import { useTheme } from '@/composables/useTheme.uts'
const { currentThemeName } = useTheme()
// Tailwind 风格类名工具函数
function tw(...classes: string[]): string {
const themePrefix = `theme-${currentThemeName.value}`
return [themePrefix, ...classes].join(' ')
}
</script>
这样就能像 Tailwind 那样直接用类名了!
1.3 方案三:CSS 变量方式(最优雅,推荐)
核心思路:CSS 变量名固定,值由逻辑层动态注入
优势:
-
✅ 最接近原生 CSS 变量使用方式 -
✅ 不需要为每个主题生成大量 CSS 类 -
✅ 只需要定义一次 CSS,值通过 JavaScript 动态设置 -
✅ 性能好,CSS 变量比动态样式更高效
步骤 1:定义 CSS 变量类
/* theme/theme-variables.css */
.page-padding {
padding: var(--p-page);
}
.card-radius {
border-radius: var(--r-lg);
}
.text-base {
font-size: var(--t-base);
}
步骤 2:在 App.uvue 中设置 CSS 变量值
<template>
<view :style="themeVars">
<!-- 应用内容 -->
</view>
</template>
<script setup lang="uts">
import { ref, onMounted } from 'vue'
import { useThemeVariables } from '@/composables/useThemeVariables.uts'
const { themeVars } = useThemeVariables()
</script>
<style>
@import '@/theme/theme-variables.css';
</style>
步骤 3:在页面中使用
<template>
<view class="page-padding">
<view class="card-radius">
<text class="text-base">响应式文本</text>
</view>
</view>
</template>
<style>
@import '@/theme/theme-variables.css';
.card-radius {
background-color: #fff;
padding: var(--p-base);
}
</style>
完整示例:
<template>
<view class="container">
<view class="card">
<text class="title">标题</text>
<text class="desc">描述文本</text>
</view>
</view>
</template>
<script setup lang="uts">
// 无需任何 JavaScript 代码!
// CSS 变量会自动根据当前主题设置值
</script>
<style>
@import '@/theme/theme-variables.css';
.container {
padding: var(--p-page);
}
.card {
padding: var(--p-base);
border-radius: var(--r-lg);
background-color: #fff;
}
.title {
font-size: var(--t-lg);
margin-bottom: var(--p-sm);
}
.desc {
font-size: var(--t-base);
color: #666;
}
</style>
CSS 变量命名规范:
-
--p-*:间距(padding/margin) -
--r-*:圆角(radius) -
--t-*:字体大小(text) -
--s-*:尺寸(size)
1.4 完整示例:使用主题化类名
步骤 1:在页面中引入主题 CSS
<style>
@import '@/common-theme.css';
</style>
步骤 2:在根元素添加主题类名
<template>
<view :class="themeClass">
<!-- 现在可以直接使用 Tailwind 风格的类名了! -->
<view class="p-base rounded-lg">
<text class="text-base">响应式文本</text>
</view>
</view>
</template>
<script setup lang="uts">
import { computed } from 'vue'
import { useTheme } from '@/composables/useTheme.uts'
import { tw } from '@/utils/theme-classes.uts'
const { currentThemeName } = useTheme()
// 方式1:手动添加主题类名
const themeClass = computed(() => {
return `theme-${currentThemeName.value}`
})
// 方式2:使用 tw() 工具函数(推荐)
// 在需要的地方使用 tw('p-base', 'rounded-lg')
</script>
步骤 3:使用 tw() 工具函数(更优雅)
<template>
<view :class="tw('p-base', 'rounded-lg', 'flex', 'flex-col')">
<text :class="tw('text-base', 'text-primary')">
响应式文本
</text>
</view>
</template>
<script setup lang="uts">
import { tw } from '@/utils/theme-classes.uts'
// tw() 会自动添加当前主题前缀
// 返回: 'theme-mobile-medium p-base rounded-lg flex flex-col'
</script>
优势:
-
✅ 像 Tailwind 一样直接用类名 -
✅ 自动根据屏幕尺寸切换主题 -
✅ 类型安全,编译时检查 -
✅ 性能好,CSS 类比动态样式更高效
二、主题切换
2.1 自动适配(默认)
系统会根据屏幕尺寸自动选择主题:
-
小屏手机 (< 480px) →
mobile-small -
常规手机 (480px - 768px) →
mobile-medium -
大屏手机 (768px - 1024px) →
mobile-large -
平板 (1024px - 1920px) →
tablet -
大屏平板 (>= 1920px) →
tablet-large
2.2 手动切换主题
<script setup lang="uts">
import { useTheme } from '@/composables/useTheme.uts'
const { currentThemeName, setTheme, theme } = useTheme()
// 切换到平板主题
function switchToTablet() {
setTheme('tablet')
console.log('当前主题:', currentThemeName.value)
console.log('间距 base:', theme.value?.spacing.base) // 20px
}
// 切换回自动模式
function switchToAuto() {
// 重新根据屏幕尺寸选择
// 需要在 useTheme 中添加 autoSelectTheme 调用
}
</script>
三、常用场景
3.1 卡片组件
<template>
<view :style="cardStyle" class="card">
<text :style="titleStyle" class="card-title">{{ title }}</text>
<text :style="descStyle" class="card-desc">{{ desc }}</text>
</view>
</template>
<script setup lang="uts">
import { computed } from 'vue'
import { useTheme } from '@/composables/useTheme.uts'
const props = defineProps<{
title: string
desc: string
}>()
const { spacing, borderRadius, fontSize } = useTheme()
const cardStyle = computed(() => {
return {
padding: spacing.value('base') + 'px', // p-4
borderRadius: borderRadius.value('lg') + 'px', // rounded-lg
marginBottom: spacing.value('lg') + 'px' // mb-5
}
})
const titleStyle = computed(() => {
return {
fontSize: fontSize.value('lg') + 'px', // text-lg
marginBottom: spacing.value('sm') + 'px' // mb-2
}
})
const descStyle = computed(() => {
return {
fontSize: fontSize.value('base') + 'px' // text-base
}
})
</script>
<style>
.card {
background-color: #fff;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.card-title {
font-weight: bold;
color: #333;
}
.card-desc {
color: #666;
}
</style>
3.2 按钮组件
<template>
<button :style="buttonStyle" class="btn" @click="handleClick">
<text :style="textStyle">{{ text }}</text>
</button>
</template>
<script setup lang="uts">
import { computed } from 'vue'
import { useTheme } from '@/composables/useTheme.uts'
const props = defineProps<{
text: string
size?: 'sm' | 'base' | 'lg'
}>()
const { spacing, borderRadius, fontSize, size } = useTheme()
const buttonStyle = computed(() => {
const buttonSize = props.size ?? 'base'
return {
paddingLeft: spacing.value('lg') + 'px', // px-5
paddingRight: spacing.value('lg') + 'px',
paddingTop: spacing.value('md') + 'px', // py-3
paddingBottom: spacing.value('md') + 'px',
borderRadius: borderRadius.value('base') + 'px', // rounded
minWidth: size.value(buttonSize) + 'px' // w-base
}
})
const textStyle = computed(() => {
const buttonSize = props.size ?? 'base'
return {
fontSize: fontSize.value(buttonSize) + 'px' // text-base
}
})
function handleClick() {
// 处理点击事件
}
</script>
<style>
.btn {
background-color: #007aff;
color: #fff;
border: none;
}
</style>
3.3 列表布局
<template>
<view :style="listStyle" class="list-container">
<view
v-for="item in list"
:key="item.id"
:style="itemStyle"
class="list-item"
>
{{ item.name }}
</view>
</view>
</template>
<script setup lang="uts">
import { computed } from 'vue'
import { useTheme } from '@/composables/useTheme.uts'
const list = ref([
{ id: 1, name: '项目1' },
{ id: 2, name: '项目2' },
{ id: 3, name: '项目3' }
])
const { spacing, borderRadius } = useTheme()
const listStyle = computed(() => {
return {
padding: spacing.value('base') + 'px' // p-4
}
})
const itemStyle = computed(() => {
return {
padding: spacing.value('md') + 'px', // p-3
marginBottom: spacing.value('sm') + 'px', // mb-2
borderRadius: borderRadius.value('base') + 'px' // rounded
}
})
</script>
<style>
.list-container {
display: flex;
flex-direction: column;
}
.list-item {
background-color: #f5f5f5;
}
</style>
四、工具函数封装
4.1 创建 Tailwind 风格工具函数
// utils/theme-utils.uts
import { useTheme } from '@/composables/useTheme.uts'
/**
* 获取 Tailwind 风格的样式对象
*/
export function useTailwindStyles() {
const { spacing, borderRadius, fontSize, size } = useTheme()
return {
// 间距
p: (key: string) => ({ padding: spacing.value(key) + 'px' }),
px: (key: string) => ({
paddingLeft: spacing.value(key) + 'px',
paddingRight: spacing.value(key) + 'px'
}),
py: (key: string) => ({
paddingTop: spacing.value(key) + 'px',
paddingBottom: spacing.value(key) + 'px'
}),
m: (key: string) => ({ margin: spacing.value(key) + 'px' }),
mx: (key: string) => ({
marginLeft: spacing.value(key) + 'px',
marginRight: spacing.value(key) + 'px'
}),
my: (key: string) => ({
marginTop: spacing.value(key) + 'px',
marginBottom: spacing.value(key) + 'px'
}),
// 圆角
rounded: (key: string) => ({ borderRadius: borderRadius.value(key) + 'px' }),
// 字体
text: (key: string) => ({ fontSize: fontSize.value(key) + 'px' }),
// 尺寸
w: (key: string) => ({ width: size.value(key) + 'px' }),
h: (key: string) => ({ height: size.value(key) + 'px' })
}
}
4.2 使用工具函数
<template>
<view :style="{ ...styles.p('base'), ...styles.rounded('lg') }">
<text :style="styles.text('base')">文本</text>
</view>
</template>
<script setup lang="uts">
import { useTailwindStyles } from '@/utils/theme-utils.uts'
const styles = useTailwindStyles()
</script>
五、最佳实践
5.1 组合使用静态类和动态样式
<template>
<!-- 静态类用于布局、颜色等不随主题变化的部分 -->
<view class="flex flex-col bg-white">
<!-- 动态样式用于间距、圆角、字体等随主题变化的部分 -->
<view :style="cardStyle">
<text :style="textStyle">内容</text>
</view>
</view>
</template>
5.2 缓存计算结果
<script setup lang="uts">
import { computed } from 'vue'
import { useTheme } from '@/composables/useTheme.uts'
const { spacing, borderRadius, fontSize } = useTheme()
// 使用 computed 缓存,避免重复计算
const cardStyle = computed(() => ({
padding: spacing.value('base') + 'px',
borderRadius: borderRadius.value('lg') + 'px'
}))
</script>
5.3 提取公共样式对象
<script setup lang="uts">
// 在组件外部定义,避免重复创建
const { spacing, borderRadius, fontSize } = useTheme()
export const commonStyles = {
card: computed(() => ({
padding: spacing.value('base') + 'px',
borderRadius: borderRadius.value('lg') + 'px'
})),
text: computed(() => ({
fontSize: fontSize.value('base') + 'px'
}))
}
</script>
六、注意事项
-
性能优化:使用
computed缓存样式对象,避免重复计算 - 类型安全:使用 UTS 类型定义,确保类型安全
- 主题切换:切换主题后需要重新计算样式,确保响应式更新
- 默认值:为所有主题变量提供合理的默认值
- 测试覆盖:在不同屏幕尺寸设备上测试主题自动切换
文档版本:v1.0
创建日期:2026-01-23