First version
This commit is contained in:
112
tp-hsplitter.js
Normal file
112
tp-hsplitter.js
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
@license
|
||||
Copyright (c) 2023 trading_peter
|
||||
*/
|
||||
|
||||
import { LitElement, html, css } from 'lit';
|
||||
|
||||
class TpHSplitter extends LitElement {
|
||||
static get styles() {
|
||||
return [
|
||||
css`
|
||||
:host {
|
||||
display: grid;
|
||||
grid-template-rows: 1fr 5px 1fr;
|
||||
}
|
||||
|
||||
:host([tophidden]),
|
||||
:host([bottomhidden]) {
|
||||
grid-template-rows: 1fr;
|
||||
}
|
||||
|
||||
[hidden] {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
#top,
|
||||
#bottom {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.splitter {
|
||||
background: var(--lighter-black);
|
||||
cursor: row-resize;
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.splitter:hover {
|
||||
background: var(--hl-color);
|
||||
opacity: 0.5;
|
||||
}
|
||||
`
|
||||
];
|
||||
}
|
||||
|
||||
render() {
|
||||
const { topHidden, bottomHidden } = this;
|
||||
|
||||
return html`
|
||||
<div id="top" ?hidden=${topHidden}><slot name="top"></slot></div>
|
||||
<div class="splitter" @mousedown=${this._enableDrag} ?hidden=${topHidden || bottomHidden}></div>
|
||||
<div id="bottom" ?hidden=${bottomHidden}><slot name="bottom"></slot></div>
|
||||
`;
|
||||
}
|
||||
|
||||
static get properties() {
|
||||
return {
|
||||
topHidden: { type: Boolean, reflect: true },
|
||||
bottomHidden: { type: Boolean, reflect: true },
|
||||
rows: { type: String },
|
||||
_dragging: { type: Boolean },
|
||||
};
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this._disableDrag = this._disableDrag.bind(this);
|
||||
this._resize = this._resize.bind(this);
|
||||
}
|
||||
|
||||
shouldUpdate(changes) {
|
||||
if (changes.has('rows')) {
|
||||
this.style.gridTemplateRows = this.rows;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
get top() {
|
||||
return this.shadowRoot.querySelector('#top');
|
||||
}
|
||||
|
||||
get bottom() {
|
||||
return this.shadowRoot.querySelector('#bottom');
|
||||
}
|
||||
|
||||
_enableDrag(e) {
|
||||
document.addEventListener('mouseup', this._disableDrag)
|
||||
document.addEventListener('mousemove', this._resize);
|
||||
document.body.style['userSelect'] = 'none';
|
||||
this._startY = e.clientY;
|
||||
this._topHeight = this.top.offsetHeight;
|
||||
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.gridTemplateRows, bubbles: true, composed: true }));
|
||||
}
|
||||
|
||||
_resize(e) {
|
||||
const delta = e.clientY - this._startY;
|
||||
this.style.gridTemplateRows = `${this._topHeight + delta}px 5px 1fr`;
|
||||
this.dispatchEvent(new CustomEvent('resized', { detail: null, bubbles: true, composed: true }));
|
||||
}
|
||||
}
|
||||
|
||||
window.customElements.define('tp-hsplitter', TpHSplitter);
|
Reference in New Issue
Block a user