GithubHelp home page GithubHelp logo

bloycey / react-native-simple-styles Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 78 KB

Let's not reinvent the wheel. A stylesheet based CSS solution for React Native.

Home Page: https://www.npmjs.com/package/react-native-simple-styles

JavaScript 100.00%
react-native css stylesheets

react-native-simple-styles's Introduction

React Native Simple Styles npm version npm version

Not everyone wants to write scoped CSS. This package allows you to write CSS that:

  • All lives in the one file;
  • Is applied using a classNames prop (similar to React);
  • Is compatible with an atomic CSS approach;
  • Doesn't need to be repeated for every component;
  • Doesn't require you to import and use StyleSheet.create() in every component

Usage

Write your CSS in a single style.js file and also import the react-native-simple-styles npm package here. You should also make sure to export the react-native-simple-styles package from here using whatever name you like (this will make your life easier with importing later!). Use standard CSS-in-JS object format. You can organise your CSS however you like. E.g.

// styles.js

import reactNativeSimpleStyles from "react-native-simple-styles";

const headings = {
	h1: { fontSize: 56 },
	h2: { fontSize: 36 },
	h3: { fontSize: 24 },
}

const textUtilities = {
	uppercase: { textTransform: 'uppercase'},
	italic: { fontStyles: 'italic'}
}

export const styles = {
	centerAll: {
		display: 'flex',
		alignItems: 'center',
		justifyContent: 'center'
	},
	hScreen: {
		height: '100vh'
	}
	...headings,
	...textUtilities,
}

export const simpleStyles = reactNativeSimpleStyles;

In your components pass in your stylesheet to the react-native-simple-styles function you exported from style.js. All of the standard React Native core components will be returned that you can use as normal. The only difference is that they now accept a className prop that you can pass your classNames to.

import { styles, simpleStyles) from "./styles/style";
const { View, Text } = simpleStyles(styles);

const Example = () => (
	<View className="centerAll hScreen">
		<Text className="h1 uppercase italic">
			Just like native CSS (almost!)
		</Text>
	</View>
);

export default Example;

You can also mix and match classNames and inline styles and it will work fine.

<Text className='uppercase' style={{ fontWeight: bold }> This works fine too. </Text>

Under the hood

Under the hood reactNativeSimpleStyles is simply a higher order compnent that wraps the regular react native components.

This HOC uses React Native's StyleSheet component along with your stylesheet to find your styles and abstracts away the manual process of creating a StyleSheet for each component. Since the StyleSheet component is used under the hood you still get the same performance benefits (as compared to simply using inline styles, for example)

Disclaimers & Contributions

Full disclosure that I am not primarily a React Native developer and I may have overlooked certain issues that may exist with this library.

If you would like to enhance this library, fix bugs, add typescript support, add tests, or otherwise contribute in any other way you are more than welcome to raise an issue or make a PR.

react-native-simple-styles's People

Contributors

bloycey avatar

Stargazers

Clément L. avatar Abdullah Sheikh avatar  avatar smboy86 avatar Balasubramanian.M avatar  avatar

Watchers

 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.