1
1
import * as React from "react" ;
2
2
3
- export type DivProps = React . HTMLProps < HTMLImageElement > ;
4
- export type ImageProps = React . HTMLProps < HTMLDivElement > ;
5
-
6
3
export interface ProgressiveImageProps {
7
4
preview : string ;
8
5
src : string ;
9
- background ?: boolean ;
10
- backgroundImages ?: string [ ] ;
11
6
transitionTime ?: number ;
12
7
timingFunction ?: string ;
13
- maxBlur ? : number ;
8
+ initialBlur : number ;
14
9
}
15
10
16
11
export interface ProgressiveImageState {
17
12
src : string ;
18
13
blur : number ;
19
14
}
20
15
21
- export class ProgressiveImage extends React . Component < ProgressiveImageProps & DivProps & ImageProps , ProgressiveImageState > {
22
-
23
- private clonedProps : React . HTMLProps < HTMLDivElement & HTMLImageElement > = { } ;
16
+ export class ProgressiveImage extends React . Component < ProgressiveImageProps , ProgressiveImageState > {
24
17
25
18
static defaultProps = {
26
19
transitionTime : 500 ,
27
20
timingFunction : "ease" ,
28
- maxBlur : 10
21
+ initialBlur : 10
29
22
} ;
30
23
31
24
componentWillMount ( ) {
32
- const { src, preview, maxBlur} = this . props ;
33
- this . setState ( { src : "" , blur : maxBlur as number } ) ;
34
- this . cloneProps ( ) ;
25
+ const { src, preview} = this . props ;
26
+ const initialBlur = this . props . initialBlur as number ;
35
27
this . fetch ( preview )
36
- . then ( previewDataURI => this . setState ( { src : previewDataURI , blur : maxBlur as number } ) )
28
+ . then ( previewDataURI => this . setState ( { src : previewDataURI , blur : initialBlur } ) )
37
29
. then ( ( ) => this . fetch ( src ) )
38
- . then ( srcDataURI => this . setState ( { src : srcDataURI , blur : 0 } ) ) ;
30
+ . then ( srcDataURI => this . setState ( { src : srcDataURI , blur : initialBlur } ) ) ;
39
31
}
32
+
40
33
render ( ) {
41
- const { src, style , background } = this . props ;
42
- return background ?
43
- < div style = { Object . assign ( this . getBackgroundStyle ( ) , style ) } { ... this . clonedProps } > { this . props . children } </ div >
44
- :
45
- < img src = { src } style = { Object . assign ( this . getStyle ( ) , style ) } { ... this . clonedProps } />
46
- ;
34
+ const { src} = this . state ;
35
+ const { children } = this . props ;
36
+ if ( ! children || typeof children !== "function" ) {
37
+ throw new Error ( "ProgressiveImage requires a function as its only child" ) ;
38
+ }
39
+ return children ( src , this . getStyle ( ) ) ;
47
40
}
48
41
49
42
private fetch ( src : string ) : Promise < string > {
@@ -54,12 +47,6 @@ export class ProgressiveImage extends React.Component<ProgressiveImageProps & Di
54
47
} ) ;
55
48
}
56
49
57
- private cloneProps ( ) {
58
- Object . keys ( this . props )
59
- . filter ( prop => [ "style" , "src" , "preview" , "background" , "transitionTime" , "timingFunction" , "backgroundImages" , "maxBlur" , "children" ] . indexOf ( prop ) === - 1 )
60
- . forEach ( prop => this . clonedProps [ prop ] = this . props [ prop ] ) ;
61
- }
62
-
63
50
private getStyle ( ) {
64
51
const { transitionTime, timingFunction} = this . props ;
65
52
const { blur} = this . state ;
@@ -68,13 +55,4 @@ export class ProgressiveImage extends React.Component<ProgressiveImageProps & Di
68
55
transition : `filter ${ transitionTime } ms ${ timingFunction } `
69
56
} ;
70
57
}
71
-
72
- private getBackgroundStyle ( ) {
73
- const { src} = this . state ;
74
- const { backgroundImages} = this . props ;
75
- const style = {
76
- backgroundImage : `${ backgroundImages ? `${ backgroundImages . join ( "," ) } ,` : "" } url(${ src } )`
77
- } ;
78
- return Object . assign ( style , this . getStyle ( ) ) ;
79
- }
80
58
}
0 commit comments