0.0.19 • Published 12 days ago

tailwind-for-react-native v0.0.19

Weekly downloads
-
License
-
Repository
github
Last release
12 days ago

Get started

Installation

Install this library with npm:

npm i tailwind-for-react-native

or with yarn:

yarn add tailwind-for-react-native

Example

React Native v0.73.4 default wrote with tailwind-for-react-native below.

  import React from 'react';
  import type {PropsWithChildren} from 'react';
  import {
    SafeAreaView,
    ScrollView,
    StatusBar,
    Text,
    useColorScheme,
    View,
  } from 'react-native';

  import {
    Colors,
    DebugInstructions,
    Header,
    LearnMoreLinks,
    ReloadInstructions,
  } from 'react-native/Libraries/NewAppScreen';
  import {styled, TWRNProvider, useTW} from 'tailwind-for-react-native';

  const CustomSafeAreaView = styled(SafeAreaView)`
    bg-lighter
    dark:bg-darker
  `;

  const CustomScrollView = styled(ScrollView)`
    bg-lighter
    dark:bg-darker
  `;

  const ContentContainer = styled(View)`
    bg-white
    dark:bg-black
  `;

  const BoldText = styled(Text)`
    font-bold
  `;

  const SectionContainer = styled(View)`
    mt-32
    px-24
  `;

  const SectionTitle = styled(Text)`
    font-size-24
    font-semibold
    color-black
    dark:color-white
  `;

  const SectionText = styled(Text)`
    mt-8
    font-size-18
    font-normal
    color-dark
    dark:color-light
  `;

  type SectionProps = PropsWithChildren<{
    title: string;
  }>;

  function Section({children, title}: SectionProps): React.JSX.Element {
    return (
      <SectionContainer>
        <SectionTitle>{title}</SectionTitle>
        <SectionText>{children}</SectionText>
      </SectionContainer>
    );
  }

  function YourApp(): React.JSX.Element {
    const isDarkMode = useColorScheme() === 'dark';
    const {tw} = useTW();
    const statusBarBackgroundColor = tw('bg-lighter dark:bg-darker');
    return (
      <CustomSafeAreaView>
        <StatusBar
          barStyle={isDarkMode ? 'light-content' : 'dark-content'}
          backgroundColor={statusBarBackgroundColor.backgroundColor}
        />
        <CustomScrollView contentInsetAdjustmentBehavior="automatic">
          <Header />
          <ContentContainer>
            <Section title="Step One">
              Edit <BoldText>App.tsx</BoldText> to change this screen and then
              come back to see your edits.
            </Section>
            <Section title="See Your Changes">
              <ReloadInstructions />
            </Section>
            <Section title="Debug">
              <DebugInstructions />
            </Section>
            <Section title="Learn More">
              Read the docs to discover what to do next:
            </Section>
            <LearnMoreLinks />
          </ContentContainer>
        </CustomScrollView>
      </CustomSafeAreaView>
    );
  }

  const App = () => {
    const mode = useColorScheme();
    return (
      <TWRNProvider theme={{mode, colors: Colors}}>
        <YourApp />
      </TWRNProvider>
    );
  };

  export default App;

Styled function

Create new componentes with styled function

The styled function allows us to create components from another component using tagged template literals. We use styled and pass it as a parameter a component that accepts a style prop.

import { Pressable, Text } from 'react-native';
import { styled } from 'tailwind-for-react-native';

const Button = styled(Pressable)`
  border-radius-8
  p-6
  h-50
  w-70%
  justify-center
  items-center
`;

const ButtonText = styled(Text)`
  font-size-16
  color-#fff
`;

const MyComponent = () => {
  return (
    <Button>
      <ButtonText>
        Press me
      </ButtonText>
    </Button>
  );
};

Pass props

You can pass props to the components created by the styled function, and access them in the interpolation. The following example receives a color for the background color of the button.

import { Pressable, Text } from "react-native";
import { styled } from "tailwind-for-react-native";

// This component uses color prop to set backgroundColor style
// and uses #fff as default value
const Button = styled(Pressable)`
  bg-${({ color }) => color ?? "#fff"}
`;

const App = () => {
  return (
    <>
      {/* This button is red */}
      <Button color="#f00">
        <Text>Press me</Text>
      </Button>
      {/* This button is blue */}
      <Button color="#00f">
        <Text>Press me</Text>
      </Button>
    </>
  );
};

export default App;

Hook useTW

import { Pressable, Text } from "react-native";
import { useTW } from "tailwind-for-react-native";

