@@ -8,12 +8,14 @@ description: '基于 packages-runtime 的跨端示例:uni-app + Web、Taro + W
88
99## Demo 1:uni-app + Web(Vue + TS)
1010
11+ 同一份 ` tv ` 实例即可覆盖小程序与 H5,通过环境变量决定是否关闭 escape/unescape。
12+
1113### 共享变体(` src/ui/button.variants.ts ` )
1214
1315``` ts
14- import { tv , create } from ' @weapp-tailwindcss/variants'
16+ import { create } from ' @weapp-tailwindcss/variants'
1517
16- type ButtonConfig = Parameters <typeof tv >[0 ]
18+ type ButtonConfig = Parameters <ReturnType < typeof create >[ ' tv ' ] >[0 ]
1719
1820const config: ButtonConfig = {
1921 base: ' inline-flex items-center justify-center rounded-md px-3 py-2 text-sm font-medium' ,
@@ -33,14 +35,18 @@ const config: ButtonConfig = {
3335 },
3436}
3537
36- let tvRuntime = tv
37-
38+ // 跨平台框架请自行判断是否为 H5 构建,通常可从环境变量读取。
39+ let isH5 = false
3840// #ifdef H5
39- const { tv : tvWeb } = create ({ escape: false , unescape: false })
40- tvRuntime = tvWeb
41+ isH5 = true
4142// #endif
4243
43- export const button = tvRuntime (config )
44+ const { tv } = create ({
45+ escape: ! isH5 ,
46+ unescape: ! isH5 ,
47+ })
48+
49+ export const button = tv (config )
4450```
4551
4652### 小程序 / H5 共用页面(` src/pages/index/index.vue ` )
@@ -65,12 +71,14 @@ import { button } from '@/ui/button.variants'
6571
6672## Demo 2:Taro + Web(React + TSX)
6773
74+ 同一份 ` tv ` 实例即可覆盖小程序与 H5,通过环境变量决定是否关闭 escape/unescape。
75+
6876### 共享变体(` src/ui/card.variants.ts ` )
6977
7078``` ts
71- import { tv , create } from ' @weapp-tailwindcss/variants'
79+ import { create } from ' @weapp-tailwindcss/variants'
7280
73- type CardConfig = Parameters <typeof tv >[0 ]
81+ type CardConfig = Parameters <ReturnType < typeof create >[ ' tv ' ] >[0 ]
7482
7583const config: CardConfig = {
7684 base: ' rounded-xl border border-[#E2E8F0] bg-white p-4 shadow-sm' ,
@@ -85,11 +93,14 @@ const config: CardConfig = {
8593 },
8694}
8795
96+ // 跨平台框架请自行判断是否为 H5 构建,通常可从环境变量读取。
8897const isH5 = process .env .TARO_ENV === ' h5'
89- const { tv : tvWeb } = create ({ escape: false , unescape: false })
90- const tvRuntime = isH5 ? tvWeb : tv
98+ const { tv } = create ({
99+ escape: ! isH5 ,
100+ unescape: ! isH5 ,
101+ })
91102
92- export const card = tvRuntime (config )
103+ export const card = tv (config )
93104```
94105
95106### 小程序页面(` src/pages/index/index.tsx ` )
0 commit comments