1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | declare global {
|
18 | namespace NodeJS {
|
19 |
|
20 | interface ReadableStream {}
|
21 | }
|
22 | }
|
23 |
|
24 | import * as CSS from 'csstype';
|
25 | import * as React from 'react';
|
26 | import * as hoistNonReactStatics from 'hoist-non-react-statics';
|
27 |
|
28 | export type CSSProperties = CSS.Properties<string | number>;
|
29 |
|
30 | export type CSSPseudos = { [K in CSS.Pseudos]?: CSSObject };
|
31 |
|
32 | export interface CSSObject extends CSSProperties, CSSPseudos {
|
33 | [key: string]: CSSObject | string | number | undefined;
|
34 | }
|
35 |
|
36 | export type CSSKeyframes = object & { [key: string]: CSSObject };
|
37 |
|
38 | export interface ThemeProps<T> {
|
39 | theme: T;
|
40 | }
|
41 |
|
42 | export type ThemedStyledProps<P, T> = P & ThemeProps<T>;
|
43 | export type StyledProps<P> = ThemedStyledProps<P, AnyIfEmpty<DefaultTheme>>;
|
44 | export type IntrinsicElementsKeys = keyof JSX.IntrinsicElements;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | type Defaultize<P, D> = P extends any
|
51 | ? string extends keyof P
|
52 | ? P
|
53 | : Pick<P, Exclude<keyof P, keyof D>> &
|
54 | Partial<Pick<P, Extract<keyof P, keyof D>>> &
|
55 | Partial<Pick<D, Exclude<keyof D, keyof P>>>
|
56 | : never;
|
57 |
|
58 | type ReactDefaultizedProps<C, P> = C extends { defaultProps: infer D } ? Defaultize<P, D> : P;
|
59 |
|
60 | type MakeAttrsOptional<C extends string | React.ComponentType<any>, O extends object, A extends keyof any> = Omit<
|
61 | ReactDefaultizedProps<
|
62 | C,
|
63 | React.ComponentPropsWithRef<C extends IntrinsicElementsKeys | React.ComponentType<any> ? C : never>
|
64 | > &
|
65 | O,
|
66 | A
|
67 | > &
|
68 | Partial<
|
69 | Pick<React.ComponentPropsWithRef<C extends IntrinsicElementsKeys | React.ComponentType<any> ? C : never> & O, A>
|
70 | >;
|
71 |
|
72 | export type StyledComponentProps<
|
73 |
|
74 | C extends string | React.ComponentType<any>,
|
75 |
|
76 | T extends object,
|
77 |
|
78 | O extends object,
|
79 |
|
80 | A extends keyof any,
|
81 |
|
82 | FAsC extends string | React.ComponentType<any> = C
|
83 | > =
|
84 |
|
85 | O extends object
|
86 | ? WithOptionalTheme<
|
87 | MakeAttrsOptional<C, O, A> & MakeAttrsOptional<FAsC, O, A>,
|
88 | T
|
89 | > &
|
90 | WithChildrenIfReactComponentClass<C>
|
91 | : never;
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | type WithChildrenIfReactComponentClass<C extends string | React.ComponentType<any>> = C extends React.ComponentClass<
|
98 | any
|
99 | >
|
100 | ? { children?: React.ReactNode | undefined }
|
101 | : {};
|
102 |
|
103 | type StyledComponentPropsWithAs<
|
104 | C extends string | React.ComponentType<any>,
|
105 | T extends object,
|
106 | O extends object,
|
107 | A extends keyof any,
|
108 | AsC extends string | React.ComponentType<any> = C,
|
109 | FAsC extends string | React.ComponentType<any> = C
|
110 | > = StyledComponentProps<C, T, O, A, FAsC> & { as?: AsC | undefined; forwardedAs?: FAsC | undefined };
|
111 |
|
112 | export type FalseyValue = undefined | null | false;
|
113 | export type Interpolation<P> = InterpolationValue | InterpolationFunction<P> | FlattenInterpolation<P>;
|
114 |
|
115 |
|
116 | export type FlattenInterpolation<P> = ReadonlyArray<Interpolation<P>>;
|
117 | export type InterpolationValue = string | number | FalseyValue | Keyframes | StyledComponentInterpolation | CSSObject;
|
118 | export type SimpleInterpolation = InterpolationValue | FlattenSimpleInterpolation;
|
119 | export type FlattenSimpleInterpolation = ReadonlyArray<SimpleInterpolation>;
|
120 |
|
121 | export type InterpolationFunction<P> = (props: P) => Interpolation<P>;
|
122 |
|
123 | type Attrs<P, A extends Partial<P>, T> = ((props: ThemedStyledProps<P, T>) => A) | A;
|
124 |
|
125 | export type ThemedGlobalStyledClassProps<P, T> = WithOptionalTheme<P, T> & {
|
126 | suppressMultiMountWarning?: boolean | undefined;
|
127 | };
|
128 |
|
129 | export interface GlobalStyleComponent<P, T> extends React.ComponentClass<ThemedGlobalStyledClassProps<P, T>> {}
|
130 |
|
131 | // remove the call signature from StyledComponent so Interpolation can still infer InterpolationFunction
|
132 | type StyledComponentInterpolation =
|
133 | | Pick<StyledComponentBase<any, any, any, any>, keyof StyledComponentBase<any, any>>
|
134 | | Pick<StyledComponentBase<any, any, any>, keyof StyledComponentBase<any, any>>;
|
135 |
|
136 | // abuse Pick to strip the call signature from ForwardRefExoticComponent
|
137 | type ForwardRefExoticBase<P> = Pick<React.ForwardRefExoticComponent<P>, keyof React.ForwardRefExoticComponent<any>>;
|
138 |
|
139 | // Config to be used with withConfig
|
140 | export interface StyledConfig<O extends object = {}> {
|
141 | // TODO: Add all types from the original StyledComponentWrapperProperties
|
142 | shouldForwardProp?: ((prop: keyof O, defaultValidatorFn: (prop: keyof O) => boolean) => boolean) | undefined;
|
143 | }
|
144 |
|
145 | // extracts React defaultProps
|
146 | type ReactDefaultProps<C> = C extends { defaultProps: infer D } ? D : never;
|
147 |
|
148 | // any doesn't count as assignable to never in the extends clause, and we default A to never
|
149 | export type AnyStyledComponent = StyledComponent<any, any, any, any> | StyledComponent<any, any, any>;
|
150 |
|
151 | export type StyledComponent<
|
152 | C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
|
153 | T extends object,
|
154 | O extends object = {},
|
155 | A extends keyof any = never
|
156 | > = // the "string" allows this to be used as an object key
|
157 | // I really want to avoid this if possible but it's the only way to use nesting with object styles...
|
158 | string &
|
159 | StyledComponentBase<C, T, O, A> &
|
160 | hoistNonReactStatics.NonReactStatics<C extends React.ComponentType<any> ? C : never>;
|
161 |
|
162 | export interface StyledComponentBase<
|
163 | C extends string | React.ComponentType<any>,
|
164 | T extends object,
|
165 | O extends object = {},
|
166 | A extends keyof any = never
|
167 | > extends ForwardRefExoticBase<StyledComponentProps<C, T, O, A>> {
|
168 | // add our own fake call signature to implement the polymorphic 'as' prop
|
169 | (props: StyledComponentProps<C, T, O, A> & { as?: never | undefined; forwardedAs?: never | undefined }): React.ReactElement<
|
170 | StyledComponentProps<C, T, O, A>
|
171 | >;
|
172 | <AsC extends string | React.ComponentType<any> = C, FAsC extends string | React.ComponentType<any> = AsC>(
|
173 | props: StyledComponentPropsWithAs<AsC, T, O, A, AsC, FAsC>,
|
174 | ): React.ReactElement<StyledComponentPropsWithAs<AsC, T, O, A, AsC, FAsC>>;
|
175 |
|
176 | withComponent<WithC extends AnyStyledComponent>(
|
177 | component: WithC,
|
178 | ): StyledComponent<
|
179 | StyledComponentInnerComponent<WithC>,
|
180 | T,
|
181 | O & StyledComponentInnerOtherProps<WithC>,
|
182 | A | StyledComponentInnerAttrs<WithC>
|
183 | >;
|
184 | withComponent<WithC extends keyof JSX.IntrinsicElements | React.ComponentType<any>>(
|
185 | component: WithC,
|
186 | ): StyledComponent<WithC, T, O, A>;
|
187 | }
|
188 |
|
189 | export interface ThemedStyledFunctionBase<
|
190 | C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
|
191 | T extends object,
|
192 | O extends object = {},
|
193 | A extends keyof any = never
|
194 | > {
|
195 | (first: TemplateStringsArray): StyledComponent<C, T, O, A>;
|
196 | (
|
197 | first:
|
198 | | TemplateStringsArray
|
199 | | CSSObject
|
200 | | InterpolationFunction<ThemedStyledProps<StyledComponentPropsWithRef<C> & O, T>>,
|
201 | ...rest: Array<Interpolation<ThemedStyledProps<StyledComponentPropsWithRef<C> & O, T>>>
|
202 | ): StyledComponent<C, T, O, A>;
|
203 | <U extends object>(
|
204 | first:
|
205 | | TemplateStringsArray
|
206 | | CSSObject
|
207 | | InterpolationFunction<ThemedStyledProps<StyledComponentPropsWithRef<C> & O & U, T>>,
|
208 | ...rest: Array<Interpolation<ThemedStyledProps<StyledComponentPropsWithRef<C> & O & U, T>>>
|
209 | ): StyledComponent<C, T, O & U, A>;
|
210 | }
|
211 |
|
212 | export interface ThemedStyledFunction<
|
213 | C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
|
214 | T extends object,
|
215 | O extends object = {},
|
216 | A extends keyof any = never
|
217 | > extends ThemedStyledFunctionBase<C, T, O, A> {
|
218 | // Fun thing: 'attrs' can also provide a polymorphic 'as' prop
|
219 | // My head already hurts enough so maybe later...
|
220 | attrs<
|
221 | U,
|
222 | NewA extends Partial<StyledComponentPropsWithRef<C> & U> & {
|
223 | [others: string]: any;
|
224 | } = {}
|
225 | >(
|
226 | attrs: Attrs<StyledComponentPropsWithRef<C> & U, NewA, T>,
|
227 | ): ThemedStyledFunction<C, T, O & NewA, A | keyof NewA>;
|
228 |
|
229 | withConfig: <Props extends O = O>(
|
230 | config: StyledConfig<StyledComponentPropsWithRef<C> & Props>,
|
231 | ) => ThemedStyledFunction<C, T, Props, A>;
|
232 | }
|
233 |
|
234 | export type StyledFunction<C extends keyof JSX.IntrinsicElements | React.ComponentType<any>> = ThemedStyledFunction<
|
235 | C,
|
236 | any
|
237 | >;
|
238 |
|
239 | type ThemedStyledComponentFactories<T extends object> = {
|
240 | [TTag in keyof JSX.IntrinsicElements]: ThemedStyledFunction<TTag, T>;
|
241 | };
|
242 |
|
243 | export type StyledComponentInnerComponent<C extends React.ComponentType<any>> = C extends StyledComponent<
|
244 | infer I,
|
245 | any,
|
246 | any,
|
247 | any
|
248 | >
|
249 | ? I
|
250 | : C extends StyledComponent<infer I, any, any>
|
251 | ? I
|
252 | : C;
|
253 | export type StyledComponentPropsWithRef<
|
254 | C extends keyof JSX.IntrinsicElements | React.ComponentType<any>
|
255 | > = C extends AnyStyledComponent
|
256 | ? React.ComponentPropsWithRef<StyledComponentInnerComponent<C>>
|
257 | : React.ComponentPropsWithRef<C>;
|
258 | export type StyledComponentInnerOtherProps<C extends AnyStyledComponent> = C extends StyledComponent<
|
259 | any,
|
260 | any,
|
261 | infer O,
|
262 | any
|
263 | >
|
264 | ? O
|
265 | : C extends StyledComponent<any, any, infer O>
|
266 | ? O
|
267 | : never;
|
268 | export type StyledComponentInnerAttrs<C extends AnyStyledComponent> = C extends StyledComponent<any, any, any, infer A>
|
269 | ? A
|
270 | : never;
|
271 |
|
272 | export interface ThemedBaseStyledInterface<T extends object> extends ThemedStyledComponentFactories<T> {
|
273 | <C extends AnyStyledComponent>(component: C): ThemedStyledFunction<
|
274 | StyledComponentInnerComponent<C>,
|
275 | T,
|
276 | StyledComponentInnerOtherProps<C>,
|
277 | StyledComponentInnerAttrs<C>
|
278 | >;
|
279 | <C extends keyof JSX.IntrinsicElements | React.ComponentType<any>>(
|
280 |
|
281 |
|
282 | component: C,
|
283 | ): ThemedStyledFunction<C, T>;
|
284 | }
|
285 |
|
286 | export type ThemedStyledInterface<T extends object> = ThemedBaseStyledInterface<AnyIfEmpty<T>>;
|
287 | export type StyledInterface = ThemedStyledInterface<DefaultTheme>;
|
288 |
|
289 | export interface BaseThemedCssFunction<T extends object> {
|
290 | (first: TemplateStringsArray | CSSObject, ...interpolations: SimpleInterpolation[]): FlattenSimpleInterpolation;
|
291 | (
|
292 | first: TemplateStringsArray | CSSObject | InterpolationFunction<ThemedStyledProps<{}, T>>,
|
293 | ...interpolations: Array<Interpolation<ThemedStyledProps<{}, T>>>
|
294 | ): FlattenInterpolation<ThemedStyledProps<{}, T>>;
|
295 | <P extends object>(
|
296 | first: TemplateStringsArray | CSSObject | InterpolationFunction<ThemedStyledProps<P, T>>,
|
297 | ...interpolations: Array<Interpolation<ThemedStyledProps<P, T>>>
|
298 | ): FlattenInterpolation<ThemedStyledProps<P, T>>;
|
299 | }
|
300 |
|
301 | export type ThemedCssFunction<T extends object> = BaseThemedCssFunction<AnyIfEmpty<T>>;
|
302 |
|
303 |
|
304 | type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
305 | type WithOptionalTheme<P extends { theme?: T | undefined }, T> = Omit<P, 'theme'> & {
|
306 | theme?: T | undefined;
|
307 | };
|
308 | type AnyIfEmpty<T extends object> = keyof T extends never ? any : T;
|
309 |
|
310 | export interface ThemedStyledComponentsModule<T extends object, U extends object = T> {
|
311 | default: ThemedStyledInterface<T>;
|
312 |
|
313 | css: ThemedCssFunction<T>;
|
314 |
|
315 |
|
316 | keyframes(strings: TemplateStringsArray | CSSKeyframes, ...interpolations: SimpleInterpolation[]): Keyframes;
|
317 |
|
318 | createGlobalStyle<P extends object = {}>(
|
319 | first: TemplateStringsArray | CSSObject | InterpolationFunction<ThemedStyledProps<P, T>>,
|
320 | ...interpolations: Array<Interpolation<ThemedStyledProps<P, T>>>
|
321 | ): GlobalStyleComponent<P, T>;
|
322 |
|
323 | withTheme: WithThemeFnInterface<T>;
|
324 | ThemeProvider: ThemeProviderComponent<T, U>;
|
325 | ThemeConsumer: React.Consumer<T>;
|
326 | ThemeContext: React.Context<T>;
|
327 | useTheme(): T;
|
328 |
|
329 |
|
330 | isStyledComponent: typeof isStyledComponent;
|
331 |
|
332 | ServerStyleSheet: typeof ServerStyleSheet;
|
333 | StyleSheetManager: typeof StyleSheetManager;
|
334 | }
|
335 |
|
336 | declare const styled: StyledInterface;
|
337 |
|
338 | export const css: ThemedCssFunction<DefaultTheme>;
|
339 |
|
340 | export type BaseWithThemeFnInterface<T extends object> = <C extends React.ComponentType<any>>(
|
341 |
|
342 |
|
343 | component: React.ComponentProps<C> extends { theme?: T | undefined } ? C : never,
|
344 | ) => React.ForwardRefExoticComponent<WithOptionalTheme<React.ComponentPropsWithRef<C>, T>>;
|
345 | export type WithThemeFnInterface<T extends object> = BaseWithThemeFnInterface<AnyIfEmpty<T>>;
|
346 | export const withTheme: WithThemeFnInterface<DefaultTheme>;
|
347 |
|
348 | export function useTheme(): DefaultTheme;
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 | export interface DefaultTheme {}
|
358 |
|
359 | export interface ThemeProviderProps<T extends object, U extends object = T> {
|
360 | children?: React.ReactNode | undefined;
|
361 | theme: T | ((theme: U) => T);
|
362 | }
|
363 | export type BaseThemeProviderComponent<T extends object, U extends object = T> = React.ComponentClass<
|
364 | ThemeProviderProps<T, U>
|
365 | >;
|
366 | export type ThemeProviderComponent<T extends object, U extends object = T> = BaseThemeProviderComponent<
|
367 | AnyIfEmpty<T>,
|
368 | AnyIfEmpty<U>
|
369 | >;
|
370 | export const ThemeProvider: ThemeProviderComponent<AnyIfEmpty<DefaultTheme>>;
|
371 | // NOTE: this technically starts as undefined, but allowing undefined is unhelpful when used correctly
|
372 | export const ThemeContext: React.Context<AnyIfEmpty<DefaultTheme>>;
|
373 | export const ThemeConsumer: typeof ThemeContext['Consumer'];
|
374 |
|
375 | export interface Keyframes {
|
376 | getName(): string;
|
377 | }
|
378 |
|
379 | export function keyframes(
|
380 | strings: TemplateStringsArray | CSSKeyframes,
|
381 | ...interpolations: SimpleInterpolation[]
|
382 | ): Keyframes;
|
383 |
|
384 | export function createGlobalStyle<P extends object = {}>(
|
385 | first: TemplateStringsArray | CSSObject | InterpolationFunction<ThemedStyledProps<P, DefaultTheme>>,
|
386 | ...interpolations: Array<Interpolation<ThemedStyledProps<P, DefaultTheme>>>
|
387 | ): GlobalStyleComponent<P, DefaultTheme>;
|
388 |
|
389 | export function isStyledComponent(target: any): target is StyledComponent<any, any>;
|
390 |
|
391 | export class ServerStyleSheet {
|
392 | collectStyles(tree: React.ReactNode): React.ReactElement<{ sheet: ServerStyleSheet }>;
|
393 |
|
394 | getStyleTags(): string;
|
395 | getStyleElement(): Array<React.ReactElement<{}>>;
|
396 | interleaveWithNodeStream(readableStream: NodeJS.ReadableStream): NodeJS.ReadableStream;
|
397 | readonly instance: this;
|
398 | seal(): void;
|
399 | }
|
400 |
|
401 | export type StylisPlugin = (
|
402 | context: number,
|
403 | selector: string[],
|
404 | parent: string[],
|
405 | content: string,
|
406 | line: number,
|
407 | column: number,
|
408 | length: number,
|
409 | ) => string | void;
|
410 |
|
411 | export interface StyleSheetManagerProps {
|
412 | disableCSSOMInjection?: boolean | undefined;
|
413 | disableVendorPrefixes?: boolean | undefined;
|
414 | stylisPlugins?: StylisPlugin[] | undefined;
|
415 | sheet?: ServerStyleSheet | undefined;
|
416 | target?: HTMLElement | undefined;
|
417 | }
|
418 |
|
419 | export class StyleSheetManager extends React.Component<StyleSheetManagerProps> {}
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 | export type CSSProp<T = AnyIfEmpty<DefaultTheme>> = string | CSSObject | FlattenInterpolation<ThemeProps<T>>;
|
449 |
|
450 | export default styled;
|