All Projects → kristerkari → React Native Sass Transformer

kristerkari / React Native Sass Transformer

Licence: mit
Use Sass to style your React Native apps.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Native Sass Transformer

Style Resources Loader
CSS processor resources loader for webpack
Stars: ✭ 214 (+41.72%)
Mutual labels:  loader, scss, sass
Sass Loader
Compiles Sass to CSS
Stars: ✭ 3,718 (+2362.25%)
Mutual labels:  loader, scss, sass
Sass Vars Loader
Use Sass variables defined in Webpack config or in external Javascript or JSON files
Stars: ✭ 112 (-25.83%)
Mutual labels:  loader, scss, sass
Compass Inuit
💮 Compass extension for inuit.css. More than 40k users served!
Stars: ✭ 127 (-15.89%)
Mutual labels:  scss, sass
Frasco
Quick starter for Jekyll including full setup for Sass, PostCSS, Autoprefixer, stylelint, Webpack, ESLint, imagemin, Browsersync, etc.
Stars: ✭ 123 (-18.54%)
Mutual labels:  scss, sass
Fast
Develop, build, deploy, redeploy, and teardown frontend projects fast.
Stars: ✭ 126 (-16.56%)
Mutual labels:  scss, sass
Sscss
Light Sass lib for managing your font-size, margin, padding, and position values across breakpoints.
Stars: ✭ 119 (-21.19%)
Mutual labels:  scss, sass
Mixins
sass mixins
Stars: ✭ 132 (-12.58%)
Mutual labels:  scss, sass
Hocus Pocus
Universal and lightweight stylesheet starter kit
Stars: ✭ 128 (-15.23%)
Mutual labels:  scss, sass
Skeleton Sass
Skeleton Sass is a highly modular version of Skeleton CSS
Stars: ✭ 151 (+0%)
Mutual labels:  scss, sass
Glup
Some of the gulp tutorial -《gulp笔记》
Stars: ✭ 136 (-9.93%)
Mutual labels:  scss, sass
Mq Scss
Extremely powerful Sass media query mixin. Allows you to create almost any media query you can imagine.
Stars: ✭ 122 (-19.21%)
Mutual labels:  scss, sass
Refills
Refills is maintained by the thoughtbot design team. It is funded by thoughtbot, inc. and the names and logos for thoughtbot are trademarks of thoughtbot, inc.
Stars: ✭ 1,523 (+908.61%)
Mutual labels:  scss, sass
Baseguide
Lightweight and robust CSS framework for prototyping and production code.
Stars: ✭ 127 (-15.89%)
Mutual labels:  scss, sass
Quickmenu
The new era of mobile navigation for the web, we're out of hamburgers.
Stars: ✭ 119 (-21.19%)
Mutual labels:  scss, sass
Awesome Sass
Sass is an extension of CSS that adds power and elegance to the basic language. It allows you to use variables, nested rules, mixins, inline imports, and more, all with a fully CSS-compatible syntax. Sass helps keep large stylesheets well-organized, and get small stylesheets up and running quickly.
Stars: ✭ 1,714 (+1035.1%)
Mutual labels:  scss, sass
Gulp Starter Kit
A simple Gulp 4 Starter Kit for modern web development.
Stars: ✭ 134 (-11.26%)
Mutual labels:  scss, sass
Shards Ui
🎨Shards is a beautiful & modern Bootstrap 4 UI kit packed with extra templates and components.
Stars: ✭ 1,718 (+1037.75%)
Mutual labels:  scss, sass
Grass
A near-feature-complete Sass compiler written purely in Rust
Stars: ✭ 143 (-5.3%)
Mutual labels:  scss, sass
Rollup Plugin Styles
🎨 Universal Rollup plugin for styles: PostCSS, Sass, Less, Stylus and more.
Stars: ✭ 116 (-23.18%)
Mutual labels:  scss, sass

react-native-sass-transformer NPM version Downloads per month contributions welcome

Use Sass to style your React Native apps.

Behind the scenes the Sass files are transformed to react native style objects (look at the examples).

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

How does it work?

Your App.scss file might look like this:

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

When you import your stylesheet:

import styles from "./App.scss";

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:

Plain React Native:

<MyElement style={styles.myClass} />

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

React Native CSS modules using className property:

<MyElement className={styles.myClass} />

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

React Native CSS modules using styleName property:

<MyElement styleName="myClass my-dashed-class" />

Please use the .scss file extension for SCSS syntax and the .sass file extension for indented Sass syntax.

