All Projects → z0al → React Native Styled.macro

z0al / React Native Styled.macro

Licence: mit
⚛️ A Utility-First Styling Library for React Native

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Native Styled.macro

Reactnativelaravellogin
Sample App for login using laravel 5.5 React Native and Redux
Stars: ✭ 75 (-15.73%)
Mutual labels:  expo
App Store Tailwind
Mojave App Store Rebuild with Tailwind CSS, Electron and Vue
Stars: ✭ 82 (-7.87%)
Mutual labels:  tailwindcss
Tailwind Boilerplate
Tailwind CSS + Vite Boilerplate
Stars: ✭ 86 (-3.37%)
Mutual labels:  tailwindcss
Stts
A starter template for Svelte, Tailwind, Typescript, and Snowpack
Stars: ✭ 78 (-12.36%)
Mutual labels:  tailwindcss
Tailwindcss Pseudo Elements
TailwindCSS Plugin that adds variants of pseudo elements.
Stars: ✭ 81 (-8.99%)
Mutual labels:  tailwindcss
Tailwindcss Tables
Bootstrap styled tables for Tailwind CSS
Stars: ✭ 84 (-5.62%)
Mutual labels:  tailwindcss
Pingcrm Svelte
🦊 Ping CRM Svelte - A demo app to illustrate how Inertia.js works with Laravel and Svelte (hosted on a heroku free dyno).
Stars: ✭ 74 (-16.85%)
Mutual labels:  tailwindcss
Docker Django Example
A production ready example Django app that's using Docker and Docker Compose.
Stars: ✭ 86 (-3.37%)
Mutual labels:  tailwindcss
Sonwan Ui
SonWan UI is a modular UI component library based on figma design to build your next React Web Application.
Stars: ✭ 75 (-15.73%)
Mutual labels:  tailwindcss
Vue Cli Plugin Tailwind
Utility-first, modern CSS for your Vue app using TailwindCSS.
Stars: ✭ 85 (-4.49%)
Mutual labels:  tailwindcss
Postcss Elm Tailwind
put some tailwind in your elm
Stars: ✭ 80 (-10.11%)
Mutual labels:  tailwindcss
React Native Bottomsheet Reanimated
React Native bottom sheet with fully native 60 FPS animations and awesome user experience
Stars: ✭ 80 (-10.11%)
Mutual labels:  expo
Tailwind Examples
A collection of web pages built in Tailwind CSS v0.7.4
Stars: ✭ 84 (-5.62%)
Mutual labels:  tailwindcss
Expo Three Demo
🍎👩‍🏫 Collection of Demos for THREE.js in Expo!
Stars: ✭ 76 (-14.61%)
Mutual labels:  expo
Covid Volunteers
Organizing and matching volunteers with COVID-19 projects
Stars: ✭ 87 (-2.25%)
Mutual labels:  tailwindcss
React Native Blockchain Poll
Source code of bringing-the-blockchain-to-react-native blog post.
Stars: ✭ 75 (-15.73%)
Mutual labels:  expo
Starter Kit Cool Writings
Statamic Starter Kit: Cool Writings
Stars: ✭ 84 (-5.62%)
Mutual labels:  tailwindcss
Expo Boilerplate
React Native/Expo starting boilerplate with basic features (auth, tabs, i18n, redux, validation, notifications)
Stars: ✭ 88 (-1.12%)
Mutual labels:  expo
Vue Shopping Cart
Simple shopping cart with Vue.js and Deno.js
Stars: ✭ 87 (-2.25%)
Mutual labels:  tailwindcss
Reactconfbr App
React Conf BR App built with create-react-native-app
Stars: ✭ 84 (-5.62%)
Mutual labels:  expo

Disclaimer: This library is still in Beta. Use with caution. The Roadmap for v1.0.0 is available here.

💅 styled.macro

A Utility-first Styling Library for React Native.

Features

  • Zero-overhead: Styles get injected via the StyleSheet API during compilation.
  • 🍂 Minimal footprint: Styles that are never used won't make it to the final App bundle.
  • 🎲 Variants support: Conditionally style based on Platform, Layout or Screen size ... etc.
  • 💅 Style props: Supports common style-related Component props e.g. numberOfLines.
  • 🔌 Customizable (Coming soon): Optionally override the default theme by adding styled.config.js file

Table of Contents

Getting started

Compatible with React Native v0.62.0 or later

