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