All Projects โ†’ emotion-js โ†’ Facepaint

emotion-js / Facepaint

Responsive style values for css-in-js.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Facepaint

Xwind
Tailwind CSS as a templating language in JS and CSS-in-JS
Stars: โœญ 249 (-52.39%)
Mutual labels:  css-in-js, emotion
Twin.macro
๐Ÿฆนโ€โ™‚๏ธ Twin blends the magic of Tailwind with the flexibility of css-in-js (emotion, styled-components, stitches and goober) at build time.
Stars: โœญ 5,137 (+882.22%)
Mutual labels:  css-in-js, emotion
spring-keyframes
โœŒ๏ธ1.4kb library to generate css keyframes in css-in-js based on a spring algorithm, with emotion
Stars: โœญ 65 (-87.57%)
Mutual labels:  emotion, css-in-js
React Awesome Reveal
React components to add reveal animations using the Intersection Observer API and CSS Animations.
Stars: โœญ 346 (-33.84%)
Mutual labels:  css-in-js, emotion
react-awesome-reveal
React components to add reveal animations using the Intersection Observer API and CSS Animations.
Stars: โœญ 564 (+7.84%)
Mutual labels:  emotion, css-in-js
Styled Components Vs Emotion
a short doc comparing the popular CSS-in-JS libraries styled-components and emotion
Stars: โœญ 204 (-60.99%)
Mutual labels:  css-in-js, emotion
tsstyled
A small, fast, and simple CSS-in-JS solution for React.
Stars: โœญ 52 (-90.06%)
Mutual labels:  emotion, css-in-js
Filbert Js
A lightweight(~1kb) css-in-js framework
Stars: โœญ 167 (-68.07%)
Mutual labels:  css-in-js, emotion
benefit
โœจ Utility CSS-in-JS library that provides a set of low-level, configurable, ready-to-use styles
Stars: โœญ 51 (-90.25%)
Mutual labels:  emotion, css-in-js
visage
Visage design system
Stars: โœญ 12 (-97.71%)
Mutual labels:  emotion, css-in-js
Emotion
๐Ÿ‘ฉโ€๐ŸŽค CSS-in-JS library designed for high performance style composition
Stars: โœญ 14,177 (+2610.71%)
Mutual labels:  css-in-js, emotion
Design System Utils
๐Ÿ‘ฉโ€๐ŸŽจ Access your design tokens with ease
Stars: โœญ 465 (-11.09%)
Mutual labels:  css-in-js, emotion
Grid
This package has moved and renamed
Stars: โœญ 2,079 (+297.51%)
Mutual labels:  css-in-js, emotion
Styled Ppx
Typed styled components in Reason, OCaml and ReScript
Stars: โœญ 236 (-54.88%)
Mutual labels:  css-in-js, emotion
Vue Emotion
Seamlessly use emotion (CSS-in-JS) with Vue.js
Stars: โœญ 173 (-66.92%)
Mutual labels:  css-in-js, emotion
monad-ui
Utility First CSS-in-JS
Stars: โœญ 33 (-93.69%)
Mutual labels:  emotion, css-in-js
Next Dark Mode
๐ŸŒ‘ Enable dark mode for Next.js apps
Stars: โœญ 133 (-74.57%)
Mutual labels:  css-in-js, emotion
Goober
๐Ÿฅœ goober, a less than 1KB ๐ŸŽ‰ css-in-js alternative with a familiar API
Stars: โœญ 2,317 (+343.02%)
Mutual labels:  css-in-js, emotion
Styled-Responsive-Media-Queries
Responsive Media Queries for Emotion styled or styled-components โ€” Standard size from Chrome DevTools.
Stars: โœญ 33 (-93.69%)
Mutual labels:  emotion, css-in-js
satchel
The little bag of CSS-in-JS superpowers
Stars: โœญ 14 (-97.32%)
Mutual labels:  emotion, css-in-js

facepaint

Dynamic style values for css-in-js.

import { css } from 'emotion'
import facepaint from 'facepaint'

const mq = facepaint([
  '@media(min-width: 420px)',
  '@media(min-width: 920px)',
  '@media(min-width: 1120px)'
])

const myClassName = css(mq({
  color: ['red', 'green', 'blue', 'darkorchid'],
}))

Install

npm i facepaint -S

or

yarn add facepaint

API

facepaint function

facepaint(selectors: Array<Selector>) : DynamicStyleFunction

Arguments

  • breakpoints

    const mq = facepaint([
      '@media(min-width: 420px)',
      '@media(min-width: 920px)',
      '@media(min-width: 1120px)'
    ])
    
  • options

    const mq = facepaint(
      [...],
      {
        literal: true|false,
        overlap: true|false
      }
    )
    
    • literal boolean (Default: false) - output should match arguments given to facepaint exactly

      By default, the first value in a value array is applied without a media query or selector and the rest of the values are applied as children of media queries or selectors. When literal is set to true the values given to a specific css property mapped 1:1 with the arguments provided to facepaint.

      Given the following:

      const mq = facepaint([
        '@media(min-width: 420px)'
        '@media(min-width: 920px)'
      ], { literal: true })
      
      const expandedStyles = mq({
        color: ['red', 'green']
      })
      

      The output of expandedStyles will be:

      { 
        '@media(min-width: 420px)': {
          color: 'red'
        },
        '@media(min-width: 920px)': {
          color: 'green'
        }
      }
      

      The output is missing any styles on the base style object because the values are mapped to the arguments supplied to facepaint literally.

    • overlap boolean (Default: false) - overlap values that occur in multiple media queries or slots

      Given the following:

      const mq = facepaint([
        '@media(min-width: 420px)'
      ], { overlap: true })
      
      const expandedStyles = mq({
        color: ['red', 'red']
      })
      

      The value of expandedStyles would not contain any media query breakpoints. This is an optimization to remove bytes from the final code.

      { color: 'red' }
      

      vs.

      { 
        color: 'red',
        '@media(min-width: 420px)': {
          color: 'red'
        }
      }
      

      The downside of enabling this option is that when attempting to overwrite the value of color in another style definition the expected media query will be missing.

      const style1 = css(mq({ color: ['red', 'red'] }))
      const style2 = css({ color: 'blue' })
      const composedStyles = css(style1, style2)
      

      style1's output will NOT contain the media query and value for red at 420px due to the overlap: true optimization.

      The developer that created composedStyles might expect the following output.

      { 
        color: 'blue',
        '@media(min-width: 420px)': {
          color: 'red'
        }
      }
      

      Due to our overlap: true optimization however, the final output will be the following.

      { color: 'blue' }
      

