1.0.14 • Published 2 years ago

styled-velocity-emotion v1.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Styled-Velocity with Emotion

Styled-Velocity with Emotion, a React-based styled prop and UI library to increase your velocity and help build your Design System.

This package is based upon the learnings of my other package called styled-velocity which used styled-components under the hood. This time around I have switched to using the emotion api, and I also reduced the bundle size by about 80%, as well as the runtime. I have found emotion plays a bit nicer with Next.js and has better support these days.

Install

yarn add @emotion/react @emotion/styled styled-velocity-emotion

or

npm install @emotion/react @emotion/styled styled-velocity-emotion

Components

All of the following have access to the sharedProps.

Import example:

import { Element, Flex } 'styled-velocity-emotion'

Available Props on Element

propcss propertyconversionType
cssany css
bgbackground
bgImagebackground-image
bgSizebackground-size
borderborder
borderLeftborder-left
borderRightborder-right
borderBottomborder-bottom
borderRadiusborder-radius
boxShadowbox-shadow
mmargin
mtmargin-top
mbmargin-bottom
mlmargin-left
mrmargin-right
mxml and mr
mymt and mb
ppadding
ptpadding-top
pbpadding-bottom
plpadding-left
prpadding-right
pxpl and pr
pypt and pb
hheight
wwidth
minhmin-height
minwmin-width
maxhmax-height
maxwmax-width
ttop
bbottom
lleft
rright
xr and l
yt and b
ccw and chgetCells
cwcells widegetCells
chcells height (cells tall)getCells
mincwmin cells widegetCells
maxcwmax cells widegetCells
minchmin cells heightgetCells
maxchmax cells heightgetCells
cmcells margingetCells
cmlcells margin-leftgetCells
cmrcells margin-rightgetCells
cmtcells margin-topgetCells
cmbcells margin-bottomgetCells
cmxcml and cmrgetCells
cmycmt and cmbgetCells
cpcells paddinggetCells
cplcells padding-leftgetCells
cprcells padding-rightgetCells
cptcells padding-topgetCells
cpbcells padding-bottomgetCells
cpxcpl and cprgetCells
cpycpt and cpbgetCells
ctcells topgetCells
cbcells bottomgetCells
clcells leftgetCells
crcells rightgetCells
cxcr and clgetCells
cyct and cbgetCells
ctransformcells transformgetCellsTranslate
directionflex-direction
wrapflex-wrap
alignjustify-contentgetFlexProperty
valignalign-itemsgetFlexProperty
valignContentalign-contentgetFlexProperty
growflex-grow
shrinkflex-shrink
fbflex-basis
positionposition
zz-index
zIndexz-index
colorcolor
textColorcolor
textAligntext-align
textTransformtext-transform
fontFamilyfont-family
fontSizefont-size
fontWeightfont-weight
lineHeightline-height

Available Props on Flex, which extends Element

propdesc
columnsGenerates widths on children if using Element or Flex.Item

Breakpoints

About

All of the Element props can take a single value, or an object of Breakpoints. You have all the choices you could possibly use for creating style within breakpoints. This system is mobile first, but there's several ways to mix it up.

Example

Let's using the width css property for the following examples...

No breakpoint
w={50}
Mobile: 50, Tablet: 100, Desktop: 150

If you want to create a breakpoint, supply an object, and use the key as the breakpoint. Make sure to give the lowest a 0, if it's min-width 0, it won't wrap it in a media query.

w={{ 0: 50, 768: 100, 1024: 150 }}

returns

width: 50px;

@media (min-width: 768px) {
  width: 100px;
}
@media (min-width: 1024px) {
  width: 150px;
}
Mobile: 50, Tablet: 100, Desktop

If you want to create a breakpoint that has min and max, supply a key that is a string like so...

w={{ '768-1024': 100 }}

returns

@media (min-width: 768px) and (max-width: 1024px) {
  width: 100px;
}

Pretty cool, eh?

Scaling Font Sizes

This min-max string key is how the scaling pixel values work. If you wanted to scale anything from one pixel value to another, like font-size for instance, you can do this...

fontSize={{
  0: 12,
  '414-1023': [12, 36],
  1024: 36
}}

returns

font-size: 12px;

@media (min-width: 414px) and (max-width: 1023px) {
  // Insert css formula to scale the font-size from 12px to 36px based on screen width
}

@media (min-width: 1024px) {
  font-size: 36px;
}

conversionType

Notice the conversionType? This should clue you into what is possible for the given prop.

getCells

This takes an int and converts it to the cells width (or height). Thus cw={2} makes the element 2 cells wide (based on your grid config).

getCellsTranslate

Similar to getCells, but takes an object. ctransform={{ 0: {x: 0.5, y: 0} }} would offset the x coords of the element by 1/2 cells width.

getFlexProperty

Flex has some pretty unintuitive properties. For instance, valign="top" refers to the vertical alignment an element (assuming you staying with direction=row). You can of course pass the original css flex properties as well if you are used to them.

inputoutput
topflex-start
leftflex-start
startflex-start
bottomflex-end
rightflex-end
endflex-end
middlecenter
centercenter

Config in the <head>

You'll want to include a few things in your head tag.

<script>
  !(function () {
    var rootStyle = document.documentElement.style

    function srcWidth() {
      rootStyle.setProperty(
        '--scr',
        'calc(100vw - ' + (window.innerWidth - document.documentElement.clientWidth) + 'px)',
      )
    }
    document.addEventListener('DOMContentLoaded', srcWidth)
    srcWidth()
  })()
</script>
<style>
  :root {
    --scr: 100vw;
    --sv-cells: 40;
    --sv-cell: calc(var(--scr) / var(--sv-cells));
  }

  /* Stop your cells from scaling at a desired breakpoint */
  @media (min-width: 2000px) {
    :root {
      --sv-cell: 50px;
    }
  }
</style>
1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago