|
| 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