Giter Site home page Giter Site logo

jacob-ebey / styled-components-ts Goto Github PK

View Code? Open in Web Editor NEW
151.0 8.0 7.0 124 KB

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 ๐Ÿ’…

License: Do What The F*ck You Want To Public License

TypeScript 19.08% JavaScript 80.92%

styled-components-ts's Introduction

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 react, styledComponents and styledComponentsTS
import * as React from 'react'
import styledComponents from 'styled-components'
import styledComponentsTS from 'styled-components-ts'

// Create an interface for the component
export interface MyImageProps {
  size: number
  borderColor?: string
  borderSize?: number
}

// Create a styled component with our props interface
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 react, styledComponents and styledComponentsTS
import * as React from 'react'
import styledComponents from 'styled-components'
import styledComponentsTS from 'styled-components-ts'

// Import our image and it's props
import MyImage, { MyImageProps } from './MyImage'

// Create an interface for the component that extends the base image props
export interface ExpandedImageProps extends MyImageProps {
  backgroundColor?: string
}

// Create a styled component with our props interface that extends MyImage
const ExpandedImage = styledComponentsTS<ExpandedImageProps>(MyImage.extend)`
  background-color: ${props => props.backgroundColor || 'unset'};
`

export default ExpandedImage

Using styled-components v4+?

If you're using a version of styled-components greater than 4.0.0 you do not need this library. See below for example usage:

const Button = styled.div<{ selected?: boolean }>`
  background: ${props => props.selected ? 'red' : 'blue'};
`

// or
interface IButton {
  selected?: boolean
}
const Button = styled.div<IButton>`
  background: ${props => props.selected ? 'red' : 'blue'};
`

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

styled-components-ts's People

Contributors

jacob-ebey avatar mrmartineau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

styled-components-ts's Issues

Setup CI

Seems there is a bit of interest here, so I'm going to setup CI in the next few days and start addressing some of the comments.

Thanks for your patience everyone, my attention has been elsewhere.

No exported member 'StyledComponentClass'

I get this error when using this package:

project/node_modules/styled-components-ts/lib/styled-components-ts.d.ts
(3,32): Module '"project/node_modules/@types/styled-components/index"' has no exported member 'StyledComponentClass'. Did you mean 'StyledComponentBase'?

Any idea?

    "styled-components": "^4.1.2",
    "styled-components-ts": "^0.0.15"

NPM package >=0.0.9 is incomplete

When I load the NPM package of 0.0.8, it has the following in the node_modules folder:

LICENSE
README.md
dist
examples
node_modules
package.json
src
typings

Any version >=0.0.9 has the following in its folder:

LICENSE
README.md
node_modules
package.json
src

styled_components_ts___default(...) is not a function

hi.. i'm getting the following error..

__WEBPACK_IMPORTED_MODULE_1_styled_components_ts___default(...) is not a function

import styled from 'styled-components';
import styledProps from 'styled-components-ts';

interface FooStyledProps {
    bg : string;
}

const FooStyled = styledProps<FooStyledProps>( styled.div )`
    ul {
        li {
            background: ${ ( props ) => props.bg };
        }
    }
`;

export { FooStyled as default };

am i doing something wrong?

Dependency on React 16?

Does this truly depend on React 16? If not, you should list the earliest version that works so that projects don't unnecessarily depend on 16.

Use with styledComponents.withComponent

Is there a way to use this with Styled Component's withComponent method?

I've tried various takes but consistently get type mis-match errors.

import styledComponents from 'styled-components'
import styledComponentsTS from 'styled-components-ts';

interface ButtonProps {
  color: string
}

export const Button = styledComponentsTS<ButtonProps>(styledComponents.a)`
  border: none;
  background-color: ${props => props.color };
`

interface BorderedButtonProps extends ButtonProps {
  borderColor: string
}

export const BorderedButton = styledComponentsTS<BorderedButtonProps>(Button.extend)`
  border: 2px solid ${props => props.borderColor };
`

// Ideally output a link ("<a>")
export const ButtonLink = styledComponentsTS<ButtonProps>(Button.withComponent('a'))`
  border: 2px solid ${props => props.borderColor };
`

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.