1.0.7 • Published 7 years ago

styled-responsive v1.0.7

Weekly downloads
41
License
MIT
Repository
-
Last release
7 years ago

Quick Start

npm install --save styled-components styled-responsive

import react from 'react';
import styled from 'styled-components';
import { media, mediaFromProps } from 'styled-responsive';

const Text = styled.span`
    ${ media('xs_')`
      font-size: 1em;
      padding: 1em;
    `}
    ${ media('sm_')`
      font-size: 1.3em;
      padding: 1.3em;
    `}
    ${ media('md_')`
      font-size: 2em;
      padding: 2em;
    `}  
    ${ (props) => { return mediaFromProps( {}, props) } }  
`

const myOtherComponent = props => {
    const styles = {
        xs: {
            margin:'5px',
            backgroundColor:'papayawhip',
            fontFamily:'sans-serif',
            color:'tomato',
        }
    }

    return <Text xs_={{styles.xs}} sm_color='darkseagreen' md_backgroundColor='aliceblue'>Hello</Text>
}

Result:

Result of quick start components

Getting Started

The goal is to be able to design anything with responsiveness in mind. So far, we provide two ways:

1. Component side

Here you explictly specify style rules for each breakpoints within your component.

//myComponents/Text.js
import styled from 'styled-components';
import { media } from 'styled-responsive';

const Text = styled.span`
  ${ media('sm_')`
    // everything here will apply under the 'sm' breakpoint and up
    font-size: 1em;
  `}
  ${ media('xl_')`
    // everything here will apply under the 'xl' breakpoint and up
    font-size: 2em;
  `}  
`
export default Text

2. From props

Here you add superpowers to your components: authorizing breakpoint-based style props.

//myComponents/Text.js
import styled from 'styled-components'
import { mediaFromProps } from 'styled-responsive'

const Text = styled.span`
  ${ (props) => { return mediaFromProps( {}, props) } }
`
export default Text

Now just use whatever camelcased css and put any of xs_, sm_, md_, lg_ or xl_ in front of it: properties will apply according to each breakpoint.

For example:

  • xs_backgroundColor:'red' will apply backgroundColor:'red' from the xs_ breakpoint and up.
  • sm_display:'none' will apply display:'none' from the sm_ breakpoint and up.
  • md_fontSize:'1em' will apply fontSize:'1em' from the md_ breakpoint and up.
  • lg_paddingRight:'5px' will apply paddingRight:'5px' from the lg_ breakpoint and up.
  • xl_minHeight:'300px' will apply minHeight:'300px' from the xl_ breakpoint and up.

...got it?!

Wait... you can also provide an object to any of these prefixed props. For example:

  • xs_={{backgroundColor:'red', fontSize:'1em', padding:'1em'}}
  • sm_={{backgroundColor:'green', fontSize:'2em', padding:'2em'}}
  • md_={{backgroundColor:'blue', fontSize:'3em', padding:'3em'}}
  • lg_={{backgroundColor:'pink', fontSize:'4em', padding:'4em'}}
  • xl_={{backgroundColor:'orange', fontSize:'5em', padding:'5em'}}

Obviously, you can combine both at will. So this is valid:

<MyComponent xs_={{backgroundColor:'red', fontSize:'1em', padding:'1em'}} xs_fontSize='3em' />

As in the style prop, the last property always replaces previous ones. This means that, in our example, fontSize will be set to the latest assigned value: xs_fontSize='3em'.

Back to our Text component, we could consume it like this:

//OtherComponent.js
import React, { Component } from 'react'
import Text from './myComponents/Text.js'

