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.
Some third-party components work out of the box! Libraries like React Native Reanimated that are built on top of React Native’s core components (View, Text, etc.) automatically support
className
without wrapping. You only need withUniwind
when the underlying implementation uses custom native components or doesn’t forward the style
prop properly.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-bg-red-500" /> |
backgroundColor | backgroundColorClassName | <Component backgroundColorClassName="accent-bg-blue-500" /> |
borderColor | borderColorClassName | <Component borderColorClassName="accent-bg-gray-300" /> |
tintColor | tintColorClassName | <Component tintColorClassName="accent-bg-green-500" /> |
Example: Using Auto-Mapped Color Props
Custom Prop Mapping
For advanced use cases, you can define custom mappings to map specific props to style properties. This is particularly useful for SVG components or components with non-standard prop names.Mapping with Style Properties
Map a custom className prop to a specific CSS property:strokeClassName
is the new prop you’ll use in JSXstroke
is the original component prop being mappedaccentColor
is the CSS property to extract from the className
Mapping Background Colors
You can map any style property, including background colors:Mapping Entire Style Objects
If you omit thestyleProperty
, withUniwind
will map the entire resolved style object instead of a single property:
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
Create reusable styled components: Define your wrapped components in a separate file and export them for reuse throughout your app.
Performance consideration: Wrap components at the module level (outside your component functions) to avoid recreating the wrapper on every render.