1.1.12 • Published 3 years ago

react-native-picasso v1.1.12

Weekly downloads
48
License
MIT
Repository
github
Last release
3 years ago

react-native-picasso

This is NOT yet another components library !

npm npm

demonstration

Motivation

Picasso is a toolkit to help you create layouts for your React Native apps in a clear and declarative way. No more ugly inline styles or duplicate stylesheets. You can now use classNames as you would do on the web.

Having a strong design system helps you to focus on functionnality instead of design fine tuning and endless replacements.

This will give your app a strong single source of truth. You declare your design system once and then consume it in each of your screens.

This was heavily inspired by Bootstrap

Installation

yarn add react-native-picasso

or with npm

npm i --save react-native-picasso

Documentation

Full documentation can be found here : https://meienberger.github.io/react-native-picasso/

Usage

Picasso ships with a set of drop in replacements components. You can import the following three components : View, SafeAreaView, Text

import { View, Text, SafeAreaView } from 'react-native-picasso'

All those components are sharing the same API as their usual react-native counter party. But they have an aditionnal prop called className. The className property consists of a string of pre-defined classes separated by a space.

Classes are constructed the following way : {property}-{value}

Using this string, you can describe the apparence of your components.

For example the following className="ml-md p-lg" would be translated to margin-left: medium; padding: large.

All the values are declared in a defaultTheme variable. Which is :

{
    colors: {
        background: '#eeeeee',
        primary: '#00B386',
        secondary: '#dedede',
    },
    textColors: {
        primary: '#333333',
        secondary: '#666666',
        white: '#ffffff',
    },
    font: {
        family: 'Helvetica',
        sizes: {
            sm: 12,
            md: 16,
            lg: 24,
            xl: 32,
            xxl: 40,
        },
        weights: {
            light: '100',
            normal: 'normal',
            bold: 'bold',
            extrabold: '900'
        },
    },
    elevated: {
        shadowColor: '#000000',
        shadowOffset: { width: 0, height: 5 },
        shadowOpacity: 0.2,
        shadowRadius: 3,
        elevation: 5,
    },
    radius: {
        sm: 5,
        md: 10,
        lg: 20,
        xl: 40,
        round: 1000,
    },
    spacing: {
        sm: 8,
        md: 16,
        lg: 24,
        xl: 32,
        xxl: 40,
    },
}

If we take the previous example, margin and padding are using the "spacing" values. So the final result of className="ml-md p-lg" would be :

{
    marginLeft: 16,
    padding: 24
}

Here is a table of all the possible properties and values you can use inside a className.

Base (Available for all components)

PropertyPossible valuesDescription
psm, md, lg, xl, xxlpadding
plsm, md, lg, xl, xxlpaddingLeft
prsm, md, lg, xl, xxlpaddingRight
ptsm, md, lg, xl, xxlpaddingTop
pbsm, md, lg, xl, xxlpaddingBottom
pxsm, md, lg, xl, xxlpaddingHorizontal
pysm, md, lg, xl, xxlpaddingVertical
------------------------------------------------
msm, md, lg, xl, xxlmargin
mlsm, md, lg, xl, xxlmarginLeft
mrsm, md, lg, xl, xxlmarginRight
mtsm, md, lg, xl, xxlmarginTop
mbsm, md, lg, xl, xxlmarginBottom
mxsm, md, lg, xl, xxlmarginHorizontal
mysm, md, lg, xl, xxlmarginVertical
------------------------------------------------
flexrow, columnflexDirection
flexany numberflex: value
alignselfcenter, start, end, stretch, baselinealignSelf

View specific

PropertyPossible valuesDescription
elevatedno valueAdds a drop shadow to your view
radiussm, md, lg, xlborderRadius
radiustlsm, md, lg, xlborderTopLeftRadius
radiustrsm, md, lg, xlborderTopRightRadius
radiusblsm, md, lg, xlborderBottomLeftRadius
radiusbrsm, md, lg, xlborderBottomRightRadius
bgprimary, secondary, backgroundbackgroundColor
justifycontentcenter, start, end, around, between, evenlyjustifyContent
alignitemscenter, start, end, stretch, baselinealignItems
bordercolorprimary, secondary, background, borderborderColor
bany numberborderWidth
brany numberborderRightWidth
blany numberborderLeftWidth
btany numberborderTopWidth
bbany numberborderBottomWidth

