Primitive is a collection of low-level components that render basic HTML elements. Each primitive supports an asChild prop for composition using the Slot pattern, allowing you to merge props and refs with a child element.
Installation
Available Primitives
The following primitive elements are available:Primitive.aPrimitive.buttonPrimitive.divPrimitive.formPrimitive.h2Primitive.h3Primitive.imgPrimitive.inputPrimitive.labelPrimitive.liPrimitive.navPrimitive.olPrimitive.pPrimitive.selectPrimitive.spanPrimitive.svgPrimitive.ul
Props
Each primitive accepts all standard HTML attributes for its element type, plus:When
true, the primitive will merge its props and ref with its immediate child element instead of rendering its own element. This enables powerful composition patterns.Default: falseUsage
Basic Usage
Using asChild for Composition
Custom Button Component
Polymorphic Component Pattern
Type Definitions
Utilities
dispatchDiscreteCustomEvent
A utility function for dispatching custom events within discrete React events while ensuring proper batching behavior.Usage
When to Use
Use Primitives when:- Building component libraries with consistent base elements
- You need the
asChildcomposition pattern - Creating polymorphic components that can render as different elements
- Building accessible components that merge props and refs correctly
Notes
The
asChild prop uses the Slot pattern from @radix-ui/react-slot. When enabled, the Primitive merges its props and ref with the immediate child element instead of rendering its own DOM node.Primitives automatically set a symbol on the window object (
window[Symbol.for('radix-ui')]) to help with debugging and detecting Radix UI components in the browser.The
dispatchDiscreteCustomEvent utility uses ReactDOM.flushSync to ensure custom events dispatched within discrete events (like clicks) are properly batched by React. This prevents unexpected batching behavior in React 18+.