Skip to content

Commit b57da8a

Browse files
author
fpapado
committed
Accept 'sizes', refactor LazyImageFull
Breaking: LazyImageFull now only takes children as a function, not render. Improvement: Simplified LazyImageFull code in a bunch of places, such that it only concerns itself with the observing and loading.
1 parent cd385a8 commit b57da8a

File tree

2 files changed

+20
-48
lines changed

2 files changed

+20
-48
lines changed

src/LazyImage.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface LazyImageRenderPropArgs {
1212
src?: string;
1313
srcSet?: string;
1414
alt?: string;
15+
sizes?: string;
1516
}
1617

1718
export interface LazyImageProps extends CommonLazyImageProps {

src/LazyImageFull.tsx

+19-48
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,11 @@ export interface CommonLazyImageProps {
3333
/** Valid props for LazyImageFull */
3434
export interface LazyImageFullProps extends CommonLazyImageProps {
3535
/** Children should be either a function or a node */
36-
children?:
37-
| ((args: LazyImageFullRenderPropArgs) => React.ReactNode)
38-
| React.ReactNode;
39-
40-
/** Render prop boolean indicating inView state */
41-
render?: (args: LazyImageFullRenderPropArgs) => React.ReactNode;
36+
children: (args: RenderCallbackArgs) => React.ReactNode;
4237
}
4338

4439
/** Values that the render props take */
45-
export interface LazyImageFullRenderPropArgs {
40+
export interface RenderCallbackArgs {
4641
imageState: ImageState;
4742
src?: string;
4843
srcSet?: string;
@@ -102,8 +97,6 @@ export class LazyImageFull extends React.Component<
10297
// Bind methods
10398
// This would be nicer with arrow functions and class properties,
10499
// but holding off until they are settled.
105-
this.renderInner = this.renderInner.bind(this);
106-
this.renderLazy = this.renderLazy.bind(this);
107100
this.onInView = this.onInView.bind(this);
108101
this.onLoadSuccess = this.onLoadSuccess.bind(this);
109102
this.onLoadError = this.onLoadError.bind(this);
@@ -114,13 +107,13 @@ export class LazyImageFull extends React.Component<
114107
if (inView === true) {
115108
// If src is not specified, then there is nothing to preload; skip to Loaded state
116109
if (!this.props.src) {
117-
this.setState((state, props) => ({
110+
this.setState((state, _props) => ({
118111
...state,
119112
imageState: ImageState.LoadSuccess
120113
}));
121114
} else {
122115
// Kick off request for Image and attach listeners for response
123-
this.setState((state, props) => ({
116+
this.setState((state, _props) => ({
124117
...state,
125118
imageState: ImageState.Loading
126119
}));
@@ -153,47 +146,25 @@ export class LazyImageFull extends React.Component<
153146

154147
// Render function
155148
render() {
156-
if (this.props.loadEagerly) {
149+
const { children, loadEagerly, observerProps, ...cbProps } = this.props;
150+
151+
if (loadEagerly) {
157152
// If eager, skip the observer and view changing stuff; resolve the imageState as loaded.
158-
return this.renderInner(this.props, {
159-
imageState: ImageState.LoadSuccess
160-
});
153+
return children({ imageState: ImageState.LoadSuccess, ...cbProps });
161154
} else {
162-
return this.renderLazy(this.props, this.state);
155+
return (
156+
<Observer
157+
rootMargin="50px 0px"
158+
threshold={0.01}
159+
{...observerProps}
160+
onChange={this.onInView}
161+
triggerOnce
162+
>
163+
{children({ imageState: this.state.imageState, ...cbProps })}
164+
</Observer>
165+
);
163166
}
164167
}
165-
166-
// TODO: split out
167-
renderLazy(props: LazyImageFullProps, state: { imageState: ImageState }) {
168-
const { observerProps } = props;
169-
170-
return (
171-
<Observer
172-
rootMargin="50px 0px"
173-
threshold={0.01}
174-
{...observerProps}
175-
onChange={this.onInView}
176-
triggerOnce
177-
>
178-
{this.renderInner(props, state)}
179-
</Observer>
180-
);
181-
}
182-
183-
renderInner(props: LazyImageFullProps, state: { imageState: ImageState }) {
184-
const { src, srcSet, alt, sizes, render, children } = props;
185-
const { imageState } = state;
186-
187-
return (
188-
<React.Fragment>
189-
{typeof render === "function"
190-
? render({ src, srcSet, alt, sizes, imageState })
191-
: typeof children === "function"
192-
? children({ src, srcSet, alt, sizes, imageState })
193-
: null}
194-
</React.Fragment>
195-
);
196-
}
197168
}
198169

199170
// Utilities

0 commit comments

Comments
 (0)