Overview
ThewithUniwind higher-order component (HOC) wraps any React Native component to add className prop support. This is essential for using third-party components with Uniwind’s Tailwind-based styling system.
Why Use withUniwind?
Many popular React Native libraries export components that don’t natively support theclassName prop. Instead, they accept the traditional style prop. The withUniwind wrapper bridges this gap, allowing you to use Tailwind classes with any component.
Problem
Solution
Automatic Prop Mapping
withUniwind automatically maps props based on their names. Any prop containing style or color in its name is automatically mapped.
No manual mapping needed! The
style prop is automatically mapped to className, and color-related props get their own *ClassName variants.Automatic Mappings
Here are some examples of how props are automatically mapped:| Original Prop | Mapped ClassName Prop | Example Usage |
|---|---|---|
style | className | <Component className="p-4" /> |
color | colorClassName | <Component colorClassName="accent-red-500" /> |
backgroundColor | backgroundColorClassName | <Component backgroundColorClassName="accent-blue-500" /> |
borderColor | borderColorClassName | <Component borderColorClassName="accent-gray-300" /> |
tintColor | tintColorClassName | <Component tintColorClassName="accent-green-500" /> |
Example: Using Auto-Mapped Color Props
Custom Prop Mapping
For advanced use cases, you can define custom mappings to map className props to component props. This is useful for components with non-standard prop names likewidth, size, fill, or stroke.
How Mapping Works
The mapping object structure is:Understanding the structure:
- Object key (
targetProp) → The prop that the component actually receives fromClassName→ The new className prop you’ll write in your JSXstyleProperty→ Which CSS property value to extract and pass to the target prop
Example: Mapping a Width Prop
Some components accept awidth prop as a number or string, not a style object. Here’s how to map it:
Example: Mapping a Size Prop
Example: SVG Stroke and Fill
SVG components often usestroke and fill props that accept color values:
Mapping Entire Style Objects
If you omitstyleProperty, the entire resolved style object is passed to the target prop:
Third-Party UI Components
API Reference
Function Signature
Parameters
The React component to wrap with className support.
Optional custom prop mappings. Each mapping defines how to convert a className prop to a component prop.Mapping structure:
Return Value
A wrapped component that accepts
className and auto-generated *ClassName props, in addition to all original component props.Best Practices
Related
useResolveClassNames
Convert classNames to style objects at runtime
Third-Party Components
Learn about styling third-party component libraries