-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.d.ts
357 lines (277 loc) · 7.62 KB
/
index.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import {Component as ReactComponent, ComponentClass, HTMLProps, ReactNode} from 'react';
/**
Automatically binds your `Component` subclass methods to the instance.
@see https://github.com/sindresorhus/auto-bind#autobindreactself-options
@param self - Object with methods to bind.
@example
```
import {autoBind} from 'react-extras';
class Foo extends Component {
constructor(props) {
super(props);
autoBind.react(this);
}
// …
}
```
*/
/**
Conditionally join CSS class names together.
@param input - Accepts a combination of strings and objects. Only object keys with truthy values are included. Anything else is ignored.
@example
```
import {classNames} from 'react-extras';
classNames('unicorn', 'rainbow');
//=> 'unicorn rainbow'
classNames({awesome: true, foo: false}, 'unicorn', {rainbow: false});
//=> 'awesome unicorn'
classNames('unicorn', null, undefined, 0, 1, {foo: null});
//=> 'unicorn'
const buttonType = 'main';
classNames({[`button-${buttonType}`]: true});
//=> 'button-main'
```
@example
```
import {classNames} from 'react-extras';
const Button = props => {
const buttonClass = classNames(
'button',
{
[`button-${props.type}`]: props.type,
'button-block': props.block,
'button-small': props.small
}
);
console.log(buttonClass);
//=> 'button button-success button-small'
return <button className={buttonClass}>…</button>;
};
```
*/
/**
Returns a boolean of whether the given `Component` is a functional stateless component.
@see https://javascriptplayground.com/functional-stateless-components-react/
*/
export function isStatelessComponent(component: ComponentClass): boolean;
/**
Returns the display name of the given `Component`.
@see https://reactjs.org/docs/react-component.html#displayname
*/
export function getDisplayName(component: ComponentClass): string;
/**
A boolean of whether you're running in a context with a DOM.
Can be used to check if your component is running in the browser or if it's being server-rendered.
@see https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction
*/
export const canUseDOM: boolean;
interface IfProps {
/**
Condition to check. Children are only rendered if `true`.
*/
readonly condition: boolean;
/**
Children to render if `condition` is `true`.
*/
readonly children?: ReactNode;
/**
If you need the children to not be evaluated when `condition` is `false`, pass a function to the `render` prop that returns the children.
*/
readonly render?: () => ReactNode;
}
/**
React component that renders the children if the `condition` prop is `true`.
Beware that even though the children are not rendered when the `condition` is `false`, they're still evaluated.
If you need it to not be evaluated on `false`, you can pass a function to the `render` prop that returns the children:
@example
```
import {If} from 'react-extras';
<div>
<If condition={props.error} render={() => (
<h1>{props.error}</h1>
)}/>
</div>
```
Or you could just use plain JavaScript:
@example
```
import {If} from 'react-extras';
<div>
{props.error && (
<h1>{props.error}</h1>
)}
</div>
```
*/
export class If extends ReactComponent<IfProps> {}
interface ChooseOtherwiseProps {
/**
Children to render in the default case.
*/
readonly children?: ReactNode;
/**
If you need the children to not be evaluated when a `<Condition.When>` component has a true condition, pass a function to the `render` prop that returns the children.
*/
readonly render?: () => ReactNode;
}
export class ChooseOtherwise extends ReactComponent<ChooseOtherwiseProps> {}
/**
React component similar to a switch case. `<Choose>` has 2 children components:
- `<Choose.When>` that renders the children if the `condition` prop is `true`.
- `<Choose.Otherwise>` that renders the children if has no `<Choose.When>` with `true` prop `condition`.
Note that even when the children are not rendered, they're still evaluated.
@example
```
import {Choose} from 'react-extras';
<div>
<Choose>
<Choose.When condition={props.success}>
<h1>{props.success}</h1>
</Choose.When>
<Choose.When condition={props.error}>
<h1>{props.error}</h1>
</Choose.When>
<Choose.Otherwise>
<h1>😎</h1>
</Choose.Otherwise>
</Choose>
</div>
```
@example
```
<div>
{(() => {
if (props.success) {
return <h1>{props.success}</h1>;
}
if (props.error) {
return <h1>{props.error}</h1>;
}
return <h1>😎</h1>;
})()}
</div>
```
*/
export class Choose extends ReactComponent {
/**
Renders the children if the `condition` prop is `true`.
Use with `Choose` and `Choose.Otherwise`.
*/
static When: typeof If;
/**
Renders the children if there is no `<Choose.When>` with `true` prop `condition`.
Use with `Choose` and `Choose.When`.
*/
static Otherwise: typeof ChooseOtherwise;
}
interface ForProps<T> {
/**
Items to iterate over. `render` will be called once per item.
*/
readonly of: readonly T[];
/**
Returns the element to render corresponding to an `item`.
*/
readonly render?: (item: T, index: number) => ReactNode;
}
/**
React component that iterates over the `of` prop and renders the `render` prop.
@example
```
import {For} from 'react-extras';
<div>
<For of={['🌈', '🦄', '😎']} render={(item, index) =>
<button key={index}>{item}</button>
}/>
</div>
```
Or you could just use plain JavaScript:
@example
```
<div>
{['🌈', '🦄', '😎'].map((item, index) =>
<button key={index}>{item}</button>
)}
</div>
```
*/
export class For<T> extends ReactComponent<ForProps<T>> {}
interface ImageProps extends HTMLProps<HTMLImageElement> {
/**
URL of the image. Use instead of `src`.
*/
readonly url: string;
/**
Fallback URL to display if the image does not load.
Default: Hide the image if it fails to load.
*/
readonly fallbackUrl?: string;
}
/**
React component that improves the `<img>` element.
It makes the image invisible if it fails to load instead of showing the default broken image icon. Optionally, specify a fallback image URL.
@example
```
import {Image} from 'react-extras';
<Image
url="https://sindresorhus.com/unicorn.jpg"
fallbackUrl="https://sindresorhus.com/rainbow.jpg"
/>
```
It supports all the props that `<img>` supports, but you use the prop `url` instead of `src`.
*/
export class Image extends ReactComponent<ImageProps> {}
interface ElementClassProps {
/**
Classes to add to the root element.
Either a single class or multiple classes separated by space.
*/
readonly add?: string;
/**
Classes to remove from the root element.
Either a single class or multiple classes separated by space.
*/
readonly remove?: string;
}
/**
Renderless React component that can add and remove classes to the root `<html>` element. It accepts an `add` prop for adding classes, and a `remove` prop for removing classes. Both accept either a single class or multiple classes separated by space.
@example
```
import {If, RootClass} from 'react-extras';
<If condition={props.isDarkMode}>
<RootClass add="dark-mode"/>
</If>
```
@example
```
import {RootClass} from 'react-extras';
<RootClass add="logged-in paid-user" remove="promo"/>
```
*/
export class RootClass extends ReactComponent<ElementClassProps> {}
/**
Same as `<RootClass/>` but for `<body>`.
Prefer `<RootClass/>` though, because it's nicer to put global classes on `<html>` as you can consistently prefix everything with the class:
@example
```css
.dark-mode body {
background: #000;
}
.dark-mode a {
…
}
```
With `<BodyClass/>` you need to do:
@example
```css
body.dark-mode {
background: #000;
}
.dark-mode a {
…
}
```
*/
export class BodyClass extends ReactComponent<ElementClassProps> {}
export {default as classNames} from '@sindresorhus/class-names';
export {default as autoBind} from 'auto-bind/react';