1.0.1 • Published 8 years ago

gr8r v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
8 years ago

gr8r

Functional CSS-in-JS shorthand utilties.

npm i gr8r

Note: gr8r is under active development!

Not quite ready for use, there are still some todos:

  • Testing
  • Breakpoints and common declarations (cxs)
  • Extended documentation and example page

Features

  • Customizable: Adjust breakpoints, units, grid, etc...
  • Extendable: Add your own shorthand methods.
  • Prefixed: Handles browser prefixing out of the box.
  • Ultralight: Generated on the fly—no stylesheets.

Use short-hand utilities within JS to generate your styles in the DOM. For a CSS-only solution, check out gr8. Prepped for use with Browserify, pairs nicely with choo and yo-yo. Shoutout understyle, f(css), etc…

gr8 and gr8r are maintained by Jon Gacnik and Jon-Kyle and used extensively within projects at Folder Studio.

Usage

const gr8r = require('gr8r/html')

gr8r('¡estupendo!', 'c6 ttu')

Content

Accepts either an HTML Element, string or array as the first argument. An HTML element is always returned.

  • HTML Element: The gr8 class(es) are added, which is then returned.
  • String/Array: A new element is returned containing the content.

An example using yo-yo:

const gr8r = require('gr8r/html')
const yo = require('yo-yo')

gr8r('We construct and keep on constructing...', 'c12 m2')
// <div class="cxs-3919501705">We construct...

gr8r(yo`<header>yet intuition is still a good thing.</header>`, 'c12 m2')
// <header class="cxs-3919501705">yet intuition…

Arguments

Methods can contain variable arguments such as c6 and p2, or predefined rules like ttu. It’s also possible to pass a string as an argument using : as a delimiter. For example, you could add a custom method called bg and use it with the shorthand bg:black.

Units

It’s possibible to set a custom unit when declaring the value of a method. Fallsback to options unit.

gr8r('When I haven’t any blue…', 'p20px')
gr8r('... I use red.', 'p2')

Breakpoints

Methods can be redeclared for each breakpoint. Additionally, use an integer as a key to get ultra-specific.

gr8r('Put this there.', 'p2', {
  xl: 'p6',
  lg: 'p4',
  600: 'p3.5',
  md: 'p3',
  sm: 'p2'
})

Options

Several options are configurable by calling setOptions.

const gr8r = require('gr8r')

gr8r.setOptions({
  breakpoints: {
    xl: '1439px',
    lg: '1260px',
    md: '1023px',
    sm: '767px'
  },
  // Min or max width
  max: true,
  // Grid
  cols: 12,
  // Default unit when none is passed
  unit: 'rem'
})

Custom Methods

It’s possible to extend gr8r by creating your own methods. Ensure you’re returning an object which contains the appropriately formatted CSS styles. Prefixing is handled for you.

const gr8r = require('gr8r')

gr8r.addMethod({
  match: 'omg',
  value: ({ omg }) => {
    const color = 'gold'
    return { border: '20px solid ' + color }
  }
}

gr8r.e('It works!', 'p3 c6 omg', { md: 'p1' })

Methods

Background

UtilityValueAttributeReturns
bgscbackground-sizecover
bgsctbackground-sizecontain
bgpcbackground-positioncenter
bgptbackground-positiontop
bgprbackground-positionright
bgpbbackground-positionbottom
bgplbackground-positionleft
bgrnbackground-repeatno-repeat
bgrxbackground-repeatrepeat-x
bgrybackground-repeatrepeat-y
bgnbackgroundnone

Shortcuts

UtilityValueReturns
bgcbsc bgpc bgrn
bgctbsct bgpc bgrn

Column

Columns are calculated by comparing the passed value against the cols option. By default there are 12 columns, so passing 6 will return a width of 50%. Additionally, you can offset a column (co) in a similar way, which will push the column to the right by returning margin-left.

UtilityValueAttributeReturns
cint/floatwidth(value/cols)%
coint/floatmargin-left(value/cols)%

Dev

This is useful for seeing the underlying structure of an element for debugging purposes.

UtilityValueAttributeReturns
devoutline(nested structure)

Display

UtilityValueAttributeReturns
xdisplayflex
dbdisplayblock
dibdisplayinline-block
didisplayinline
dtdisplaytable
dtcdisplaytable-cell
dtrdisplaytable-row
dndisplaynone

Flex(box)

UtilityValueAttributeReturns
xacalign-itemscenter
xabalign-itemsbaseline
xasalign-itemsstretch
xafsalign-itemsflex-start
xafealign-itemsflex-end
xdrflex-directionrow
xdrrflex-directionrow-reverse
xdcflex-directioncolumn
xdcrflex-directioncolumn-reverse
xjcjustify-contentcenter
xjbjustify-contentspace-between
xjajustify-contentspace-around
xjcjustify-contentflex-start
xjejustify-contentflex-end
xwflex-wrapwrap
xwrflex-wrapwrap-reverse
xwnflex-wrapno-wrap

Float

UtilityValueAttributeReturns
flfloatleft
frfloatright
fnfloatnone

Margin

UtilityValueAttributeReturns
mint/floatmargin{value}{unit}
mtint/floatmargin-top{value}{unit}
mrint/floatmargin-right{value}{unit}
mbint/floatmargin-bottom{value}{unit}
mlint/floatmargin-left{value}{unit}

Misc

UtilityValueAttributeReturns
curpcursorpointer
curdcursordefault
usnuser-selectnone
usauser-selectauto
ustuser-selecttext
uscuser-selectcontain
usalluser-selectall

Opacity

Value is 0-100.

UtilityValueAttributeReturns
oint/floatopacity{value/100}

Overflow

UtilityValueAttributeReturns
ofhoverflowhidden
ofhxoverflowhidden-x
ofhyoverflowhidden-y
ofsoverflowscroll
ofsxoverflowscroll-x
ofsyoverflowscroll-y

Padding

UtilityValueAttributeReturns
pint/floatpadding{value}{unit}
ptint/floatpadding-top{value}{unit}
print/floatpadding-right{value}{unit}
pbint/floatpadding-bottom{value}{unit}
plint/floatpadding-left{value}{unit}

Positioning

UtilityValueAttributeReturns
psfpositionfixed
psapositionabsolute
psrpositionrelative
tint/floattop{value}{unit}
rint/floattop{value}{unit}
bint/floattop{value}{unit}
lint/floattop{value}{unit}

Size

UtilityValueAttributeReturns
hint/floatheight{value}{unit}
vhint/floatheight{value}vh
wint/floatheight{value}{unit}
vwint/floatheight{value}vw

Typography

UtilityValueAttributeReturns
lhint/floatline-height{value}
fsint/floatfont-size{value}{unit}
fsifont-styleitalic
fsnfont-stylenormal
fwbfont-weightbold
fwnfont-weightnormal
taltext-alignleft
tactext-aligncenter
tartext-alignright
tdltext-decorationline-through
tdntext-decorationnone
tdotext-decorationoverline
tdutext-decorationunderline
ttctext-transformcapitalize
ttltext-transformlowercase
ttntext-transformnone
ttutext-transformuppercase
vabvertical-alignbottom
vablvertical-alignbaseline
vamvertical-alignmiddle
vatvertical-aligntop

z-index

UtilityValueAttributeReturns
zint/floatz-index{value}