Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type definition #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"main": "dist/vue-emotion.cjs.js",
"cdn": "dist/vue-emotion.min.js",
"unpkg": "dist/vue-emotion.min.js",
"types": "types/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "npm run lint && echo 'no tests!'",
"test": "npm run lint && npm run dtslint && echo 'no tests!'",
"lint": "xo",
"dtslint": "dtslint types",
"prepublishOnly": "npm run build",
"build": "bili --format umd,cjs,es,umd-min --module-name emotionVue",
"example": "poi",
Expand All @@ -27,6 +29,7 @@
"devDependencies": {
"babel-plugin-emotion": "^9.1.0",
"bili": "^3.0.13",
"dtslint": "^0.3.0",
"emotion": "^9.1.0",
"eslint-config-rem": "^3.0.0",
"markdown-toc": "^1.1.0",
Expand All @@ -48,6 +51,7 @@
},
"dependencies": {
"emotion-utils": "^9.1.0",
"nano-assign": "^1.0.0"
"nano-assign": "^1.0.0",
"vue-tsx-support": "^1.2.0"
}
}
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ function stringifyClass(klass) {
return klass.join(' ')
}
if (typeof klass === 'object') {
return Object.keys(klass)
.filter(key => Boolean(klass[key]))
.join(' ')
// prettier-ignore
return Object.keys(klass).filter(key => Boolean(klass[key])).join(' ')
}
return klass
}
Expand Down
103 changes: 103 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// TypeScript Version: 2.3
// tslint:disable-next-line:no-implicit-dependencies
import Vue from 'vue'
import { TsxComponent } from 'vue-tsx-support/lib/api'
import { Interpolation as EmotionInterpolation } from 'emotion'

export * from 'emotion'

export type InterpolationFn<Props = {}> = (
props: Props
) => EmotionInterpolation | InterpolationFn<Props>

export type InterpolationTypes<Props = {}> =
| InterpolationFn<Props>
| EmotionInterpolation

export type Interpolation<Props = {}> =
| InterpolationTypes<Props>
| Array<InterpolationTypes<Props>>

export interface Options {
string?: string
}

type ElementProps<
Tag extends keyof JSX.IntrinsicElements
> = JSX.IntrinsicElements[Tag]

export interface StyledComponent<Props, IntrinsicProps>
extends TsxComponent<Vue, Props & IntrinsicProps> {
withComponent<Tag extends keyof JSX.IntrinsicElements>(
tag: Tag
): StyledComponent<Props, ElementProps<Tag>>

withComponent(component: any): StyledComponent<Props, {}>

displayName: string
__emotion_styles: string[]
__emotion_base: string | {}
__emotion_real: ThemedVueEmotionInterface
}

export type ObjectStyleAttributes =
| object
| object[]
| { [key: string]: ObjectStyleAttributes }

export interface CreateStyled<Props, IntrinsicProps> {
// overload for template string as styles
(
strings: TemplateStringsArray,
...vars: Array<Interpolation<Props & IntrinsicProps>>
): StyledComponent<Props, IntrinsicProps>

// overload for object as styles
(
...styles: Array<
| ObjectStyleAttributes
| ((props: Props & IntrinsicProps) => ObjectStyleAttributes)
>
): StyledComponent<Props, IntrinsicProps>
}

type ShorthandsFactories = {
[Tag in keyof JSX.IntrinsicElements]: {
// overload for template string as styles
<Props = {}>(
strings: TemplateStringsArray,
...vars: Array<Interpolation<Props & JSX.IntrinsicElements[Tag]>>
): StyledComponent<Props, ElementProps<Tag>>

// overload for object as styles
<Props = {}>(
...styles: Array<
| ObjectStyleAttributes
| ((props: Props & JSX.IntrinsicElements[Tag]) => ObjectStyleAttributes)
>
): StyledComponent<Props, ElementProps<Tag>>
}
}

export interface ThemedVueEmotionInterface extends ShorthandsFactories {
// overload for dom tag
<Props, Tag extends keyof JSX.IntrinsicElements>(
tag: Tag,
options?: Options
): // tslint:disable-next-line:no-unnecessary-generics
CreateStyled<Props, ElementProps<Tag>>

// overload for component
<Props, CustomProps>(
component: any,
options?: Options
): // tslint:disable-next-line:no-unnecessary-generics
CreateStyled<Props & CustomProps, {}>
}

export interface ThemedVueEmotionModule {
default: ThemedVueEmotionInterface
}

declare const styled: ThemedVueEmotionInterface
export default styled
83 changes: 83 additions & 0 deletions types/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import styled, { flush, ThemedVueEmotionInterface } from './'

let Component
let mount

/*
* Inference HTML Tag Props
*/
Component = styled.div({ color: 'red' })
mount = <Component onClick={(event: any) => event} />

Component = styled('div')({ color: 'red' })
mount = <Component onClick={(event: any) => event} />

Component = styled.div`
color: red;
`
mount = <Component onClick={(event: any) => event} />

Component = styled('div')`
color: red;
`
mount = <Component onClick={(event: any) => event} />

Component = styled.a({ color: 'red' })
mount = <Component href="#" />

Component = styled('a')({ color: 'red' })
mount = <Component href="#" />

/*
* Passing custom props
*/
interface CustomProps {
lookColor: string
}

Component = styled.div<CustomProps>(
{ color: 'blue' },
props => ({
background: props.lookColor
}),
props => ({
border: `1px solid ${props.lookColor}`
})
)
mount = <Component lookColor="red" />

Component = styled<CustomProps, 'div'>('div')({ color: 'blue' }, props => ({
background: props.lookColor
}))
mount = <Component lookColor="red" />

const anotherColor = 'blue'
Component = styled<CustomProps, 'div'>('div')`
background: ${props => props.lookColor};
color: ${anotherColor};
`
mount = <Component lookColor="red" />

/*
* withComponent
*/

interface CustomProps3 {
bgColor: string
}

Component = styled.div<CustomProps3>(props => ({
bgColor: props.bgColor
}))

const Link = Component.withComponent('a')
mount = <Link href="#" bgColor="red" />

const Button = Component.withComponent('button')
mount = <Button type="submit" bgColor="red" />

/*
* Can use emotion helpers importing from vue-emotion
*/

flush()
21 changes: 21 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"declaration": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"jsx": "preserve",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true
},
"include": [
"./*.ts",
"./*.tsx",
"../node_modules/vue-tsx-support/enable-check.d.ts"
]
}
8 changes: 8 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-relative-import-in-test": false,
"no-empty-interface": false,
"semicolon": false
}
}
Loading