UNPKG

@histoire/plugin-svelte

Version:

Histoire plugin for Svelte 4/5 and SvelteKit 2/3 support

1 lines • 2.1 kB
import{callSetupFunctions as e,createWrappedComponent as t,getLegacyStateApi as n,mountSvelteComponent as r}from"../util/svelte.js";import i from"./RenderStory.svelte";import a from"./RenderVariant.svelte";import{syncState as o}from"./util.js";import s from"./Wrap.svelte";import{components as c}from"@histoire/controls";import{defineComponent as l,h as u,onMounted as d,onUnmounted as f,ref as p,watch as m}from"@histoire/vendors/vue";import*as h from"virtual:$histoire-generated-global-setup";import*as g from"virtual:$histoire-setup";import{useHistoireGlobals as _}from"@histoire/shared";var v=l({name:`RenderStory`,props:{variant:{type:Object,required:!0},story:{type:Object,required:!0},slotName:{type:String,default:`default`}},setup(t,{emit:s}){let c=p(),l,u,v=[];function b(e,t){document.addEventListener(e,t);let n=()=>document.removeEventListener(e,t);return v.push(n),{off:n}}async function x(){u=document.createElement(`div`),c.value.appendChild(u);let d=[],{off:f}=b(`SvelteRegisterComponent`,e=>{let{component:t}=e.detail;d.push(t)}),p=await r(t.story.file.component,{target:u,props:{Hst:{Story:i,Variant:a,...y()}},context:new Map(Object.entries({__hstStory:t.story,__hstVariant:t.variant,__hstSlot:t.slotName}))},`client`);l=p.app,v.push(()=>{p.destroy()});let m=d.find(e=>e.$$&&l?.$$&&e.$$===l.$$)??l;f(),d=[];function x(){let e=m?.$replace;e&&(m.$replace=(...t)=>{let n=e.apply(m,t);return m=n??m,n})}x();let S=n(m);if(S){let{apply:e,stop:n}=o(t.variant.state,e=>{S.injectState(e)});v.push(n);let r,i=()=>{e(S.captureState()),r=requestAnimationFrame(i)};r=requestAnimationFrame(i),v.push(()=>{cancelAnimationFrame(r)}),e(S.captureState())}let C={app:l,globals:_(),story:t.story,variant:t.variant};await e(h,g,C,t.variant.setupApp,u),s(`ready`)}function S(){v.forEach(e=>e()),v=[],u&&=(u.parentNode?.removeChild(u),null),l=null}return m(()=>t.story.id,async()=>{S(),await x()}),d(async()=>{await x()}),f(()=>{S()}),{el:c}},render(){return u(`div`,{ref:`el`})}});function y(){let e={};for(let t in c)e[t.substring(3)]=b(c[t]);return e}function b(e){return t(s,e)}export{v as default};