tp-splitter/tp-vsplitter.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-02-06 14:16:10 +01:00
/**
@license
Copyright (c) 2023 trading_peter
*/
import { LitElement, html, css } from 'lit';
class TpVSplitter extends LitElement {
static get styles() {
return [
css`
:host {
display: grid;
grid-template-columns: 1fr 5px 1fr;
}
#left,
#right {
position: relative;
overflow: hidden;
}
.splitter {
2024-03-15 14:01:41 +01:00
background: var(--tp-splitter-line-color, #3b3b3b);
2023-02-06 14:16:10 +01:00
cursor: col-resize;
opacity: 1;
}
.splitter:hover {
2024-03-15 14:01:41 +01:00
background: var(--tp-splitter-line-color-hover, #007dd1);
2023-02-06 14:16:10 +01:00
opacity: 0.5;
}
`
];
}
render() {
return html`
<div id="left"><slot name="left"></slot></div>
<div class="splitter" @mousedown=${this._enableDrag}></div>
<div id="right"><slot name="right"></slot></div>
`;
}
static get properties() {
return {
columns: { type: String },
_dragging: { type: Boolean },
};
}
constructor() {
super();
this._disableDrag = this._disableDrag.bind(this);
this._resize = this._resize.bind(this);
}
get left() {
return this.shadowRoot.querySelector('#left');
}
get right() {
return this.shadowRoot.querySelector('#right');
}
shouldUpdate(changes) {
if (changes.has('columns')) {
this.style.gridTemplateColumns = this.columns;
}
return true;
}
_enableDrag(e) {
document.addEventListener('mouseup', this._disableDrag)
document.addEventListener('mousemove', this._resize);
document.body.style['userSelect'] = 'none';
this._startX = e.clientX;
this._topWidth = this.left.offsetWidth;
this._dragging = true;
}
_disableDrag() {
document.removeEventListener('mouseup', this._disableDrag)
document.removeEventListener('mousemove', this._resize);
document.body.style['userSelect'] = '';
this._dragging = false;
this.dispatchEvent(new CustomEvent('resize-done', { detail: this.style.gridTemplateColumns, bubbles: true, composed: true }));
}
_resize(e) {
const delta = e.clientX - this._startX;
this.style.gridTemplateColumns = `${this._topWidth + delta}px 5px 1fr`;
this.dispatchEvent(new CustomEvent('resized', { detail: null, bubbles: true, composed: true }));
}
}
window.customElements.define('tp-vsplitter', TpVSplitter);