跳至主要内容

stylex.defineVars

创建可以在代码库中的任何位置的 stylex.create 调用中导入和使用的全局 CSS 自定义属性(变量)。

function defineVars<Styles extends {[key: string]: Value}>(
styles: Styles,
): Vars<{[key in keyof Styles]: string}>;

示例用法:

您必须在 .stylex.js(或 .stylex.ts)文件中将变量定义为命名导出。

vars.stylex.js
import * as stylex from '@stylexjs/stylex';

export const colors = stylex.defineVars({
accent: 'blue',
background: 'white',
line: 'gray',
textPrimary: 'black',
textSecondary: '#333',
});

然后,您可以在任何 stylex.create 调用中导入和使用这些变量。

import * as stylex from '@stylexjs/stylex';
import { colors } from './vars.stylex.js';

const styles = stylex.create({
container: {
color: colors.textPrimary,
backgroundColor: colors.background,
},
});