UNPKG

vite-plugin-symfony

Version:

A Vite plugin to integrate easily Vite in your Symfony application

61 lines (60 loc) 1.6 kB
// src/stimulus/helpers/svelte/render_controller.ts import { Controller } from "@hotwired/stimulus"; import { mount, unmount } from "svelte"; var render_controller_default = class extends Controller { app; props; intro; static values = { component: String, props: Object, intro: Boolean }; connect() { this.element.innerHTML = ""; this.props = this.propsValue ?? void 0; this.intro = this.introValue ?? void 0; this.dispatchEvent("connect"); const importedSvelteModule = window.resolveSvelteComponent(this.componentValue); const onload = (svelteModule) => { const Component = svelteModule.default; this._destroyIfExists(); this.app = mount(Component, { target: this.element, props: this.props, intro: this.intro }); this.element.root = this.app; this.dispatchEvent("mount", { component: Component }); }; if (typeof importedSvelteModule === "function") { importedSvelteModule().then(onload); } else { onload(importedSvelteModule); } } disconnect() { this._destroyIfExists(); this.dispatchEvent("unmount"); } _destroyIfExists() { if (this.element.root !== void 0) { unmount(this.element.root); delete this.element.root; } } dispatchEvent(name, payload = {}) { const detail = { componentName: this.componentValue, props: this.props, intro: this.intro, ...payload }; this.dispatch(name, { detail, prefix: "svelte" }); } }; export { render_controller_default as default };