import type { PanelReport } from './panel-scope-key'; import { defineComponent, h } from 'vue'; import { Prop, toNative } from 'vue-facing-decorator'; import TsxComponent, { Component } from '../../app/vuetsx'; import { PANEL_SCOPE_KEY } from './panel-scope-key'; interface PanelScopeArgs { /** Fires when the first panel inside opens and when the last one closes. */ toggled?: (open: boolean) => void; /** Renders this element around the content, and renders {@link openCssClass} on it while a panel is open. */ tag?: string; cssClass?: string; openCssClass?: string; } /** * The content in an update of its own: the class flips on every open and close, and that must not render a product picker * or a calendar again. It renders when the host's slot changes and when something the slot reads does. */ const PanelScopeContent = defineComponent({ props: { content: Function }, render() { return this.content?.(); }, }); /** * A floating panel lives inside ancestors that can clip it or stack under their siblings. Such an ancestor is rendered * through this: with `tag`, the scope renders the element and its open class itself; without it, the component that * renders the ancestor listens on `toggled`, keeps one reactive field and renders the class. `Dropdown`, `SmartDropdown`, * `DaterangePicker` and `DatetimePicker` inject the report and never see the host. Fires on state change only. * * @internal */ @Component({ provide(this: { report: PanelReport }) { return { [PANEL_SCOPE_KEY]: this.report }; }, }) class PanelScopeComponent extends TsxComponent implements PanelScopeArgs { @Prop() toggled?: (open: boolean) => void; @Prop() tag?: string; @Prop() cssClass?: string; @Prop() openCssClass?: string; /** The last one that opened. Opening the next panel closes the previous, in either order: only this one can clear it. */ openPanel: number | null = null; report(panelId: number, open: boolean): void { const wasOpen = this.openPanel != null; if (open) { this.openPanel = panelId; } else if (this.openPanel == panelId) { this.openPanel = null; } if ((this.openPanel != null) != wasOpen) { this.toggled?.(!wasOpen); } } render() { const content = h(PanelScopeContent, { content: this.$slots.default }); if (this.tag == null) { return content; } return h( this.tag, { class: [ this.cssClass, this.openPanel != null ? this.openCssClass : null, ], }, [content], ); } } const PanelScope = toNative(PanelScopeComponent); export default PanelScope;