Svelte 5 Runes

14. Svelte 5 Universal Runes & State Management

Master the Svelte 5 signal reactivity runtime: universal runes outside components, class-based state modules, $bindable props, and typed snippets.

runes-state.svelte
<script lang="ts">
  // Svelte 5 Universal Runes in Components and TS Classes
  class CartState {
    items = $state<{ name: string; price: number }[]>([]);
    total = $derived(this.items.reduce((sum, item) => sum + item.price, 0));

    addItem(name: string, price: number) {
      this.items.push({ name, price });
    }
  }

  const cart = new CartState();
  let { title = 'Store Cart' }: { title?: string } = $props();
</script>

<div class="p-6 border rounded-2xl bg-white dark:bg-slate-900">
  <h2 class="text-xl font-bold">{title} (Total: ${cart.total})</h2>
  <button 
    onclick={() => cart.addItem('Pro License', 49)} 
    class="mt-4 px-4 py-2 bg-indigo-600 text-white rounded-xl text-base"
  >
    Add Item (${cart.items.length})
  </button>
</div>

Svelte 5 Universal Runes & State Architecture

Svelte 5 replaces legacy stores and component-bound reactivity with fine-grained signal runes that execute uniformly across components, classes, and standalone TypeScript modules.

  • Universal Runes: $state, $derived, and $effect work anywhere—including TypeScript classes inside .svelte.ts files.
  • Snippets ({#snippet} & @render): Replaces fragile slot mechanisms with strongly typed, parameterizable markup blocks.
  • Explicit Prop Bindings ($props() & $bindable()): Declares whether a component property allows two-way parent binding.
Class-Based Runes Store Simulator Universal Runes
Items Count

2

Subtotal

$148

Total ($derived)

$133.20

Web Engine 2026 Core License
$99
Edge Microservice Blueprint
$49