项目作者: farwayer

项目描述 :
Tachyons-like styles for React Native
高级语言: JavaScript
项目地址: git://github.com/farwayer/react-native-tstyles.git
创建时间: 2019-02-25T20:38:44Z
项目社区:https://github.com/farwayer/react-native-tstyles

开源协议:

下载


React Native Tachyons Styles

Tachyons-like styles for React Native

NPM version

Configuration

There are two possible options: using default styles or init with a custom
config.

Default styles

  1. /* View styles: {
  2. marginHorizontal: 16,
  3. flex: 1,
  4. flexDirection: 'row',
  5. justifyContent: 'flex-end',
  6. } */
  7. import s from 'react-native-tstyles'
  8. function MyView() {
  9. return (
  10. <View style={s`mh16 f1 row jcfe`}>
  11. ...
  12. </View>
  13. )
  14. }

Init with custom config

ui/styles.js

  1. import {createStyles} from 'react-native-tstyles'
  2. export default createStyles({
  3. dimensions: [14], // extra, default in dimensions.js
  4. fontSizes: [56], // extra, default 0-48
  5. indexes: [14], // for zIndex and elevation; extra, default 0-10
  6. colors: {
  7. White: '#ffffff',
  8. Purple: '#6963d6',
  9. Yellow: '#FFFF00',
  10. },
  11. styles: { // custom extra styles
  12. paper: {
  13. elevation: 1,
  14. shadowOffset: {
  15. width: 1,
  16. height: 1,
  17. },
  18. shadowRadius: 1,
  19. shadowOpacity: 0.2,
  20. borderRadius: 2,
  21. backgroundColor: 'white',
  22. },
  23. },
  24. })

paper-with-text.js

  1. /* View styles: {
  2. marginHorizontal: 14,
  3. flex: 1,
  4. alignItems: 'center',
  5. elevation: 1,
  6. shadowOffset: {
  7. width: 1,
  8. height: 1,
  9. },
  10. shadowRadius: 1,
  11. shadowOpacity: 0.2,
  12. borderRadius: 2,
  13. backgroundColor: 'white',
  14. } */
  15. /* Text styles: {
  16. fontSize: 56,
  17. color: '#6963d6',
  18. } */
  19. import s from 'ui/styles'
  20. export default function PaperWithText({text, warn}) {
  21. return (
  22. <View style={s`mh14 f1 aic paper`}>
  23. <Text style={s(`fs56`, warn ? `yellow` : `purple`)}>
  24. {text}
  25. </Text>
  26. </View>
  27. )
  28. }

Preventing extra rendering memo’ized and Pure- components

s take care result style property do not change if source styles are the same.
Styles checked by the reference not by the value. So prevent using new js
objects each render time like s('row', {height: 160}). If you need custom
style than you should create it once and use as function argument
s('row', heightStyle).

cn() helper: classname for ReactNative

You can use cn() helper for conditional styles.

  1. s.cn(
  2. [flag1, onStyles1, offStyles1],
  3. [flag2, onStyles2, offStyles2],
  4. ...
  5. )
  1. import s from 'react-native-tstyles'
  2. function SelectableText({
  3. enabled,
  4. selected,
  5. ...props
  6. }) {
  7. const textStyle = s.cn(
  8. [enabled, 'fs16', 'fs14'], // if enabled than s.fs16 else s.fs14
  9. [selected, 'ttu b'], // if selected than s.ttu and s.b
  10. )
  11. return (
  12. <Text {...props} style={textStyle}></Text>
  13. )
  14. }