styled-break
🍨 Create your responsive styled components with breeze using custom Styled Components HOC!
- minimalist api, less import more styling!
- declarative, highly abstracted yet powerful!
- 0 dependency, small footprint!
- tested and production ready!
🙌 Mapping now available in 2.0.0!
🚨 Latest update 2.1.0: fix React unrecognized prop warning.
Installation
npm i styled-break
Demo
Try it at Code Sandbox!
Usage
import React from 'react'import render from 'react-dom'import styledBreak from 'styled-break'import css from 'styled-components' const config = breakpoints: xs: 0 sx: 501 sm: 576 md: 768 lg: 992 xl: 1200 sLevel: 3 const styledHOC = const DivStyled = const mappingProp = JSON const mappingValue = a b c css` border-radius: px px px px;` const Demo = return <DivStyled ='500' = ='cyan' = />
which is equivalent to
{{{{{{{
Core utilities
1. styledBreak(config)
const config = breakpoints: xs: 0 sx: 501 sm: 576 md: 768 lg: 992 xl: 1200 sLevel: 3 const cssR styledR styledHOC =
config
-
config(optional): object made of
breakpoints
andsLevel
props.- breakpoints(optional): object where default value is
bootstrap
breakpoints: {xs: 0, sm: 576, md: 768, lg: 992, xl: 1200}. You can define as many breakpoints you want.- props: you can name your breakpoint whatever name you want, ⚠️however please avoid including underscore
_
in props name. - values: The value should be the minimum value of your breakpoint (the unit is
px
).
- props: you can name your breakpoint whatever name you want, ⚠️however please avoid including underscore
- sLevel(optional): is global setting of class specificity level, default value is
one
. You can nest specificity level individually to have finer control on class specificity level.
- breakpoints(optional): object where default value is
the output of styledBreak
are styledR
and styledHOC
HOC plus a cssR
helper function.
2. styledCss
const styledCss = xs_m: `width: 100px;` sm_md: `width: 200px;` xl: css` `
this is your responsive object, the props name have 4 combination for every breakpoint, let take break point xs
, sm
and md
as example where minimum of xs
is 0, sm
is 576 and md
is 768.
here is how you do without
, max
, min
, only
, and between
query:
a.without
to write style with no media query, name your prop as _
const styledCss = _: `width: 100px;`
which equivalent to
this advantage of this over String Form is, this can be written together with media queries.
b.min
append _n
or nothing
anything to the breakpoint prop name:
const styledCss = xs: `width: 100px;` // ORconst styledCss = xs_n: `width: 100px;`
which equivalent to
{
c.max
append _m
to the breakpoint prop name:
const styledCss = sm_m: `width: 100px;`
which equivalent to
{
the value max width is next breakpoint - 0.02
if there is no next breakpoint, the value is 999999
d.between
append _anotherBreakpointName
to the breakpoint prop name:
const styledCss = xs_sm: `width: 100px;`
which equivalent to
{
it takes xs
min width and md
max width.
e.only
append _o
or _theSameBreakpointName
to the breakpoint prop name:
const styledCss = xs_o: `width: 100px;` // ORconst styledCss = xs_xs: `width: 100px;`
which equivalent to
{
It takes xs
min width and xs
max width.
Default Values
if the 1st breakpoint doesn't exist, the whole media query doesn't exist, no style would be applied.
if the 1st breakpoint exist but 2nd breakpoint doesn't exist, such as appending _randomZT2t2
, there would be no 2nd media query, which mean, it has only min
media query.
String Form
styledCss
can also be just string without any breakpoint needed, which mean the style is applied without any media query.
const styledCss = `width: 100px;`
However if you need to write non media query style together with media query style, it is better to use without.
Function Interpolation
Of course you can interpolate function just like you do in Styled Component (because that is the whole point), simply use Styled Component css
helper function.
import css from 'styled-components' // with breakpointsconst styledCss = xs_o: css` ` // or without any breakpointconst styledCss = css` `
you don't need css
helper if you are not doing function interpolation, this is stated in Styled Component doc.
However you can pass function without css
helper if the the function is not interpolated in template string:
const styledCss = `width: px;`
Class Specificity Level
On top of the septicity you set, you can control individual css property specificity level, the end result is global specificity level times individual specificity level.
// if your global specificity level is 3const styledCss = _: `width: 50px;` xs: `&&{ width: 100px; }` // the total specificity level is 2*3 = 6 md: `width: 200px;` // the total specificity level is 3
Mapping
Mapping is added in 2.0.0, this is useful if you want to repeat a style with different arguments, mapping is carried out by adding a prop in styledCss object.
- Mapping props: an stringified object literal where:
- prop: obey the same rule as config breakpoint prop.
- value: can be array or single value, it will be passed as argument to the mapping value.
- Mapping value: callback that accept the value from Mapping prop and return string or interpolated string (with the help of
css
helper).
const mappingProp = JSON const mappingValue = a b c d `border-radius: px px px px;` const styledCss = mappingProp: mappingValue
which equivalent to
{{
If you have only one argument, in this case you can drop the array notation(no issue if you keep it as array), finally you can of course do function interpolation.
import css from 'styled-components' const mappingProp = JSONconst mappingValue = css` border-radius: px; ` const styledCss = mappingProp: mappingValue
Create Responsive Styled Component
1. styledHOC(component)(level) <--Recommended
creates a component that accept styledCSS
prop that take styledCSS
object (see styledCss for more information).
- component(required): the component can be Html or React component, see below code for example
- level(optional): override the
sLevel
pass to styledBreak, the default value isstyledBreak
'ssLevel
.
import css from 'styled-components'//or you also could import it from styled-break// import { css } from 'styled-break' // to create styled html componentconst DivStyled = 1 // to create a styled react componentconst ButtonStyled = 2 const Demo = return <> <ButtonStyled =` ;` /> <DivStyled ='500' = /> </>
reminder: always use css
helper to interpolate the function.
2. styledR(component)(styledCss,level)
styledR is basically an extended styled
of Styled Component
- component(required): same as
component
object described in styledHOC. - styledCss(required): same as
styledCss
object described in styledCss. - level(optional): same as
level
number described in styledHOC.
usage example
// to create styled html componentconst DivStyled = xs_o: css` ` 1 // to create a styled react componentconst ButtonStyled = `width: 100px;` 2
3. cssR(styledCss,level)
if you don't like to create responsive styled component with styledR
or styledHOC
and you want to use the convention styled
api of Styled Component, then this is what you need.
- styledCss(required): same as styledCss object described in styledCss.
- level(optional): same as
level
number described in styledHOC.
import styled css from 'styled-components' const DivStyled = styleddiv` `
reminder: always use css
helper to interpolate the function.
Acknowledgement
- Styled Component Breakpoint for mapping api inspiration.
To Do
- add styledCss prop name for non media query
- implement map api
- add more media type
- further abstraction