JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q29499F
  • License MIT

A React Native library that allows you to use Sass- and CSS-like functionalities, like nesting and shared styles, without losing the default experience of creating React Native stylesheets.

Package Exports

  • native-sass
  • native-sass/index.ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (native-sass) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

native-sass

A React Native library that allows you to use Sass- and CSS-like functionalities, like nesting and shared styles. With this library, you can nest styles and use shared styles to apply properties to multiple style objects at once, without losing the default experience of creating React Native stylesheets.

Installation

To use native-sass, just run this command from your terminal if you're using npm:

npm install native-sass

Or use the following if you're using yarn:

yarn add native-sass

Nesting

Suppose we have the following StyleSheet:

import { StyleSheet } from 'react-native';

const styles = StyleSheet.create({
  // More style rules...
  dialogWrapper: {
    // dialogWrapper styles
  },
  dialogTitle: {
    // dialogTitle styles
  },
  dialogMsg: {
    // dialogMsg styles
  },
  dialogActionsBtn: {
    // dialogActionsBtn styles
  },
  dialogActionsBtnText: {
    // dialogActionsBtnText styles
  },
  // More style rules...
});

This stylesheet has a lot of style objects that we might want to nest. We can do that by using the sassy function from native-sass as follows:

import { StyleSheet } from 'react-native';
import { sassy } from 'native-sass';

const styles = StyleSheet.create(sassy({
  // More style rules...
  dialog: {
    wrapper: {
      // dialogWrapper styles
    },
    
    title: {
      // dialogTitle styles
    },

    msg: {
      // dialogMsg styles
    },

    actions: {
      btn: {
        // dialogActionsBtn styles

        text: {
          // dialogActionsBtnText styles
        }
      }
    }
  },
  // More style rules...
}));

This object passed to sassy will be flattened into the object of the previous snippet. The nested keys are capitalized and concatenated with the parent keys, so dialog.actions.btn.text becomes dialogActionsBtnText. The nested styles are then merged in order to return the object that the StyleSheet.create() method expects.

Shared values

Suppose we have the following stylesheet:

import { StyleSheet } from 'react-native';

const styles = StyleSheet.create({
  // More style rules...
  cards: {
    width: 200,
    minHeight: 200,
    borderRadius: 25,
    borderWidth: 2,
    borderColor: 'red',
    justifyContent: 'center',
    alignItems: 'center',
    gap: 8,
    backgroundColor: 'black'
  },

  card: {
    width: 50,
    minHeight: 50,
    borderWidth: 2,
    borderColor: 'red',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'pink'
  },
  // More style rules...
});

We can recycle this shared styles as follows with the sassy function:

import { StyleSheet } from 'react-native';
import { sassy } from 'native-sass';

const styles = StyleSheet.create(sassy({
  // More style rules...
  'cards, card': {
    borderWidth: 2,
    borderColor: 'red',
    justifyContent: 'center',
    alignItems: 'center'
  },

  cards: {
    width: 200,
    minHeight: 200,
    borderRadius: 25,
    gap: 8,
    backgroundColor: 'black'
  },

  card: {
    width: 50,
    minHeight: 50,
    backgroundColor: 'pink'
  }
  // More style rules...
}));

We wrap the keys we want to apply the shared styles to with quotes and separate them with commas.

Mixins

Mixins can be applied to a style object using the built-in JavaScript spread operator ..., so no need to use sassy (unless nesting or shared styles are present). Example:

import { StyleSheet } from 'react-native';

const calculateSpacing = (value) => ({
  padding: value,
  margin: value / 2
});

const styles = StyleSheet.create({
  card: {
    color: 'white',
    width: 50,
    height: 50,
    ...calculateSpacing(20),
  }
});