native-stylist
v1.1.1
Published
[](https://www.npmjs.com/package/native-stylist)
Readme
Native Stylist
Native Stylist is an npm package designed to streamline the process of styling React Native components. It provides a set of utilities to manage styles more efficiently and consistently.
This was inspired by, but is way less powerful than, Tailwind CSS + NativeWind.
Table of Contents
Installation
You can install Native Stylist using npm or yarn:
npm install native-stylistor
yarn add native-stylistAPI
<View /> Component
The View component exported from Native Stylist is an enhanced version of the standard React Native View component. It supports additional boolean properties for margins and paddings, making it easier to apply spacing.
The spacing unit is 8. In the future I'd like to allow for changing this, but for now it's set.
Margin Properties
m1tom8: Sets margin of 1 to 8 units on all sides.mt1tomt8: Sets margin top of 1 to 8 units.mr1tomr8: Sets margin right of 1 to 8 units.mb1tomb8: Sets margin bottom of 1 to 8 units.ml1toml8: Sets margin left of 1 to 8 units.mHalf,mtHalf,mrHalf,mbHalf,mlHalf: Sets margin on one or all sides to a half unit
Padding Properties
p1top8: Sets padding of 1 to 8 units on all sides.pt1topt8: Sets padding top of 1 to 8 units.pr1topr8: Sets padding right of 1 to 8 units.pb1topb8: Sets padding bottom of 1 to 8 units.pl1topl8: Sets padding left of 1 to 8 units.pHalf,ptHalf,prHalf,pbHalf,plHalf: Sets padding on one or all sides to a half unit
Gap Properties
gap1togap8: Sets gap of 1 to 8 units between children.
Grow/Shrink Properties
grow1: Sets{ flexGrow: 1 }shrink1: Sets{ flexShrink: 1 }
Layout
absolute: Sets{ position: 'absolute' }relative: Sets{ position: 'relative' }pinToTop: Sets{ top: 0 }pinToRight: Sets{ right: 0 }pinToBottom: Sets{ bottom: 0 }pinToLeft: Sets{ left: 0 }fullWidth: Sets{ width: '100%' }halfWidth: Sets{ width: '50%' }flex1: Sets{ flex: 1 }flexRow: Sets{ flexDirection: 'row' }flexColumn: Sets{ flexDirection: 'column' }justifyStart: Sets{ justifyContent: 'flex-start' }justifyCenter: Sets{ justifyContent: 'center' }justifyEnd: Sets{ justifyContent: 'flex-end' }justifyBetween: Sets{ justifyContent: 'space-between' }justifyAround: Sets{ justifyContent: 'space-around' }alignStart: Sets{ alignItems: 'flex-start' }alignCenter: Sets{ alignItems: 'center' }alignEnd: Sets{ alignItems: 'flex-end' }z1toz4: SetszIndexto 1 to 4br1tobr4: SetsborderRadiusto 14, 8, 16, and 32 unitsoverflowHidden: Sets{ overflow: 'hidden' }
Shadow Properties
shadow: Sets a shadow with a default configurationshadowUp: Sets a shadow that goes up, good for using on a fix bottom bar
Background Color Properties
(This needs to be improved)
bgWhite: Sets background color to whitebgGray: Sets background color to gray
<Text /> Component
The Text component exported from Native Stylist is an enhanced version of the standard React Native Text component. It supports additional boolean properties for margins and paddings, making it easier to apply spacing.
The spacing unit is 8. In the future I'd like to allow for changing this, but for now it's set.
Color Properties
light: Sets{ color: '#ffffff' }lightish: Sets{ color: '#757575' }error: Sets{ color: '#B30000' }brand: Sets the color to the brand color (see ###Customization)
Size, Weight, and Style Properties
bold: Sets{ fontWeight: 'bold' }or{ fontFamily: 'your-specified-bold-font} if set.smallSets{ fontSize: 16 }- This is the default sizexsmallSets{ fontSize: 14 }mediumSets{ fontSize: 20 }largeSets{ fontSize: 24 }xlargeSets{ fontSize: 32 }xxlargeSets{ fontSize: 48 }underline: Sets{ textDecorationLine: 'underline' }strikeThrough: Sets{ textDecorationLine: 'line-through' }italic: Sets{ fontStyle: 'italic' }
Other Properties
shadow: Sets a shadow with a default configurationcenter: Sets{ textAlign: 'center' }
Margin Properties
m1tom8: Sets margin of 1 to 8 units on all sides.mt1tomt8: Sets margin top of 1 to 8 units.mr1tomr8: Sets margin right of 1 to 8 units.mb1tomb8: Sets margin bottom of 1 to 8 units.ml1toml8: Sets margin left of 1 to 8 units.mHalf,mtHalf,mrHalf,mbHalf,mlHalf: Sets margin on one or all sides to a half unit
Padding Properties
p1top8: Sets padding of 1 to 8 units on all sides.pt1topt8: Sets padding top of 1 to 8 units.pr1topr8: Sets padding right of 1 to 8 units.pb1topb8: Sets padding bottom of 1 to 8 units.pl1topl8: Sets padding left of 1 to 8 units.pHalf,ptHalf,prHalf,pbHalf,plHalf: Sets padding on one or all sides to a half unit
Usage
import { SafeAreaView, SafeAreaProvider } from 'react-native-safe-area-context';
import { Text, View } from 'native-stylist';
export default function App() {
return (
<SafeAreaProvider>
<SafeAreaView>
<Text ml2 xlarge bold>
This is a Title
</Text>
<Text ml2 medium lightish>
Maybe a subtitle here
</Text>
<View pt4 pl2 pr2 flexRow justifyBetween>
<Text>I am on the left</Text>
<Text>right over here</Text>
</View>
<View mt4 flexRow gap1 style={{ height: 100 }}>
<View
p1
flex1
alignStart
justifyStart
style={{ backgroundColor: 'red', alignItems: 'flex-start' }}>
<Text light>Red</Text>
</View>
<View
p1
flex1
alignCenter
justifyCenter
style={{ backgroundColor: 'green' }}>
<Text light>Green</Text>
</View>
<View
p1
flex1
alignEnd
justifyEnd
style={{ backgroundColor: 'blue' }}>
<Text light>Blue</Text>
</View>
</View>
<View mt4 relative style={{ height: 200, backgroundColor: '#ff7800' }}>
<View m2 absolute z1 pinToTop pinToRight bgWhite shadow br2 p1>
<Text bold>X</Text>
</View>
</View>
</SafeAreaView>
</SafeAreaProvider>
);
}Examples
Fonts and Colors
Instead of using the default colors and fonts, you can customize them by using the StylistTheme context provider. This will allow you to set your own colors and fonts in once place. You'll need to wrap your app with this provider
import { SafeAreaView, SafeAreaProvider } from 'react-native-safe-area-context';
import { Text, View, StylistTheme } from 'native-stylist';
export default function App() {
return (
<SafeAreaProvider>
<StylistTheme.Provider
value={{
fontFamily: 'maven-pro',
fontFamilyBold: 'maven-pro-black',
brandColor: colors.green,
}}
>
<Text>This text and any further text nested inside the provider will have its `fontFamily` set to `maven-pro` and bold `fontFamily` set to `maven-pro-black.</Text>
<Text brand>This text color will match that passed in the `brandColor` valu ein the provider.</Text>
</StylistTheme>
</SafeAreaProvider>
);