JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q35789F
  • License MIT

Easily declare prop-based conditions when using styled components.

Package Exports

  • styled-when

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (styled-when) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

styled-when

Easily declare prop-based conditions when using styled components.

Compatible with styled-components and emotion.

Install

npm i styled-when

Usage

Select when prop's value is truthy. Use for simple checks.

import when from 'styled-when'

const Button = styled.button`
    font-weight: normal;

    ${when('important')} {
        font-weight: bold;
    }
`

Select when callback returns a truthy value. Use for more complex or multiple conditions.

import when from 'styled-when'

const Button = styled.button`
    color: white;
    background: cornflowerblue;
    border: 1px solid cornflowerblue;

    ${when(props => props.variant === 'secondary')} {
        color: cornflowerblue;
        background: white;
    }
`

Select when value is true or false. Use for constant conditions.

import when from 'styled-when'

const Button = styled.button`
    font-weight: regular;

    ${when(window.EMBEDDED_IN_ANDROID_APP)} {
        font-weight: bold;
        text-transform: uppercase;
    }
`

API

when(prop: string) => (props: Props) => string
when(callback: Function) => (props: Props) => string
when(condition: boolean) => string