SCHEMA

svelte
<script lang="ts">
	import { Dialogue } from "svelte-dialogue-tree";
	const tree = {
		branch1: [ // πŸ“Œ dialogue starts from the tree's first key 
			// TextLeaf
			// TextLeaf
			// TextLeaf
			// TextLeaf
			// TextLeaf βœ… any number of TextLeafs can populate a branch
		],
		branch3: [
			// TextLeaf
			// ComponentLeaf
			// TextLeaf
			// ComponentLeaf
			// ComponentLeaf βœ… any number of ComponentLeafs can be placed anywhere
		],
		branch4: [
			// TextLeaf
			// TextLeaf
			// ChoiceLeaf βœ… ChoiceLeafs should always be at the end of branches
		],
		invalid1: [
			// TextLeaf
			// ChoiceLeaf ❌ ChoiceLeafs should always be at the end of branches
			// TextLeaf
		],
		invalid2: [
			// TextLeaf
			// ChoiceLeaf 
			// ChoiceLeaf ❌ there cannot be more than one ChoiceLeaf in a single branch
		],
	};

	const characters = { // ❔ optional Dialogue parameter
		character1: {
			name: "name of character1",
			avatarSrc: "character1.jpg" // ❔ optional parameter 
		},
		character2: {
			name: "name of character2",
		},
	}
</script>
  
<!-- WITH CHARACTERS -->
<Dialogue {tree} {characters} /> βœ…

<!-- OR WITHOUT CHARACTERS -->
<Dialogue {tree} /> βœ…

TYPES

TextLeaf

A TextLeaf can be in following types: string, TextObject, a function that returns a string, a function that returns a TextObject.

typescript
// string
"something"

// narration string
"** something is being narrated **"

// HTML string
"<a href="/something">something</a>"

// TextObject
{ text: "something", character: "jason" }

// function that returns a string
() => "something" 

// function that returns a TextObject
() => { text: "something", character: "jason" }

TextObject

A TextObject requires a text property and at least one (1) additional property.
Additional properties are:
onSpawn and character.

typescript
// TextObject  
{ 
	text: "something",
	character: "jason"
}

// TextObject  
{ 
	text: "something",
	onSpawn: () => console.log("something spawned.")
}

// TextObject (it can have both of the additional properties)
{ 
	text: "something",
	character: "jason",
	onSpawn: () => console.log("something spawned.")
}

TextObject.text

The string that will be displayed.

typescript
const tree = {
  start: [
    "Hello",
    { 
      text: "world", 
      onSpawn: () => console.log("spawned world")
    }
  ]
}

TextObject.onSpawn

The function that is assigned to onSpawn is executed when the TextObject enters the DOM.

typescript
let health = 100;

function fallFromCliff() {
  health -= 50;
}

const tree = {
  start: [
    "** You wake up near a cliff. **",
    { 
      text: "** You panic and fall from the cliff. **", 
      onSpawn: fallFromCliff 
    }
  ]
}

TextObject.character

character should be a key of a CharacterCollection.

typescript
const characters = {
  gotrek: {
    name: "Gotrek the Trollslayer",
    avatarSrc: "gotrek.jpg"
  }
}

const tree = {
  start: [
    "** You open your eyes and see a dwarf looking at you in disdain **",
    { 
      text: "Wake up, manling.",
      character: "gotrek" 
    }
  ]
}

CharacterCollection

CharacterCollection is an object which holds CharacterKeys as keys and Characters as values.

typescript
const characters = {
  gotrek: {
    name: "Gotrek the Trollslayer",
    avatarSrc: "gotrek.jpg"
  }
}

const tree = {
  start: [
    "** You open your eyes and see a dwarf looking at you in disdain **"
    { text: "Wake up, manling.", character: "gotrek" }
  ]
}

Character

A Character requires a name and can have an optional avatarSrc.

typescript
const gotrek = {
	name: "Gotrek the Trollslayer",
	avatarSrc: "gotrek.jpg"
}

const felix = {
	name: "Felix Jaeger",
	// avatarSrc is optional
}

ComponentLeaf

A ComponentLeaf requires a component property which accepts a Svelte component. The other property is the optional args property, which is passed to the given component as a destructured argument.

typescript
import { Component } from "./Component.svelte";

// ComponentLeaf
{
	component: Component,
	args: { // ❔ optional
		count: 5,
		color: "red"
	}
}

ChoiceLeaf

A ChoiceLeaf can be an array of ChoiceObjects or a function that returns an array of ChoiceObjects.

typescript
// ChoiceLeaf
[
	// ChoiceObject
	// ChoiceObject
]

// ChoiceLeaf
() => [
	// ChoiceObject
	// ChoiceObject
]

ChoiceObject

Prop Type Required
label string βœ”
text string βœ”
next BranchKey | Branch<BranchKey, CharacterKey> | (() => BranchKey) βœ”
titleTag string | () => string
disabled boolean | () => boolean
typescript
{ 
	/** REQUIRED */
	label: "YES",
	text: "I think that's right",
	next: "yesBranch"

	/** OPTIONAL */
	titleTag: "some title"
	disabled: true 
}

ChoiceObject.label

label is the text that will appear on the choice button.

typescript
{ 
	// code
	label: "YES",
}

ChoiceObject.text

text is the content that will appear on the dialogue after choice is made.

