Coder Social home page Coder Social logo

dantehemerson / slsx Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 7 KB

:nail_care: A tiny utility for constructing React Native Web styles conditionally

License: MIT License

JavaScript 100.00%
react-native styles classes clsx react-native-library library stylesheet

slsx's Introduction

slsx

A tiny utility for constructing React Native styles conditionally

DISCLAIMER: At this moment it only works with react-native-web.

This module is based on clsx.

This module is available in three formats:

  • ES Module: dist/slsx.m.js
  • CommonJS: dist/slsx.js
  • UMD: dist/slsx.min.js

Install

npm install --save slsx

or

yarn add slsx

Usage

import slsx from 'slsx';

// Your styles
const styles = StyleSheet.create({
  foo: {
    backgroundColor: 'red'
  },
  bar: {
    fontSize: 18
  },
  baz: {
    borderColor: 4
  },
  bat: {
    width: 80
  },
  qux: {
    marginVertical: 20
  },
  det: {
    paddingVertical: 10
  },
  met: {
    paddingHorizontal: 20
  }
});


// =>  =  'similar to'

slsx(styles.foo, true && styles.bar, styles.baz);
//=> [styles.foo, styles.bar, styles.baz]

slsx({ [styles.foo]: true, [styles.bar]: false, [styles.baz]: isTrue() });
//=> [styles.foo, styles.baz]

// Objects
slsx({ [styles.foo]: true }, { [styles.bar]: false }, null);
//=> [styles.foo]

// Arrays
slsx([styles.foo, null, false, styles.bar]);
//=> [styles.foo, styles.bar]

// Arrays (variadic)
slsx(
  [styles.foo], 
  ['', null, false, styles.bar], 
  [[styles.baz, [[styles.bat]]]]
);
//=> [styles.foo, styles.bar, styles.baz, styles.bat]

// Kitchen sink (with nesting)
slsx(
  styles.foo, 
  [
    1 && styles.bar, 
    { 
      [styles.baz]: false, 
      [styles.bat]: null 
    }, 
    [styles.qux, [styles.det]]
  ], 
  styles.met
);
//=> [styles.foo, styles.bar, styles.qux, styles.det, styles.met]

You can also test on Codesanbox

API

slsx(...input)

Returns: number[]

input

Type: Mixed

The slsx function can take any number of arguments, each of which can be an Object, Array, Boolean, or String.

Important: Any values that cannot be transformed into numbers or are negative numbers are discarded!

slsx(-123, true, false, '', null, undefined, NaN, '-77', 'bar', 'baz');
//=> ''

License

MIT © Dante Calderon

slsx's People

Contributors

dantehemerson avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  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.