THEME_CSS_VARIABLES_EXAMPLE.md 4.32 KB

CSS 变量主题系统使用示例

一、在 App.uvue 中初始化

<template>
    <!-- 在根元素上绑定 CSS 变量 -->
    <view :style="themeVars">
        <!-- 应用内容会通过 pages.json 自动加载 -->
    </view>
</template>

<script setup lang="uts">
import { ref, onMounted } from 'vue'
import { useThemeVariables } from '@/composables/useThemeVariables.uts'

const { themeVars } = useThemeVariables()
</script>

<style>
/* 引入主题 CSS 变量定义 */
@import '@/theme/theme-variables.css';
</style>

注意:如果 App.uvue 没有根 view,可以在每个页面的根元素上设置。

二、在页面中使用

方式 1:直接在页面根元素设置(推荐)

<template>
    <view :style="themeVars" class="page">
        <view class="page-padding">
            <view class="card-radius">
                <text class="text-base">响应式文本</text>
            </view>
        </view>
    </view>
</template>

<script setup lang="uts">
import { useThemeVariables } from '@/composables/useThemeVariables.uts'

const { themeVars } = useThemeVariables()
</script>

<style>
@import '@/theme/theme-variables.css';

.page {
    display: flex;
    flex-direction: column;
}

.card-radius {
    background-color: #fff;
    padding: var(--p-base);
}
</style>

方式 2:使用全局 CSS 变量(如果已在 App.uvue 设置)

<template>
    <view class="page">
        <view class="page-padding">
            <view class="card-radius">
                <text class="text-base">响应式文本</text>
            </view>
        </view>
    </view>
</template>

<script setup lang="uts">
// 无需任何 JavaScript 代码!
// CSS 变量已在 App.uvue 中设置
</script>

<style>
@import '@/theme/theme-variables.css';

.page {
    display: flex;
    flex-direction: column;
}

.card-radius {
    background-color: #fff;
    padding: var(--p-base);
}
</style>

三、完整示例

卡片组件

<template>
    <view class="card">
        <text class="card-title">{{ title }}</text>
        <text class="card-desc">{{ desc }}</text>
    </view>
</template>

<script setup lang="uts">
defineProps<{
    title: string
    desc: string
}>()
</script>

<style>
@import '@/theme/theme-variables.css';

.card {
    padding: var(--p-base);
    border-radius: var(--r-lg);
    background-color: #fff;
    margin-bottom: var(--p-md);
}

.card-title {
    font-size: var(--t-lg);
    margin-bottom: var(--p-sm);
    color: #333;
}

.card-desc {
    font-size: var(--t-base);
    color: #666;
}
</style>

按钮组件

<template>
    <button :style="buttonStyle" class="btn">
        <text :style="textStyle">{{ text }}</text>
    </button>
</template>

<script setup lang="uts">
import { computed } from 'vue'

const props = defineProps<{
    text: string
    size?: 'sm' | 'base' | 'lg'
}>()

const buttonStyle = computed(() => {
    const size = props.size ?? 'base'
    return {
        paddingLeft: `var(--p-${size})`,
        paddingRight: `var(--p-${size})`,
        paddingTop: 'var(--p-md)',
        paddingBottom: 'var(--p-md)',
        borderRadius: 'var(--r-base)'
    }
})

const textStyle = computed(() => {
    const size = props.size ?? 'base'
    return {
        fontSize: `var(--t-${size})`
    }
})
</script>

<style>
.btn {
    background-color: #007aff;
    color: #fff;
    border: none;
}
</style>

四、CSS 变量命名规范

变量前缀 含义 示例
--p-* 间距(padding/margin) --p-base, --p-lg
--r-* 圆角(radius) --r-lg, --r-full
--t-* 字体大小(text) --t-base, --t-lg
--s-* 尺寸(size) --s-base, --s-lg

五、优势对比

CSS 变量方式 vs 主题化类名方式

特性 CSS 变量 主题化类名
代码量 少(只需定义一次) 多(每个主题一套类)
性能 好(CSS 变量) 好(CSS 类)
灵活性 高(可任意组合) 中(需要预定义类)
学习成本 低(标准 CSS) 中(需要了解工具函数)
推荐度 ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐

六、最佳实践

  1. 在 App.uvue 中统一设置:如果可能,在 App.uvue 的根元素上设置 CSS 变量,所有页面自动继承
  2. 使用语义化变量名:--p-page 比 --p-base 更语义化
  3. 组合使用:CSS 变量 + 静态类名(如 flex, items-center)
  4. 类型安全:使用 TypeScript/UTS 类型定义,避免拼写错误

文档版本:v1.0
创建日期:2026-01-23