Skip to content

Commit fb8160d

Browse files
Merge pull request #311 from rmunson/bug/lazyload-wrapper-class-conflicts
2 parents fa6cc88 + e7b4590 commit fb8160d

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ If you provide a number, that will be how many `ms` to wait; if you provide `tru
181181

182182
[demo](https://twobin.github.io/react-lazyload/examples/#/debounce)
183183

184+
### classNamePrefix
185+
186+
Type: String Default: `lazyload`
187+
188+
While rendering, Lazyload will add some elements to the component tree in addition to the wrapped component children.
189+
190+
The `classNamePrefix` prop allows the user to supply their own custom class prefix to help:
191+
# Avoid class conflicts on an implementing app
192+
# Allow easier custom styling
193+
194+
These being:
195+
# A wrapper div, which is present at all times (default )
196+
184197
### wheel
185198

186199
**DEPRECATED NOTICE**

lib/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,19 @@ var LazyLoad = function (_Component) {
350350
}, {
351351
key: 'render',
352352
value: function render() {
353+
var _props2 = this.props,
354+
height = _props2.height,
355+
children = _props2.children,
356+
placeholder = _props2.placeholder,
357+
classNamePrefix = _props2.classNamePrefix;
358+
359+
353360
return _react2.default.createElement(
354361
'span',
355-
{ className: 'lazyload-wrapper', ref: this.setRef },
356-
this.visible ? this.props.children : this.props.placeholder ? this.props.placeholder : _react2.default.createElement('div', {
357-
style: { height: this.props.height },
358-
className: 'lazyload-placeholder'
362+
{ className: classNamePrefix + '-wrapper', ref: this.setRef },
363+
this.visible ? children : placeholder ? placeholder : _react2.default.createElement('div', {
364+
style: { height: height },
365+
className: classNamePrefix + '-placeholder'
359366
})
360367
);
361368
}
@@ -365,6 +372,7 @@ var LazyLoad = function (_Component) {
365372
}(_react.Component);
366373

367374
LazyLoad.propTypes = {
375+
classNamePrefix: _propTypes2.default.string,
368376
once: _propTypes2.default.bool,
369377
height: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
370378
offset: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.arrayOf(_propTypes2.default.number)]),
@@ -380,6 +388,7 @@ LazyLoad.propTypes = {
380388
};
381389

382390
LazyLoad.defaultProps = {
391+
classNamePrefix: 'lazyload',
383392
once: false,
384393
offset: 0,
385394
overflow: false,

src/index.jsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,23 @@ class LazyLoad extends Component {
329329
}
330330

331331
render() {
332+
const {
333+
height,
334+
children,
335+
placeholder,
336+
classNamePrefix
337+
} = this.props;
338+
332339
return (
333-
<span className="lazyload-wrapper" ref={this.setRef}>
340+
<span className={`${classNamePrefix}-wrapper`} ref={this.setRef}>
334341
{this.visible ? (
335-
this.props.children
336-
) : this.props.placeholder ? (
337-
this.props.placeholder
342+
children
343+
) : placeholder ? (
344+
placeholder
338345
) : (
339346
<div
340-
style={{ height: this.props.height }}
341-
className="lazyload-placeholder"
347+
style={{ height: height }}
348+
className={`${classNamePrefix}-placeholder`}
342349
/>
343350
)}
344351
</span>
@@ -347,6 +354,7 @@ class LazyLoad extends Component {
347354
}
348355

349356
LazyLoad.propTypes = {
357+
classNamePrefix: PropTypes.string,
350358
once: PropTypes.bool,
351359
height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
352360
offset: PropTypes.oneOfType([
@@ -365,6 +373,7 @@ LazyLoad.propTypes = {
365373
};
366374

367375
LazyLoad.defaultProps = {
376+
classNamePrefix: 'lazyload',
368377
once: false,
369378
offset: 0,
370379
overflow: false,

test/specs/lazyload.spec.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('LazyLoad', () => {
4747
expect(document.querySelector('.lazyload-placeholder')).to.exist;
4848
expect(document.querySelector('.treasure')).to.not.exist;
4949
});
50-
50+
5151
it('should NOT update when invisble', (done) => {
5252
ReactDOM.render(
5353
<div>
@@ -131,7 +131,21 @@ describe('LazyLoad', () => {
131131

132132
expect(document.querySelector('.my-placeholder')).to.exist;
133133
});
134-
});
134+
it('should render `placeholder` and `wrapper` elements with custom `classNamePrefix` when provided', () => {
135+
ReactDOM.render(
136+
<div>
137+
<LazyLoad height={9999} classNamePrefix="custom-lazyload">
138+
<span className="something">123</span>
139+
</LazyLoad>
140+
<LazyLoad height={9999} classNamePrefix="custom-lazyload">
141+
<span className="invisible">123</span>
142+
</LazyLoad>
143+
</div>, div);
144+
console.log(div.innerHTML);
145+
expect(document.querySelector('.custom-lazyload-wrapper')).to.exist;
146+
expect(document.querySelector('.custom-lazyload-placeholder')).to.exist;
147+
});
148+
});
135149

136150
describe('Checking visibility', () => {
137151
it('should consider visible when top edge is visible', () => {

0 commit comments

Comments
 (0)