All Projects → rootstrap → react-native-use-styles

rootstrap / react-native-use-styles

Licence: MIT license
A classy approach to manage your react native styles.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-native-use-styles

Preset
A simple CSS preset for 2020
Stars: ✭ 146 (+121.21%)
Mutual labels:  styles
jvm-dump-proxy
A proxy DLL for Windows to dump JVM classes at JNI level
Stars: ✭ 53 (-19.7%)
Mutual labels:  classes
MulleObjC
💎 A collection of Objective-C root classes for mulle-objc
Stars: ✭ 50 (-24.24%)
Mutual labels:  classes
Layouts
Grab-and-go layouts for React
Stars: ✭ 202 (+206.06%)
Mutual labels:  styles
ta-json
Type-aware JSON serializer/parser
Stars: ✭ 67 (+1.52%)
Mutual labels:  classes
postcss-typed-css-classes
PostCSS plugin that generates typed entities from CSS classes for chosen programming language.
Stars: ✭ 12 (-81.82%)
Mutual labels:  classes
Figma Theme
Generate development-ready theme JSON files from Figma Styles
Stars: ✭ 130 (+96.97%)
Mutual labels:  styles
HostsFileManagement
Hosts file management on Windows systems using PowerShell classes
Stars: ✭ 25 (-62.12%)
Mutual labels:  classes
Heir
Helper functions for prototypical inheritance in JavaScript
Stars: ✭ 61 (-7.58%)
Mutual labels:  classes
Attrs
Python Classes Without Boilerplate
Stars: ✭ 3,786 (+5636.36%)
Mutual labels:  classes
SoftUni-Software-Engineering
SoftUni- Software Engineering
Stars: ✭ 47 (-28.79%)
Mutual labels:  classes
spruceup
Spruce up CSS classes
Stars: ✭ 12 (-81.82%)
Mutual labels:  classes
The-Java-Workshop
A New, Interactive Approach to Learning Java
Stars: ✭ 65 (-1.52%)
Mutual labels:  classes
Dropcss
An exceptionally fast, thorough and tiny unused-CSS cleaner
Stars: ✭ 2,102 (+3084.85%)
Mutual labels:  styles
lua-classy
Class-based OO library for Lua
Stars: ✭ 28 (-57.58%)
Mutual labels:  classes
Figma Tokens
Official Repo of the Figma Plugin 'Figma Tokens'
Stars: ✭ 134 (+103.03%)
Mutual labels:  styles
claxed
Classes with the same style of Styled-Components
Stars: ✭ 17 (-74.24%)
Mutual labels:  classes
prop-styles
Utility to create flexible React components which accept props to enable/disable certain styles.
Stars: ✭ 31 (-53.03%)
Mutual labels:  styles
CustomWebRadioButton
An example of a make radio-button design on the web.
Stars: ✭ 15 (-77.27%)
Mutual labels:  styles
tailwind-cascade
Override TailwindCSS classes for component composition
Stars: ✭ 28 (-57.58%)
Mutual labels:  classes

npm version

A classy approach

Did you ever want to organize your styles properly? This library contains an easy to use API that lets you do it in a classy way.

Installation

npm i react-native-use-styles

Usage

Using styles

import useStyles from './my-namespaced-styles';

const Component = ()  {
  const s = useStyles();

  return (
    <Text styles={s`.global .namespaced`}>
      Hello World!
    </Text>
  );
}

Note that we are classy now, and nobody would deny it. Next we'll define our .global and .namespaced styles to use them in our components as we are doing in this example.

Global styles

global-styles.js

import { GlobalStyles } from 'react-native-use-styles';

GlobalStyles({
  global: 'flex:1 fx:dir:row',
});

We are using aliases or shortcuts to define our styles. This is equivalent to do:

import { GlobalStyles } from 'react-native-use-styles';

GlobalStyles({
  global: {
    flex: 1,
    flexDirection: 'row',
  },
});

Namespaced styles

my-namespaced-styles.js

import { Styles } from 'react-native-use-styles';

export default Styles({
  reused: 'bg:color:green',
  namespaced: '.global .reused color:purple',
});

Namespaced styles are a way to isolate a group of styles for a particular part of your app, it could be styles for a component, a screen, etc. This is a way to group semantically and avoid collisions between your styles. Note that we are exporting the result of our namespaced definition.

Constants

import { GlobalStyles } from 'react-native-use-styles';

GlobalStyles({
  constants: {
    purple: 'purple',
  },
  path: 'color:$purple',
  object: { color: '$purple' },
});

You can define constants in your global or namespaced styles that will be available to reuse with the $ prefix.

Computed and Dynamic styles

Computed styles:

import useStyles from './my-namespaced-styles';

const Component = ()  {
  const isPurple = useState(true);
  const s = useStyles([isPurple]);

  return (
    <Text styles={s`fx:1 &purple`}>
      Hello World!
    </Text>
  );
}

Computed styles are prefixed with the & character. Note that we are passing isPurple as a hook's dependency to track it changes. We can then use this dependency in our computed styles as following.

import { Styles } from 'react-native-use-styles';

export default Styles({
  computed: {
    purple: ([isPurple]) => ({ color: isPurple ? 'purple' : 'black' });
  }
});

If the dependencies change, only styles with a computed in it will be recomputed.

Dynamic styles:

import useStyles from './my-namespaced-styles';

const Component = ()  {
  const isPurple = useState(true);
  const s = useStyles();

  return (
    <Text styles={s`fx:1 ${isPurple && '.purple'}`}>
      Hello World!
    </Text>
  );
}

And a simple style definition as following:

import { Styles } from 'react-native-use-styles';

export default Styles({
  purple: { color: 'purple' },
});

There are plenty more things you can do with useStyles, learn more in User Guide

Definition order

You want your global styles to be defined or imported before all the other styles. So just import your global styles at the top of your App.js or your main entry point; before the imports of your custom or navigation component.

App.js

import './globalStyles'; // ultra safe zone
import React from 'react';

import CustomComponent from './CustomComponent';

export default function App() {
  return <CustomComponent />;
}

List of aliases

This is the current list of aliases available, we plan to add more.

bot = bottom;
col = column;
dir = direction;
fx = flex;
lt = left;
rt = right;
bg = background;
txt = text;
jf = justify;
pd = padding;
wd = width;
hg = height;

Performance

This library was created with performance in mind; useStyles has multiple cache layers to avoid unnecessary renders, calculations, and transformations. More info in the User Guide

Contributing

We plan to keep working in the library to optimize and add new features (contributions are welcome). If you have an idea that could make this library better we would love to hear it. Please take a look at our Contributing Guidelines to get to know the rules and how to get started with your contribution.

How to run the demo app

In order to contribute with some code you will need to test your changes within the demo app. At the moment the mechanism that we are using to test the lib inside the app is to import it locally. That means that:

You need to install the dependencies on the library in production mode (npm install --only=prod) so you don't have problems with dual installations of react-native each time you make a change, you need to do a force install of the library inside the demo folder. If the problem persists, after you test or build the app you encounter duplicated issues, you may want to delete the modules of one of the packages, the lib or the demo app, to solve this issue.

If you are comfortable using something like Wix's wml, it could provide a better development experience for you. We wanted the main contributing option to not require any extra installations or knowledge. Symlinks have not worked and that is why we recommend Wml.

License

react-native-use-styles is available under the MIT license. See the LICENSE file for more info.

Credits

react-native-use-styles is maintained by Rootstrap with the help of our contributors.

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