yarn add react-native-styled.macro babel-plugin-macros

Add babel-plugin-macros to your Babel config (if you haven't already)

// babel.config.js
module.exports = function (api) {
	return {
		plugins: ['macros'],
		// ... other stuff
	};
};

To use the library simply import the macro as follows:

import styled from 'react-native-styled.macro';

const Heading = ({ text }) => (
	<Text
		{...styled([
			'my-4',
			'text-2xl',
			'text-gray-900',
			'font-semibold',
			'letter-wide',
		])}
	>
		{text}
	</Text>
);

The compiled output for the above code will look something like the following:

import { Text } from 'react-native';
+import { StyleSheet } from 'react-native';
+import { rem } from 'react-native-styled.macro/path/not/relevant';
-import styled from 'react-native-styled.macro';

+const styles = StyleSheet.create({
+	_default: {
+		marginVertical: rem(1),
+		fontSize: rem(1.5),
+		color: '#1a202c',
+		fontWeight: '600',
+		letterSpacing: rem(0.025),
+	},
+});

const Heading = ({ text }) => (
	<Text
-		{...styled([
-			'my-4',
-			'text-2xl',
-			'text-gray-900',
-			'font-semibold',
-			'letter-wide',
-		])}
+		{...{
+			style: styles._default,
+		}}
	>
		{text}
	</Text>
);

How does it work?

  • styled (you can name it anything) is a Babel Macro which means it will be executed during compilation.
  • It will map the given styles and resolve the necessary style attributes/props.
  • It will try to merge styles of the same variant if possible so we don't end up creating an object for every style e.g. text-2xl.
  • For the best performance, it will then use the good/old StyleSheet.create to create the styles as you should normally do by yourself in a React Native app.

The output for any code you write will look more or less the same as above. The only exception is a style with multiple variants because we need to add logic to switch styles at runtime (same as you would do e.g. using Platform.select())

Available Styles

See docs/styles.md

Variants

Platform (Built-in)

Enables Platform-specific style. Based on the value of Platform.OS.

Possible values: android, ios, web or whatever the value of Platform.OS.

Example:

styled([
	'bg-white',
	'web:bg-purple-600',
	'android:bg-green-600',
	'ios:bg-blue-600',
]);

Layout (Built-in)

Enables Layout-specific style. Based on the value of I18nManager.isRTL.

Possible keys: ltr or rtl.

Example:

styled(['text-auto', 'rtl:text-right', 'ltr:text-left']);

Responsive

Built on the top of React Native's useWindowDimensions hook. Possible keys: sm, md, lg, xl or custom values (see below).

Example

import styled from 'react-native-styled.macro';
import { useWindowVariant } from 'react-native-styled.macro/lib';

const MyComponent = () => {
	const windowVariant = useWindowVariant();

	return (
		<Text
			{...styled(['w-full', 'md:w-64'], {
				...windowVariant /* other variants */,
			})}
		>
			My text
		</Text>
	);
};

You can also pass custom breakpoints as follows:

// Note: passing a custom object will remove the default breakpoints e.g. `sm`.
useWindowVariant({
	tablet: 640,
	laptop: 768,
	// .. anything really
});

// use it later
styled(['tablet:w-full', 'laptop:w-64']);

Dark mode

Since styled accepts arbitrary keys as variants supporting Dark mode can be easily acheived as follows:

import { useColorScheme } from 'react-native';
import styled from 'react-native-styled.macro';

const MyComponent = () => {
	// Can either be 'dark' or 'light'
	const colorScheme = useColorScheme();

	return (
		<Text
			{...styled(['text-black', 'dark:text-white'], {
				dark: colorScheme === 'dark',
			})}
		>
			My text
		</Text>
	);
};

Best Practices

Group variant styles together

Do NOT

styled(['web:bg-gray-100', 'bg-white', 'text-black', 'web:rounded']);

Do

styled(['bg-white', 'text-black', 'web:rounded', 'web:bg-gray-100']);

In addition to the readability concern, it also enables some compile-time optimizations.

Prior Art

  • Tailwind CSS (website): Tailwind is a great utility-first CSS framework. We borrowed the utility-first approach from there and re-imagined how it can be used in React Native apps to build user interfaces faster without additional Runtime overhead.

Alternatives

  • tailwind-react-native (github): A very promising Tailwind-like library made with React Native in mind.

License

MIT © Ahmed T. Ali

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