Skip to content

Commit f6870a4

Browse files
authored
Add wrapperProps for <span> (#70)
* Add wrapper props to image components * Update docs * Address PR feedback
1 parent 6a5aff9 commit f6870a4

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default MyImage;
7777
| useIntersectionObserver | `Boolean` | true | Whether to use browser's IntersectionObserver when available. |
7878
| visibleByDefault | `Boolean` | false | Whether the image must be visible from the beginning. |
7979
| wrapperClassName | `String` | | In some occasions (for example, when using a placeholderSrc) a wrapper span tag is rendered. This prop allows setting a class to that element. |
80+
| wrapperProps | `Object` | null | Props that should be passed to the wrapper span when it is rendered (for example, when using placeholderSrc or effect) |
8081
| ... | | | Any other image attribute |
8182

8283

@@ -146,7 +147,6 @@ export default Article;
146147
| useIntersectionObserver | `Boolean` | true | Whether to use browser's IntersectionObserver when available. |
147148
| visibleByDefault | `Boolean` | false | Whether the component must be visible from the beginning. |
148149

149-
150150
## Using `trackWindowScroll` HOC to improve performance
151151

152152
When you have many elements to lazy load in the same page, you might get poor performance because each one is listening to the scroll/resize events. In that case, it's better to wrap the deepest common parent of those components with a HOC to track those events (`trackWindowScroll`).
@@ -193,6 +193,7 @@ You must set the prop `scrollPosition` to the lazy load components. This way, th
193193
| placeholder | `ReactClass` | `<span>` | React element to use as a placeholder. |
194194
| threshold | `Number` | 100 | Threshold in pixels. So the image starts loading before it appears in the viewport. |
195195
| visibleByDefault | `Boolean` | false | Whether the image must be visible from the beginning. |
196+
| wrapperProps | `Object` | null | Props that should be passed to the wrapper span when it is rendered (for example, when using placeholderSrc or effect) |
196197
| ... | | | Any other image attribute |
197198

198199
Component wrapped with `trackWindowScroll` (in the example, `Gallery`)

src/components/LazyLoadImage.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class LazyLoadImage extends React.Component {
4040
useIntersectionObserver,
4141
visibleByDefault,
4242
wrapperClassName,
43+
wrapperProps,
4344
...imgProps
4445
} = this.props;
4546

@@ -89,6 +90,7 @@ class LazyLoadImage extends React.Component {
8990
placeholderSrc,
9091
width,
9192
wrapperClassName,
93+
wrapperProps,
9294
} = this.props;
9395
const { loaded } = this.state;
9496

@@ -114,18 +116,26 @@ class LazyLoadImage extends React.Component {
114116
height: height,
115117
width: width,
116118
}}
119+
{...wrapperProps}
117120
>
118121
{lazyLoadImage}
119122
</span>
120123
);
121124
}
122125

123126
render() {
124-
const { effect, placeholderSrc, visibleByDefault } = this.props;
127+
const {
128+
effect,
129+
placeholderSrc,
130+
visibleByDefault,
131+
wrapperClassName,
132+
wrapperProps,
133+
} = this.props;
125134

126135
const lazyLoadImage = this.getLazyLoadImage();
136+
const needsWrapper = (effect || placeholderSrc) && !visibleByDefault;
127137

128-
if ((!effect && !placeholderSrc) || visibleByDefault) {
138+
if (!needsWrapper && !wrapperClassName && !wrapperProps) {
129139
return lazyLoadImage;
130140
}
131141

@@ -144,6 +154,7 @@ LazyLoadImage.propTypes = {
144154
useIntersectionObserver: PropTypes.bool,
145155
visibleByDefault: PropTypes.bool,
146156
wrapperClassName: PropTypes.string,
157+
wrapperProps: PropTypes.object,
147158
};
148159

149160
LazyLoadImage.defaultProps = {

0 commit comments

Comments
 (0)