class OtherComponent extends Component {
  //...
  render(){
    const styles = {
      medium: {backgroundColor:'pink'}
    }

    return(
      <Text xs_backgroundColor='red' sm_={{backgroundColor:'white'}} md_={styles.medium} lg_backgroundColor='green' xl_backgroundColor='blue'/>
    )
  }
}
`

Here, Text will display a span with a...

  • ...red background on xs_ breakpoint and up
  • ...white background on sm_ breakpoint an up
  • ...pink background on md_ breakpoint an up
  • ...green background on lg_ breakpoint an up
  • ...blue background on xl_ breakpoint an up

Customizing Breakpoints

styled-responsive comes with most common breakpoints:

const defaultBreakpoints = {
  xs_:'only screen and (min-width: 0px)',
  sm_:'only screen and (min-width: 576px)',
  md_:'only screen and (min-width: 768px)',
  lg_:'only screen and (min-width: 992px)',
  xl_:'only screen and (min-width: 1200px)'
}

Now, who are we to impose those to you ?! Just override it ! You can easily apply your own media queries by creating a similar object like:

//myBreakpoints.js
export const myBreakpoints = {
  phones_: 'only screen and (min-width: 0px)',
  tablets_: 'only screen and (min-width: 400px)',
  desktops_: 'only screen and (min-width: 800px)'
}

Note: all standard media queries are available to you, so you are free to define more complex queries matching your use-cases like portrait, max-width, min-height etc...

Now, you can provide it to media() :

import myBreakpoints from './myBreakpoints.js'

const Text = styled.span`
  ${ media('phones_', myBreakpoints)`
    // everything here will apply under the 'phones_' breakpoint and up
    font-size: 1em;
  `}
  ${ media('desktops_', myBreakpoints)`
    // everything here will apply under the 'desktops_' breakpoint and up
    font-size: 2em;
  `}  
`

or mediaFromProps() :

import myBreakpoints from './myBreakpoints.js'

const Text = styled.span`
  ${ (props) => { return mediaFromProps( {breakpoints:myBreakpoints}, props) } }
