@hone-ui/sx

中文文档
0.2.0发布于 2026/11/01MITTypeScriptES Module

Build plugin that compiles the sx prop to static CSS.

原始使用文档
文档目录4 节
  1. Setup
  2. Options
  3. What compiles
  4. Matching your theme

@hone-ui/sx

Build plugin that compiles the sx prop to static CSS, so no styling code runs in the browser.

<Box sx={{ p: 2, color: 'primary.main', '&:hover': { opacity: 0.8 } }} />

becomes a class backed by a generated stylesheet:

.hn-sx-k3J9aQ2x {
  padding: calc(2 * var(--hn-spacing));
  color: var(--hn-palette-primary-main);
}
.hn-sx-k3J9aQ2x:hover {
  opacity: 0.8;
}

Setup

Vite:

// vite.config.ts
import react from '@vitejs/plugin-react';
import sx from '@hone-ui/sx/vite';
import { defineConfig } from 'vite';

// List sx() before react(), and keep react() so Fast Refresh works.
export default defineConfig({
  plugins: [sx(), react()],
});

Delete the Vite template's src/index.css and App.css, or put them in an earlier layer. Their unlayered rules override the library.

Next.js, with Turbopack or webpack:

// next.config.ts
import { withSx } from '@hone-ui/sx/next';

// Pass your existing config object, such as create-next-app's, instead of {}.
// optimizePackageImports keeps `next build --webpack` from shipping every component.
export default withSx({
  experimental: { optimizePackageImports: ['@hone-ui/react'] },
});

Import app/globals.css before the look CSS in app/layout.tsx, and put its resets in @layer reset, hone-ui;. Unlayered CSS beats the library and sx.

Rollup, esbuild and webpack use @hone-ui/sx/rollup, @hone-ui/sx/esbuild and @hone-ui/sx/webpack the same way as Vite.

Options

OptionDefaultPurpose
breakpoints{ values: { xs: 0, sm: 600, md: 900, lg: 1200, xl: 1536 } }Breakpoint names and widths, in the shape createTheme takes (values, unit). Share one object with your theme.
palette{}Your theme's palette object. Each object-valued key, such as neutral: { main }, is a palette key. Share one object with your theme.
paletteKeys[]Extra palette keys, such as ['brand'], so 'brand.500' resolves to a theme variable. main, light, dark and contrastText work for any key without it. Merged with palette.
spacingnoneYour theme's spacing array, such as [0, 4, 8, 16], so p: 3 compiles to 16px.
importSources['@hone-ui/react']Packages whose components take sx. Components from local paths and the same file are always compiled.
include/\.[jt]sx$/Files to transform.
cacheDir'node_modules/.cache/hone-ui-sx'Where generated CSS files go.
editortrueIn dev, stamps components with data-hn-source for the visual editor. Production ignores it.

What compiles

Static values, theme values, breakpoint objects and arrays, nested selectors, @media, @container and @supports blocks all compile to CSS. Values that change at runtime, such as width: size, compile to a CSS variable that the component sets. Conditional entries, such as sx={[base, active && { color: 'red' }]}, compile to one class per combination, up to three conditions. Members of const style objects in the same file, such as sx={styles.root}, compile too.

The plugin compiles sx on components from @hone-ui/react, on components from your own files, and on wrappers of those such as memo(Box). Another library's component, even wrapped in styled(...) or memo(...), keeps its sx untouched.

A function of the theme, such as sx={(theme) => ({ zIndex: theme.zIndex.drawer + 1 })}, needs the theme at runtime. The plugin leaves it alone, without a warning, and the component calls it with the current theme. In an array, entries before the first function compile. The function and every entry after it stay inline, so later entries still override it. A function result becomes inline styles, so it can't hold selectors or media queries. The plugin can't tell Server Components apart, so it doesn't check where a function sits; write function sx in a file marked 'use client'.

The plugin warns with the file and line when it can't compile an sx prop: getters and methods, spreads it can't read, a runtime typography value, or more than three conditions. Those props fall back to inline styles, which can't hold selectors or media queries.

Matching your theme

Media queries and spacing arrays can't read CSS variables, so the plugin needs the theme values that live outside them. Pass the same values your createTheme call uses:

sx({
  breakpoints, // the object you pass to createTheme
  palette, // the palette you pass to createTheme; its object keys count as palette keys
  spacing: [0, 4, 8, 16, 32],
  paletteKeys: ['brand'],
});

A numeric spacing needs nothing: factors multiply --hn-spacing, which ThemeProvider sets. A function scale can't be passed to the plugin. Write it out as an array.

版本与发布记录

@hone-ui/sx 共发布 5 个版本,首次发布于 2026/10/22;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.2.0latest2026/11/012 天前
0.0.0-canary-20261101045435canary2026/11/012 天前
0.1.02026/10/2410 天前
0.0.0-canary-202610221552062026/10/2211 天前
0.0.0-stage2026/10/2211 天前

关于 @hone-ui/sx 的常见问题

@hone-ui/sx 是什么?

构建插件,编译成静态的CSS。

如何安装 @hone-ui/sx?

在项目目录运行 npm install @hone-ui/sx,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@hone-ui/react(^0.2.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@hone-ui/sx 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@hone-ui/sx 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@hone-ui/sx 可以商用吗?

@hone-ui/sx 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@hone-ui/sx 还在维护吗?

最新版本 0.2.0 发布于 2026/11/01(2 天前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/10/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。