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
rendering.md docs/
1# Component Rendering23Svelte provides functions for mounting, hydrating, and unmounting components in the DOM. These functions are essential for programmatically controlling component lifecycle.45## Capabilities67### mount89Mounts a component to the specified target and returns the component instance with its exports.1011```typescript { .api }12/**13* Mounts a component to the given target and returns the exports14* @param component - Component to mount15* @param options - Mount configuration options16* @returns Component instance with exports17*/18function mount<Props extends Record<string, any>, Exports extends Record<string, any>>(19component: Component<Props, Exports, any>,20options: MountOptions<Props>21): Exports;22```2324**Usage Examples:**2526```typescript27import { mount } from "svelte";28import App from "./App.svelte";2930// Basic mounting31const app = mount(App, {32target: document.getElementById("app")33});3435// Mount with props36const app = mount(App, {37target: document.body,38props: {39name: "World",40count: 4241}42});4344// Mount with anchor and context45const app = mount(App, {46target: document.body,47anchor: document.getElementById("insertion-point"),48props: { message: "Hello" },49context: new Map([["theme", "dark"]]),50intro: true51});5253// Access component exports54console.log(app.someExportedValue);55app.someExportedFunction();56```5758### hydrate5960Hydrates a server-rendered component, attaching event listeners and making it interactive.6162```typescript { .api }63/**64* Hydrates a component on the given target and returns the exports65* @param component - Component to hydrate66* @param options - Hydration configuration options67* @returns Component instance with exports68*/69function hydrate<Props extends Record<string, any>, Exports extends Record<string, any>>(70component: Component<Props, Exports, any>,71options: HydrateOptions<Props>72): Exports;73```7475**Usage Examples:**7677```typescript78import { hydrate } from "svelte";79import App from "./App.svelte";8081// Hydrate server-rendered content82const app = hydrate(App, {83target: document.getElementById("app"),84props: {85initialData: window.__INITIAL_DATA__86}87});8889// Hydrate with recovery mode90const app = hydrate(App, {91target: document.getElementById("app"),92props: { data: serverData },93recover: true // Attempt to recover from hydration mismatches94});95```9697### unmount9899Unmounts a component that was previously mounted using `mount` or `hydrate`. Optionally plays outro transitions.100101```typescript { .api }102/**103* Unmounts a component that was previously mounted104* @param component - Component instance to unmount105* @param options - Unmount options including outro transitions106* @returns Promise that resolves after transitions complete107*/108function unmount(component: Record<string, any>, options?: {109outro?: boolean;110}): Promise<void>;111```112113**Usage Examples:**114115```typescript116import { mount, unmount } from "svelte";117import App from "./App.svelte";118119// Mount component120const app = mount(App, {121target: document.body122});123124// Unmount immediately125await unmount(app);126127// Unmount with outro transitions128await unmount(app, { outro: true });129130// Programmatic component lifecycle131let currentApp = null;132133function showApp() {134if (currentApp) return;135136currentApp = mount(App, {137target: document.getElementById("app")138});139}140141async function hideApp() {142if (!currentApp) return;143144await unmount(currentApp, { outro: true });145currentApp = null;146}147```148149## Types150151```typescript { .api }152interface MountOptions<Props extends Record<string, any> = Record<string, any>> {153/** Target element where the component will be mounted */154target: Document | Element | ShadowRoot;155/** Optional node inside target to render before */156anchor?: Node;157/** Component properties */158props?: Props;159/** Context map accessible via getContext() */160context?: Map<any, any>;161/** Whether to play transitions on initial render */162intro?: boolean;163/** Deprecated: Use callback props instead */164events?: Record<string, (e: any) => any>;165}166167interface HydrateOptions<Props extends Record<string, any> = Record<string, any>> {168/** Target element containing server-rendered content */169target: Document | Element | ShadowRoot;170/** Component properties */171props?: Props;172/** Context map accessible via getContext() */173context?: Map<any, any>;174/** Whether to play transitions during hydration */175intro?: boolean;176/** Attempt to recover from hydration mismatches */177recover?: boolean;178/** Deprecated: Use callback props instead */179events?: Record<string, (e: any) => any>;180}181182interface Component<183Props extends Record<string, any> = {},184Exports extends Record<string, any> = {},185Bindings extends keyof Props | '' = string186> {187(internals: ComponentInternals, props: Props): Exports;188}189```190191## Best Practices1921931. **Always handle cleanup**: Use `unmount` to properly clean up components and prevent memory leaks1942. **Use outro transitions**: Set `outro: true` when unmounting to play exit animations1953. **Handle hydration carefully**: Use `recover: true` in development to handle server/client mismatches1964. **Context inheritance**: Components inherit context from their mounting location1975. **Props reactivity**: Mounted components don't automatically react to prop changes - use stores for dynamic updates