尧图网络科技YAOTU DIGITAL 获取报价
获取报价
首页 / 资讯中心 / 文章详情

StyleX 备忘清单:从编译配置到主题与 TypeScript 类型的完整实战速查

发布时间:2026/9/23 18:08:37

资讯中心
01
ARTICLE

StyleX 备忘清单:从编译配置到主题与 TypeScript 类型的完整实战速查

StyleX 备忘清单:从编译配置到主题与 TypeScript 类型的完整实战速查
文档知识库教程开发工具【免费下载链接】reference为开发人员分享快速参考备忘清单(速查表)项目地址https://gitcode.com/jaywcjlove/reference点击查看免费下载StyleX 是 Facebook 开源的 CSS-in-JS 界面样式系统它把样式定义为普通 JavaScript/TypeScript 对象再通过编译器在构建期提取为静态 CSS从而兼顾开发体验与运行时性能。本篇速查以当前仓库 docs/stylex.md 为核心骨架覆盖 StyleX 的生态工具、Rollup / Babel / Webpack / Next.js 各构建链路配置、样式定义与使用、主题变量系统以及 TypeScript 类型约束让你在 React 等组件化项目中可以直接照着配置和落地。读完你将掌握stylex.create、stylex.props、stylex.defineVars、stylex.createTheme等核心 API 的正确姿势并能配置出可编译、可校验、类型安全的 StyleX 工程。入门 StyleX介绍StyleX 是一个 CSS-in-JS 的用户界面样式系统。与传统的运行时 CSS-in-JS 不同StyleX 的核心思路是样式用对象语法书写由编译器在构建阶段静态分析并提取成独立的 CSS 文件组件在运行时只携带经过哈希的类名因此没有样式计算的运行时开销也天然支持 Tree Shaking。配合编辑器与构建工具可以形成一套完整的工作流StyleX IntellisenseVSCode 插件提供样式属性的智能提示VSCode 插件Vitevite-plugin-stylex 与 vite-plugin-stylex-dev后者面向开发模式Babel 插件tailwind-to-stylex 支持在 Tailwind CSS 中使用 StyleX 语法stylex-extend/babel-plugin 允许直接用 JSX 属性定义 StyleX 样式Prettierprettier-plugin-stylex-key-sort 用于自动排序样式键Bunbun-plugin-stylex 为 Bun 运行时/打包器提供支持入门模板next.js支持 StyleX 的 next.js 项目qwik使用 StyleX 和 tailwind-to-stylex 的 Qwik 项目docusaurus 3支持 StyleX 的 docusaurus 3 项目SvelteKit支持 StyleX 的 SvelteKit 项目。配置编译器任何构建工具集成 StyleX 的核心都是把编译器插件挂到构建管线上让插件在转译 JS 时提取样式。以 Rollup 为例import plg from stylexjs/rollup-plugin; const config () ({ plugins: [ plg({ ...options }) ] }) export default config;使用样式StyleX 的样式声明与使用是分离的先用stylex.create定义样式表再在组件里用stylex.props应用import * as React from react; import * as stylex from stylexjs/stylex; const styles stylex.create({ ... }); const colorStyles stylex.create({ ... });在 React 中使用function ReactDemo( { color,isActive,style } ) { return ( div {...stylex.props( styles.main, // 有条件地应用样式 isActive styles.active, // 根据属性选择样式变体 colorStyles[color], // 将样式作为 props 传递 style, )} / ); }stylex.props接受任意多个样式对象参数自动完成去重、合并与优先级处理并把结果展开为className与style两个属性。上面的写法同时演示了条件样式、变体索引与透传外部样式三种常见用法。定义样式样式是使用对象语法和create()API 定义的import * as stylex from stylexjs/stylex; const styles stylex.create({ root: { width: 100%, maxWidth: 800, minHeight: 40, }, });数字类型的值如800、40会被编译器按 px 处理字符串值则原样保留因此这里的maxWidth最终生成max-width: 800px而width生成width: 100%。安装与编译器配置StyleX 的安装分为运行时包与**编译器构建期插件**两类前者是应用依赖后者按构建工具选择其一或组合使用。原文档给出了从 Rollup 到 Webpack、Next.js 的完整配置这里全部保留并补充参数说明。StyleX 运行时包npm install --save stylexjs/stylex编译器生产Rollupnpm install --save-dev stylexjs/rollup-plugin修改 Rollup 配置rollup.config.jsimport stylexPlugin from stylexjs/rollup-plugin; const config { input: ./index.js, output: { file: ./.build/bundle.js, format: es, }, // 确保在 Babel 之前使用 stylex 插件 plugins: [stylexPlugin({ // 必需项。生成的 CSS 文件的文件路径。 fileName: ./.build/stylex.css, // 默认值false dev: false, // 所有生成的类名的前缀 classNamePrefix: x, // CSS 变量支持所必需 unstable_moduleResolution: { // 类型commonJS | haste // 默认值commonJS type: commonJS, // 项目根目录的绝对路径 rootDir: __dirname, }, })], }; export default config;要点fileName是生产构建唯一必需的选项指定提取出的 CSS 输出位置dev为false表示生产模式编译器会输出最终静态 CSSunstable_moduleResolution用于解析.stylex.js变量文件的模块定位方式若要使用 CSS 变量主题则必须配置。编译器开发Babelnpm install --save-dev stylexjs/babel-plugin修改 Babel 配置babel.config.jsimport styleX from stylexjs/babel-plugin; const config { plugins: [ [styleX, { dev: true, // 设置为 true 以进行快照测试 // 默认值false test: false, // CSS 变量支持所必需 unstable_moduleResolution: { // 类型commonJS | haste // 默认值commonJS type: commonJS, // 项目根目录的绝对路径 rootDir: __dirname, } }], ], }; export default config;开发模式下dev: true插件会在浏览器中注入一个style标签实现 HMR 热更新test: true则适合在快照测试中生成稳定输出。编译器生产Webpacknpm install --save-dev stylexjs/webpack-plugin修改 Webpack 配置webpack.config.jsconst StylexPlugin require(stylexjs/webpack-plugin); const path require(path); const config (env, argv) ({ entry: { main: ./src/index.js, }, output: { path: path.resolve(__dirname, .build), filename: [name].js, }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: babel-loader, }, ], }, plugins: [ // 确保在 Babel 之前使用 stylex 插件 new StylexPlugin({ filename: styles.[contenthash].css, // 获取 webpack 的模式并为开发设置值 dev: argv.mode development, // 使用静态生成的 CSS 文件而不是运行时注入的 CSS。 // 即使在开发环境中也是如此。 runtimeInjection: false, // 可选的。默认值x classNamePrefix: x, // CSS 变量支持所必需 unstable_moduleResolution: { // 类型commonJS | haste // 默认值commonJS type: commonJS, // 项目根目录的绝对路径 rootDir: __dirname, }, }), ], cache: true, }); module.exports config;Webpack 插件按 webpack 的mode自动切换devfilename支持[contenthash]占位符以做长效缓存runtimeInjection: false强制使用静态 CSS 文件而不是运行时注入即使在开发环境也如此。编译器生产Next.jsnpm install --save-dev stylexjs/nextjs-plugin \ stylexjs/babel-plugin rimraf在package.json添加配置{ scripts: { ..., predev: rimraf .next, prebuild: rimraf .next } }修改 Babel 配置.babelrc.jsmodule.exports { presets: [next/babel], plugins: [ [ stylexjs/babel-plugin, { dev: process.env.NODE_ENV development, test: process.env.NODE_ENV test, runtimeInjection: false, genConditionalClasses: true, treeshakeCompensation: true, unstable_moduleResolution: { type: commonJS, rootDir: __dirname, }, }, ], ], };修改 Next.js 配置next.config.mjs/** type {import(next).NextConfig} */ import stylexPlugin from stylexjs/nextjs-plugin; const nextConfig {}; const __dirname new URL(., import.meta.url).pathname; export default stylexPlugin({ rootDir: __dirname, })(nextConfig);Next.js 集成点genConditionalClasses为条件样式生成独立类名以配合:hover等场景treeshakeCompensation补偿编译后的 Tree Shaking 效果predev/prebuild中rimraf .next保证每次构建前清理缓存目录避免陈旧样式残留。仅限本地开发要开始使用 StyleX 而无需配置编译器和构建过程可以安装本地开发运行时npm install --save-dev stylexjs/dev-runtime开发运行时必须导入到应用程序的JavaScript入口点并进行配置import inject from stylexjs/dev-runtime; inject({ classNamePrefix: x, dev: true, test: false, });stylexjs/dev-runtime在浏览器中模拟编译器的运行时行为适合原型验证或纯客户端调试但它不是生产方案——生产构建仍应使用正式的编译器插件输出静态 CSS。用 ESLint 捕捉错误npm install --save-dev stylexjs/eslint-pluginStyleX 编译器不会验证你的样式并且会编译许多无效样式。当你创作样式时应该使用 ESLint 插件来捕获这些错误。修改 ESLint 配置.eslintrc.jsmodule.exports { plugins: [stylexjs], rules: { stylexjs/valid-styles: error, }, };把stylexjs/valid-styles设为error后诸如重复键、非法值等样式书写错误会在 lint 阶段直接暴露而不是等编译产物出错。定义样式创建样式stylex.create接受一个对象键是样式名值是该样式名的属性集合import * as stylex from stylexjs/stylex; const styles stylex.create({ base: { fontSize: 16, lineHeight: 1.5, color: rgb(60,60,60), }, highlighted: { color: rebeccapurple, }, });伪类先看一个朴素版本——样式名为button的背景样式import * as stylex from stylexjs/stylex; const styles stylex.create({ button: { backgroundColor: lightblue, }, });在该样式的:hover和:active伪类上分别设置值import * as stylex from stylexjs/stylex; const styles stylex.create({ button: { backgroundColor: { default: lightblue, :hover: blue, :active: darkblue, }, }, });写法上把属性值从标量改成对象其中default是默认值其余键是伪类/条件选择器。编译器会按需为不同条件生成独立的类避免在 JS 里手写事件切换样式。伪元素import * as stylex from stylexjs/stylex; const styles stylex.create({ input: { // 伪元素 ::placeholder: { color: #999, }, color: { default: #333, // 伪类 :invalid: red, }, }, });伪元素如::placeholder作为普通属性名书写值为一个嵌套对象同时同一属性color也可以与伪类:invalid组合出条件值。媒体查询和其他 规则import * as stylex from stylexjs/stylex; const styles stylex.create({ base: { width: { default: 800, media (max-width: 800px): 100%, media (min-width: 1540px): 1366, }, }, });同样媒体查询也可以作为样式值中的“条件”。这里width在默认 800px、窄屏 100%、宽屏 1366px 之间按媒体查询切换全部由编译器产出为静态 CSS 规则。组合条件当需要组合媒体查询和伪选择器时嵌套超过“一层”import * as stylex from stylexjs/stylex; const styles stylex.create({ button: { color: { default: var(--blue-link), :hover: { default: null, media (hover: hover): scale(1.1), }, :active: scale(0.9), }, }, });:hover分支内部再嵌套media (hover: hover)实现“支持悬停的设备上才放大”内层default: null表示悬停时默认不改变回退到外层默认值。后备样式CSS 中常用多行声明实现特性回退.header { position: fixed; position: -webkit-sticky; position: sticky; }使用firstThatWorks函数来实现相同的目的import * as stylex from stylexjs/stylex; const styles stylex.create({ header: { position: stylex.firstThatWorks(sticky, -webkit-sticky, fixed), }, });stylex.firstThatWorks按浏览器支持顺序依次生成回退声明支持sticky用sticky否则退回-webkit-sticky再不济使用fixed编译器自动输出多条降级规则。关键帧动画使用stylex.keyframes()函数来定义关键帧动画import * as stylex from stylexjs/stylex; const fadeIn stylex.keyframes({ from: {opacity: 0}, to: {opacity: 1}, }); const styles stylex.create({ base: { animationName: fadeIn, animationDuration: 1s, }, });keyframes返回的动画名可以像普通值一样赋给animationName编译器负责生成keyframes规则并处理哈希命名。动态样式import * as stylex from stylexjs/stylex; const styles stylex.create({ // 函数参数必须是简单标识符 // -- 不允许解构或默认值 bar: (height) ({ height, // 函数体必须是对象字面量 // -- 不允许使用 { return {} } }), }); function MyComponent() { // height 的值在编译时不能确定。 const [height, setHeight] useState(10); return div {...stylex.props(styles.bar(height))} /; }注意动态样式是一项高级功能应谨慎使用。对于大多数用例条件样式应该足够了。它的限制很严格参数必须是简单标识符不允许解构或默认值函数体必须是对象字面量不允许{ return {} }形式因为编译器需要静态分析整个调用关系。使用样式合并样式import * as stylex from stylexjs/stylex; const styles stylex.create({ base: { fontSize: 16, lineHeight: 1.5, color: grey, }, highlighted: { color: rebeccapurple, }, }); div {...stylex.props( styles.base, styles.highlighted )} /;当多个样式包含同一属性时后出现的样式优先生效——上面的组合里color取rebeccapurple。如果样式的顺序颠倒文本将为灰色div {...stylex.props( styles.highlighted, styles.base )} /条件样式div {...stylex.props( styles.base, props.isHighlighted styles.highlighted, isActive ? styles.active : styles.inactive, )} /通过使用常见的 JavaScript 模式例如三元表达式和运算符可以在运行时有条件地应用样式。stylex.props忽略虚假值例如null、undefined或false——这保证了条件分支可以放心地内联在参数列表里。样式变体import * as stylex from stylexjs/stylex; const styles stylex.create({ violet: { backgroundColor: { default: blueviolet, :hover: darkviolet, }, color: white, }, gray: { backgroundColor: { default: gainsboro, :hover: lightgray, }, }, // ... more variants here ... });然后通过使用variant属性作为样式对象上的键来应用适当的样式function Button({variant, ...props}) { return ( button {...props} {...stylex.props(styles[variant])} / ); }变体模式把“一组互斥的外观”建模为样式表里的并列键调用方只需传入variant名即可索引到对应样式非常适合 Button、Badge 这类多形态组件。样式作为道具CustomComponent style{styles.base} /stylex.props函数返回具有className和style的对象。当样式要合并到组件内时不要这样用// ❌ 不要这样使用! ⚠️ CustomComponent style{stylex.props(styles.base)} /正确做法是把原始样式数组直接传给stylepropCustomComponent style{[ styles.base, isHighlighted styles.highlighted ]} /原因在于styleprop 约定接收的是“样式对象的数组”或单个样式对象而stylex.props(...)返回的是已处理的 DOM 属性对象两者协议不同混用会导致样式丢失或结构错误。接受组件中的样式import * as stylex from stylexjs/stylex; // Local Styles const styles stylex.create({ base: { /*...*/ }, }); function CustomComponent({style}) { return ( div {...stylex.props(styles.base, style)} / ); }将其与stylex.props函数一起应用组件在自己的本地样式之后拼接调用方传入的style因此外部样式可以覆盖内部默认值这是实现可定制组件的标准手法。“取消设置”样式import * as stylex from stylexjs/stylex; const styles stylex.create({ base: { color: null, }, });将样式属性设置为null会删除 StyleX 之前为其应用的任何样式。这在“重置/继承”场景中很有用——例如把color: null传给某个元素后该元素不再受上游 StyleX 颜色的影响。主题StyleX 的主题建立在 CSS 变量自定义属性之上先用defineVars声明变量组再用createTheme为变量组创建具体主题。使用媒体查询import * as stylex from stylexjs/stylex; // 可以使用常量来避免重复媒体查询 const DARK media (prefers-color-scheme: dark); export const colors stylex.defineVars({ primaryText: {default: black, [DARK]: white}, secondaryText: {default: #333, [DARK]: #ccc}, accent: {default: blue, [DARK]: lightblue}, background: {default: white, [DARK]: black}, lineColor: {default: gray, [DARK]: lightgray}, });把媒体查询提取为常量DARK并用计算属性[DARK]作为键可以避免在每个变量上重复书写长字符串同时变量值天然支持按prefers-color-scheme自动切换深浅色。定义变量import * as stylex from stylexjs/stylex; export const tokens stylex.defineVars({ primaryText: black, secondaryText: #333, borderRadius: 4px, fontFamily: system-ui, sans-serif, fontSize: 16px, });这定义了 HTML 文档的:root处的变量。它们可以作为常量导入并在stylex.create调用中使用。使用变量import * as stylex from stylexjs/stylex; // 可以使用常量来避免重复媒体查询 const DARK media (prefers-color-scheme: dark); export const colors stylex.defineVars({ primaryText: {default: black, [DARK]: white}, secondaryText: {default: #333, [DARK]: #ccc}, accent: {default: blue, [DARK]: lightblue}, background: {default: white, [DARK]: black}, lineColor: {default: gray, [DARK]: lightgray}, }); export const spacing stylex.defineVars({ none: 0px, xsmall: 4px, small: 8px, medium: 12px, large: 20px, xlarge: 32px, xxlarge: 48px, xxxlarge: 96px, });然后就可以像这样导入和使用这些样式import * as stylex from stylexjs/stylex; import {colors, spacing} from ../tokens.stylex; const styles stylex.create({ container: { color: colors.primaryText, backgroundColor: colors.background, padding: spacing.medium, }, });变量在这里是“引用”而非字符串拼接编译器会把它们解析为var(--...)引用从而让主题切换得以生效。定义变量时的规则变量必须定义在.stylex.js文件中变量必须位于具有以下扩展名之一的文件中.stylex.js.stylex.mjs.stylex.cjs.stylex.ts.stylex.tsx.stylex.jsx变量必须命名为exports即必须使用命名导出// ✅ - 命名导出 export const colors stylex.defineVars({ /* ... */ }); const sizeVars { ... }; // ✅ - 另一个命名导出 export const sizes stylex.defineVars(sizeVars);不允许// ❌ - 只允许命名导出 export default stylex.defineVars({ /* ... */ }); // ❌ - 变量必须直接导出 const x stylex.defineVars({ /* ... */ }); export const colors x; // ❌ - 变量不能嵌套在另一个对象内部 export const colors { foregrounds: stylex.defineVars({ /* ... */ }), backgrounds: stylex.defineVars({ /* ... */ }), };这些约束都是为了让编译器能静态解析变量引用只有命名导出、直接导出的defineVars结果才可以在模块解析unstable_moduleResolution中被稳定定位。创建主题import * as stylex from stylexjs/stylex; import {colors, spacing} from ./tokens.stylex; // 可以使用常量来避免重复媒体查询 const DARK media (prefers-color-scheme: dark); // Dracula 主题 export const dracula stylex.createTheme(colors, { primaryText: {default: purple, [DARK]: lightpurple}, secondaryText: {default: pink, [DARK]: hotpink}, accent: red, background: {default: #555, [DARK]: black}, lineColor: red, });应用主题主题对象类似于使用stylex.create()创建的样式对象。使用stylex.props()将它们应用于元素以覆盖该元素及其所有后代的变量div {...stylex.props(dracula, styles.container)} {children} /div;主题使用注意点创建主题时必须覆盖变量组中的所有变量。这一选择是为了帮助发现意外遗漏主题可以在代码库中的任何位置使用stylex.createTheme()创建并在文件或组件之间传递如果同一变量组的多个主题应用于同一 HTML 元素则最后应用的主题获胜。变量类型import * as stylex from stylexjs/stylex; export const tokens stylex.defineVars({ primaryTxt: stylex.types.color(black), secondaryTxt: stylex.types.color(#333), borderRadius: stylex.types.length(4px), angle: stylex.types.angle(0deg), int: stylex.types.integer(2), });所有值都可以是任意字符串。要将类型分配给变量可以使用适当的类型函数包装它们color、length、angle、integer等即 StyleX 内置的类型构造器。变量类型 条件值/// tokens.stylex.js import * as stylex from stylexjs/stylex; export const colors stylex.defineVars({ primaryText: stylex.types.color({ default: black, [DARK]: white }), });用法保持不变以上内容完全有效——类型函数同样接受“条件值对象”作为参数即类型标注与default/[DARK]分支可以共存。源代码中的类型安全// tokens.stylex.js import * as stylex from stylexjs/stylex; import {tokens} from ./tokens.stylex.js; export const high stylex.defineVars({ primaryTxt: stylex.types.color(black), secondaryTxt: stylex.types.color(#222), borderRadius: stylex.types.length(8px), angle: stylex.types.angle(0deg), int: stylex.types.integer(4), });当在stylex.defineVars中使用特定类型声明变量时静态类型将强制在stylex.createTheme调用中为该变量设置主题时使用相同类型的函数——例如primaryTxt声明为color后主题里就不能赋一个数字或非法颜色字符串。动画渐变import * as stylex from stylexjs/stylex; import {tokens} from ./tokens.stylex; const rotate stylex.keyframes({ from: { [tokens.angle]: 0deg }, to: { [tokens.angle]: 360deg }, }); const styles stylex.create({ gradient: { backgroundImage: conic-gradient(from ${tokens.angle}, ...colors), animationName: rotate, animationDuration: 10s, animationTimingFunction: linear, animationIterationCount: infinite, }, })可以通过对其中使用的角度进行动画处理来对渐变进行动画处理tokens.angle同时出现在conic-gradient(from ...)与keyframes的from/to中随着变量从0deg动画到360deg渐变整体旋转且全程无需运行时 JS 参与。模拟 round()现代浏览器开始支持 CSS 中的round()函数。在尚未普及的环境中可以通过一个整数类型的变量来模拟const styles stylex.create({ gradient: { // Math.floor [tokens.int]: calc(16 / 9) // Math.round [tokens.int]: calc((16 / 9) 0.5) }, })利用整数类型变量会做取整的特性calc(16 / 9)得到向下取整结果对应Math.floor而calc((16 / 9) 0.5)等价于四舍五入对应Math.round。注意上面同一键出现了两次实际使用时按需保留其中一种写法。TypeScript 类型StyleX 提供了丰富的类型工具让样式 API 的边界在编译期即可被检查。StyleXStyles 样式 props 类型import type {StyleXStyles} from stylexjs/stylex; import * as stylex from stylexjs/stylex; type Props { ... style?: StyleXStyles, }; function MyComponent( { style, ...otherProps }: Props ) { return ( div {...stylex.props( localStyles.foo, localStyles.bar, style )} {/* ... */} /div ); }StyleXStyles是最常用的对外样式类型表示“任意合法的 StyleX 样式对象”可赋值为单个样式或样式数组。StyleXStylesWithout 禁止属性import type {StyleXStylesWithout} from stylexjs/stylex; import * as stylex from stylexjs/stylex; type NoLayout StyleXStylesWithout{ position: unknown, display: unknown, top: unknown, start: unknown, end: unknown, bottom: unknown, border: unknown, borderWidth: unknown, borderBottomWidth: unknown, borderEndWidth: unknown, borderStartWidth: unknown, borderTopWidth: unknown, margin: unknown, marginBottom: unknown, marginEnd: unknown, marginStart: unknown, marginTop: unknown, padding: unknown, paddingBottom: unknown, paddingEnd: unknown, paddingStart: unknown, paddingTop: unknown, width: unknown, height: unknown, flexBasis: unknown, overflow: unknown, overflowX: unknown, overflowY: unknown, }; type Props { // ... style?: NoLayout, }; function MyComponent({style, ...}: Props) { return ( div {...stylex.props(localStyles.foo, localStyles.bar, style)} {/* ... */} /div ); }此处对象类型中列出的属性将被禁止但所有其他样式仍将被接受。StyleXStylesWithout非常适合“不允许调用方传入影响布局的属性”这类场景例如列表项组件禁止外部修改尺寸与定位。从一组样式属性中接受import type {StyleXStyles} from stylexjs/stylex; type Props { // ... style?: StyleXStyles{ color?: string; backgroundColor?: string; borderColor?: string; borderTopColor?: string; borderEndColor?: string; borderBottomColor?: string; borderStartColor?: string; }; };泛型参数StyleXStyles{...}表示“仅接受列出的这些属性”其余属性在类型层面被拒绝。上面的例子把对外可定制范围收窄到与颜色相关的一组属性。限制样式的可能值import type {StyleXStyles} from stylexjs/stylex; type Props { ... // 只接受 marginTop 的样式其他不接受。 // marginTop 的值只能是 0、4、8 或 16。 style?: StyleXStyles{ marginTop: 0 | 4 | 8 | 16 }, };更进一步可以联合类型限定属性值域例如marginTop只允许0 | 4 | 8 | 16配合设计系统的间距刻度使用非常顺手。VarGroupimport * as stylex from stylexjs/stylex; export const vars stylex.defineVars({ color: red, backgroundColor: blue, }); export type Vars typeof vars; /* Vars VarGroup{ color: string, backgroundColor: string, } */VarGroup 是调用stylex.defineVars生成的对象的类型。它将键映射到 CSS 自定义属性的引用。通常用typeof vars即可得到对应变量的 VarGroup 类型供主题与组件类型复用。StaticStylesimport type {StaticStyles} from stylexjs/stylex; type Props { // ... style?: StaticStyles{ color?: red | blue | green; padding?: 0 | 4 | 8 | 16 | 32; backgroundColor?: string; borderColor?: string; borderTopColor?: string; borderEndColor?: string; borderBottomColor?: string; borderStartColor?: string; }; };不允许使用函数定义的动态样式。StaticStyles与StyleXStyles的区别在于它只接受静态样式对象凡是函数式动态样式在类型上都会被拒绝。Themeimport type {VarGroup} from stylexjs/stylex; import * as stylex from stylexjs/stylex; import {vars} from ./vars.stylex; export const theme: Themetypeof vars stylex.createTheme(vars, { color: red, // OK backgroundColor: blue, // OK });Themetypeof vars声明了该主题的变量组类型createTheme的第二个参数会在编译期校验必须覆盖vars中的全部变量且每个值符合对应变量的类型约束。结语这份速查覆盖了 StyleX 从“选型安装”到“类型约束”的完整链路构建层面有 Rollup / Babel / Webpack / Next.js 四套编译配置与 dev-runtime、ESLint 校验兜底写法层面有伪类、伪元素、媒体查询、组合条件、后备样式、关键帧与动态样式工程化层面有基于 CSS 变量的主题系统与一整套 TypeScript 类型工具。核心心法可以概括为三点样式对象化书写、编译器提取静态 CSS、主题建立在 CSS 变量之上。把 docs/stylex.md 中的示例按自己的构建工具落地后即可在 React 项目中享受接近原生 CSS 的性能与类型安全的样式开发体验若需与其他技术栈交叉参考可配合仓库中的 React、Tailwind CSS 等速查表一起查阅。赞分享文档知识库教程开发工具【免费下载链接】reference为开发人员分享快速参考备忘清单(速查表)项目地址https://gitcode.com/jaywcjlove/reference点击查看免费下载相关推荐StyleX 速查指南从编译器配置、样式定义到主题系统与类型安全的完整实践StyleX 速查指南从编译器配置、样式定义到主题系统与类型安全的完整实践 StyleX 是 Facebook 开源的一款 CSS in JS 用户界面样式系文档教程TypeScript 完整速查清单从 Interface 到实用类型、控制流分析与 TSConfig 配置TypeScript 完整速查清单从 Interface 到实用类型、控制流分析与 TSConfig 配置 本篇技术指南以当前开源仓库面向开发者的技术速查清文档教程GraphQL 速查表从 Schema 定义到查询与变更的完整实战指南Quick Reference 备忘清单GraphQL 速查表从 Schema 定义到查询与变更的完整实战指南Quick Reference 备忘清单 这份备忘清单面向需要快速上手或随时翻阅 G文档知识库教程开发工具创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
02
RELATED NEWS

相关资讯

更多网站建设与数字化升级内容

03
WHY YAOTU

想打造同款高转化官网?

懂行业、懂生意,从建站到增长一站式陪跑

场景化定制

不做模板站,围绕你的业务场景量身设计,小众不撞款。

营销型架构

以转化目标组织内容与路径,让官网真正带来询盘。

全周期服务

设计、开发、运营、运维一体,上线只是开始。

免费获取你的建站方案

留下需求,专属顾问 24 小时内为你输出方案建议。