shoelace/src/components/image-comparer/image-comparer.ts

131 wiersze
3.8 KiB
TypeScript
Czysty Zwykły widok Historia

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-24 11:48:03 +00:00
import '~/components/icon/icon';
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.
*
2022-03-09 20:54:18 +00:00
* @csspart base - The component's internal wrapper.
2021-06-25 20:25:46 +00:00
* @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-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
/** 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
handleDrag(event: Event) {
2020-08-21 21:27:11 +00:00
const { width } = this.base.getBoundingClientRect();
event.preventDefault();
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();
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() {
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-03-08 22:34:17 +00:00
<div part="base" id="image-comparer" class="image-comparer" @keydown=${this.handleKeyDown}>
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"
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-03-08 22:34:17 +00:00
aria-controls="image-comparer"
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">
<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:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-image-comparer': SlImageComparer;
}
}