npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rn-classes

v0.0.6

Published

Utility function to wrap around a StyleSheet.create in react-native and get a function to merge the objects. Differently from other packages it provides cascading and a good typing support.

Downloads

3

Readme

MIT License

npm bundle size

npm

npm

GitHub last commit

rn-classes

Utility function that takes an object as input and let you merge different properties into a single object with a class-like definition. It's meant to be used as a quick way to style your react native component.

Installation

You can install it with your package manager of choiche like this.

npm i rn-classes
yarn add rn-classes
pnpm i rn-classes

Usage

import { StyleSheet, Text, View } from "react-native";
import { createStyles } from "rn-classes";

const styles = StyleSheet.create({
  container: {
    padding: 16,
  },
  item: {
    borderWidth: 1,
    borderColor: "blue",
  },
  selected: {
    borderColor: "red",
  }
});

const classes = createStyles(styles);

export function Test() {
  return (
      <View style={classes("container")}>
          <Text style={classes("item selected")}>Selected item</Text>
          <Text style={classes("item")}>Item</Text>
      </View>
    </>
  );
}

You can pass to the classes function everything you could pass to clsx so, straight from their documentation

import clsx from 'clsx';
// or
import { clsx } from 'clsx';

// Strings (variadic)
clsx('foo', true && 'bar', 'baz');
//=> 'foo bar baz'

// Objects
clsx({ foo:true, bar:false, baz:isTrue() });
//=> 'foo baz'

// Objects (variadic)
clsx({ foo:true }, { bar:false }, null, { '--foobar':'hello' });
//=> 'foo --foobar'

// Arrays
clsx(['foo', 0, false, 'bar']);
//=> 'foo bar'

// Arrays (variadic)
clsx(['foo'], ['', 0, false, 'bar'], [['baz', [['hello'], 'there']]]);
//=> 'foo bar baz hello there'

// Kitchen sink (with nesting)
clsx('foo', [1 && 'bar', { baz:false, bat:null }, ['hello', ['world']]], 'cya');
//=> 'foo bar hello world cya'

What you will get back is an object that you can pass to the style tag of a react native component.

Why not use [insert package name here]?

There are already countless packages that do a similar thing but they either are not typed very well (with rn-classes you get very good autocomplete based on the shape of your styles) or they don't actually provide a "cascade-like" experience like we have in regoular CSS.

In rn-classes infact if a "class" is declared after another it will have precedence.

For example let's say that you have your styles declared as such

const styles = StyleSheet.create({
  container: {
    padding: 16,
  },
  item: {
    borderWidth: 1,
    borderColor: "blue",
  },
  selected: {
    borderColor: "red",
  }
});

adding the class classes("item selected") or the class classes("selected item") will produce the same

{
    borderWidth: 1,
    borderColor: "red",
}