Code Monkey home page Code Monkey logo

tailwind-react-native's Introduction

tailwind-react-native

this library is a configurable, react native specific port of tailwind. it supports custom configurations fairly thoroughly, has typescript definitions, and the ability to purge unused styles from your final build

Note: tailwindcss-react-native looks like the most tailwind-y solution to date - I would recommend taking a look at it before using this lib!

alternatives

install

yarn add tailwind-react-native

quickstart

quickly generate config files and customizable style function for use in your app by using the init command:

npx tailwind-react-native@latest init

api

import React from "react";
import sx from "../styles";

export default function App() {
  return (
    <View style={sx("flex-1 p-24")}>
      <View style={sx("w-24 h-24 bg-platform-red")} />
    </View>
  );
}

if you want to make use of the typescript definitions, you can use an array syntax like so:

import * as React from "react";
import sx from "../styles";

export default function App() {
  return <View style={sx(["flex-1", "p-24"])} />;
}

variants / selectors

you can add custom selectors that represent dynamic styles in your components

import sx from "../styles";

function MyComponent() {
  const theme = useColorScheme();

  const platformStyles = sx(
    Platform.select({
      ios: "bg-platform-yellow",
      android: "bg-platform-blue",
      web: "bg-platform-red",
    }),
    theme === "dark" && "bg-white"
  );

  return <View style={platformStyles} />;
}

this can be useful for applying different styles based on enums like:

  • Platform.OS
  • useColorScheme()
  • loading state

customizing your styles

this library tries its best to stay as close to tailwind as possible. that being said, there are some things that are unique to one platform or another so it's necessary to be able to customize for one platform or another. this is pretty easy to do within your tailwind.config.js file:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      // ... your other customizations

      // this will set `rounded-lg` to be 32 pts on android, and 45% on other platforms
      // this pattern is applicable to every style in your config file
      borderRadius: {
        lg: {
          android: 32,
          default: "45%",
        },
      },
    },
  },
};

build and rebuild custom styles

npx tailwind-react-native@latest build --config path/to/config --out path/for/styles

how it works:

there are two parts to this library - the cli which generates a styles.json file and the create function which takes this json and makes useful helper functions that can be used in your code:

import create from "tailwind-react-native";
import styles from "./styles.json";

const fn = create(styles);

fn("flex-1 bg-green-500 py-12");

Note: this is what the init function of the cli does for you

purging unused styles

npx tailwind-react-native@latest purge --config path/to/config --out path/for/styles

you can configure the purge function via your tailwind.config.js file:

module.exports = {
  // ...rest of your config
  purge: {
    files: "**/*.{js,tsx}", // what file extensions to scan for styles
    whitelist: ["bg-red-500", "h-12"], // what classNames to keep regardless of what is scanned
  },
};

similar to purgecss - this will scan the source code of the directory and attempt to remove any unused styles in the project. you'll likely want to do this before building and / or deploying your app to reduce the bundle sizes included (styles.json is ~200kb out of the box).

as noted by the tailwindcss docs, dynamic styles are a tough nut to crack - if you know the dynamic styles your app is using, you can specify them in the whitelist property of your tailwind config to keep these styles from being stripped.

future plans

  • more documentation on classes available
  • improve android platform colors, update ios platform colors to include UI colors

tailwind-react-native's People

Contributors

ajsmth avatar brentvatne avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.