11import * as React from "react" ;
22
3- export type DivProps = React . HTMLProps < HTMLImageElement > ;
4- export type ImageProps = React . HTMLProps < HTMLDivElement > ;
5-
63export interface ProgressiveImageProps {
74 preview : string ;
85 src : string ;
9- background ?: boolean ;
10- backgroundImages ?: string [ ] ;
116 transitionTime ?: number ;
127 timingFunction ?: string ;
13- maxBlur ? : number ;
8+ initialBlur : number ;
149}
1510
1611export interface ProgressiveImageState {
1712 src : string ;
1813 blur : number ;
1914}
2015
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 > {
2417
2518 static defaultProps = {
2619 transitionTime : 500 ,
2720 timingFunction : "ease" ,
28- maxBlur : 10
21+ initialBlur : 10
2922 } ;
3023
3124 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 ;
3527 this . fetch ( preview )
36- . then ( previewDataURI => this . setState ( { src : previewDataURI , blur : maxBlur as number } ) )
28+ . then ( previewDataURI => this . setState ( { src : previewDataURI , blur : initialBlur } ) )
3729 . then ( ( ) => this . fetch ( src ) )
38- . then ( srcDataURI => this . setState ( { src : srcDataURI , blur : 0 } ) ) ;
30+ . then ( srcDataURI => this . setState ( { src : srcDataURI , blur : initialBlur } ) ) ;
3931 }
32+
4033 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 ( ) ) ;
4740 }
4841
4942 private fetch ( src : string ) : Promise < string > {
@@ -54,12 +47,6 @@ export class ProgressiveImage extends React.Component<ProgressiveImageProps & Di
5447 } ) ;
5548 }
5649
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-
6350 private getStyle ( ) {
6451 const { transitionTime, timingFunction} = this . props ;
6552 const { blur} = this . state ;
@@ -68,13 +55,4 @@ export class ProgressiveImage extends React.Component<ProgressiveImageProps & Di
6855 transition : `filter ${ transitionTime } ms ${ timingFunction } `
6956 } ;
7057 }
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- }
8058}
0 commit comments