@cdx-ui/utils

中文文档
0.3.12发布于 2026/11/04MITTypeScriptESM + CJS近 30 天下载 3423

coverage

原始使用文档
文档目录10 节
  1. Installation
  2. Exports
  3. Common helpers
  4. Context
  5. Style context
  6. Form control
  7. Exported types
  8. Testing
  9. Further reading
  10. License

@cdx-ui/utils

coverage

Shared utilities used across FDS packages.

Provides common helpers for styling, event handling, state management, and context propagation used by @cdx-ui/primitives and @cdx-ui/components.

Installation

pnpm add @cdx-ui/utils

Exports

Common helpers

ExportDescription
cn(...inputs)Merges class names with clsx + tailwind-merge — conditionals and conflict resolution in one call.
composeEventHandlers(...handlers)Chains multiple event callbacks into a single handler; skips null/undefined entries.
useControllableState(params)Hook that supports both controlled and uncontrolled modes for a single value. Derives isControlled from whether the prop is undefined.
mergeRefs(...refs)Combines multiple refs (callback or object) into one ref callback.
flattenChildren(children)Recursively flattens React children (including Fragments) into a flat array with re-keyed elements.
ariaAttr(condition)Returns true (boolean) or undefined — shorthand for boolean ARIA attribute values.
cva(base, config)Class Variance Authority helper for authoring variant-driven className builders. VariantProps<typeof fn> derives the prop type.

Context

ExportDescription
createContext(displayName)Wrapper around React's createContext that returns a [Provider, useHook] tuple. The hook throws a descriptive error if called outside the provider — eliminates silent undefined bugs.

Style context

Used internally by @cdx-ui/components to propagate variant props (size, color, etc.) from a compound root to its sub-components.

ExportDescription
ParentContextReact context for propagating variant props from a compound root to sub-components. Wrap children in <ParentContext.Provider value={...}>.
useStyleContext(scope?)Reads the style context set by the nearest ParentContext.Provider.
useParentContext()Reads the full StyleContextMap from the nearest provider.

Form control

Low-level form-field state used by the Field and Input primitives.

ExportDescription
useFormControlRoot(props)Creates the root state object for a form field (id generation, tracks hasFeedbackText/hasHelpText for describedby assembly).
useFormControl(props)Merges component-level props with inherited form-control context. Assembles aria-describedby from feedback/help text ids.
useFormControlContext()Reads form-control state from the nearest provider.
useReportFormControlLabelFocus(isActive)Reports focus/active state to the parent FormControlContext — used by Input/Select.Trigger to trigger isFocused styling on the associated label.
useReportFormControlValidation(isInvalid, errorMessage)Reports a self-validating control's invalid state and message to the parent Field — used by DatePicker so Field.Error / Field.Helper anywhere in the field reflect it.
FormControlContextThe React context object for form-control state propagation.

Exported types

TypeSource
UseControllableStateParamscommon
VariantPropscommon
Focusableform-control
FormControlContextValueform-control
FormControlFieldPropsform-control
FormControlRootPropsform-control
FormControlRootReturnform-control
FormControlStateform-control
FormControlValidationform-control
StyleContextMapstyle

Testing

Tests are colocated next to source files (*.test.{ts,tsx}). Run via the utils Jest project:

pnpm nx run @cdx-ui/utils:test

All runtime exports have test coverage using @testing-library/react-native (renderHook, act).

Further reading

The Forge UI docs site covers setup and usage. The repository's internal authoring guides cover the patterns behind these helpers — useControllableState and composeEventHandlers in the state guide, and cn() and CVA variant authoring in the styling guide.

License

License: MIT

MIT © 2026 Digital First Holdings LLC. The full license text ships in the package as LICENSE.

版本与发布记录

@cdx-ui/utils 共发布 256 个版本,首次发布于 2026/03/04;过去 12 个月发布了 256 个版本。

版本发布日期距今
0.3.12latest2026/11/044 天前
0.3.112026/10/309 天前
0.3.102026/10/309 天前
0.3.92026/10/3010 天前
0.3.82026/10/2811 天前
0.3.72026/10/2713 天前

其他发布通道:staging → 0.1.0。

关于 @cdx-ui/utils 的常见问题

如何安装 @cdx-ui/utils?

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

@cdx-ui/utils 支持 TypeScript 吗?

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

@cdx-ui/utils 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cdx-ui/utils 可以商用吗?

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

@cdx-ui/utils 的下载量有多少?

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

@cdx-ui/utils 还在维护吗?

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