/react-native-stylus-transformer

Load Stylus files to react native style objects.

Primary LanguageJavaScriptMIT LicenseMIT

react-native-stylus-transformer

NPM version PRs Welcome

Load Stylus files to react native style objects.

This transformer can be used together with React Native CSS modules.

Usage

Step 1: Install

yarn add --dev react-native-stylus-transformer stylus

Step 2: Configure the react native packager

Add this to your rn-cli.config.js (make one if you don't have one already):

module.exports = {
  getTransformModulePath() {
    return require.resolve("react-native-stylus-transformer");
  },
  getSourceExts() {
    return ["js", "jsx", "styl"];
  }
};

...or if you are using Expo, in app.json:

{
  "expo": {
    "packagerOpts": {
      "sourceExts": ["js", "jsx", "styl"],
      "transformer": "node_modules/react-native-stylus-transformer/index.js"
    }
  }
}

How does it work?

Your App.styl file might look like this:

.myClass {
  color: blue;
}
.myOtherClass {
  color: red;
}
.my-dashed-class {
  color: green;
}

When you import your stylesheet:

import styles from "./App.styl";

Your imported styles will look like this:

var styles = {
  myClass: {
    color: "blue"
  },
  myOtherClass: {
    color: "red"
  },
  "my-dashed-class" {
    color: "green"
  }
};

You can then use that style object with an element:

<MyElement style={styles.myClass} />

<MyElement style={styles["my-dashed-class"]} />