Returns

facepaint returns a function that can be exported and used throughout your app to dynamically style based on your provided selectors.

  • The function accepts any number of arrays or objects as arguments.
  • Nested arrays are flattened.
  • Boolean, undefined, and null values are ignored.

Examples

emotion

CodeSandbox Demo

import { css } from 'emotion'
import facepaint from 'facepaint'

const mq = facepaint([
  '@media(min-width: 420px)',
  '@media(min-width: 920px)',
  '@media(min-width: 1120px)'
])

const myClassName = css(mq({
  backgroundColor: 'hotpink',
  textAlign: 'center',
  width: ['25%', '50%', '75%', '100%'],
  '& .foo': {
    color: ['red', 'green', 'blue', 'darkorchid'],
    '& img': {
      height: [10, 15, 20, 25]
    }
  }
}))

Note that the first value is considered a default value and is not a child of a media query at-rule.

The following css is generated.

.css-rbuh8g {
  background-color: hotpink;
  text-align: center;
  width: 25%;
}

@media (min-width:420px) {
  .css-rbuh8g {
    width: 50%;
  }
}

@media (min-width:920px) {
  .css-rbuh8g {
    width: 75%;
  }
}

@media (min-width:1120px) {
  .css-rbuh8g {
    width: 100%;
  }
}

.css-rbuh8g .foo {
  color: red;
}

@media (min-width:420px) {
  .css-rbuh8g .foo {
    color: green;
  }
}

@media (min-width:920px) {
  .css-rbuh8g .foo {
    color: blue;
  }
}

@media (min-width:1120px) {
  .css-rbuh8g .foo {
    color: darkorchid;
  }
}

.css-rbuh8g .foo img {
  height: 10px;
}

@media (min-width:420px) {
  .css-rbuh8g .foo img {
    height: 15px;
  }
}

@media (min-width:920px) {
  .css-rbuh8g .foo img {
    height: 20px;
  }
}

@media (min-width:1120px) {
  .css-rbuh8g .foo img {
    height: 25px;
  }
}

styled-components

import styled from 'styled-components'
import facepaint from 'facepaint'

const mq = facepaint([
  '@media(min-width: 420px)',
  '@media(min-width: 920px)',
  '@media(min-width: 1120px)'
])

const Div = styled('div')`
  ${mq({
    backgroundColor: 'hotpink',
    textAlign: 'center',
    width: ['25%', '50%', '75%', '100%'],
    '& .foo': {
      color: ['red', 'green', 'blue', 'papayawhip'],
      '& img': {
        height: ['10px', '15px', '20px', '25px']
      }
    }
  })};
`

<Div/>

The following css is generated.

.c0 {
  background-color: hotpink;
  text-align: center;
  width: 25%;
}

.c0 .foo {
  color: red;
}

.c0 .foo img {
  height: 10px;
}

@media (min-width:420px) {
  .c0 {
    width: 50%;
  }
}

@media (min-width:920px) {
  .c0 {
    width: 75%;
  }
}

@media (min-width:1120px) {
  .c0 {
    width: 100%;
  }
}

@media (min-width:420px) {
  .c0 .foo {
    color: green;
  }
}

@media (min-width:920px) {
  .c0 .foo {
    color: blue;
  }
}

@media (min-width:1120px) {
  .c0 .foo {
    color: papayawhip;
  }
}

@media (min-width:420px) {
  .c0 .foo img {
    height: 15px;
  }
}

@media (min-width:920px) {
  .c0 .foo img {
    height: 20px;
  }
}

@media (min-width:1120px) {
  .c0 .foo img {
    height: 25px;
  }
}

Pseudo Selectors

CodeSandbox Demo

import { css } from 'emotion'
import facepaint from 'facepaint'

const pseudo = facepaint([':hover', ':focus', ':active'])

const myClassName = css(
  pseudo({
    backgroundColor: 'hotpink',
    textAlign: 'center',
    width: ['25%', '50%', '75%', '100%'],
    '& .foo': {
      color: ['red', 'green', 'blue', 'darkorchid'],
      '& img': {
        height: [10, 15, 20, 25]
      }
    }
  })
)
.css-1guvnfu {
  background-color: hotpink;
  text-align: center;
  width: 25%;
}

.css-1guvnfu:hover {
  width: 50%;
}

.css-1guvnfu:focus {
  width: 75%;
}

.css-1guvnfu:active {
  width: 100%;
}

.css-1guvnfu .foo {
  color: red;
}

.css-1guvnfu .foo:hover {
  color: green;
}

.css-1guvnfu .foo:focus {
  color: blue;
}

.css-1guvnfu .foo:active {
  color: darkorchid;
}

.css-1guvnfu .foo img {
  height: 10px;
}

.css-1guvnfu .foo img:hover {
  height: 15px;
}

.css-1guvnfu .foo img:focus {
  height: 20px;
}

.css-1guvnfu .foo img:active {
  height: 25px;
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].