Skip to content

The Open Standard for Generative UI

3.3 KB
Raw Blame
---
title: "Svelte 5"
---

<Steps>

<Step title="Write the component with $props()" source="https://github.com/thesysdev/openui/blob/main/examples/app-frameworks/svelte/src/lib/components/Card.svelte">

Destructure props and renderNode from $props(). renderNode is a Snippet passed as a prop, not through context, so render children with \{@render renderNode(...)\} and forward it to any child component that renders nested nodes.

```svelte title="src/lib/components/Card.svelte"
<script lang="ts">
  import type { Snippet } from "svelte";

  let {
    props,
    renderNode,
  }: {
    props: { title?: string; children?: unknown };
    renderNode: Snippet<[unknown]>;
  } = $props();
</script>

<div>
  {#if props.title}
    <h3>{props.title}</h3>
  {/if}
  {#if props.children}
    {@render renderNode(props.children)}
  {/if}
</div>
```

</Step>

<Step title="Register components in a library" source="https://github.com/thesysdev/openui/blob/main/examples/app-frameworks/svelte/src/lib/library.ts">

Pair each .svelte file with defineComponent and a Zod schema, and list a container's allowed children with their .ref schemas. Build the system prompt with library.prompt(), or in a SvelteKit server route share the schemas and generate it with @openuidev/lang-core ^0.4.

```ts title="src/lib/library.ts"
import { createLibrary, defineComponent } from "@openuidev/svelte-lang";
import { z } from "zod";
// Card is the component above; TextContent renders props.text in a <p>.
import Card from "./components/Card.svelte";
import TextContent from "./components/TextContent.svelte";

const TextContentDef = defineComponent({
  name: "TextContent",
  props: z.object({ text: z.string() }),
  description: "Displays a block of text. Supports markdown formatting within the string.",
  component: TextContent,
});

const CardDef = defineComponent({
  name: "Card",
  props: z.object({
    title: z.string(),
    // The components Card accepts as children, by their .ref schemas.
    children: z.array(TextContentDef.ref),
  }),
  description: "A card container with a title and child components",
  component: Card,
});

export const library = createLibrary({
  components: [TextContentDef, CardDef],
  root: "Card",
});

// The system prompt to send with each model request.
export const systemPrompt = library.prompt();
```

</Step>

<Step title="Render the stream" source="https://www.openui.com/docs/api-reference/svelte-lang">

Pass the accumulated text, the library and isStreaming to Renderer, with onAction as an ordinary callback prop. The Svelte Renderer has no onError prop, so read parser errors from onParseResult.

```svelte title="AssistantMessage.svelte"
<script lang="ts">
  import { BuiltinActionType, Renderer, type ActionEvent } from "@openuidev/svelte-lang";
  // The library from src/lib/library.ts.
  import { library } from "$lib/library";

  // The model's OpenUI Lang output so far, and whether it is still streaming.
  let {
    response,
    isStreaming,
  }: { response: string | null; isStreaming: boolean } = $props();

  function handleAction(event: ActionEvent) {
    if (
      event.type === BuiltinActionType.ContinueConversation &&
      event.humanFriendlyMessage
    ) {
      // Send event.humanFriendlyMessage to your chat as the next user message.
    }
  }
</script>

<Renderer {response} {library} {isStreaming} onAction={handleAction} />
```

</Step>

</Steps>