Skip to content

Commit 9ed78cf

Browse files
committed
Inject styles
1 parent d4b3841 commit 9ed78cf

File tree

4 files changed

+101
-10
lines changed

4 files changed

+101
-10
lines changed

examples/basic/src/App.css

+51-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
.LazyLoad {
2-
opacity: 0;
3-
transition: all 2s ease-in-out;
4-
}
5-
6-
.LazyLoad.is-visible {
7-
opacity: 1;
8-
}
9-
101
.filler {
112
height: 150px;
123
}
@@ -16,3 +7,54 @@
167
overflow: scroll;
178
background-color: grey;
189
}
10+
11+
/* override default styles */
12+
.LazyLoad {
13+
background: #ccc;
14+
}
15+
16+
.LazyLoad img {
17+
transition: all 2s ease-in-out;
18+
}
19+
20+
.LazyLoad .spinner {
21+
width: 40px;
22+
height: 40px;
23+
position: absolute;
24+
top: 50%;
25+
left: 50%;
26+
margin: -20px -20px 0 0;
27+
background-color: #fff;
28+
border-radius: 100%;
29+
-webkit-animation: sk-scaleout 1.0s infinite ease-in-out;
30+
animation: sk-scaleout 1.0s infinite ease-in-out;
31+
}
32+
33+
@-webkit-keyframes sk-scaleout {
34+
0% { -webkit-transform: scale(0) }
35+
100% {
36+
-webkit-transform: scale(1.0);
37+
opacity: 0;
38+
}
39+
}
40+
41+
@keyframes sk-scaleout {
42+
0% {
43+
-webkit-transform: scale(0);
44+
transform: scale(0);
45+
} 100% {
46+
-webkit-transform: scale(1.0);
47+
transform: scale(1.0);
48+
opacity: 0;
49+
}
50+
}
51+
52+
.LazyLoad .offline {
53+
position: absolute;
54+
top: 0;
55+
left: 0;
56+
width: 100%;
57+
height: 100%;
58+
background: no-repeat center center url(data:image/svg+xml;base64,PHN2ZyBmaWxsPSIjMDAwMDAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDAgMTAwIiB4PSIwcHgiIHk9IjBweCI+PHBhdGggZD0iTTg4LjE3LDY5LjMyQTIyLDIyLDAsMCwwLDc3Ljc5LDM3Ljc3YTIyLDIyLDAsMCwwLTIxLjkxLTIwLjIsMjAuMjksMjAuMjksMCwwLDAtMTMuNjcsNC44MWw0LjU3LDUuM2ExMy4zMSwxMy4zMSwwLDAsMSw5LjEtMy4xMSwxNSwxNSwwLDAsMSwxNSwxNWMwLC4wNiwwLC4xMywwLC4xOXMwLC4yNiwwLC4zOWwtLjEsMi43NSwyLjY1Ljc1YTE1LDE1LDAsMCwxLDguNzksMjIuMVoiLz48cGF0aCBkPSJNMjguNzYsODBINjAuNDRWNzNIMjguNzZhMTUsMTUsMCwwLDEtMTUtMTVBMTcsMTcsMCwwLDEsMjMuNiw0Mi43bC0yLjc5LTYuNDJBMjMuOTEsMjMuOTEsMCwwLDAsNi43OCw1OCwyMiwyMiwwLDAsMCwyOC43Niw4MFoiLz48cmVjdCB4PSI0NS41MyIgeT0iLTQuNTgiIHdpZHRoPSI3IiBoZWlnaHQ9IjEwNi43NSIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTIwLjE0IDQ4Ljk2KSByb3RhdGUoLTQ1KSIvPjwvc3ZnPgo=);
59+
background-size: 100px 100px;
60+
}

src/LazyLoad.jsx

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import debounce from 'lodash/debounce';
66
import throttle from 'lodash/throttle';
77
import parentScroll from './utils/parentScroll';
88
import inViewport from './utils/inViewport';
9+
import injectCss from './utils/injectCSS';
10+
11+
injectCss(window);
912

1013
export default class LazyLoad extends Component {
1114
constructor(props) {
@@ -125,10 +128,22 @@ export default class LazyLoad extends Component {
125128
onError: () => { this.setState({ errored: true }); },
126129
};
127130

131+
let content;
132+
133+
if (visible) {
134+
if (loaded) {
135+
content = React.cloneElement(children, props);
136+
} else if (errored) {
137+
content = offline;
138+
} else {
139+
content = [spinner, React.cloneElement(children, props)]
140+
}
141+
}
142+
128143
return React.createElement(this.props.elementType, {
129144
className: elClasses,
130145
style: elStyles,
131-
}, visible && [spinner, offline, React.cloneElement(children, props)] );
146+
}, content);
132147
}
133148
}
134149

src/utils/css.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default `
2+
.LazyLoad img { opacity: 0; }
3+
.LazyLoad.is-loaded img { opacity: 1; }
4+
.LazyLoad {
5+
position: relative;
6+
height: 0;
7+
padding-bottom: 75%;
8+
overflow: hidden;
9+
}
10+
.LazyLoad iframe,
11+
.LazyLoad object,
12+
.LazyLoad embed,
13+
.LazyLoad video,
14+
.LazyLoad img {
15+
position: absolute;
16+
top: 0;
17+
left: 0;
18+
width: 100%;
19+
height: 100%;
20+
}`;

src/utils/injectCSS.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cssRules from './css';
2+
3+
const injectCSS = (window) => {
4+
if (window && typeof window === 'object' && window.document) {
5+
const { document } = window;
6+
const head = (document.head || document.getElementsByTagName('head')[0]);
7+
8+
const styleElement = document.createElement('style');
9+
styleElement.innerHTML = cssRules;
10+
head.appendChild(styleElement);
11+
}
12+
};
13+
14+
export default injectCSS;

0 commit comments

Comments
 (0)