Installation and configuration

Step 1: Install

yarn add --dev react-native-sass-transformer node-sass

Step 2: Configure the react native packager

For React Native v0.57 or newer / Expo SDK v31.0.0 or newer

Add this to metro.config.js in your project's root (create the file if it does not exist already):

const { getDefaultConfig } = require("metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("react-native-sass-transformer")
    },
    resolver: {
      sourceExts: [...sourceExts, "scss", "sass"]
    }
  };
})();

If you are using Expo, you also need to add this to app.json:

{
  "expo": {
    "packagerOpts": {
      "config": "metro.config.js",
      "sourceExts": ["js", "jsx", "scss", "sass"]
    }
  }
}

For React Native v0.56 or older

If you are using React Native without Expo, add this to rn-cli.config.js in your project's root (create the file if you don't have one already):

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

For Expo SDK v30.0.0 or older

If you are using Expo, instead of adding the rn-cli.config.js file, you need to add this to app.json:

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

Platform specific extensions

If you need React Native's platform specific extensions for your Sass files, you can use babel-plugin-react-native-platform-specific-extensions. Platform specific extensions for files imported using Sass' @import are supported by default.

Sass options

If you need to pass options (e.g. functions) to node-sass, you can do so by creating a transformer.js file and doing the following:

// For React Native version 0.59 or later
var upstreamTransformer = require("metro-react-native-babel-transformer");

// For React Native version 0.56-0.58
// var upstreamTransformer = require("metro/src/reactNativeTransformer");

// For React Native version 0.52-0.55
// var upstreamTransformer = require("metro/src/transformer");

// For React Native version 0.47-0.51
// var upstreamTransformer = require("metro-bundler/src/transformer");

// For React Native version 0.46
// var upstreamTransformer = require("metro-bundler/build/transformer");

var sassTransformer = require("react-native-sass-transformer");

module.exports.transform = function({ src, filename, options }) {
  if (filename.endsWith(".scss") || filename.endsWith(".sass")) {
    var opts = Object.assign(options, {
      sassOptions: {
        functions: {
          "rem($px)": px => {
            px.setValue(px.getValue() / 16);
            px.setUnit("rem");
            return px;
          }
        }
      }
    });
    return sassTransformer.transform({ src, filename, options: opts });
  } else {
    return upstreamTransformer.transform({ src, filename, options });
  }
};

After that in metro.config.js point the babelTransformerPath to that file:

const { getDefaultConfig } = require("metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("./transformer.js")
    },
    resolver: {
      sourceExts: [...sourceExts, "scss", "sass"]
    }
  };
})();

CSS Custom Properties (CSS variables)

You need version 1.4.0 or newer

:root {
  --text-color: blue;
}

.blue {
  color: var(--text-color);
}

CSS variables are not supported by default, but you can add support for them by using PostCSS and postcss-css-variables plugin.

Start by installing dependencies:

yarn add postcss postcss-css-variables react-native-postcss-transformer --dev

Add postcss-css-variables to your PostCSS configuration with one of the supported config formats, e.g. package.json, .postcssrc, postcss.config.js, etc.

After that create a transformer.js file and do the following:

// For React Native version 0.59 or later
var upstreamTransformer = require("metro-react-native-babel-transformer");

// For React Native version 0.56-0.58
// var upstreamTransformer = require("metro/src/reactNativeTransformer");

// For React Native version 0.52-0.55
// var upstreamTransformer = require("metro/src/transformer");

// For React Native version 0.47-0.51
// var upstreamTransformer = require("metro-bundler/src/transformer");

// For React Native version 0.46
// var upstreamTransformer = require("metro-bundler/build/transformer");

var sassTransformer = require("react-native-sass-transformer");
var postCSSTransformer = require("react-native-postcss-transformer");

module.exports.transform = function({ src, filename, options }) {
  if (filename.endsWith(".scss") || filename.endsWith(".sass")) {
    return sassTransformer
      .renderToCSS({ src, filename, options })
      .then(css =>
        postCSSTransformer.transform({ src: css, filename, options })
      );
  } else {
    return upstreamTransformer.transform({ src, filename, options });
  }
};

After that in metro.config.js point the babelTransformerPath to that file:

const { getDefaultConfig } = require("metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("./transformer.js")
    },
    resolver: {
      sourceExts: [...sourceExts, "scss", "sass"]
    }
  };
})();

Dependencies

This library has the following Node.js modules as dependencies:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].