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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mibu/svelte-inject

v1.0.0

Published

Svelte action to inject elements into the DOM

Downloads

2

Readme

Svelte Inject

Svelte action to inject elements into the DOM

Why

Useful when injectig Svelte into exising code at runtime, e.g. GreaseMonkey userscripts. You can already inject Svelte components, but only with one injection point per component:

new App({
	target: ..., // Insert into target
	anchor: ... // Insert before child of target
});

Consider wanting to modify a webiste by adding toggle to the navbar and some toggleable content to an existing content container:

<nav id="navbar">
	<button id="toggle"/> <!-- toggle we want to inject -->
</nav>
<div id="container">
	<div id="content"/> <!-- content we want to inject -->
</div>

You would have to create a component for each and then inject them:

new Toggle({target: document.getElementById("navbar")});
new Content({target: document.getElementById("container")});

But now you are outside the domain of the Svelte compiler and its harder to add reactivity. Lets instead create a App.svelte component and use the inject action:

<script>
	import inject from "@mibu/svelte-inject"
	let showContent = false;
</script>

<button use:inject{"#navbar"} on:click={() => showContent = !showContent}/>
<div use:inject{"#container"} class:hidden={showContent}/>

Now everything is inside one Svelte component and its also reactive.

Usage

Import inject:

<script>
	import inject from "@mibu/svelte-inject"
</script>

The inject action takes one argument {<mode>: <target>}:

  • <mode>: Where the element will be injected
  • <target>: Either an HTMLElement or a query selector string
<!-- Inject after target -->
<div use:inject={{after: document.getElementById("example")}}>
	...
</div>

<!-- Inject before target -->
<div use:inject={{before: "#example"}}>
	...
</div>

<!-- Append to target -->
<div use:inject={{append: "li.example"}}>
	...
</div>

When you pass just the HTMLElement or a query string, the element will be appended:

<!-- Append to target -->
<div use:inject={document.getElementById("example")}>
	...
</div>

<!-- Append to target -->
<div use:inject={"button.active"}>
	...
</div>

If you don't pass any arguments, the element will be appended to the body:

<!-- Append to document.body -->
<div use:inject>
	...
</div>