Skip to content

Svelte

Use staticview components in Svelte templates.

TypeScript Configuration

Add the component types to your TypeScript configuration:

tsconfig.json
{
"compilerOptions": {
"types": [
// Required: types the component tags in Svelte templates
// For one component: "@staticview/ui/types/svelte/[component-name]"
"@staticview/ui/types/svelte",
// Optional: types what `querySelector` and `createElement` return for each tag
// For one component: "@staticview/ui/types/dom/[component-name]"
"@staticview/ui/types/dom",
// Optional: declares each component type globally, with no import
// For one component: "@staticview/ui/types/global/[component-name]"
"@staticview/ui/types/global",
],
},
}

Importing Web Components

Since components register globally, import each one once, in your app's entry point:

main.ts
import "@staticview/ui/[component-name]";

Setting Properties and Attributes

Svelte sets a prop as a property when the element has one, and as an attribute otherwise:

<component-name opened={isOpen} label="Title"></component-name>

Boolean Attributes

A boolean attribute is on whenever it is present, and close-button="false" turns the close button on. Since Svelte removes only an attribute whose value is null or undefined and turns everything else into a string, close-button={false} renders close-button="false". When the component has a matching property, set it instead, since Svelte assigns it directly:

<component-name closeButton={hasCloseButton}></component-name>

Otherwise pass "" to turn it on and undefined to turn it off:

<component-name close-button={hasCloseButton ? "" : undefined}></component-name>

Handling Events

Listen to component events with on[eventName]:

<component-name onopenedchange={handleOpenedChange}></component-name>

Using Slots

Put the content between the tags, with a slot attribute for a named slot:

<component-name>
<span slot="title">Title</span>
<span>Content</span>
</component-name>