@gentleduck/variants

中文文档
1.0.0发布于 2026/05/30MITTypeScriptES Module零依赖近 30 天下载 3066

A package for creating variants of components, providing a simple and efficient way to create variants of components.

原始使用文档
文档目录7 节
  1. Quick start
  2. Features
  3. Benchmarks
  4. Running
  5. How to read the numbers
  6. Docs
  7. License

@gentleduck/variants

@gentleduck/variants

Type-safe component variants with `cva()`.

MIT - Changelog - Contributing - Docs

npm downloads MIT


Type-safe component variants with cva().

Define variant-driven class names with full TypeScript inference, compound variants, and default values. Zero dependencies.

Quick start

npm install @gentleduck/variants
import { cva, type VariantProps } from '@gentleduck/variants'

const button = cva('px-4 py-2 rounded font-medium', {
  variants: {
    intent: {
      primary: 'bg-blue-600 text-white',
      secondary: 'bg-gray-100 text-gray-800',
    },
    size: {
      sm: 'text-sm px-3 py-1',
      md: 'text-base px-4 py-2',
      lg: 'text-lg px-6 py-3',
    },
  },
  defaultVariants: {
    intent: 'primary',
    size: 'md',
  },
  compoundVariants: [
    { intent: 'primary', size: 'lg', className: 'uppercase tracking-wide' },
  ],
})

button()                          // default: primary + md
button({ intent: 'secondary' })  // secondary + md
button({ size: 'lg' })           // primary + lg + compound applied

type ButtonVariants = VariantProps<typeof button>

Features

  • Compound variants -- apply classes when multiple variant values match
  • Default variants -- fallback values when no prop is passed
  • Type inference -- VariantProps<typeof fn> extracts variant props for component interfaces
  • Composable -- pass a cva() result as the base of another cva() call
  • Arrays and objects -- nested class arrays and conditional objects supported
  • Zero dependencies -- no runtime deps, "sideEffects": false

Benchmarks

This package ships a benchmark script that compares @gentleduck/variants against class-variance-authority, tailwind-variants, and clsx across three axes:

  • Bundle size (gzipped): shipped (raw file npm serves), fullApi (bundle export *, minified), realImport (bundle the main export with a side-effect sink, minified — what an app actually pays).
  • Runtime (ns/op): four scenarios (defaults, one variant, variant + size, with className merge), each in warm mode (same props reused — favors memoizing libraries) and cold mode (unique className per call — defeats memoization). Reports median, p95, p99, stddev, and relative margin of error.
  • Features: self-reported capability matrix.

Running

# from this package
bun run benchmark

# from the monorepo root
bun run benchmark:variants

Output is written to public/benchmarks/results.json and mirrored to apps/duck-ui-docs/public/data/benchmarks/variants.json. Each JSON includes the host environment (CPU, node, bun, platform, git commit), wall-clock durations, and a reproCommand string for provenance.

How to read the numbers

Memoization is a first-class feature of this library, so the warm mode reflects its best case. The cold mode is reported alongside to show the non-memoized cost honestly — that's a fairer number when comparing to libraries that don't memoize. Bundle sizes are static; runtime numbers depend on the machine and should be interpreted relative to the other libraries in the same run, not as absolute constants.

Docs

gentleduck.org

License

MIT

版本与发布记录

@gentleduck/variants 共发布 15 个版本,首次发布于 2025/08/05;过去 12 个月发布了 8 个版本。

版本发布日期距今
1.0.0latest2026/05/305 个月前
0.1.242026/05/205 个月前
0.1.232026/05/066 个月前
0.1.222026/04/196 个月前
0.1.212026/03/158 个月前
0.1.202026/03/108 个月前

关于 @gentleduck/variants 的常见问题

@gentleduck/variants 是什么?

A package for creating variants of components, providing a simple and efficient way to create variants of components.

如何安装 @gentleduck/variants?

在项目目录运行 npm install @gentleduck/variants,或使用 pnpm add / yarn add / bun add。

@gentleduck/variants 支持 TypeScript 吗?

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

@gentleduck/variants 支持 ESM 还是 CommonJS?

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

@gentleduck/variants 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@gentleduck/variants 可以商用吗?

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

@gentleduck/variants 的下载量有多少?

根据 npm 官方下载统计,@gentleduck/variants 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,066 次,日均约 102 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gentleduck/variants 还在维护吗?

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