Programmatic API
Access the parser and constants programmatically for advanced use cases.
parseClassName
Section titled “parseClassName”Parse className strings to React Native styles:
import { parseClassName } from "@mgcrea/react-native-tailwind";
// Basic usageconst styles = parseClassName("m-4 p-2 bg-blue-500");// Returns: { margin: 16, padding: 8, backgroundColor: '#3B82F6' }With Custom Theme
Section titled “With Custom Theme”const customStyles = parseClassName("m-4 bg-primary font-custom", { colors: { primary: "#1d4ed8" }, fontFamily: { custom: "My Custom Font" },});// Returns: { margin: 16, backgroundColor: '#1d4ed8', fontFamily: 'My Custom Font' }Type Signature
Section titled “Type Signature”function parseClassName( className: string, customTheme?: { colors?: Record<string, string>; fontFamily?: Record<string, string>; },): ViewStyle | TextStyle | ImageStyle;Constants
Section titled “Constants”Access default color and spacing scales:
COLORS
Section titled “COLORS”import { COLORS } from "@mgcrea/react-native-tailwind";
const blueColor = COLORS["blue-500"]; // '#3B82F6'const grayColor = COLORS["gray-300"]; // '#D1D5DB'SPACING_SCALE
Section titled “SPACING_SCALE”import { SPACING_SCALE } from "@mgcrea/react-native-tailwind";
const spacing = SPACING_SCALE[4]; // 16const largeSpacing = SPACING_SCALE[8]; // 32Use Cases
Section titled “Use Cases”Dynamic Style Generation
Section titled “Dynamic Style Generation”import { parseClassName } from "@mgcrea/react-native-tailwind";import { useState } from "react";
function DynamicStyles() { const [color, setColor] = useState("blue");
// Generate styles dynamically const buttonStyles = parseClassName(`bg-${color}-500 p-4 rounded-lg`);
return ( <Pressable style={buttonStyles}> <Text>Dynamic Button</Text> </Pressable> );}Custom Style Builder
Section titled “Custom Style Builder”import { parseClassName, COLORS } from "@mgcrea/react-native-tailwind";
function createButtonStyle(variant: "primary" | "secondary") { const variantClasses = { primary: "bg-blue-500 text-white", secondary: "bg-gray-200 text-gray-900", };
return parseClassName(`p-4 rounded-lg ${variantClasses[variant]}`);}
// Usageconst primaryStyle = createButtonStyle("primary");const secondaryStyle = createButtonStyle("secondary");Testing
Section titled “Testing”import { parseClassName } from "@mgcrea/react-native-tailwind";
describe("Button styles", () => { it("should apply correct padding", () => { const styles = parseClassName("p-4"); expect(styles.padding).toBe(16); });
it("should apply correct background color", () => { const styles = parseClassName("bg-blue-500"); expect(styles.backgroundColor).toBe("#3B82F6"); });});Style Composition
Section titled “Style Composition”import { parseClassName } from "@mgcrea/react-native-tailwind";
function composeStyles(...classNames: string[]) { return classNames.map((className) => parseClassName(className));}
// Usageconst styles = composeStyles( "bg-white p-4", "border border-gray-200", "rounded-lg shadow");
<View style={styles}> <Text>Composed styles</Text></View>Integration with Theme Provider
Section titled “Integration with Theme Provider”import { parseClassName } from "@mgcrea/react-native-tailwind";import { useTheme } from "./ThemeProvider";
function ThemedComponent() { const { colors } = useTheme();
const styles = parseClassName("p-4 bg-primary text-white", { colors: { primary: colors.brand, }, });
return ( <View style={styles}> <Text>Themed content</Text> </View> );}Important Notes
Section titled “Important Notes”- The programmatic API parses styles at runtime, not compile-time
- For production apps, prefer using
classNameprop for compile-time optimization - Use the programmatic API for:
- Testing
- Dynamic style generation
- Custom tooling
- Style composition utilities
Performance Considerations
Section titled “Performance Considerations”The programmatic API has some overhead since it parses at runtime:
| Approach | Performance | When to Use |
|---|---|---|
className prop (compile-time) | ⚡ Zero overhead | Production code (recommended) |
| Programmatic API | 🐌 Runtime parsing | Testing, tooling, dynamic generation |
Runtime tw | 🚀 Memoized parsing | Fully dynamic styling needs |
Related
Section titled “Related”- Runtime tw - For fully dynamic styling
- Custom Colors - Extend color palette
- Troubleshooting - Common issues