tp-splitter/tp-hsplitter.js
2025-01-28 11:07:57 +01:00

130 lines
3.6 KiB
JavaScript

/**
@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 var(--tp-splitter-width, 5px) 1fr;
}
:host([tophidden]),
:host([bottomhidden]) {
grid-template-rows: 1fr !important;
}
[hidden] {
display: none !important;
}
#top,
#bottom {
position: relative;
overflow: hidden;
}
#splitter {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
background: var(--tp-splitter-line-color, #3b3b3b);
cursor: row-resize;
opacity: 1;
z-index: 2;
}
#splitter:hover {
background: var(--tp-splitter-line-color-hover, #007dd1);
opacity: 0.5;
}
`
];
}
render() {
const { topHidden, bottomHidden } = this;
return html`
<div id="top" part="top" ?hidden=${topHidden}><slot name="top"></slot></div>
<div id="splitter" part="splitter" @mousedown=${this._enableDrag} ?hidden=${topHidden || bottomHidden}><slot name="splitter"></slot></div>
<div id="bottom" part="bottom" ?hidden=${bottomHidden}><slot name="bottom"></slot></div>
`;
}
static get properties() {
return {
topHidden: { type: Boolean, reflect: true },
bottomHidden: { type: Boolean, reflect: true },
dragging: { type: Boolean, reflect: true },
minSideHeight: { type: Number },
};
}
constructor() {
super();
this._disableDrag = this._disableDrag.bind(this);
this._resize = this._resize.bind(this);
}
get top() {
return this.shadowRoot.querySelector('#top');
}
get bottom() {
return this.shadowRoot.querySelector('#bottom');
}
get splitter() {
return this.shadowRoot.querySelector('#splitter')
}
disconnectedCallback() {
super.disconnectedCallback();
document.removeEventListener('mouseup', this._disableDrag)
document.removeEventListener('mousemove', this._resize);
document.body.style['userSelect'] = '';
}
_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;
const totalHeight = this.offsetHeight;
const minHeight = this.minSideHeight || 0;
// Get splitter width from CSS variable, fallback to 5px.
const splitterWidth = parseInt(getComputedStyle(this).getPropertyValue('--tp-splitter-width')) || 5;
// Calculate new top height while ensuring it stays within bounds.
let newTopHeight = Math.max(minHeight, Math.min(this._topHeight + delta, totalHeight - splitterWidth - minHeight));
this.style.gridTemplateRows = `${newTopHeight}px ${splitterWidth}px 1fr`;
this.dispatchEvent(new CustomEvent('resized', { detail: null, bubbles: true, composed: true }));
}
}
window.customElements.define('tp-hsplitter', TpHSplitter);