2022-02-26 15:48:56 +00:00
|
|
|
import { html, LitElement } from 'lit';
|
2021-05-27 21:00:43 +00:00
|
|
|
import { customElement, property, query } from 'lit/decorators.js';
|
2021-09-29 12:40:26 +00:00
|
|
|
import { styleMap } from 'lit/directives/style-map.js';
|
2022-03-02 14:28:34 +00:00
|
|
|
import '~/components/icon/icon';
|
|
|
|
import { autoIncrement } from '~/internal/auto-increment';
|
|
|
|
import { drag } from '~/internal/drag';
|
|
|
|
import { emit } from '~/internal/event';
|
|
|
|
import { clamp } from '~/internal/math';
|
|
|
|
import { watch } from '~/internal/watch';
|
2021-07-10 00:45:44 +00:00
|
|
|
import styles from './image-comparer.styles';
|
2021-07-12 14:36:06 +00:00
|
|
|
|
2020-08-21 21:27:11 +00:00
|
|
|
/**
|
|
|
|
* @since 2.0
|
2020-12-09 13:17:49 +00:00
|
|
|
* @status stable
|
2020-08-21 21:27:11 +00:00
|
|
|
*
|
2021-02-26 14:09:13 +00:00
|
|
|
* @dependency sl-icon
|
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @slot before - The before image, an `<img>` or `<svg>` element.
|
|
|
|
* @slot after - The after image, an `<img>` or `<svg>` element.
|
|
|
|
* @slot handle-icon - The icon used inside the handle.
|
2020-08-24 13:32:39 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @event sl-change - Emitted when the position changes.
|
2021-05-17 22:04:28 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @csspart base - The component's base wrapper.
|
|
|
|
* @csspart before - The container that holds the "before" image.
|
|
|
|
* @csspart after - The container that holds the "after" image.
|
|
|
|
* @csspart divider - The divider that separates the images.
|
|
|
|
* @csspart handle - The handle that the user drags to expose the after image.
|
2021-06-24 22:24:54 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @cssproperty --divider-width - The width of the dividing line.
|
|
|
|
* @cssproperty --handle-size - The size of the compare handle.
|
2020-08-21 21:27:11 +00:00
|
|
|
*/
|
2021-03-18 13:04:23 +00:00
|
|
|
@customElement('sl-image-comparer')
|
2021-03-09 00:14:32 +00:00
|
|
|
export default class SlImageComparer extends LitElement {
|
2021-07-10 00:45:44 +00:00
|
|
|
static styles = styles;
|
2020-08-21 21:27:11 +00:00
|
|
|
|
2021-03-06 17:01:39 +00:00
|
|
|
@query('.image-comparer') base: HTMLElement;
|
|
|
|
@query('.image-comparer__handle') handle: HTMLElement;
|
2020-08-21 21:27:11 +00:00
|
|
|
|
2022-01-16 05:47:14 +00:00
|
|
|
private readonly attrId = autoIncrement();
|
|
|
|
private readonly containerId = `comparer-container-${this.attrId}`;
|
|
|
|
|
2020-08-21 21:27:11 +00:00
|
|
|
/** The position of the divider as a percentage. */
|
2021-07-01 00:04:46 +00:00
|
|
|
@property({ type: Number, reflect: true }) position = 50;
|
2021-03-06 17:01:39 +00:00
|
|
|
|
2022-01-16 05:47:14 +00:00
|
|
|
handleDrag(event: Event) {
|
2020-08-21 21:27:11 +00:00
|
|
|
const { width } = this.base.getBoundingClientRect();
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
2022-01-16 05:47:14 +00:00
|
|
|
drag(this.base, x => {
|
|
|
|
this.position = parseFloat(clamp((x / width) * 100, 0, 100).toFixed(2));
|
2020-08-21 21:27:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown(event: KeyboardEvent) {
|
|
|
|
if (['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) {
|
|
|
|
const incr = event.shiftKey ? 10 : 1;
|
|
|
|
let newPosition = this.position;
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
2022-01-16 05:47:14 +00:00
|
|
|
if (event.key === 'ArrowLeft') {
|
|
|
|
newPosition -= incr;
|
|
|
|
}
|
|
|
|
if (event.key === 'ArrowRight') {
|
|
|
|
newPosition += incr;
|
|
|
|
}
|
|
|
|
if (event.key === 'Home') {
|
|
|
|
newPosition = 0;
|
|
|
|
}
|
|
|
|
if (event.key === 'End') {
|
|
|
|
newPosition = 100;
|
|
|
|
}
|
2020-08-21 21:27:11 +00:00
|
|
|
newPosition = clamp(newPosition, 0, 100);
|
|
|
|
|
|
|
|
this.position = newPosition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 13:26:35 +00:00
|
|
|
@watch('position', { waitUntilFirstUpdate: true })
|
2021-03-06 19:39:48 +00:00
|
|
|
handlePositionChange() {
|
2021-06-24 22:24:54 +00:00
|
|
|
emit(this, 'sl-change');
|
2021-03-06 19:39:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:27:11 +00:00
|
|
|
render() {
|
2021-02-26 14:09:13 +00:00
|
|
|
return html`
|
2022-01-16 05:47:14 +00:00
|
|
|
<div part="base" class="image-comparer" @keydown=${this.handleKeyDown} id=${this.containerId}>
|
2020-08-21 21:27:11 +00:00
|
|
|
<div class="image-comparer__image">
|
|
|
|
<div part="before" class="image-comparer__before">
|
2021-03-06 17:01:39 +00:00
|
|
|
<slot name="before"></slot>
|
2020-08-21 21:27:11 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div
|
|
|
|
part="after"
|
|
|
|
class="image-comparer__after"
|
2021-03-06 17:01:39 +00:00
|
|
|
style=${styleMap({ clipPath: `inset(0 ${100 - this.position}% 0 0)` })}
|
2020-08-21 21:27:11 +00:00
|
|
|
>
|
2021-03-06 17:01:39 +00:00
|
|
|
<slot name="after"></slot>
|
2020-08-21 21:27:11 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div
|
|
|
|
part="divider"
|
|
|
|
class="image-comparer__divider"
|
2022-01-16 05:47:14 +00:00
|
|
|
style=${styleMap({ left: `${this.position}%` })}
|
2021-03-06 17:01:39 +00:00
|
|
|
@mousedown=${this.handleDrag}
|
|
|
|
@touchstart=${this.handleDrag}
|
2020-08-21 21:27:11 +00:00
|
|
|
>
|
|
|
|
<div
|
|
|
|
part="handle"
|
|
|
|
class="image-comparer__handle"
|
2020-08-24 13:22:52 +00:00
|
|
|
role="scrollbar"
|
2021-03-06 17:01:39 +00:00
|
|
|
aria-valuenow=${this.position}
|
2020-08-21 21:27:11 +00:00
|
|
|
aria-valuemin="0"
|
|
|
|
aria-valuemax="100"
|
2022-01-16 05:47:14 +00:00
|
|
|
aria-controls=${this.containerId}
|
2021-02-26 14:09:13 +00:00
|
|
|
tabindex="0"
|
2020-08-21 21:27:11 +00:00
|
|
|
>
|
2021-01-25 13:56:59 +00:00
|
|
|
<slot name="handle-icon">
|
2021-04-20 13:37:19 +00:00
|
|
|
<sl-icon class="image-comparer__handle-icon" name="grip-vertical" library="system"></sl-icon>
|
2021-01-25 13:56:59 +00:00
|
|
|
</slot>
|
2020-08-21 21:27:11 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-26 14:09:13 +00:00
|
|
|
`;
|
2020-08-21 21:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 14:07:38 +00:00
|
|
|
|
2021-03-12 14:09:08 +00:00
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'sl-image-comparer': SlImageComparer;
|
|
|
|
}
|
|
|
|
}
|