-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage-compare-shadowdom.webc
More file actions
85 lines (75 loc) · 1.82 KB
/
image-compare-shadowdom.webc
File metadata and controls
85 lines (75 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<template webc:root :id="uid" aria-label="Compare two images">
<template shadowrootmode="open">
<style webc:keep>
input[type='range'] {
width: 100%;
}
label {
position: absolute;
overflow: hidden;
white-space: nowrap;
height: 1px;
width: 1px;
}
.slider {
display: block;
width: 100%;
height: auto;
object-fit: cover;
grid-area: 2/1;
z-index: 1;
height: 100%;
display: flex;
align-items: center;
}
</style>
<div class="slider">
<label :for="uid + '-slider'">How much of the image do you want to be hidden?</label>
<input type="range" :name="name" :value="value" :aria-controls="uid" :id="uid + '-slider'">
</div>
<slot webc:keep></slot>
</template>
<slot>Don’t forget the images!</slot>
</template>
<style webc:scoped>
:host {
display: grid;
max-width: 20em;
}
:host img {
display: block;
width: 100%;
height: auto;
object-fit: cover;
grid-area: 2/1;
}
:host img:first-of-type {
clip-path: inset(0 0 0 var(--position));
}
</style>
<style @html="`#${uid} { --position: ${value}%; }`"></style>
<script>
customElements.define("image-compare-shadowdom", class extends HTMLElement {
connectedCallback() {
if(this.hasAttribute("disabled")) {
return;
}
// Polyfill Declarative Shadow Root
let root = this.shadowRoot;
if(!this.shadowRoot) {
let tmpl = this.querySelector(":scope > template[shadowrootmode]");
root = this.attachShadow({ mode: "open" });
root.appendChild(tmpl.content.cloneNode(true));
}
this.range = root.querySelector("input[type='range']");
this.range.addEventListener("input", (e) => this.oninput(e.target.value));
// Synchronize the value if it changed before JS
this.oninput(this.range.value);
}
oninput(value) {
requestAnimationFrame(() => {
this.style.setProperty("--position", `${value}%`);
});
}
})
</script>