twrnc Documentation
repository·master·Indexed 25 days ago
https://github.com/jaredh159/tailwind-react-native-classnamesA high-performance styling library for React Native that provides a Tailwind CSS-like API. It supports utility classes, platform prefixes, dark mode, media queries, and JIT-style arbitrary values. The library includes a tagged template literal syntax, a `tw.style()` method for complex conditional styling, and hooks like `useDeviceContext` and `useAppColorScheme` for managing device state and color schemes.
What's inside twrnc
- The library implements many Tailwind JIT (Just-In-Time) features, allowing you to use arbitrary values for colors, spacing, and more using square bracket notation.
Enable device-context prefixes with useDeviceContext
masterTo enable prefixes that depend on runtime device data (like
dark:,lg:,portrait:, etc.), you must call theuseDeviceContexthook once at the root of your component hierarchy. This connects thetwfunction to the device's current state.import tw from './lib/tailwind'; // or 'twrnc' if no custom config import { useDeviceContext } from 'twrnc'; export default function App() { useDeviceContext(tw); return ( <View style={tw`bg-white dark:bg-black`}> <Text style={tw`text-black dark:text-white`}>Hello</Text> </View> ); }Expo Users: Ensure
userInterfaceStyleis set toautomaticin yourapp.jsonto allow thedark:prefix to work:{ "expo": { "userInterfaceStyle": "automatic" } }Create a custom configured version of tw
masterIf you have a custom
tailwind.config.js, you can create a customized version of thetwfunction using thecreateutility. This allows your app to respect your specific theme, breakpoints, and plugins.```js // lib/tailwind.js import { create } from 'twrnc'; // create the customized version... const tw = create(require(`../../tailwind.config.js`)); // ... and then this becomes the main function your app uses export default tw;NOTE If you are using
export default {}in yourtailwind.config.js, pass thedefaulttocreate:require('../../tailwind.config.js').default.Migrate from v1 to v2 (Package Rename and Config Change)
masterTo migrate from the original package to
v2.x.x:- Uninstall the old package and install
twrnc:npm uninstall tailwind-react-native-classnames npm install twrnc - Update all imports from
from 'tailwind-react-native-classnames'tofrom 'twrnc'. - If using a
tailwind.config.js, remove thetw-rn-styles.jsonfile and pass your config directly to thecreatefunction.
const tw = create(require(`../../tailwind.config.js`));- Uninstall the old package and install
Fix Breakpoint Boundary behavior in v4.x.x
masterIn
v4.0.0, breakpoint boundaries were updated to be inclusive of the minimum value, matching TailwindCSS behavior. For example,md:bg-blacknow applies exactly at768pxinstead of starting at769px(as it did inv3.x.x).If your layout depends on the previous exclusive behavior, you can restore it by manually adjusting your
screensconfiguration in your theme settings to offset the values by 1px.module.exports = { theme: { screens: { sm: '641px', md: '769px', lg: '1025px', xl: '1281px', }, }, };Install twrnc via npm
masterTo use Tailwind React Native Classnames in your project, install the
twrncpackage using npm.npm install twrncManually control Dark Mode with useAppColorScheme
masterIf you want to control the color scheme explicitly (e.g., via an in-app toggle) rather than following the system settings, configure
useDeviceContextto opt out of device changes and use theuseAppColorSchemehook.import { useDeviceContext, useAppColorScheme } from 'twrnc'; import tw from './lib/tailwind'; export default function App() { useDeviceContext(tw, { observeDeviceColorSchemeChanges: false, initialColorScheme: `light`, // 'light' | 'dark' | 'device' }); const [colorScheme, toggleColorScheme, setColorScheme] = useAppColorScheme(tw); return ( <TouchableOpacity onPress={toggleColorScheme}> <Text style={tw`text-black dark:text-white`}>Switch Color Scheme</Text> </TouchableOpacity> ); }Configure VS Code Intellisense for twrnc
masterTo enable autocomplete for
twrncfunctions in VS Code, add the following to your settings for the official Tailwind CSS IntelliSense extension. This tells the extension to recognizetwand its variants as valid class attribute functions.// VS Code Settings "tailwindCSS.classAttributes": [ // ... "style" ], "tailwindCSS.classFunctions": ["tw", "tw.color", "tw.style"]Initialize color schemes in v4.x.x using useDeviceContext()
masterIn
v4.0.0, the way to manually control the app color scheme (opting out of device color scheme changes) has changed.useAppColorScheme(): No longer accepts a second parameter for initialization. It is now safe to call this hook multiple times anywhere in your app to read or modify the scheme.useDeviceContext(): Initialization logic has moved here. This hook should be called once at the root of your app.
To control the color scheme manually, use the
observeDeviceColorSchemeChangesandinitialColorSchemeoptions withinuseDeviceContext().// Old v3.x.x way useDeviceContext(tw, { - withDeviceColorScheme: false, + observeDeviceColorSchemeChanges: false, + initialColorScheme: "light", }); // useAppColorScheme no longer takes a second param -const [colorScheme, ...] = useAppColorScheme(tw, `light`); +const [colorScheme, ...] = useAppColorScheme(tw);Configure JetBrains IDEs Intellisense for twrnc
masterFor WebStorm or IntelliJ, navigate to Settings | Languages & Frameworks | Style Sheets | Tailwind CSS and add the following configuration.
Note: You must have a
tailwind.config.jsfile in your project root (even if it only containsexport default {}) for the Tailwind LSP to start correctly.// JetBrains Settings "classAttributes": [ // ... "style" ], "classFunctions": ["tw", "tw.color", "tw.style"]Migrate to v3.x.x (Color Renames)
masterWhen migrating tov3.x.x, be aware of color renames introduced by Tailwind CSS v3. Specifically,green,yellow, andpurpleare mapped to their extended colors, andgrayscales have been renamed. You may need to update yourtailwind.config.jsto re-alias these colors to maintain your existing design.Use the default tagged template literal syntax
masterThe default export of
twrncis a tagged template function. You can pass space-separated Tailwind classes to it to receive a React Native style object. This is the most common way to apply styles.import tw from 'twrnc'; tw`pt-6 bg-blue-100`; // -> { paddingTop: 24, backgroundColor: 'rgba(219, 234, 254, 1)' }