Spec Registry
Help your agents use open-source better. Learn more.
Find usage specs for your project’s dependencies
- Author
- tessl
- Last updated
- Spec files
npm-svelte
Describes: npm/svelte
- Description
- A cybernetically enhanced web application framework that compiles to highly optimized JavaScript with reactive state management and component-based architecture.
- Author
- tessl
- Last updated
index.md docs/
1# Svelte23Svelte is a revolutionary compile-time web application framework that transforms declarative component syntax into highly optimized vanilla JavaScript. Unlike traditional frameworks, Svelte performs the work during the build step, generating minimal and efficient code that surgically updates the DOM without requiring a virtual DOM layer.45## Package Information67- **Package Name**: svelte8- **Package Type**: npm9- **Language**: JavaScript with TypeScript support10- **Installation**: `npm install svelte`1112## Core Imports1314```typescript15import { onMount, onDestroy } from "svelte";16import { mount, hydrate } from "svelte";17```1819For CommonJS:2021```javascript22const { onMount, onDestroy } = require("svelte");23const { mount, hydrate } = require("svelte");24```2526Module-specific imports:2728```typescript29import { compile, parse } from "svelte/compiler";30import { fade, fly, slide } from "svelte/transition";31import { spring, tweened } from "svelte/motion";32import { writable, readable, derived } from "svelte/store";33import { flip } from "svelte/animate";34import { cubicOut, elasticOut, bounceIn } from "svelte/easing";35```3637## Basic Usage3839```typescript40import { mount } from "svelte";41import App from "./App.svelte";4243// Mount a component44const app = mount(App, {45target: document.getElementById("app"),46props: {47name: "World"48}49});5051// Use lifecycle functions in components52import { onMount, onDestroy } from "svelte";5354let count = 0;55let interval;5657onMount(() => {58interval = setInterval(() => {59count += 1;60}, 1000);61});6263onDestroy(() => {64clearInterval(interval);65});66```6768## Architecture6970Svelte is built around several key components:7172- **Compiler**: Transforms `.svelte` components and rune-based JavaScript into optimized code73- **Runtime**: Minimal runtime for reactivity, lifecycle management, and DOM updates74- **Reactivity System**: Runes-based reactive state with `$state`, `$derived`, and `$effect`75- **Component System**: Component mounting, hydration, and lifecycle management76- **Store System**: External state management with reactive stores77- **Animation & Transitions**: Built-in animation utilities and transition effects78- **Legacy Support**: Compatibility layer for migrating from Svelte 47980## Capabilities8182### Component Lifecycle8384Core component lifecycle functions for managing component initialization, updates, and cleanup.8586```typescript { .api }87function onMount<T>(fn: () => NotFunction<T> | Promise<NotFunction<T>> | (() => any)): void;88function onDestroy(fn: () => any): void;89function beforeUpdate(fn: () => void): void;90function afterUpdate(fn: () => void): void;91```9293[Component Lifecycle](./lifecycle.md)9495### Component Rendering9697Functions for mounting, hydrating, and unmounting Svelte components in the DOM.9899```typescript { .api }100function mount<Props extends Record<string, any>, Exports extends Record<string, any>>(101component: Component<Props, Exports, any>,102options: MountOptions<Props>103): Exports;104105function hydrate<Props extends Record<string, any>, Exports extends Record<string, any>>(106component: Component<Props, Exports, any>,107options: {} extends Props ? {108target: Document | Element | ShadowRoot;109props?: Props;110events?: Record<string, (e: any) => any>;111context?: Map<any, any>;112intro?: boolean;113recover?: boolean;114} : {115target: Document | Element | ShadowRoot;116props: Props;117events?: Record<string, (e: any) => any>;118context?: Map<any, any>;119intro?: boolean;120recover?: boolean;121}122): Exports;123124function unmount(component: Record<string, any>, options?: { outro?: boolean }): Promise<void>;125```126127[Component Rendering](./rendering.md)128129### Context Management130131API for sharing data between parent and child components through context.132133```typescript { .api }134function getContext<T>(key: any): T;135function setContext<T>(key: any, context: T): T;136function hasContext(key: any): boolean;137function getAllContexts<T extends Map<any, any> = Map<any, any>>(): T;138```139140[Context Management](./context.md)141142### Reactivity Control143144Functions for controlling reactive updates and execution timing.145146```typescript { .api }147function tick(): Promise<void>;148function settled(): Promise<void>;149function untrack<T>(fn: () => T): T;150function flushSync<T = void>(fn?: (() => T) | undefined): T;151function getAbortSignal(): AbortSignal;152```153154[Reactivity Control](./reactivity.md)155156### Runes System157158Svelte 5's runes-based reactivity system for declaring reactive state, derived values, and effects.159160```typescript { .api }161declare function $state<T>(initial: T): T;162declare function $state<T>(): T | undefined;163164declare function $derived<T>(expression: T): T;165166declare function $effect(fn: () => void | (() => void)): void;167168declare function $props(): any;169170declare function $bindable<T>(fallback?: T): T;171172declare function $inspect<T extends any[]>(173...values: T174): { with: (fn: (type: 'init' | 'update', ...values: T) => void) => void };175176declare function $host<El extends HTMLElement = HTMLElement>(): El;177```178179[Runes System](./runes.md)180181### Snippet Creation182183Functions for creating snippets programmatically from JavaScript functions.184185```typescript { .api }186function createRawSnippet<T extends unknown[]>(fn: (...args: T) => {187render: () => string;188setup?: (element: Element) => void | (() => void);189}): Snippet<T>;190```191192### Compiler API193194Functions for compiling Svelte components and parsing source code.195196```typescript { .api }197function compile(source: string, options: CompileOptions): CompileResult;198function compileModule(source: string, options: ModuleCompileOptions): CompileResult;199function parse(source: string, options?: ParseOptions): AST.Root;200function preprocess(source: string, preprocessor: PreprocessorGroup | PreprocessorGroup[], options?: PreprocessOptions): Promise<Processed>;201```202203[Compiler API](./compiler.md)204205### Motion & Animation206207Animation utilities including springs, tweens, and FLIP animations.208209```typescript { .api }210class Spring<T> {211constructor(value: T, options?: SpringOpts);212set(value: T, options?: SpringUpdateOpts): Promise<void>;213target: T;214get current(): T;215}216217class Tween<T> {218constructor(value: T, options?: TweenedOptions<T>);219set(value: T, options?: TweenedOptions<T>): Promise<void>;220get current(): T;221target: T;222}223224function flip(node: Element, params: FlipParams): AnimationConfig;225```226227[Motion & Animation](./motion.md)228229### Transitions230231Built-in transition effects for element enter/exit animations.232233```typescript { .api }234function fade(node: Element, params?: FadeParams): TransitionConfig;235function fly(node: Element, params?: FlyParams): TransitionConfig;236function slide(node: Element, params?: SlideParams): TransitionConfig;237function scale(node: Element, params?: ScaleParams): TransitionConfig;238function blur(node: Element, params?: BlurParams): TransitionConfig;239function draw(node: SVGElement & { getTotalLength(): number }, params?: DrawParams): TransitionConfig;240```241242[Transitions](./transitions.md)243244### Easing Functions245246Mathematical easing functions for smooth animation curves.247248```typescript { .api }249function linear(t: number): number;250function cubicOut(t: number): number;251function cubicIn(t: number): number;252function cubicInOut(t: number): number;253function elasticOut(t: number): number;254function bounceOut(t: number): number;255```256257[Easing Functions](./easing.md)258259### Store System260261External state management with reactive stores for sharing state across components.262263```typescript { .api }264function writable<T>(value?: T, start?: StartStopNotifier<T>): Writable<T>;265function readable<T>(value?: T, start?: StartStopNotifier<T>): Readable<T>;266function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>) => T, initial_value?: T): Readable<T>;267function get<T>(store: Readable<T>): T;268```269270[Store System](./stores.md)271272### Events273274Utilities for event handling and DOM event management.275276```typescript { .api }277function on(278element: EventTarget,279event: string,280handler: EventListener,281options?: AddEventListenerOptions | boolean282): () => void;283```284285### Attachments286287System for creating and managing element attachments as an alternative to actions.288289```typescript { .api }290function createAttachmentKey(): symbol;291function fromAction<E extends EventTarget, T>(292action: Action<E, T>,293fn?: () => T294): Attachment<E>;295```296297### Server-Side Rendering298299Functions for rendering Svelte components on the server.300301```typescript { .api }302function render<Comp extends Component<any>>(303component: Comp,304options?: RenderOptions<ComponentProps<Comp>>305): RenderOutput;306```307308[Server-Side Rendering](./ssr.md)309310### Window Reactivity311312Reactive values for window properties like dimensions, scroll position, and device characteristics.313314```typescript { .api }315const innerWidth: ReactiveValue<number | undefined>;316const innerHeight: ReactiveValue<number | undefined>;317const scrollX: ReactiveValue<number | undefined>;318const scrollY: ReactiveValue<number | undefined>;319const online: ReactiveValue<boolean | undefined>;320const devicePixelRatio: ReactiveValue<number | undefined>;321```322323[Window Reactivity](./reactivity-window.md)324325### Legacy Compatibility326327Utilities for migrating from Svelte 4 and working with legacy components.328329```typescript { .api }330function createClassComponent<Props, Exports>(331component: Component<Props, Exports>332): LegacyComponentConstructor<Props, Exports>;333334function asClassComponent<Props, Exports>(335component: Component<Props, Exports>336): Component<Props, Exports>;337```338339[Legacy Compatibility](./legacy.md)340341## Types342343```typescript { .api }344interface Component<345Props extends Record<string, any> = {},346Exports extends Record<string, any> = {},347Bindings extends keyof Props | '' = string348> {349(internals: ComponentInternals, props: Props): Exports;350}351352interface MountOptions<Props extends Record<string, any> = Record<string, any>> {353target: Document | Element | ShadowRoot;354anchor?: Node;355events?: Record<string, (e: any) => any>;356context?: Map<any, any>;357intro?: boolean;358} & ({} extends Props359? { props?: Props }360: { props: Props });361362interface HydrateOptions<Props extends Record<string, any> = Record<string, any>> {363target: Document | Element | ShadowRoot;364events?: Record<string, (e: any) => any>;365context?: Map<any, any>;366intro?: boolean;367recover?: boolean;368} & ({} extends Props369? { props?: Props }370: { props: Props });371372interface Snippet<Parameters extends unknown[] = []> {373(...args: Parameters): SnippetReturn;374}375376type ComponentProps<Comp extends Component<any, any>> =377Comp extends Component<infer Props, any> ? Props : never;378379type NotFunction<T> = T extends Function ? never : T;380381interface ComponentInternals {}382383interface SnippetReturn {}384385interface EventDispatcher<EventMap extends Record<string, any>> {386<Type extends keyof EventMap>(387...args: null extends EventMap[Type]388? [type: Type, parameter?: EventMap[Type] | null | undefined]389: undefined extends EventMap[Type]390? [type: Type, parameter?: EventMap[Type] | null | undefined]391: [type: Type, parameter: EventMap[Type]]392): boolean;393}394395interface Action<Element = HTMLElement, Parameter = undefined> {396<Node extends Element>(397...args: undefined extends Parameter398? [node: Node, parameter?: Parameter]399: [node: Node, parameter: Parameter]400): void | ActionReturn<Parameter>;401}402403interface ActionReturn<Parameter = undefined> {404update?: (parameter: Parameter) => void;405destroy?: () => void;406}407408interface Attachment<T extends EventTarget = Element> {409(element: T): void | (() => void);410}411412interface RenderOutput {413head: string;414html: string;415body: string;416}417```