100 lines
2.3 KiB
JavaScript
100 lines
2.3 KiB
JavaScript
|
/**
|
||
|
@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 {
|
||
|
background: var(--lighter-black);
|
||
|
cursor: col-resize;
|
||
|
opacity: 1;
|
||
|
}
|
||
|
|
||
|
.splitter:hover {
|
||
|
background: var(--hl-color);
|
||
|
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);
|