import { Prop, toNative } from 'vue-facing-decorator'; import TsxComponent, { Component } from '../../app/vuetsx'; import { ModalUtils } from '../modal/modal-utils'; import './css/index.css'; interface LongContentContainerArgs { maxHeight?: string; cssClass?: string; showMoreText?: string; showLessText?: string; renderShowMore?: (h) => void; } @Component class LongContentContainerComponent extends TsxComponent implements LongContentContainerArgs { @Prop() maxHeight?: string; @Prop() cssClass?: string; @Prop() showMoreText?: string; @Prop() showLessText?: string; @Prop() renderShowMore?: (h) => void; expansionNeeded: boolean = null; isExpanded: boolean = false; /** * Measures the content once the browser has laid it out, and only when its size or its box's size changed. * Measuring in `updated()` read `scrollHeight` right after every patch: a forced layout on every render of the * page around this container, which re-renders it with every render of its own (it takes slot content). */ private sizeObserver: ResizeObserver | null = null; mounted() { if (this.observeSize()) { return; } ModalUtils.handleModalMountDelay(this, () => { this.reMeasure(); }); } updated() { if (this.sizeObserver != null) { return; } ModalUtils.handleModalMountDelay(this, () => { this.reMeasure(); }); } beforeUnmount() { this.sizeObserver?.disconnect(); this.sizeObserver = null; } /** * Watches the content and the box that clips it; false where there is nothing to watch with. A container in a * modal that is not shown yet has no size, so its first notification comes when the modal shows: no mount delay. */ private observeSize(): boolean { const $el = this.$refs.innerContent as HTMLElement; if (typeof ResizeObserver === 'undefined' || $el == null) { return false; } this.sizeObserver = new ResizeObserver(() => this.reMeasure()); this.sizeObserver.observe($el); if ($el.parentElement != null) { this.sizeObserver.observe($el.parentElement); } return true; } getRootStyle() { if (this.maxHeight?.length > 0) { return `max-height:${this.maxHeight}`; } return null; } getToggleButtonText(): string { if (this.isExpanded) { return this.showLessText; } else { return this.showMoreText; } } toggleIsExpanded() { this.isExpanded = !this.isExpanded; } onToggleKeydown(e: KeyboardEvent) { if (e.key !== 'Enter' && e.key !== ' ') { return; } e.preventDefault(); this.toggleIsExpanded(); } reMeasure() { if (this.isExpanded) { return; } const $el = this.$refs.innerContent as HTMLElement; const $root = $el?.parentElement; // A notification still in flight when the container unmounted. if ($root == null) { return; } const scrollHeight = $el.scrollHeight; if (scrollHeight > $root.clientHeight) { if (this.expansionNeeded != true) { this.expansionNeeded = true; } $root.classList.add('lcc-expansion-needed'); } else { if (this.expansionNeeded != false) { this.expansionNeeded = false; } $root.classList.remove('lcc-expansion-needed'); } } render(h) { // The default "show more / less" toggle is a div: role, tabindex and Enter / Space make it a // keyboard-operable button without changing how it looks. A custom `renderShowMore` brings its own // control (e.g. a Button), whose click bubbles to this div. const defaultToggle = this.renderShowMore == null; return (
0 ? ` ${this.cssClass}` : ''}`}>
{this.$slots.default?.()}
{ this.toggleIsExpanded(); }} onKeydown={defaultToggle ? (e: KeyboardEvent) => this.onToggleKeydown(e) : undefined} > {this.renderShowMore != null ? this.renderShowMore(h) : (
{this.getToggleButtonText()}
)}
); } } const LongContentContainer = toNative(LongContentContainerComponent); export default LongContentContainer;