Skip to content

Commit

Permalink
Add type for create-emotion
Browse files Browse the repository at this point in the history
  • Loading branch information
Ailrun committed May 19, 2018
1 parent 0cb4947 commit cdcb3fe
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/create-emotion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "The Next Generation of CSS-in-JS.",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"types": "types/index.d.ts",
"files": [
"src",
"dist",
Expand All @@ -12,6 +13,7 @@
"scripts": {
"build": "npm-run-all clean rollup",
"clean": "rimraf dist",
"test:typescript": "dtslint types",
"rollup": "rollup -c ../../rollup.config.js",
"watch": "rollup -c ../../rollup.config.js --watch"
},
Expand All @@ -20,6 +22,7 @@
"@emotion/memoize": "^0.6.1",
"@emotion/stylis": "^0.6.5",
"@emotion/unitless": "^0.6.2",
"csstype": "^2.5.2",
"stylis": "^3.5.0",
"stylis-rule-sheet": "^0.0.10"
},
Expand Down
85 changes: 85 additions & 0 deletions packages/create-emotion/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
// TypeScript Version: 2.3

import * as CSS from 'csstype';

export interface MultiDimensionalArray<T> extends Array<T | MultiDimensionalArray<T>> {}

export type CSSBaseObject = CSS.PropertiesFallback<number | string>;
export type CSSPseudoObject<C> = { [K in CSS.Pseudos]?: CSSObject<C> };
export interface CSSOthersObject<C> {
[propertiesName: string]: Interpolation<C>;
}
export interface CSSObject<C> extends CSSBaseObject, CSSPseudoObject<C>, CSSOthersObject<C> {}

export type FunctionInterpolation<C> = (mergedProps: any, context: C) => Interpolation<C> | void;

export interface ArrayInterpolation<C> extends Array<Interpolation<C>> {}

export type Interpolation<C> =
| undefined | null | boolean | string | number
| CSSObject<C>
| ArrayInterpolation<C>
| FunctionInterpolation<C>
;

export type FunctionClassNameArg = () => ClassNameArg | void;
export interface ArrayClassNameArg extends Array<ClassNameArg> {}

export type ClassNameArg =
| undefined | null | boolean | string
| { [key: string]: boolean }
| FunctionClassNameArg
| ArrayClassNameArg
;

export interface StyleSheet {
inject(): void;
speedy(bool: boolean): void;
insert(rule: string, sourceMap?: string): void;
flush(): void;
}

export interface EmotionCache {
registered: {
[key: string]: string;
};
inserted: {
[key: string]: string;
};
nonce?: string;
key: string;
}

export interface Emotion<C> {
flush(): void;
hydrate(ids: Array<string>): void;
cx(...classNames: Array<ClassNameArg>): string;
merge(className: string, sourceMap?: string): string;
getRegisteredStyles(registeredStyles: Array<string>, classNames: string): string;
css(...args: Array<Interpolation<C>>): string;
injectGlobal(...args: Array<Interpolation<C>>): void;
keyframes(...args: Array<Interpolation<C>>): string;
sheet: StyleSheet;
caches: EmotionCache;
}

export interface EmotionBaseContext<C> {
__SECRET_EMOTION__?: Emotion<C>;
}

export type StylisPlugins =
| null
| ((...args: Array<any>) => any)
| Array<(...args: Array<any>) => any>
;

export interface EmotionOption {
nonce?: string;
stylisPlugins?: StylisPlugins;
prefix?: boolean | ((key: string, value: string, context: 1 | 2 | 3) => boolean);
key?: string;
container?: HTMLElement;
}

export default function createEmotion<C extends EmotionBaseContext<C>>(context: C, options?: EmotionOption): Emotion<C>;
127 changes: 127 additions & 0 deletions packages/create-emotion/types/tests.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import createEmotion from '../';

const {
flush,
hydrate,
cx,
merge,
getRegisteredStyles,
css,
injectGlobal,
keyframes,
sheet,
caches,
} = createEmotion({});

flush();

hydrate([]);
hydrate(['123']);

cx();
cx(undefined);
cx(null);
cx(true);
cx('123');
cx('123', null, 'pf');
cx({
abc: false,
fp: true,
});
cx([]);
cx(['cl', {
fp: true,
}]);

merge('abc def fpfp');

getRegisteredStyles([], 'abc');
getRegisteredStyles(['abc'], 'bcd');
getRegisteredStyles([], 'abc def fpfw');

css();
css(1);
css('abc');
css(true);

css([]);
css([1]);
css([['abc', 'asdf'], 'efw']);

css(() => {
return 1;
});
css((a: number, b: {}) => {
return 1;
});

css({
':active': {
borderRadius: '2px',
overflowAnchor: 'none',
clear: ['both', 'left'],
},
'::before': {
borderRadius: '2px',
},
});

css(true, true);
css('fa', 1123);
css(['123'], 'asdf');
css('fa', () => {
});
css('pa--emp', () => {
return null;
});
css('squid', () => {
return ['other-squid', {
alignSelf: 'middle',
}];
});

injectGlobal();
injectGlobal(30);
injectGlobal('this-is-class');
injectGlobal({});
injectGlobal([{
animationDelay: '200ms',
}]);

keyframes();
keyframes({
from: {
marginLeft: '100%',
},
to: {
marginLeft: '50%',
},
});
keyframes([{
from: {
marginLeft: '100%',
},
to: {
marginLeft: '50%',
},
}, {
'0%': {
width: '100px',
},
'50%': {
width: '50px',
},
'100%': {
width: '120px',
},
}]);

sheet.flush();
sheet.inject();
sheet.insert('');
sheet.speedy(false);

caches.inserted;
caches.key;
caches.nonce;
caches.registered;
27 changes: 27 additions & 0 deletions packages/create-emotion/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"baseUrl": "../",
"forceConsistentCasingInFileNames": true,
"jsx": "react",
"lib": [
"es6",
"dom"
],
"module": "commonjs",
"noEmit": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strict": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"target": "es5",
"typeRoots": [
"../"
],
"types": []
},
"include": [
"./*.ts",
"./*.tsx"
]
}
7 changes: 7 additions & 0 deletions packages/create-emotion/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-relative-import-in-test": false,
"array-type": [true, "generic"]
}
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3805,6 +3805,10 @@ cssom@0.3.x, "cssom@>= 0.3.2 < 0.4.0":
dependencies:
cssom "0.3.x"

csstype@^2.5.2:
version "2.5.2"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.5.2.tgz#4534308476ceede8fbe148b9b99f9baf1c80fa06"

currently-unhandled@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"
Expand Down

0 comments on commit cdcb3fe

Please sign in to comment.