Skip to content

Commit e0285be

Browse files
keithamusLindsey Stanek
andcommitted
initial implementation
Co-authored-by: Lindsey Stanek <[email protected]>
1 parent 4053f58 commit e0285be

File tree

3 files changed

+108
-32
lines changed

3 files changed

+108
-32
lines changed

examples/index.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
5-
<title>custom-element demo</title>
5+
<title>image-compare demo</title>
66
</head>
77
<body>
8-
<custom-element></custom-element>
8+
<image-compare>
9+
<img slot="left" width="500" alt="placeholder of a random cute animal" src="https://source.unsplash.com/featured/?cute,animal&left" />
10+
<img slot="right" width="500" alt="placeholder of a random cute animal" src="https://source.unsplash.com/featured/?cute,animal&right" />
11+
</image-compare>
912

1013
<script type="module">
11-
// import 'https://unpkg.com/@github/custom-element-boilerplate@latest/dist/custom-element.js'
12-
import '../src/custom-element.ts'
14+
// import 'https://unpkg.com/@github/image-compare-boilerplate@latest/dist/image-compare.js'
15+
import '../src/image-compare-element.ts'
1316
</script>
1417
</body>
1518
</html>

src/custom-element.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/image-compare-element.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const stylesheet = new CSSStyleSheet()
2+
stylesheet.replaceSync(`
3+
:host {
4+
display: block;
5+
width: 500px;
6+
padding: 22px;
7+
}
8+
div {
9+
position: relative;
10+
}
11+
.left {
12+
position: absolute;
13+
overflow: hidden;
14+
z-index: 99;
15+
}
16+
input {
17+
position: absolute;
18+
left: 0;
19+
top: 0;
20+
bottom: 0;
21+
z-index: 100;
22+
height: auto;
23+
right: 0;
24+
appearance: none;
25+
-webkit-appearance: none;
26+
background: none;
27+
margin: -44px 0 0 -44px
28+
}
29+
input::-webkit-slider-thumb {
30+
appearance: none;
31+
background: hotpink;
32+
border-radius: 8px;
33+
border: 4px solid black;
34+
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.25);
35+
width: 44px;
36+
height: 44px;
37+
margin-left: 22px;
38+
}
39+
::slotted(img) {
40+
top: 0;
41+
aspect-ratio: 1;
42+
object-fit: cover;
43+
}
44+
`)
45+
46+
/**
47+
* An example Custom Element. This documentation ends up in the
48+
* README so describe how this elements works here.
49+
*
50+
* You can event add examples on the element is used with Markdown.
51+
*
52+
* ```
53+
* <image-compare></image-compare>
54+
* ```
55+
*/
56+
class ImageCompareElement extends HTMLElement {
57+
#renderRoot!: ShadowRoot
58+
59+
get #left() {
60+
return this.#renderRoot.querySelector('.left')
61+
}
62+
63+
get #input() {
64+
return this.#renderRoot.querySelector('input')
65+
}
66+
67+
connectedCallback(): void {
68+
this.#renderRoot = this.attachShadow({mode: 'open', delegatesFocus: true})
69+
this.#renderRoot.adoptedStyleSheets.push(stylesheet)
70+
this.#renderRoot.innerHTML = `
71+
<div>
72+
<input type="range" step="">
73+
<div class="left"><slot name="left"></slot></div>
74+
<div><slot name="right"></slot></div>
75+
</div>
76+
`
77+
this.#renderRoot.addEventListener('input', this)
78+
this.#update(this.#input.value)
79+
}
80+
81+
handleEvent(event: Event) {
82+
this.#update(event.target.value)
83+
}
84+
85+
#update(value: number) {
86+
this.#left.style.width = `${value}%`
87+
}
88+
}
89+
90+
declare global {
91+
interface Window {
92+
ImageCompareElement: typeof ImageCompareElement
93+
}
94+
}
95+
96+
export default ImageCompareElement
97+
98+
if (!window.customElements.get('image-compare')) {
99+
window.ImageCompareElement = ImageCompareElement
100+
window.customElements.define('image-compare', ImageCompareElement)
101+
}

0 commit comments

Comments
 (0)