Text specific

PropertyPossible valuesDescription
weightlight, normal, bold, extraboldfontWeight
aligncenter, left, right, justifytextAlign
colorprimary, secondary, whitecolor
sizesm, md, lg, xl, xxlfontSize

Each value of each property is mapped to a specific section of the theme.

Examples

<View className="p-md radius-md elevated">{/* Content */}<View>

Translates into

<View style={{
    padding: 16,
    borderRadius: 10,
    shadowColor: '#000000',
    shadowOffset: { width: 0, height: 5 },
    shadowOpacity: 0.2,
    shadowRadius: 3,
    elevation: 5
}}>
    {/* Content */}
<View>

Customizing the theme

Picasso ships with a default theme. But you most probably won't use the default one and want to use your own colors, sizes and values.

This can be achieved by using the ThemeProvider component. What you need to do is to simply import the theme provider and wrap your whole app into it. Then you can pass a custom theme variable to override specific parts of the default theme.

For example in your App.js file :

import React from 'react'
import { ThemeProvider } from 'react-native-picasso'

const theme = {
  colors: {
    primary: '#00B386',
    secondary: '#dedede',
  },
  textColors: {
    primary: '#333333',
  },
  spacing: {
    sm: 8,
    md: 16,
    lg: 24,
    xl: 32,
    xxl: 40,
  },
}

const App = () => {
  return <ThemeProvider theme={theme}>{/* Rest of your app */}</ThemeProvider>
}

export default App

The ThemeProvider will look at your custom theme and apply those values to the default theme. This way, you can only override specific aspects of the theme and keep the default values for the rest.

Be careful to follow the original structure of the theme. Otherwise your styles may break.

Merging with classic styles

Sometimes, the classes available are not sufficient for your specific need. In order to give you enough flexibility with the components, you can still use the default style property in combination with picasso classes.

<View className="m-xl" style={{ opacity: 0.5 }} />

Creating your own Picasso component

If you want to add all the Picasso utility classes to any component, you can do so by using the createPicassoComponent HOC. Simply wrap your component inside the HOC and start using the className prop.

import React from 'react'
import { Image } from 'react-native'
import { createPicassoComponent } from 'react-native-picasso'

const PicassoImage = createPicassoComponent(Image)

const App = () => {
  return (
    <>
      <PicassoImage className="m-lg radius-md" style={{ height: 100, width: 100 }} source={{require('./path/to/image')}} />
    </>
  )
}

export default App

Adding your own classes

You can add your own values to use as classes. For example if you wanted to add a complex style for a card. You just need to add a new property at the root of your custom theme. And then you can simply use the class anywhere in your app on a Picasso component.

import React from 'react'
import { ThemeProvider, View, Text } from 'react-native-picasso'

const theme = {
  card: {
    backgroundColor: '#dedede',
    shadowColor: '#000000',
    shadowOffset: { width: 0, height: 5 },
    shadowOpacity: 0.2,
    shadowRadius: 3,
    elevation: 5,
    alignItems: 'center',
    justifyContenr: 'center',
    borderRadius: 20,
  },
}

const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <View className="card">
        <Text>Hello from card</Text>
      </View>
    </ThemeProvider>
  )
}

export default App

You can also add custom values to existing properties and all the properties which are using this specific set of values will have access to it. For example :

import React from 'react'
import { ThemeProvider, View, Text } from 'react-native-picasso'

const theme = {
  colors: {
    mycolor: '#00B386',
  },
  textColors: {
    supercolor: '#452eff',
  },
  radius: {
    insane: 100,
  },
}

const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <View className="bg-mycolor radius-insane">
        <Text className="color-supercolor">Hello from custom color</Text>
      </View>
    </ThemeProvider>
  )
}

export default App

Acknowledgements

TODO

  • TypeScript
1.1.12

3 years ago

1.1.11

3 years ago

1.1.9

3 years ago

1.1.10

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

4 years ago

1.0.29

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.23

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago