import { LitElement, PropertyValueMap, css, html, nothing } from "lit"; import { customElement, state } from 'lit/decorators.js'; import { Ref, createRef, ref } from "lit/directives/ref.js"; @customElement("thermal-bar") export class ThermalBar extends LitElement { static styles = css` .container { // width: 100%; display: flex; gap: 5px; position: relative; } .ruler { width: 100%; position: absolute; height: 0; top: 0; left: 0; } .ruler-item {} .ruler-item__current { border: 1px solid transparent; height: 0; margin-top: -1px; content: ""; } .ruler-item__content { border: 1px solid red; position: absolute; display: none; } .content { display: flex; gap: calc( 5px ); width: max-content; align-items: center; } .icon { width: var( --thermal-gap ); line-height: 0; } .collapsed-menu { --thermal-direction: column; --thermal-collapsible-display: block !important; --thermal-collapsible-width: 100%; --thermal-collapsible-grow: 1; } `; @state() protected collapsed: boolean = false; @state() protected lastContentWidth!: number; @state() protected drawerRef: Ref = createRef(); protected contentRef: Ref = createRef(); protected rulerContentRef: Ref = createRef(); protected observer!: ResizeObserver; connectedCallback(): void { super.connectedCallback(); } protected firstUpdated(_changedProperties: PropertyValueMap | Map): void { super.firstUpdated( _changedProperties ); this.observer = new ResizeObserver( ( entries ) => { // if the bar is not collapsed, store the content width if ( this.collapsed === false ) { const contentWidth = this.contentRef.value!.clientWidth; this.lastContentWidth = contentWidth; } const entry = entries[0]; // If content is larger, expand it if ( this.lastContentWidth < entry.contentRect.width ) { if ( this.collapsed ) { this.collapsed = false; } } // If content is smaller, collapse it else { if ( this.collapsed === false ) { this.collapsed = true; } } } ); this.observer.observe( this.drawerRef.value! ); } disconnectedCallback(): void { super.disconnectedCallback(); if ( this.drawerRef.value ) { this.observer.unobserve( this.drawerRef.value ); } this.observer.disconnect(); } protected render(): unknown { return html` ${this.collapsed === false ? html` ` : nothing} ${this.collapsed ? html` ` : nothing } `; } }