-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
/
Copy pathwithStyles.d.ts
120 lines (103 loc) · 3.85 KB
/
withStyles.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import * as React from 'react';
import { PropInjector, CoerceEmptyInterface, IsEmptyInterface } from '@material-ui/types';
import * as CSS from 'csstype';
import * as JSS from 'jss';
import { DefaultTheme } from '../defaultTheme';
// Disable automatic export
export {};
type JSSFontface = CSS.FontFace & { fallbacks?: CSS.FontFace[] };
/**
* Allows the user to augment the properties available
*/
export interface BaseCSSProperties extends CSS.Properties<number | string> {
'@font-face'?: JSSFontface | JSSFontface[];
}
export interface CSSProperties extends BaseCSSProperties {
// Allow pseudo selectors and media queries
[k: string]: BaseCSSProperties[keyof BaseCSSProperties] | CSSProperties;
}
export type BaseCreateCSSProperties<Props extends object = {}> = {
[P in keyof BaseCSSProperties]: BaseCSSProperties[P] | ((props: Props) => BaseCSSProperties[P])
};
export interface CreateCSSProperties<Props extends object = {}>
extends BaseCreateCSSProperties<Props> {
// Allow pseudo selectors and media queries
[k: string]:
| BaseCreateCSSProperties<Props>[keyof BaseCreateCSSProperties<Props>]
| CreateCSSProperties<Props>;
}
/**
* This is basically the API of JSS. It defines a Map<string, CSS>,
* where
* - the `keys` are the class (names) that will be created
* - the `values` are objects that represent CSS rules (`React.CSSProperties`).
*
* if only `CSSProperties` are matched `Props` are inferred to `any`
*/
export type StyleRules<Props extends object = {}, ClassKey extends string = string> = Record<
ClassKey,
IsEmptyInterface<Props> extends true
? CSSProperties | (() => CSSProperties)
: CreateCSSProperties<Props> | ((props: Props) => CreateCSSProperties<Props>)
>;
/**
* @internal
*/
export type StyleRulesCallback<Theme, Props extends object, ClassKey extends string = string> = (
theme: Theme,
) => StyleRules<Props, ClassKey>;
export type Styles<Theme, Props extends object, ClassKey extends string = string> =
| StyleRules<Props, ClassKey>
| StyleRulesCallback<Theme, Props, ClassKey>;
export interface WithStylesOptions<Theme = DefaultTheme> extends JSS.StyleSheetFactoryOptions {
defaultTheme?: Theme;
flip?: boolean;
withTheme?: boolean;
name?: string;
}
export type ClassNameMap<ClassKey extends string = string> = Record<ClassKey, string>;
/**
* @internal
*/
export type ClassKeyInferable<Theme, Props extends object> = string | Styles<Theme, Props>;
export type ClassKeyOfStyles<StylesOrClassKey> = StylesOrClassKey extends string
? StylesOrClassKey
: StylesOrClassKey extends StyleRulesCallback<any, any, infer ClassKey>
? ClassKey
: StylesOrClassKey extends StyleRules<any, infer ClassKey>
? ClassKey
: never;
/**
* infers the type of the props used in the styles
*/
export type PropsOfStyles<StylesType> = StylesType extends Styles<any, infer Props>
? CoerceEmptyInterface<Props>
: {};
/**
* infers the type of the theme used in the styles
*/
export type ThemeOfStyles<StylesType> = StylesType extends Styles<infer Theme, any> ? Theme : {};
export type WithStyles<
StylesType extends ClassKeyInferable<any, any>,
IncludeTheme extends boolean | undefined = false
> = (IncludeTheme extends true ? { theme: ThemeOfStyles<StylesType> } : {}) & {
classes: ClassNameMap<ClassKeyOfStyles<StylesType>>;
innerRef?: React.Ref<any>;
} & PropsOfStyles<StylesType>;
export interface StyledComponentProps<ClassKey extends string = string> {
/**
* Override or extend the styles applied to the component.
*/
classes?: Partial<ClassNameMap<ClassKey>>;
innerRef?: React.Ref<any>;
}
export default function withStyles<
StylesType extends Styles<any, any>,
Options extends WithStylesOptions<ThemeOfStyles<StylesType>> = {}
>(
style: StylesType,
options?: Options,
): PropInjector<
WithStyles<StylesType, Options['withTheme']>,
StyledComponentProps<ClassKeyOfStyles<StylesType>> & PropsOfStyles<StylesType>
>;