Skip to content

Commit 35a4e76

Browse files
author
fpapado
committed
refactor: Remove cls render prop argument
BREAKING CHANGE The cls property was not adding much, since we are not using it as a hook for styling in the library. There is an argument that we could use it for fallbacks, but that requires the user to set the style tag anyway. Thus, it is best to leave it undefined. It also bloated the syntax quite a bit, with a mess of squgglies that can easily be messed up; JSX prop, arrow function, and then destructuring. {({cls}) => ... }. I think this is more convenient for people to use, especially weighed against the lack of benefit to the library.
1 parent 1d15b54 commit 35a4e76

File tree

3 files changed

+77
-138
lines changed

3 files changed

+77
-138
lines changed

README.md

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,8 @@ import { LazyImage } from "react-lazy-images";
106106

107107
<LazyImage
108108
src="https://www.fillmurray.com/g/600/400"
109-
placeholder={({ cls }) => (
110-
<img src="https://www.fillmurray.com/g/60/40" className={cls} />
111-
)}
112-
actual={({ cls }) => (
113-
<img src="https://www.fillmurray.com/g/600/400" className={cls} />
114-
)}
109+
placeholder={() => <img src="https://www.fillmurray.com/g/60/40" />}
110+
actual={() => <img src="https://www.fillmurray.com/g/600/400" />}
115111
/>;
116112
```
117113

@@ -135,28 +131,28 @@ Thus, whether you want to display a simple `<img>`, your own `<Image>`, or even
135131
src={https://www.fillmurray.com/g/600/400}
136132
// This is rendered first
137133
placeholder={
138-
({cls}) =>
139-
<img src="https://www.fillmurray.com/g/60/40" className={cls} />
134+
() =>
135+
<img src="https://www.fillmurray.com/g/60/40" />
140136
}
141137
// This is rendered once in view
142138
actual={
143-
({cls}) =>
144-
<img src="https://www.fillmurray.com/g/600/400" className={cls} />
139+
() =>
140+
<img src="https://www.fillmurray.com/g/600/400" />
145141
}
146142
/>
147143

148144
// Perhaps you want a container?
149145
<LazyImage
150146
placeholder={
151-
({cls}) =>
152-
<div className={`LazyImage-Placeholder ${cls}`}">
147+
() =>
148+
<div className={`LazyImage-Placeholder`}">
153149
<img src="https://www.fillmurray.com/g/60/40"/>
154150
</div>
155151
}
156152
actual={
157-
({cls}) =>
158-
<div className={`LazyImage-Actual ${cls}`}>
159-
<img src="https://www.fillmurray.com/g/600/400" className={cls} />
153+
() =>
154+
<div className={`LazyImage-Actual`}>
155+
<img src="https://www.fillmurray.com/g/600/400" />
160156
</div>
161157
}
162158
/>
@@ -178,15 +174,15 @@ This behaviour is provided with the `src` prop:
178174
<LazyImage
179175
src="https://www.fillmurray.com/g/600/400"
180176
placeholder={
181-
({cls}) =>
182-
<div className={`LazyImage-Placeholder ${cls}`}">
177+
() =>
178+
<div className={`LazyImage-Placeholder`}">
183179
<img src="https://www.fillmurray.com/g/60/40"/>
184180
</div>
185181
}
186182
actual={
187-
({cls}) =>
188-
<div className={`LazyImage-Actual ${cls}`}>
189-
<img src="https://www.fillmurray.com/g/600/400" className={cls} />
183+
() =>
184+
<div className={`LazyImage-Actual`}>
185+
<img src="https://www.fillmurray.com/g/600/400" />
190186
</div>
191187
}
192188
/>
@@ -202,17 +198,14 @@ You can choose what to display on Loading and Error using the render props `load
202198
<div className="bg-light-silver h5 w-100">
203199
<LazyImage
204200
src="https://www.fillmurray.com/notanimage"
205-
placeholder={({ cls }) => <div className={cls} />}
206-
actual={({ cls }) => (
207-
<img src="https://www.fillmurray.com/notanimage" className={cls} />
208-
)}
209-
loading={({ cls }) => (
210-
<div className={cls}>
201+
actual={() => <img src="https://www.fillmurray.com/notanimage" />}
202+
loading={() => (
203+
<div>
211204
<p className="pa3 f5 lh-copy near-white">Loading...</p>
212205
</div>
213206
)}
214-
error={({ cls }) => (
215-
<div className={`bg-light-red h-100 w-100 ${cls}`}>
207+
error={() => (
208+
<div className="bg-light-red h-100 w-100">
216209
<p>There was an error fetching this image :(</p>
217210
</div>
218211
)}
@@ -235,12 +228,8 @@ This behaviour is available by using a `loadEagerly` prop:
235228
<LazyImage
236229
loadEagerly
237230
src="https://www.fillmurray.com/g/600/400"
238-
placeholder={({ cls }) => (
239-
<img src="https://www.fillmurray.com/g/60/40" className={cls} />
240-
)}
241-
actual={({ cls }) => (
242-
<img src="https://www.fillmurray.com/g/600/400" className={cls} />
243-
)}
231+
placeholder={() => <img src="https://www.fillmurray.com/g/60/40" />}
232+
actual={() => <img src="https://www.fillmurray.com/g/600/400" />}
244233
/>
245234
```
246235

src/LazyImage.tsx

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import Observer, {IntersectionObserverProps} from 'react-intersection-observer';
44
/**
55
* Values that the render props take
66
*/
7-
interface RenderPropArgs {
8-
cls: string;
9-
}
7+
// interface RenderPropArgs {}
108

119
/**
1210
* Valid props for LazyImage
@@ -17,22 +15,22 @@ export interface LazyImageProps {
1715
srcSet?: string;
1816

1917
/** Component to display once image has loaded */
20-
actual: (RenderPropArgs) => React.ReactElement<{}>;
18+
actual: () => React.ReactElement<{}>;
2119

2220
/** Component to display while image has not been requested
2321
* @default: undefined
2422
*/
25-
placeholder?: (RenderPropArgs) => React.ReactElement<{}>;
23+
placeholder?: () => React.ReactElement<{}>;
2624

2725
/** Component to display while the image is loading
2826
* @default placeholder, if defined
2927
*/
30-
loading?: (RenderPropArgs) => React.ReactElement<{}>;
28+
loading?: () => React.ReactElement<{}>;
3129

3230
/** Component to display if the image fails to load
3331
* @default actual (broken image)
3432
*/
35-
error?: (RenderPropArgs) => React.ReactElement<{}>;
33+
error?: () => React.ReactElement<{}>;
3634

3735
/** Whether to skip checking for viewport and always show the 'actual' component
3836
* @see https://github.com/fpapado/react-lazy-images/#eager-loading--server-side-rendering-ssr
@@ -128,11 +126,7 @@ export class LazyImage extends React.Component<LazyImageProps, LazyImageState> {
128126
}
129127

130128
renderEager({actual}: LazyImageProps) {
131-
return (
132-
<React.Fragment>
133-
{actual({cls: 'LazyImage LazyImage-Actual'})}
134-
</React.Fragment>
135-
);
129+
return <React.Fragment>{actual()}</React.Fragment>;
136130
}
137131

138132
renderLazy(state, {observerProps, ...rest}: LazyImageProps) {
@@ -158,25 +152,18 @@ export class LazyImage extends React.Component<LazyImageProps, LazyImageState> {
158152
) {
159153
switch (imageState) {
160154
case 'NotAsked':
161-
return (
162-
!!placeholder && placeholder({cls: 'LazyImage LazyImage-Placeholder'})
163-
);
155+
return !!placeholder && placeholder();
164156

165157
case 'Loading':
166158
// Only render loading if specified, otherwise placeholder
167-
return !!loading
168-
? loading({cls: 'LazyImage LazyImage-Loading'})
169-
: !!placeholder &&
170-
placeholder({cls: 'LazyImage LazyImage-Placeholder'});
159+
return !!loading ? loading() : !!placeholder && placeholder();
171160

172161
case 'LoadSuccess':
173-
return actual({cls: 'LazyImage LazyImage-Placeholder'});
162+
return actual();
174163

175164
case 'LoadError':
176165
// Only render error if specified, otherwise actual
177-
return !!error
178-
? error({cls: 'LazyImage LazyImage-Error'})
179-
: actual({cls: 'LazyImage LazyImage-Placeholder'});
166+
return !!error ? error() : actual();
180167
}
181168
}
182169
}

0 commit comments

Comments
 (0)