Svelte • Reference

svelte/legacy

On this page

This module provides various functions for use during the migration, since some features can’t be replaced one to one with new features. All imports are marked as deprecated and should be migrated away from over time.

import {
	asClassComponent,
	createBubbler,
	createClassComponent,
	handlers,
	LegacyComponentType,
	nonpassive,
	once,
	passive,
	preventDefault,
	run,
	self,
	stopImmediatePropagation,
	stopPropagation,
	trusted
} from 'svelte/legacy';

createClassComponent

Use this only as a temporary solution to migrate your imperative component code to Svelte 5.

Takes the same options as a Svelte 4 component and the component function and returns a Svelte 4 compatible component.

export function createClassComponent<Props extends Record<string, any>, Exports extends Record<string, any>, Events extends Record<string, any>, Slots extends Record<string, any>>(options: ComponentConstructorOptions<Props> & {
		component: ComponentType<SvelteComponent<Props, Events, Slots>> | Component<Props>;
	}): SvelteComponent<Props, Events, Slots> & Exports;

asClassComponent

Use this only as a temporary solution to migrate your imperative component code to Svelte 5.

Takes the component function and returns a Svelte 4 compatible component constructor.

export function asClassComponent<Props extends Record<string, any>, Exports extends Record<string, any>, Events extends Record<string, any>, Slots extends Record<string, any>>(component: SvelteComponent<Props, Events, Slots> | Component<Props>): ComponentType<SvelteComponent<Props, Events, Slots> & Exports>;

run

Use this only as a temporary solution to migrate your component code to Svelte 5.

Runs the given function once immediately on the server, and works like $effect.pre on the client.

export function run(fn: () => void | (() => void)): void;

handlers

deprecated

Function to mimic the multiple listeners available in svelte 4

export function handlers(...handlers: EventListener[]): EventListener;

createBubbler

Use this only as a temporary solution to migrate your automatically delegated events in Svelte 5.

Function to create a bubble function that mimic the behavior of on:click without handler available in svelte 4.

export function createBubbler(): (type: string) => (event: Event) => boolean;

LegacyComponentType

Support using the component as both a class and function during the transition period

export type LegacyComponentType = {
		new (o: ComponentConstructorOptions): SvelteComponent;
		(...args: Parameters<Component<Record<string, any>>>): ReturnType<Component<Record<string, any>, Record<string, any>>>;
	};

trusted

deprecated

Substitute for the trusted event modifier

export function trusted(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void;

self

deprecated

Substitute for the self event modifier

export function self(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void;

stopPropagation

deprecated

Substitute for the stopPropagation event modifier

export function stopPropagation(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void;

once

deprecated

Substitute for the once event modifier

export function once(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void;

stopImmediatePropagation

deprecated

Substitute for the stopImmediatePropagation event modifier

export function stopImmediatePropagation(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void;

preventDefault

deprecated

Substitute for the preventDefault event modifier

export function preventDefault(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void;

passive

deprecated

Substitute for the passive event modifier, implemented as an action

export function passive(node: HTMLElement, [event, handler]: [event: string, handler: () => EventListener]): void;

nonpassive

deprecated

Substitute for the nonpassive event modifier, implemented as an action

export function nonpassive(node: HTMLElement, [event, handler]: [event: string, handler: () => EventListener]): void;