const App = () => {
  const {tw} = useTW();
  // Create a style object for your components with tw function
  const buttonStyle = tw('bg-#fff');
  const textStyle = tw('color-#00f font-size-10');
  return (
    <>
      <Button style={buttonStyle}>
        <Text style={textStyle}>Press me</Text>
      </Button>
    </>
  );
};

export default App;

The tw function

  tw: (string) => styles

In addition to the styled function, the tw function allows us to create a style from a string of text with the styles we want to apply.

Dark mode

  mode: 'light' | 'dark' - default 'light'

We can use mode to know if our current state is dark or light.

Colores

  colors: {
    light: Object,
    dark: Object,
  }

The colors object allows us to access the colors available for use in our styles.

TWRNProvider

We can extend the functionality of the library by passing values to the TWRNProvider.

mode

We can control the current mode state of the provider, either 'light' or 'dark'.

colors

Pass a colors object with light or dark fields with its colors to the provider to be able to access them through styles that accept a color, such as backgroundColor(bg) or color(color).

styles

Sometimes we need to add more styles than the library has by default. To do this, we can pass a new styles object to the provider and use it within our functions.

Responsive design

A way to create interfaces that adapt to the dimensions of the screens of different devices is to use percentages. When we create styles, we can pass values to some properties with the functions within styles hp(number) and wp(number), which will return as a value, the percentage of the height or width of the screen, respectively.

import { Button }  from 'react-native';
import { styled }  from 'tailwind-for-react-native';

const SmallButton = styled(Button)`
  w-wp(50)
  h-hp(10)
`;

The code above creates a new component from a React Native button and adds the styles width with 50% of the device screen width, and height with 10% of the device screen height. You can use hp and wp in any style that accepts numeric values.

Dark mode

It is possible to condition styles to be applied only in dark mode, if we want to control the dark mode state it is possible to use TWRNProvider and pass the value of mode to theme prop. We can even combine it with the useColorScheme hook, which provides and subscribes to updates of color schemes from the Appearance module and updates our styles when a change made by the user in the device mode is detected.

import { useColorScheme } from 'react-native';
import { TWRNProvider } from 'tailwind-for-react-native';

const App = () => {
  const mode = useColorScheme();
  return (
    <TWRNProvider theme={{mode}}>
      <YourApp />
    </TWRNProvider>
  );
};

In your application

const Container = styled(View)`
  bg-white
  dark:bg-black
`;

The "bg-white" style, which returns a "backgroundColor: 'white'" value, will always be applied, however, it can be overridden by the second style: "dark:bg-black", when the mode configured in the TWRNProvider state is "dark".

Plataforma (iOS, Android)

We can condition styles to a specific platform (Android or iOS) by prefixing the style name with "android" or "ios" respectively, for the styles we want to condition.

const Container = styled(View)`
  android:bg-#a4c639
  ios:bg-#5856d6
`;

"The backgroundColor with the value #a4c639 will only be applied on devices with Android operating system, and the backgroundColor #5856d6 will only be applied on iOS devices."

Available Computed Properties

Available computed properties needs a dash following by the desired value

NOTE: wp(number) and hp(number) will calculate the window's width and height. Example: If your device width is 390px, wp(50) will return 195. Also hp(number) its based on device height.

Margin

ClassAffected PropertiesAccepted Values
mmarginnumber, string, wp(number), hp(number)
mtmarginTopnumber, string, wp(number), hp(number)
mrmarginRightnumber, string, wp(number), hp(number)
mbmarginBottomnumber, string, wp(number), hp(number)
mlmarginLeftnumber, string, wp(number), hp(number)
mxmarginLeft, marginRightnumber, string, wp(number), hp(number)
mymarginTop, marginBottomnumber, string, wp(number), hp(number)

Padding

ClassAffected PropertiesAccepted Values
ppaddingnumber, string, wp(number), hp(number)
ptpaddingTopnumber, string, wp(number), hp(number)
prpaddingRightnumber, string, wp(number), hp(number)
pbpaddingBottomnumber, string, wp(number), hp(number)
plpaddingLeftnumber, string, wp(number), hp(number)
pxpaddingLeft, paddingRightnumber, string, wp(number), hp(number)
pypaddingTop, paddingBottomnumber, string, wp(number), hp(number)

Height

ClassAffected PropertiesAccepted Values
hheightnumber, string, wp(number), hp(number)

Min Height

ClassAffected PropertiesAccepted Values
min-hminHeightnumber, string, wp(number), hp(number)

Max Height