typescript
{ 
	// code
	text: "I think so.",
}

ChoiceObject.next

next is the key of the branch dialogue will jump to. You can use next in three (3) different ways:

  1. 1.BranchKey
  2. 2.A function that returns a BranchKey
  3. 3.A nested branch

typescript
// (1) A BranchKey (string)
{
	// code
	next: "yesBranch"
}

// (2) A function that returns a BranchKey
{
	// code
	next: () => "yesBranch" 
}

// (3) A nested branch
{
	// code
	next: [
		"Great! Let's go bowling."
		"How would you like to go?"
		[
			{ 
				label: "πŸš—",
				text: "How about we take your Honda?", 
				next: [
					"Oh, it's actually broken. We can take a cab though."
				]
			},
			{ 
				label: "πŸšΆβ€β™€οΈ",
				text: "It's not that far, we can walk.", 
				next: [
					"Sure!"
					"Would you like to race?"
					[
						{       
							label: "YES",
							text: "I don't know. ** You start running **", 
							next: [
								"** You win **"
								// Nesting can go forever 
							]
						},
						{       
							label: "No",
							text: "Nah, I don't feel like it.", 
							next: [
								"** You don't race **"
								// Nesting can go forever 
							]
						}
					]
				] 
			},
			{ 
				label: "Cancel the date",
				text: "You know what? I forgot I had some things to do.", 
				next: "cancelBranch" 
			},
		]
	]
}

ChoiceObject.titleTag

titleTag is the title that will appear when a mouse is hovered over the choice button. It can be a string or a function that returns a string.

typescript
// a string
{ 
	// code
	titleTag: "some title",
}

// or a function that returns a string
{ 
	// code
	titleTag: () => Math.random() < 0.5 ? "Not enough." : "Good enough.",
}

ChoiceObject.disabled

disabled determines whether the choice button is disabled or not. Can be boolean or a function that returns a boolean.

typescript
// boolean
{ 
	// code
	disabled: true,
}

// or a function that returns a boolean
{ 
	// code
	disabled: () => Math.random() < 0.5,
}
If you are using functions to disable choices, make sure you have at least one choice that is never disabled to prevent freezing the conversation.

PROPS

Name Type Value Description
tree Required object - Pass the DialogueTree object.
characters object {} Pass the CharacterCollection object.
containerClass string sdt-container Provide classes for the container of Dialogue component.
choiceContainerClass string sdt-choice-container Provide classes for the container of choices.
choiceClass string sdt-choice Provide classes for individual choice buttons.
npcContainerClass string sdt-npc-container Provide classes for the container of npcText and charContainer.
npcTextClass string sdt-npc Provide classes for NPC replies.
charContainerClass string sdt-char-container Provide classes for the container of charName and charAvatar
charNameClass string sdt-char-name Provide classes for NPC names.
charAvatarClass string sdt-char-avatar Provide classes for NPC avatars (<img> element).
playerTextClass string sdt-player Provide classes for player replies.
narrationClass string sdt-narration Provide classes for narration texts.
jumperClass string sdt-jumper Provide classes for the jumper. Jumper appears when the dialogue is scrolled up.
jumperText string JUMP TO BOTTOM The text that will appear on jumper button.
npcIn Function fly Provide intro functions for npc replies.
npcInOptions object { x: -200 } Provide options object for the npc intro function.
playerIn Function fly Provide intro functions for player replies.
playerInOptions object { x: 200 } Provide options object for the player intro function.
choiceIn Function scale Provide intro functions for choice buttons.
choiceInOptions object {} Provide options object for the choice button intro function.
choiceStaggerGap number 200 Provide a number in ms for staggering button intro function.
narrationIn Function scale Provide intro functions for narration texts.
narrationInOptions object {} Provide options object for the narration text intro function.
nextLineKey string Space Provide an event code to trigger next line.

LAYOUT

containerClass
npcContainerClass
charContainerClass
charAvatarClass
charNameClass
npcTextClass
playerTextClass
narrationClass
choiceContainerClass
choiceClass
jumperClass

EVENTS

Name Detail Description
dialogueEnd - Fires when dialogue has ended.
componentEvent user defined Fires when a component dispatches componentEvent.
componentEnd user defined Fires when a component dispatches componentEnd. If a component enters the Dialogue, Dialogue will not continue until this event is fired.

BINDINGS

You can call nextLine by binding the Dialogue component to a variable.

svelte
<script>
	import { Dialogue } from 'svelte-dialogue-tree';

	const tree = {
		start: ['hello', 'world', 'what', 'is', 'up']
	};

	let dialogueComponent;

	function nextLine() {
		dialogueComponent.nextLine();
	}
</script>
	
<Dialogue {tree} bind:this={dialogueComponent} />

<button class="..." on:click={nextLine}>NEXT LINE</button>
				

NARRATION

You can use narration to convey information as a third person. Wrap the content in double asterisk (**) to render it as a narration text.

TIP: You can include multiple narration texts inside a string. They will be rendered seperately.
typescript
const tree = {
	start: [
		'** You wake up near a cliff. **',
		{
			text: '** You panic and fall from the cliff. ** npc text',
			onSpawn: () => console.log("fell from cliff")
		},
		'** narration ** npc text ** narration **'
	]
};

TextObject.character information will not be rendered if TextObject.text is inside double asterisks.