"use strict";(self.webpackChunkdocs=self.webpackChunkdocs||[]).push([["9932"],{4520:function(e,t,s){s.r(t),s.d(t,{importsDemo:()=>u,multi:()=>p,multiFiles:()=>m,processorsDemo:()=>h,default:()=>x,frontMatter:()=>a,customRoot:()=>g,metadata:()=>n,assets:()=>d,toc:()=>v,contentTitle:()=>c});var n=JSON.parse('{"id":"languages/svelte","title":"Svelte","description":"Svelte is a framework for building user interfaces on the web. It uses a compiler to turn declarative components written in HTML, CSS and JavaScript.","source":"@site/docs/languages/svelte.mdx","sourceDirName":"languages","slug":"/languages/svelte","permalink":"/docs/languages/svelte","draft":false,"unlisted":false,"editUrl":"https://github.com/live-codes/livecodes/tree/develop/docs/docs/languages/svelte.mdx","tags":[],"version":"current","frontMatter":{},"sidebar":"docsSidebar","previous":{"title":"Sucrase","permalink":"/docs/languages/sucrase"},"next":{"title":"Tailwind CSS","permalink":"/docs/languages/tailwindcss"}}'),o=s("5893"),r=s("65"),i=s("3365"),l=s("8500");let a={},c="Svelte",d={},h={svelte:`
h1 {msg}
`},u={svelte:`
`}},v=[{value:"Usage",id:"usage",level:2},{value:"Demo",id:"demo",level:3},{value:"CSS Frameworks",id:"css-frameworks",level:3},{value:"Languages and Pre-Processors",id:"languages-and-pre-processors",level:3},{value:"Module Imports",id:"module-imports",level:3},{value:"Multiple Components",id:"multiple-components",level:3},{value:"Importing External Components",id:"importing-external-components",level:3},{value:"Root Element",id:"root-element",level:3},{value:"Language Info",id:"language-info",level:2},{value:"Name",id:"name",level:3},{value:"Extensions",id:"extensions",level:3},{value:"Editor",id:"editor",level:3},{value:"Compiler",id:"compiler",level:2},{value:"Version",id:"version",level:3},{value:"Code Formatting",id:"code-formatting",level:2},{value:"Starter Template",id:"starter-template",level:2},{value:"Links",id:"links",level:2}];function f(e){let t={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",li:"li",p:"p",pre:"pre",ul:"ul",...(0,r.a)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(t.header,{children:(0,o.jsx)(t.h1,{id:"svelte",children:"Svelte"})}),"\n",(0,o.jsxs)(t.p,{children:[(0,o.jsx)(t.a,{href:"https://svelte.dev/docs/svelte/overview",children:"Svelte"})," is a framework for building user interfaces on the web. It uses a compiler to turn declarative components written in HTML, CSS and JavaScript."]}),"\n",(0,o.jsx)(t.h2,{id:"usage",children:"Usage"}),"\n",(0,o.jsxs)(t.p,{children:["Svelte components can be used as documented in the ",(0,o.jsx)(t.a,{href:"https://svelte.dev/docs/svelte/svelte-files",children:"docs"}),". See below for usage."]}),"\n",(0,o.jsx)(t.h3,{id:"demo",children:"Demo"}),"\n","\n",(0,o.jsx)(i.Z,{template:"svelte",height:"400"}),"\n",(0,o.jsx)(t.h3,{id:"css-frameworks",children:"CSS Frameworks"}),"\n",(0,o.jsxs)(t.p,{children:[(0,o.jsx)(t.a,{href:"/docs/features/css#css-processors",children:"CSS Frameworks"})," supported in LiveCodes (e.g. ",(0,o.jsx)(t.a,{href:"/docs/languages/tailwindcss",children:"Tailwind CSS"}),", ",(0,o.jsx)(t.a,{href:"/docs/languages/unocss",children:"UnoCSS"}),", ",(0,o.jsx)(t.a,{href:"/docs/languages/windicss",children:"WindiCSS"}),") can detect class names added in Svelte components.\nMake sure that the required utility is enabled (from style editor menu or in ",(0,o.jsx)(t.code,{children:"processors"})," property of ",(0,o.jsx)(t.a,{href:"/docs/configuration/configuration-object#processors",children:"configuration object"}),")."]}),"\n",(0,o.jsxs)(t.p,{children:["See ",(0,o.jsx)(t.a,{href:"#multiple-components",children:"example below"}),"."]}),"\n",(0,o.jsx)(t.h3,{id:"languages-and-pre-processors",children:"Languages and Pre-Processors"}),"\n",(0,o.jsxs)(t.p,{children:["Many of the ",(0,o.jsx)(t.a,{href:"/docs/languages/",children:"languages supported in LiveCodes"})," can be used. The value of ",(0,o.jsx)(t.code,{children:"lang"})," attribute can be the language name (specified in its documentation page) or any of its aliases (extensions)."]}),"\n",(0,o.jsxs)(t.p,{children:["You may wrap the markup in a ",(0,o.jsx)(t.code,{children:"template"})," element if you want to specify the ",(0,o.jsx)(t.code,{children:"lang"})," attribute."]}),"\n","\n",(0,o.jsx)(l.Z,{params:h,code:h.svelte,language:"html",formatCode:!1}),"\n",(0,o.jsx)(t.h3,{id:"module-imports",children:"Module Imports"}),"\n",(0,o.jsxs)(t.p,{children:["npm modules can be imported as described in the section about ",(0,o.jsx)(t.a,{href:"/docs/features/module-resolution",children:"module resolution"}),", including bare module imports and importing from different CDNs. Stylesheets imported in the ",(0,o.jsx)(t.code,{children:"script"})," block are added as ",(0,o.jsx)(t.code,{children:''})," tags in the page ",(0,o.jsx)(t.code,{children:"head"}),"."]}),"\n",(0,o.jsx)(t.p,{children:"Example:"}),"\n","\n",(0,o.jsx)(l.Z,{params:u,code:u.svelte,language:"html",formatCode:!1}),"\n",(0,o.jsxs)(t.p,{children:["Module imports can be customized using import maps as described in ",(0,o.jsx)(t.a,{href:"/docs/features/module-resolution#custom-module-resolution",children:"module resolution"})," documentations."]}),"\n",(0,o.jsx)(t.h3,{id:"multiple-components",children:"Multiple Components"}),"\n",(0,o.jsxs)(t.p,{children:["Svelte is supported in both ",(0,o.jsx)(t.a,{href:"/docs/features/projects#markup-editor",children:"markup"})," and ",(0,o.jsx)(t.a,{href:"/docs/features/projects#script-editor",children:"script"})," editors."]}),"\n",(0,o.jsx)(t.p,{children:"This allows having a component in the markup editor that imports (and passes props to) a component in the script editor. The opposite is not supported."}),"\n",(0,o.jsxs)(t.p,{children:["This can be done using relative import of a file name in the same directory. Any file name will resolve to the component in the script editor,\ne.g. ",(0,o.jsx)(t.code,{children:"./script.svelte"}),", ",(0,o.jsx)(t.code,{children:"./Component.svelte"}),", ",(0,o.jsx)(t.code,{children:"./Counter.svelte"}),", etc."]}),"\n","\n",(0,o.jsx)(i.Z,{config:p}),"\n",(0,o.jsxs)(t.p,{children:["Please note that LiveCodes ",(0,o.jsx)(t.a,{href:"/docs/features/projects",children:"does not have the concept of a file system"}),". However, you can configure ",(0,o.jsx)(t.a,{href:"/docs/configuration/configuration-object#markup",children:"editor options"})," like ",(0,o.jsx)(t.code,{children:"title"}),", ",(0,o.jsx)(t.code,{children:"order"})," and ",(0,o.jsx)(t.code,{children:"hideTitle"})," to simulate multiple files, change editor order or even hide editors."]}),"\n",(0,o.jsx)(t.p,{children:"Example:"}),"\n","\n",(0,o.jsx)(i.Z,{config:m}),"\n",(0,o.jsxs)(t.p,{children:["When both markup and script editors use Svelte, the component in the markup editor is used as the main component rendered in the ",(0,o.jsx)(t.a,{href:"#root-element",children:"root element"}),".\nTo render the component in the script editor, it has to be imported and used by the main component."]}),"\n",(0,o.jsx)(t.h3,{id:"importing-external-components",children:"Importing External Components"}),"\n",(0,o.jsxs)(t.p,{children:["External Svelte components can be imported. The import URL has to be an absolute URL ending with ",(0,o.jsx)(t.code,{children:".svelte"})," extension. Any bare or relative imports in the imported files are resolved and compiled recursively."]}),"\n",(0,o.jsx)(t.p,{children:"Example:"}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-html",children:"
`,f=`
`.trimStart();return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(l,{className:`container_Egsj ${e.className}`,style:{height:i||"50vh",...e.style},appUrl:a.G,...e,config:{theme:d,themeColor:"hsl(215, 8%, 60%)",..."object"==typeof e.config?e.config:{}}}),!1!==e.showCode&&(0,n.jsx)(g,{js:u,ts:p,react:m,vue:v,svelte:f})]})}},8500:function(e,t,s){s.d(t,{Z:()=>c});var n=s("5893");s("7294");var o=s("6735");function r(e){let{children:t,fallback:s}=e;return(0,o.Z)()?(0,n.jsx)(n.Fragment,{children:t?.()}):s??null}var i=s("1705"),l=s("8294"),a=s("1858");function c(e){let{params:t,config:s,code:o,language:c="js",codeTitle:d="",showLineNumbers:h=!1,formatCode:u=!0,linkText:p="Run in LiveCodes",style:m={},className:g=""}=e,v=(0,l.r)({appUrl:a.G,params:t,config:s});return(0,n.jsxs)("div",{style:{marginBottom:"30px",...m},className:g,children:[o&&(0,n.jsx)(r,{children:()=>(0,n.jsx)(i.Z,{language:c,title:d,showLineNumbers:h,children:u?function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"js";return window.prettier?.format(e,{parser:"html"===t?"html":"babel",plugins:window.prettierPlugins})}(o,c):o})}),(0,n.jsxs)("a",{href:v,target:"_blank",rel:"noreferrer",children:[p,(0,n.jsx)("svg",{width:"12",height:"12","aria-hidden":"true",viewBox:"0 0 24 24",className:"iconExternalLink_node_modules-@docusaurus-theme-classic-lib-theme-Icon-ExternalLink-styles-module",style:{marginLeft:"4px"},children:(0,n.jsx)("path",{fill:"currentColor",d:"M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"})})]})]})}},8294:function(e,t,s){s.d(t,{T:function(){return o},r:function(){return r}});var n=s(7728);async function o(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};"object"!=typeof e||e instanceof HTMLElement||!e.headless&&"headless"!==e.view||(t=e,e=null);let{config:s={},headless:n,loading:o="lazy",view:i}=t,l=n||"headless"===i,a=null,c=null;if("string"==typeof e)a=document.querySelector(e);else if(e instanceof HTMLElement)a=e;else if(!(l&&"object"==typeof e))throw Error("A valid container element is required.");if(!a){if(l)k(a=document.createElement("div")),document.body.appendChild(a);else throw Error(`Cannot find element: "${e}"`)}let d=new URL(r(t)),h=d.origin;d.searchParams.set("embed","true"),d.searchParams.set("loading",l?"eager":o);let u=globalThis.process;d.searchParams.set("sdkVersion",u?.env?.SDK_VERSION||"latest"),"object"==typeof s&&Object.keys(s).length>0&&d.searchParams.set("config","sdk");let p=t.params;"object"==typeof p&&Object.keys(p).length>0&&JSON.stringify(p).length<1800&&Object.keys(p).forEach(e=>{d.searchParams.set(e,encodeURIComponent(String(p[e])))});let m=!1,g="Cannot call API methods after calling `destroy()`.",v=await new Promise(e=>{if(!a)return;let t=a.dataset.height||a.style.height;if(t&&!l){let e=isNaN(Number(t))?t:t+"px";a.style.height=e}"false"===a.dataset.defaultStyles||l||(a.style.backgroundColor||="#fff",a.style.border||="1px solid black",a.style.borderRadius||="8px",a.style.boxSizing||="border-box",a.style.padding||="0",a.style.width||="100%",a.style.height||=a.style.height||"300px",a.style.minHeight="200px",a.style.flexGrow="1",a.style.overflow||="hidden",a.style.resize||="vertical");let n="livecodes",r=a.querySelector(`iframe.${n}`),i=r||document.createElement("iframe");i.classList.add(n),i.setAttribute("allow","accelerometer; camera; encrypted-media; display-capture; geolocation; gyroscope; microphone; midi; clipboard-read; clipboard-write; web-share"),i.setAttribute("allowtransparency","true"),i.setAttribute("allowpaymentrequest","true"),i.setAttribute("allowfullscreen","true"),i.setAttribute("sandbox","allow-same-origin allow-downloads allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-presentation allow-scripts"),i.setAttribute("loading","eager"===o?"eager":"lazy"),l?k(i):(i.style.height="100%",i.style.minHeight="200px",i.style.width="100%",i.style.margin="0",i.style.border="0",i.style.borderRadius=a.style.borderRadius),addEventListener("message",function e(t){t.source===i.contentWindow&&t.origin===h&&t.data?.type==="livecodes-init"&&(removeEventListener("message",e),c=Number(t.data.payload.appVersion.replace(/^v/,"")))}),(!c||c<46)&&addEventListener("message",function e(t){t.source===i.contentWindow&&t.origin===h&&t.data?.type==="livecodes-get-config"&&(removeEventListener("message",e),i.contentWindow?.postMessage({type:"livecodes-config",payload:s},h))}),i.onload=()=>{e(i)},i.src=d.href,r||a.appendChild(i)}),f=new Promise(e=>{addEventListener("message",function t(s){s.source===v.contentWindow&&s.origin===h&&s.data?.type==="livecodes-ready"&&(removeEventListener("message",t),e(),f.settled=!0)})}),x=()=>m?Promise.reject(g):new Promise(async e=>{f.settled&&e(),v.contentWindow?.postMessage({type:"livecodes-load"},h),await f,e()}),j=(e,t)=>new Promise(async(s,n)=>{if(m)return n(g);await x();let o=E();addEventListener("message",function t(r){if(r.source===v.contentWindow&&r.origin===h&&r.data?.type==="livecodes-api-response"&&r.data?.id===o&&r.data.method===e){removeEventListener("message",t);let e=r.data.payload;e?.error?n(e.error):s(e)}}),v.contentWindow?.postMessage({method:e,id:o,args:t},h)}),y={},b=["load","ready","code","console","tests","destroy"],w=(e,t)=>{if(m)throw Error(g);return b.includes(e)?(j("watch",[e]),y[e]||(y[e]=[]),y[e]?.push(t),{remove:()=>{y[e]=y[e]?.filter(e=>e!==t),y[e]?.length===0&&j("watch",[e,"unsubscribe"])}}):{remove:()=>void 0}},S=e=>({"livecodes-app-loaded":"load","livecodes-ready":"ready","livecodes-change":"code","livecodes-console":"console","livecodes-test-results":"tests","livecodes-destroy":"destroy"})[e];addEventListener("message",async e=>{let t=S(e.data?.type??"");if(e.source!==v.contentWindow||e.origin!==h||!t||!y[t])return;let s=e.data?.payload;y[t]?.forEach(e=>{e(s)})});let C=()=>{Object.values(y).forEach(e=>{e.length=0}),v?.remove?.(),m=!0};function k(e){e.style.position="absolute",e.style.top="0",e.style.visibility="hidden",e.style.opacity="0"}"lazy"===o&&"IntersectionObserver"in window&&new IntersectionObserver((e,t)=>{e.forEach(async e=>{e.isIntersecting&&(await x(),t.unobserve(a))})},{rootMargin:"150px"}).observe(a);let E=()=>(String(Math.random())+Date.now().toFixed()).replace("0.","");return{load:()=>x(),run:()=>j("run"),format:e=>j("format",[e]),getShareUrl:e=>j("getShareUrl",[e]),getConfig:e=>j("getConfig",[e]),setConfig:e=>j("setConfig",[e]),getCode:()=>j("getCode"),show:(e,t)=>j("show",[e,t]),runTests:()=>j("runTests"),onChange:e=>w("code",e),watch:w,exec:function(e){for(var t=arguments.length,s=Array(t>1?t-1:0),n=1;nf.settled?j("destroy").then(C):m?Promise.reject(g):(C(),Promise.resolve())}}function r(){let e,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},{appUrl:s="https://livecodes.io",params:o={},config:r={},headless:i,import:l,lite:a,view:c,...d}=t;try{e=new URL(s)}catch{throw Error(`${s} is not a valid URL.`)}let h=new URLSearchParams;Object.entries(d).forEach(t=>{let[s,n]=t;void 0!==n&&e.searchParams.set(s,String(n))});let u="headless"===t.view||i;if(a&&(console.warn('Deprecation notice: "lite" option is deprecated. Use "config: { mode: \'lite\' }" instead.'),"object"==typeof r&&null==r.mode?r.mode="lite":e.searchParams.set("lite","true")),c&&(console.warn('Deprecation notice: The "view" option has been moved to "config.view". For headless mode use "headless: true".'),"object"==typeof r&&null==r.view&&"headless"!==c?r.view=c:e.searchParams.set("view",c)),"string"==typeof r)try{new URL(r),e.searchParams.set("config",encodeURIComponent(r))}catch{throw Error('"config" is not a valid URL or configuration object.')}else r&&"object"==typeof r&&Object.keys(r).length>0&&(r.title&&"Untitled Project"!==r.title&&e.searchParams.set("title",r.title),r.description&&r.description.length>0&&e.searchParams.set("description",r.description),h.set("config","code/"+(0,n.compressToEncodedURIComponent)(JSON.stringify(r))));if(o&&"object"==typeof o&&Object.keys(o).length>0)try{h.set("params",(0,n.compressToEncodedURIComponent)(JSON.stringify(o)))}catch{Object.keys(o).forEach(t=>{e.searchParams.set(t,encodeURIComponent(String(o[t])))})}return l&&e.searchParams.set("x",encodeURIComponent(l)),u&&e.searchParams.set("headless","true"),e.hash=h.toString(),e.href}},65:function(e,t,s){s.d(t,{Z:function(){return l},a:function(){return i}});var n=s(7294);let o={},r=n.createContext(o);function i(e){let t=n.useContext(r);return n.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function l(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:i(e.components),n.createElement(r.Provider,{value:t},e.children)}}}]);