npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

svelte-imperative

v4.0.0

Published

Lightweight helper to mount Svelte 5 components imperatively while allowing for changes to props

Downloads

148

Readme

svelte-imperative - a stateful wrapper for svelte 5 imperative components

tl;dr: Svelte 5 killed $set but didn’t replace it. This package wraps the new mount() API to give you prop updates without fighting proxies in .svelte.js files.

👉 Use this if:

  • You’re stuck with imperative components (browser extensions, partial rendering, legacy code, etc). SvelteKit is a better solution in most cases, and does this for you.
  • You miss Svelte 4’s $set, or just generally think that kind of interface is better for you
  • You’ve tried $state for reactivity with imperative components in .svelte.js and now hate proxies

Installation

bun i svelte-imperative # (or any other package manager)

You'll also need to have Svelte 5 or newer installed. For Svelte 4 and below, use the $set function with the official imperative component API instead.

Why?

Svelte 5’s new mount() forces you to pass reactive state in (there is no way to do it imperatively as in Svelte 4 anymore), but:

  • .svelte.js reactivity is janky (state breaks on direct reassignment, unlike in .svelte files)
  • State generally works best at the top-level, which might not be what you want if you have a lot of components
  • Apps not using SvelteKit get left behind, mostly

This package bridges the gap.

Usage

import { ImperativeComponent } from "svelte-imperative";
import MyComponent from "./MyComponent.svelte";

const myComponent = new ImperativeComponent(
  document.getElementById("app"),
  MyComponent,
  { message: "Hello, world!", element: "h1" }
);

myComponent.modifyProps({ message: "Goodbye, world!" }); // Partially update props. Becomes { message: 'Goodbye, world!', element: 'h1' }
myComponent.setProps({ message: "Hello, world!", element: "p" }); // Fully update props. Becomes { message: 'Hello, world!', element: 'p' }
myComponent.destroy(); // Destroy the component.

.destroy() gets called for you automatically once the component goes out of scope if you use using (using const myComponent = ...).

Type Safety

The ImperativeComponent class will automatically infer types for your props when using TypeScript. To type an instance of a component (like myComponent in the example above), you can use the ImperativeComponentOf<> generic type:

import MyComponent from './MyComponent.svelte';
import { ImperativeComponent, type ImperativeComponentOf } from 'svelte-imperative';

const createComponent = (): ImperativeComponentOf<typeof MyComponent> => new ImperativeComponent(...);
const myComponent = createComponent();

License

This project is licensed under MIT. You can use it in any project, commercial or not, without attribution (though attribution is cool, of course).

Contributing

If you want to contribute or report an issue, pull requests and issues are welcome on GitHub.