`

Obviously, if you change breakpoints names, your responsive props should be prefixed accordingly. In our example, we now should use prefixes phones_, tablets_ and desktops_ instead of the default ones (xs_, sm_, md_ etc...), like:

<MyComponent phones_={{backgroundColor:'red', fontSize:'1em', padding:'1em'}} desktops_fontSize='3em' />

Shortcuts

We are really lazy... so we created shortcuts properties that just translates into classic longer properties. This enables us to write p:'10px' which will translate to padding:'10px', or mb:'5px' which is the same as writing marginBottom:5px... Thinking this way, we also ship react-native's Vertical and Horizontal helpers for a few classic properties like padding, margin and border. So you can write mh:'auto' which is strictly the same as writing marginLeft:'auto', marginRight:'auto'. ...cool, right ?!

Here is the complete shortcuts list:

ShortcutTranslates into...ExampleTranslated example:
wwidthw:'100px'width:'100px'
hheighth:'100px'height:'100px'
minWminWidthminW:'100px'minWidth:'100px'
minHminHeightminH:'100px'minHeight:'100px'
maxWmaxWidthmaxW:'100px'maxWidth:'100px'
maxHmaxHeightmaxH:'100px'maxHeight:'100px'
bgbackgroundbg:'red'background:'red'
bgColorbackgroundColorbgColor:'red'backgroundColor:'red'
wrapflexWrap:'wrap'wrapflexWrap:'wrap'
nowrapflexWrap:'nowrap'nowrapflexWrap:'nowrap'
growflexGrow:1grow:{2}flexGrow:2
shrinkflexShrink:1shrink:{2}flexShrink:2
basisflexBasis:'auto'basis:{1/3}flexBasis:'33.3333333%'
basis:'100px'flexBasis:'100px'
basis:'50%'flexBasis:'50%'
basis:{0}flexBasis:0
rowflexDirection: 'row'rowflexDirection: 'row'
columnflexDirection: 'column'columnflexDirection: 'column'
scrolloverflow: 'auto'scrolloverflow: 'auto'
noscrolloverflow: 'hidden'noscrolloverflow: 'hidden'
hidedisplay: 'none'hidedisplay: 'none'
showdisplay: 'flex'showdisplay: 'flex'
relativeposition: 'relative'relativeposition: 'relative'
absoluteposition: 'absolute'absoluteposition: 'absolute'
alignStartalignItems: 'flex-start'alignStartalignItems: 'flex-start'
alignCenteralignItems: 'center'alignCenteralignItems: 'center'
alignEndalignItems: 'flex-end'alignEndalignItems: 'flex-end'
alignStretchalignItems: 'stretch'alignStretchalignItems: 'stretch'
alignMeStartalignSelf: 'flex-start'alignMeStartalignSelf: 'flex-start'
alignMeCenteralignSelf: 'center'alignMeCenteralignSelf: 'center'
alignMeEndalignSelf: 'flex-end'alignMeEndalignSelf: 'flex-end'
alignMeStretchalignSelf: 'stretch'alignMeStretchalignSelf: 'stretch'
justifyStartjustifyContent: 'flex-start'justifyStartjustifyContent: 'flex-start'
justifyCenterjustifyContent: 'center'justifyCenterjustifyContent: 'center'
justifyEndjustifyContent: 'flex-end'justifyEndjustifyContent: 'flex-end'
justifySpaceAroundjustifyContent: 'space-around'justifySpaceAroundjustifyContent: 'space-around'
justifySpaceBetweenjustifyContent: 'space-between'justifySpaceBetweenjustifyContent: 'space-between'
wrapStartjustifyItems: 'flex-start'wrapStartjustifyItems: 'flex-start'
wrapCenterjustifyItems: 'center'wrapCenterjustifyItems: 'center'
wrapEndjustifyItems: 'flex-end'wrapEndjustifyItems: 'flex-end'
wrapSpaceAroundjustifyItems: 'space-around'wrapSpaceAroundjustifyItems: 'space-around'
wrapSpaceBetweenjustifyItems: 'space-between'wrapSpaceBetweenjustifyItems: 'space-between'
ppaddingp:'5px'padding:'5px'
ptpaddingToppd:'5px'paddingTop:'5px'
prpaddingRightpr:'5px'paddingRight:'5px'
pbpaddingBottompb:'5px'paddingBottom:'5px'
plpaddingLeftpl:'5px'paddingLeft:'5px'
pvpaddingTop + paddingBottompv:'5px'paddingTop:'5px', paddingBottom:'5px'
phpaddingLeft + paddingRightph:'5px'paddingLeft:'5px', paddingRight:'5px'
paddingVerticalpaddingTop + paddingBottompaddingVertical:'5px'paddingTop:'5px', paddingBottom:'5px'
paddingHorizontalpaddingLeft + paddingRightpaddingHorizontal:'5px'paddingLeft:'5px', paddingRight:'5px'
mmarginm:'5px'margin:'5px'
mtmarginTopmd:'5px'marginTop:'5px'
mrmarginRightmr:'5px'marginRight:'5px'
mbmarginBottommb:'5px'marginBottom:'5px'
mlmarginLeftml:'5px'marginLeft:'5px'
mvmarginTop + marginBottommv:'5px'marginTop:'5px', marginBottom:'5px'
mhmarginLeft + marginRightmh:'5px'marginLeft:'5px', marginRight:'5px'
marginVerticalmarginTop + marginBottommarginVertical:'5px'marginTop:'5px', marginBottom:'5px'
marginHorizontalmarginLeft + marginRightmarginHorizontal:'5px'marginLeft:'5px', margin:'5px'
bborderWidthb:'5px'borderWidth:'5px'
btborderTopWidthbd:'5px'borderTopWidth:'5px'
brborderRightWidthbr:'5px'borderRightWidth:'5px'
bbborderBottomWidthbb:'5px'borderBottomWidth:'5px'
blborderLeftWidthbl:'5px'borderLeftWidth:'5px'
bvborderTopWidth + borderBottomWidthbv:'5px'borderTopWidth:'5px', borderBottomWidth:'5px'
bhborderLeftWidth + borderRightWidthbh:'5px'borderLeftWidth:'5px', borderRightWidth:'5px'

Overriding shortcuts

We don't think miliseconds lost for shortcut processing are a big pain when compared to gain in code readibility... however, for purists who just want to pass their way on this one, we provide options to override it.

1. overriding it completely: meaning "don't do it".

In the mediaFromProps() function, just specify it like this:

import myBreakpoints from './myBreakpoints.js'

const Text = styled.span`
  ${ (props) => { return mediaFromProps( {breakpoints:myBreakpoints, translatorFunc:false}, props) } }
`

Note how translatorFunc:false is part of the same object as {breakpoints}.

2. overriding using your own sbortcuts: meaning "I prefer mine".

In the mediaFromProps() function, just specify it like this:

import myBreakpoints from './myBreakpoints.js'
import myFunc from './myFunc.js'

const Text = styled.span`
  ${ (props) => { return mediaFromProps( {breakpoints:myBreakpoints, translatorFunc:myFunc}, props) } }
`

Note how translatorFunc:myFunc is part of the same object as {breakpoints}.

You can copy our mapTranslator.js file as a starter, and add/remove properties you need.

Todo

  • test with react-native
  • write unit-tests