UNPKG

5.39 kBTypeScriptView Raw
1// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
2// TypeScript Version: 3.2
3
4import * as React from 'react'
5import { ComponentSelector, Interpolation } from '@emotion/serialize'
6import { PropsOf, DistributiveOmit, Theme } from '@emotion/react'
7
8export {
9 ArrayInterpolation,
10 CSSObject,
11 FunctionInterpolation
12} from '@emotion/serialize'
13
14export { ComponentSelector, Interpolation }
15
16/** Same as StyledOptions but shouldForwardProp must be a type guard */
17export interface FilteringStyledOptions<
18 Props,
19 ForwardedProps extends keyof Props = keyof Props
20> {
21 label?: string
22 shouldForwardProp?(propName: PropertyKey): propName is ForwardedProps
23 target?: string
24}
25
26export interface StyledOptions<Props> {
27 label?: string
28 shouldForwardProp?(propName: PropertyKey): boolean
29 target?: string
30}
31
32/**
33 * @typeparam ComponentProps Props which will be included when withComponent is called
34 * @typeparam SpecificComponentProps Props which will *not* be included when withComponent is called
35 */
36export interface StyledComponent<
37 ComponentProps extends {},
38 SpecificComponentProps extends {} = {},
39 JSXProps extends {} = {}
40> extends React.FC<ComponentProps & SpecificComponentProps & JSXProps>,
41 ComponentSelector {
42 withComponent<C extends React.ComponentClass<React.ComponentProps<C>>>(
43 component: C
44 ): StyledComponent<
45 ComponentProps & PropsOf<C>,
46 {},
47 { ref?: React.Ref<InstanceType<C>> }
48 >
49 withComponent<C extends React.ComponentType<React.ComponentProps<C>>>(
50 component: C
51 ): StyledComponent<ComponentProps & PropsOf<C>>
52 withComponent<Tag extends keyof JSX.IntrinsicElements>(
53 tag: Tag
54 ): StyledComponent<ComponentProps, JSX.IntrinsicElements[Tag]>
55}
56
57/**
58 * @typeparam ComponentProps Props which will be included when withComponent is called
59 * @typeparam SpecificComponentProps Props which will *not* be included when withComponent is called
60 */
61export interface CreateStyledComponent<
62 ComponentProps extends {},
63 SpecificComponentProps extends {} = {},
64 JSXProps extends {} = {}
65> {
66 /**
67 * @typeparam AdditionalProps Additional props to add to your styled component
68 */
69 <AdditionalProps extends {} = {}>(
70 ...styles: Array<
71 Interpolation<
72 ComponentProps &
73 SpecificComponentProps &
74 AdditionalProps & { theme: Theme }
75 >
76 >
77 ): StyledComponent<
78 ComponentProps & AdditionalProps,
79 SpecificComponentProps,
80 JSXProps
81 >
82
83 (
84 template: TemplateStringsArray,
85 ...styles: Array<
86 Interpolation<ComponentProps & SpecificComponentProps & { theme: Theme }>
87 >
88 ): StyledComponent<ComponentProps, SpecificComponentProps, JSXProps>
89
90 /**
91 * @typeparam AdditionalProps Additional props to add to your styled component
92 */
93 <AdditionalProps extends {}>(
94 template: TemplateStringsArray,
95 ...styles: Array<
96 Interpolation<
97 ComponentProps &
98 SpecificComponentProps &
99 AdditionalProps & { theme: Theme }
100 >
101 >
102 ): StyledComponent<
103 ComponentProps & AdditionalProps,
104 SpecificComponentProps,
105 JSXProps
106 >
107}
108
109/**
110 * @desc
111 * This function accepts a React component or tag ('div', 'a' etc).
112 *
113 * @example styled(MyComponent)({ width: 100 })
114 * @example styled(MyComponent)(myComponentProps => ({ width: myComponentProps.width })
115 * @example styled('div')({ width: 100 })
116 * @example styled('div')<Props>(props => ({ width: props.width })
117 */
118export interface CreateStyled {
119 <
120 C extends React.ComponentClass<React.ComponentProps<C>>,
121 ForwardedProps extends keyof React.ComponentProps<C> = keyof React.ComponentProps<C>
122 >(
123 component: C,
124 options: FilteringStyledOptions<React.ComponentProps<C>, ForwardedProps>
125 ): CreateStyledComponent<
126 Pick<PropsOf<C>, ForwardedProps> & {
127 theme?: Theme
128 },
129 {},
130 {
131 ref?: React.Ref<InstanceType<C>>
132 }
133 >
134
135 <C extends React.ComponentClass<React.ComponentProps<C>>>(
136 component: C,
137 options?: StyledOptions<React.ComponentProps<C>>
138 ): CreateStyledComponent<
139 PropsOf<C> & {
140 theme?: Theme
141 },
142 {},
143 {
144 ref?: React.Ref<InstanceType<C>>
145 }
146 >
147
148 <
149 C extends React.ComponentType<React.ComponentProps<C>>,
150 ForwardedProps extends keyof React.ComponentProps<C> = keyof React.ComponentProps<C>
151 >(
152 component: C,
153 options: FilteringStyledOptions<React.ComponentProps<C>, ForwardedProps>
154 ): CreateStyledComponent<
155 Pick<PropsOf<C>, ForwardedProps> & {
156 theme?: Theme
157 }
158 >
159
160 <C extends React.ComponentType<React.ComponentProps<C>>>(
161 component: C,
162 options?: StyledOptions<React.ComponentProps<C>>
163 ): CreateStyledComponent<
164 PropsOf<C> & {
165 theme?: Theme
166 }
167 >
168
169 <
170 Tag extends keyof JSX.IntrinsicElements,
171 ForwardedProps extends keyof JSX.IntrinsicElements[Tag] = keyof JSX.IntrinsicElements[Tag]
172 >(
173 tag: Tag,
174 options: FilteringStyledOptions<JSX.IntrinsicElements[Tag], ForwardedProps>
175 ): CreateStyledComponent<
176 { theme?: Theme; as?: React.ElementType },
177 Pick<JSX.IntrinsicElements[Tag], ForwardedProps>
178 >
179
180 <Tag extends keyof JSX.IntrinsicElements>(
181 tag: Tag,
182 options?: StyledOptions<JSX.IntrinsicElements[Tag]>
183 ): CreateStyledComponent<
184 { theme?: Theme; as?: React.ElementType },
185 JSX.IntrinsicElements[Tag]
186 >
187}
188
189declare const styled: CreateStyled
190export default styled