UNPKG

@copilotkit/a2ui-renderer

Version:

A2UI Renderer for CopilotKit - render A2UI surfaces in React applications

71 lines (69 loc) 1.84 kB
import { LitElement, nothing } from "lit"; import { GenericBinder } from "@a2ui/web_core/v0_9"; //#region src/web-components/bound-component.ts var CpkA2uiBoundComponent = class extends LitElement { constructor(..._args) { super(..._args); this.binder = null; this.binderContext = null; this.propsSnapshot = {}; this.stateInitialized = false; } static { this.properties = { api: { attribute: false }, context: { attribute: false }, buildChild: { attribute: false }, renderFn: { attribute: false }, setupState: { attribute: false } }; } createRenderRoot() { return this; } connectedCallback() { super.connectedCallback(); this.style.display = "contents"; } disconnectedCallback() { this.disposeBinder(); super.disconnectedCallback(); } disposeBinder() { this.binder?.dispose(); this.binder = null; this.binderContext = null; } ensureBinder() { if (!this.api || !this.context) return; if (this.binder && this.binderContext === this.context) return; this.disposeBinder(); this.binderContext = this.context; this.binder = new GenericBinder(this.context, this.api.schema); this.propsSnapshot = this.binder.snapshot ?? {}; this.binder.subscribe((props) => { this.propsSnapshot = props ?? {}; this.requestUpdate(); }); } ensureState() { if (this.stateInitialized) return; this.stateInitialized = true; this.state = this.setupState?.(); } render() { this.ensureBinder(); this.ensureState(); if (!this.renderFn || !this.context || !this.buildChild) return nothing; return this.renderFn({ props: this.propsSnapshot, buildChild: this.buildChild, context: this.context, state: this.state, requestUpdate: () => this.requestUpdate() }); } }; //#endregion export { CpkA2uiBoundComponent }; //# sourceMappingURL=bound-component.mjs.map