Skip to content

Commit 2712086

Browse files
committed
refactor: switched to customized built-in elements
1 parent e0092f9 commit 2712086

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

demo/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
}
4646
</style>
4747
<link rel="stylesheet" href="../dist/loading-attribute-polyfill.css" />
48+
49+
<!-- Include polyfill for custom elements and built-in extends support -->
50+
<script src="//unpkg.com/@ungap/custom-elements"></script>
4851
</head>
4952
<body>
5053
<header>
@@ -136,6 +139,7 @@ <h1>
136139
alt=""
137140
width="250"
138141
height="150"
142+
is="loading-image"
139143
/>
140144

141145
<!-- Load an image right away instead of lazy-loading -->
@@ -168,6 +172,7 @@ <h1>
168172
alt=""
169173
width="250"
170174
height="150"
175+
is="loading-image"
171176
/>
172177
</picture>
173178

@@ -184,6 +189,7 @@ <h1>
184189
loading="lazy"
185190
width="250"
186191
height="150"
192+
is="loading-image"
187193
/>
188194

189195
<!-- Lazy-load an offscreen iframe when the user scrolls near it -->
@@ -193,6 +199,7 @@ <h1>
193199
height="180"
194200
loading="lazy"
195201
title="sample document"
202+
is="loading-iframe"
196203
></iframe>
197204

198205
<h2>dynamic images &amp; iframes</h2>

demo/index.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ let addDynamicContent = (event) => {
66
imageElement = document.createElement('img'),
77
iframeElement = document.createElement('iframe');
88

9+
imageElement.setAttribute('is', 'loading-image');
910
imageElement.setAttribute(
1011
'src',
11-
'img/lazyimg-src-loadinglazy.250x150.guetzli.jpg?loading=lazy&image-width=250&image-height=150'
12+
'img/lazyimg-src-loadinglazy.250x150.guetzli.jpg?loading=lazy&image-width=250&image-height=150&dynamic'
1213
);
1314
imageElement.setAttribute('loading', 'lazy');
1415
imageElement.setAttribute('alt', '..');
1516
imageElement.setAttribute('width', '250');
1617
imageElement.setAttribute('height', '150');
1718

18-
iframeElement.setAttribute('src', 'iframe.html?loading=lazy');
19+
iframeElement.setAttribute('is', 'loading-iframe');
20+
iframeElement.setAttribute('src', 'iframe.html?loading=lazy&dynamic');
1921
iframeElement.setAttribute('loading', 'lazy');
2022
iframeElement.setAttribute('title', '..');
2123
iframeElement.setAttribute('width', '320');
@@ -26,13 +28,35 @@ let addDynamicContent = (event) => {
2628

2729
document.querySelector('main').insertAdjacentElement('beforeend', divElement);
2830

29-
// Call for preparing the sample image element included the latest
30-
loadingAttributePolyfill.prepareElement(divElement.querySelector('img'));
31-
loadingAttributePolyfill.prepareElement(divElement.querySelector('iframe'));
32-
3331
event.preventDefault();
3432
};
3533

3634
document
3735
.querySelector('button.add-dynamic-content')
3836
.addEventListener('click', addDynamicContent);
37+
38+
// See https://html.spec.whatwg.org/multipage/indices.html#element-interfaces
39+
// for the list of other DOM interfaces.
40+
class LoadingImages extends HTMLImageElement {
41+
constructor() {
42+
super(); // Always call super() first in the constructor.
43+
// Call for preparing the sample image element included the latest
44+
console.log('preparing image', this);
45+
loadingAttributePolyfill.prepareElement(this);
46+
}
47+
}
48+
49+
customElements.define('loading-image', LoadingImages, { extends: 'img' });
50+
51+
// See https://html.spec.whatwg.org/multipage/indices.html#element-interfaces
52+
// for the list of other DOM interfaces.
53+
class LoadingIframes extends HTMLIFrameElement {
54+
constructor() {
55+
super(); // Always call super() first in the constructor.
56+
// Call for preparing the sample iframe element included the latest
57+
console.log('preparing iframe', this);
58+
loadingAttributePolyfill.prepareElement(this);
59+
}
60+
}
61+
62+
customElements.define('loading-iframe', LoadingIframes, { extends: 'iframe' });

0 commit comments

Comments
 (0)