Giter Club home page Giter Club logo

styled-with-props's Introduction

🎊 Deprectated 🎊

The original issue this was intending to solve has been fixed and released in TypeScript 2.9.2. See typescript#11947 and styled-components#1428 for more information.



styled-with-props

A temporary solution to prop passing with TypeScript and styled-components.


😩  Current Solution

import * as React from 'react'
import styled from 'styled-components'

interface Props {
  background: 'white' | 'black'
  className?: string
}

const Card: React.SFC<Props> = props => (
  <div className={props.className}>
    {props.children}
  </div>
)

const StyledCard = styled(Card)`
  background-color: ${props => props.background};
`

😢  Desired Solution

import styled from 'styled-components'

interface Props {
  background: 'white' | 'black'
}

const Card = styled.div<Props>`
  background-color: ${props => props.background};
`

Unfortunately, this is not currently possible due to the fact that TypeScript does not support passing generics to tagged template literals. TypeScript#11947

😎  Temporary Solution

import styled from 'styled-components'
import withProps from 'styled-with-props'

interface Props {
  background: 'white' | 'black'
}

const div = withProps<Props>('div')

const Card = styled(div)`
  background-color: ${props => props.background};
`

Why?

This takes advantage of the existing syntax used with styled-components. This is very important for things like the Babel plugin, VSCode plugin, etc..

styled-with-props's People

Contributors

nickbreaton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.