ClassAffected PropertiesAccepted Values
max-hmaxHeightnumber, string, wp(number), hp(number)

Width

ClassAffected PropertiesAccepted Values
wwidthnumber, string, wp(number), hp(number)

Min Width

ClassAffected PropertiesAccepted Values
min-wminWidthnumber, string, wp(number), hp(number)

Max Width

ClassAffected PropertiesAccepted Values
max-wmaxWidthnumber, string, wp(number), hp(number)

Left

ClassAffected PropertiesAccepted Values
lleftnumber, string, wp(number), hp(number)

Right

ClassAffected PropertiesAccepted Values
rrightnumber, string, wp(number), hp(number)

Top

ClassAffected PropertiesAccepted Values
ttopnumber, string, wp(number), hp(number)

Bottom

ClassAffected PropertiesAccepted Values
bbottomnumber, string, wp(number), hp(number)

Border Radius

ClassAffected PropertiesAccepted Values
border-radiusborderRadiusnumber, wp(number), hp(number)
border-radius-tlborderTopLeftRadiusnumber, wp(number), hp(number)
border-radius-trborderTopRightRadiusnumber, wp(number), hp(number)
border-radius-brborderBottomRightRadiusnumber, wp(number), hp(number)
border-radius-blborderBottomLeftRadiusnumber, wp(number), hp(number)

Border Width

ClassAffected PropertiesAccepted Values
border-bborderBottomWidthnumber, wp(number), hp(number)
border-eborderEndWidthnumber, wp(number), hp(number)
border-lborderLeftWidthnumber, wp(number), hp(number)
border-rborderRightWidthnumber, wp(number), hp(number)
border-sborderStartWidthnumber, wp(number), hp(number)
border-tborderTopWidthnumber, wp(number), hp(number)
border-wborderWidthnumber, wp(number), hp(number)

Background Color

ClassAffected PropertiesAccepted Values
bgbackgroundColorColor (See Constants Section), Hexadecimal color

Color

ClassAffected PropertiesAccepted Values
color (text also might work)colorColor (See Constants Section), Hexadecimal color

Font Size

ClassAffected PropertiesAccepted Values
font-sizefontSizenumber, wp(number), hp(number)

Font Weight

ClassAffected PropertiesAccepted Values
font-weightfontWeightnumber

Line Height

ClassAffected PropertiesAccepted Values
line-heightlineHeightnumber, wp(number), hp(number)

Flex

ClassAffected PropertiesAccepted Values
flexflexnumber

Flex Shrink

ClassAffected PropertiesAccepted Values
flex-shrinkflexShrinknumber

Flex Grow

ClassAffected PropertiesAccepted Values
flex-growflexGrownumber

Aspect Ratio

ClassAffected PropertiesAccepted Values
aspectaspectRationumber

Z Index

ClassAffected PropertiesAccepted Values
zzIndexnumber

Available predefined properties

Direction

Direction

ClassProperties
dir-inheritdirection: 'inherit'
dir-ltrdirection: 'ltr'
dir-rtldirection: 'rtl'

Display

Display

ClassProperties
d-flexdisplay: 'flex'
d-nonedisplay: 'none'

Flex

Align Content
ClassProperties
content-startalignContent: 'flex-start'
content-endalignContent: 'flex-end'
content-stretchalignContent: 'stretch'
content-centeralignContent: 'center'
content-betweenalignContent: 'space-between'
content-aroundalignContent: 'space-around'
Align Items
ClassProperties
items-startalignItems: 'flex-start'
items-endalignItems: 'flex-end'
items-centeralignItems: 'center'
items-baselinealignItems: 'baseline'
items-stretchalignItems: 'stretch'
Align Self
ClassProperties
self-startalignSelf: 'flex-start'
self-endalignSelf: 'flex-end'
self-centeralignSelf: 'center'
self-baselinealignSelf: 'baseline'
self-stretchalignSelf: 'stretch'
Border Style
ClassProperties
border-solidborderStyle: 'solid'
border-dottedborderStyle: 'dotted'
border-dashedborderStyle: 'dashed'
Flex Direction
ClassProperties
flex-colflexDirection: 'column'
flex-rowflexDirection: 'row'
flex-col-reverseflexDirection: 'column-reverse'
flex-row-reverseflexDirection: 'row-reverse'
Flex Wrap
ClassProperties
flex-wrapflexWrap: 'wrap'
flex-nowrapflexWrap: 'nowrap'
flex-wrap-reverseflexWrap: 'wrap-reverse'
Justify Content
ClassProperties
justify-startjustifyContent: 'flex-start'
justify-endjustifyContent: 'flex-end'
justify-centerjustifyContent: 'center'
justify-betweenjustifyContent: 'space-between'
justify-aroundjustifyContent: 'space-around'
justify-evenlyjustifyContent: 'space-evenly'

