styled-components-ts

1 min read Original article ↗

styled-components

CircleCI

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress and the added benefits of TypeScript 💅

npm install --save styled-components styled-components-ts

Getting Started

import * as React from 'react'

import styledComponents from 'styled-components'

import styledComponentsTS from 'styled-components-ts'

export interface MyImageProps {

  size: number

  borderColor?: string

  borderSize?: number

}

const MyImage = styledComponentsTS<MyImageProps>(styledComponents.img) `

  width: ${props => props.size}px;

  height: ${props => props.size}px;

  border: ${props => props.borderSize || '4px'} solid ${props => props.borderColor || 'black'}

`

export default MyImage

Now we have all the typescript goodies for MyImage like type checking and auto-complete.

import MyImage from './MyImage'

<MyImage size={300} borderColor="blue" borderSize={10} />

We can also extend our components and add new props with ease.

import * as React from 'react'

import styledComponents from 'styled-components'

import styledComponentsTS from 'styled-components-ts'

import MyImage, { MyImageProps } from './MyImage'

export interface ExpandedImageProps extends MyImageProps {

  backgroundColor?: string

}

const ExpandedImage = styledComponentsTS<ExpandedImageProps>(MyImage.extend)`

  background-color: ${props => props.backgroundColor || 'unset'};

`

export default ExpandedImage

For more information please see https://github.com/styled-components/styled-components