@gentleduck/primitives

中文文档
0.5.1发布于 2026/10/20MITTypeScriptES Module近 30 天下载 3321

Unstyled, accessibility-first UI primitives for React.

原始使用文档
文档目录6 节
  1. Why primitives
  2. Components
  3. Quick Start
  4. Features
  5. Documentation
  6. License

@gentleduck/primitives

@gentleduck/primitives

Unstyled, accessibility-first UI primitives for React.

MIT - Changelog - Contributing - Docs

npm downloads MIT


Unstyled, accessibility-first UI primitives for React.

Why primitives

  • 50-92% smaller than Radix - Alert Dialog: 1.6 KB vs 18.6 KB. Popover: 2.4 KB vs 19.6 KB. Dialog: 3.1 KB vs 10.6 KB. Shared internals (Slot, Presence, Popper, focus scope) load once instead of being duplicated per-package.
  • Full keyboard navigation + ARIA compliance out of the box - every component ships with correct roles, states, and keyboard interactions so you don't have to wire them yourself.
  • Compound component pattern - compose Root, Trigger, Content, and friends to build exactly the UI you need without prop drilling.
  • Compatible with Radix API - same naming, same asChild pattern, same data-state attributes. Migration from Radix is a namespace change in your imports.

Components

PrimitiveDescription
alert-dialogModal dialog that requires user acknowledgment
arrowPositioning arrow for floating elements
avatarUser avatar with image and fallback support
calendarDate grid with keyboard navigation and multi-view support
checkersCheckbox and switch toggle primitive
commandCommand palette with search filtering
context-menuRight-click context menu with submenus
dialogModal and non-modal dialog windows
directionRTL/LTR direction provider
dismissable-layerLayer that dismisses on outside interaction
dropdown-menuDropdown menu with submenus, checkboxes, and radio items
focus-scopeTraps and manages focus within a boundary
hover-cardCard triggered by hover with open/close delay
input-otpOne-time password input with slots and separators
menuBase menu primitive used by dropdown, context, and menubar
menubarHorizontal menu bar with keyboard navigation
mountDeferred mounting with lifecycle callbacks
navigation-menuSite navigation with animated indicator and viewport
paginationPage navigation controls
popoverFloating content anchored to a trigger
popperLow-level floating element positioning via Floating UI
portalRenders children into a DOM portal
presenceAnimate mount/unmount transitions
primitive-elementsBase Primitive element factory (asChild support)
progressDeterminate and indeterminate progress indicator
radio-groupRadio button group with roving focus
roving-focusRoving tabindex focus management for groups
selectListbox-style select with typeahead and scroll buttons
sheetSlide-out panel (re-exports dialog internals)
sliderRange slider with single or multiple thumbs
slotMerges props and ref onto a child element (asChild engine)
toggleTwo-state toggle button
toggle-groupSingle or multi-select group of toggles
tooltipAccessible tooltip with provider-level delay
visibility-hiddenVisually hidden element for screen readers

Quick Start

npm install @gentleduck/primitives
import * as Dialog from '@gentleduck/primitives/dialog'

function Demo() {
  return (
    <Dialog.Root>
      <Dialog.Trigger>Open</Dialog.Trigger>
      <Dialog.Portal>
        <Dialog.Overlay />
        <Dialog.Content>
          <Dialog.Title>Heading</Dialog.Title>
          <Dialog.Description>Body text here.</Dialog.Description>
          <Dialog.Close>Close</Dialog.Close>
        </Dialog.Content>
      </Dialog.Portal>
    </Dialog.Root>
  )
}

Features

  • asChild / Slot - render any primitive as your own element via the asChild prop, powered by the Slot utility.
  • createContextScope - scoped React context factory that prevents cross-instance state leaks.
  • forwardRef everywhere - every component forwards refs for imperative access and composition.
  • data-slot attributes - every component root emits a data-slot for style targeting without class names.
  • Presence animations - the Presence primitive lets you animate enter/exit transitions before unmounting.
  • Focus trapping - FocusScope locks focus inside modals, dialogs, and popovers with configurable loop behavior.
  • Dismiss layers - DismissableLayer handles outside clicks, Escape key, and nested layer stacking.

Documentation

gentleduck.org/docs/packages/duck-primitives

License

MIT

版本与发布记录

@gentleduck/primitives 共发布 57 个版本,首次发布于 2025/09/02;过去 12 个月发布了 26 个版本。

版本发布日期距今
0.5.1latest2026/10/2026 天前
0.5.02026/10/131 个月前
0.4.02026/10/111 个月前
0.3.12026/06/254 个月前
0.3.02026/05/305 个月前
0.2.142026/05/205 个月前

关于 @gentleduck/primitives 的常见问题

@gentleduck/primitives 是什么?

Unstyled, accessibility-first UI primitives for React.

如何安装 @gentleduck/primitives?

在项目目录运行 npm install @gentleduck/primitives,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.0.0)、tabbable(6.2.0)、react-dom(>=18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gentleduck/primitives 支持 TypeScript 吗?

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

@gentleduck/primitives 支持 ESM 还是 CommonJS?

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

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

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

@gentleduck/primitives 可以商用吗?

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

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

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

@gentleduck/primitives 还在维护吗?

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