Font

Font Weight

ClassProperties
font-thinfontWeight: '100'
font-extralightfontWeight: '200'
font-lightfontWeight: '300'
font-normalfontWeight: '400'
font-mediumfontWeight: '500'
font-semiboldfontWeight: '600'
font-boldfontWeight: '700'
font-extraboldfontWeight: '800'
font-blackfontWeight: '900'

Font Style

ClassProperties
font-style-normalfontStyle: 'normal'
font-style-italicfontStyle: 'italic'

Position

Position

ClassProperties
relativeposition: 'relative'
absoluteposition: 'absolute'

Overflow

Overflow

ClassProperties
overflow-visibleoverflow: 'visible'
overflow-hiddenoverflow: 'hidden'
overflow-scrolloverflow: 'scroll'

Text

Text Align

ClassProperties
text-autotextAlign: 'auto'
text-lefttextAlign: 'left'
text-centertextAlign: 'center'
text-righttextAlign: 'right'
text-justifytextAlign: 'justify'

Text Decoration Line

ClassProperties
nonetextDecorationLine: 'none'
underlinetextDecorationLine: 'underline'
line-throughtextDecorationLine: 'line-through'
underline-line-throughtextDecorationLine: 'underline line-through'

Constants

Colors

CodeValue
blue-50'#EFF6FF'
blue-100'#DBEAFE'
blue-200'#BFDBFE'
blue-300'#93C5FD'
blue-400'#60A5FA'
blue-500'#3B82F6'
blue-600'#2563EB'
blue-700'#1D4ED8'
blue-800'#1E40AF'
blue-900'#1E3A8A'
yellow-50'#FFFBEB'
yellow-100'#FEF3C7'
yellow-200'#FDE68A'
yellow-300'#FCD34D'
yellow-400'#FBBF24'
yellow-500'#F59E0B'
yellow-600'#D97706'
yellow-700'#B45309'
yellow-800'#92400E'
yellow-900'#78350F'
red-50'#FEF2F2'
red-100'#FEE2E2'
red-200'#FECACA'
red-300'#FCA5A5'
red-400'#F87171'
red-500'#EF4444'
red-600'#DC2626'
red-700'#B91C1C'
red-800'#991B1B'
red-900'#7F1D1D'
purple-50'#F5F3FF'
purple-100'#EDE9FE'
purple-200'#DDD6FE'
purple-300'#C4B5FD'
purple-400'#A78BFA'
purple-500'#8B5CF6'
purple-600'#7C3AED'
purple-700'#6D28D9'
purple-800'#5B21B6'
purple-900'#4C1D95'
pink-50'#FDF2F8'
pink-100'#FCE7F3'
pink-200'#FBCFE8'
pink-300'#F9A8D4'
pink-400'#F472B6'
pink-500'#EC4899'
pink-600'#DB2777'
pink-700'#BE185D'
pink-800'#9D174D'
pink-900'#831843'
indigo-50'#EEF2FF'
indigo-100'#E0E7FF'
indigo-200'#C7D2FE'
indigo-300'#A5B4FC'
indigo-400'#818CF8'
indigo-500'#6366F1'
indigo-600'#4F46E5'
indigo-700'#4338CA'
indigo-800'#3730A3'
indigo-900'#312E81'
green-50'#ECFDF5'
green-100'#D1FAE5'
green-200'#A7F3D0'
green-300'#6EE7B7'
green-400'#34D399'
green-500'#10B981'
green-600'#059669'
green-700'#047857'
green-800'#065F46'
green-900'#064E3B'
gray-50'#F9FAFB'
gray-100'#F3F4F6'
gray-200'#E5E7EB'
gray-300'#D1D5DB'
gray-400'#9CA3AF'
gray-500'#6B7280'
gray-600'#4B5563'
gray-700'#374151'
gray-800'#1F2937'
gray-900'#111827'
0.0.19

12 days ago

0.0.18

1 month ago

0.0.17

1 month ago

0.0.15

1 month ago

0.0.16

1 month ago

0.0.14

1 month ago

0.0.12

2 months ago

0.0.13

2 months ago

0.0.11

2 months ago

0.0.10

2 months ago

0.0.9

2 months ago

0.0.8

3 months ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago