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

svelte-render

v2.0.1

Published

Manage complex Svelte behaviors outside of templates with full type safety

Downloads

84,641

Readme

svelte-render

npm version npm downloads license build coverage size

Manage complex Svelte behaviors outside of templates with full type safety.

<script>
  import {Render, createRender} from 'svelte-render';
  import Avatar from './Avatar.svelte';
  // ...
  const avatar = createRender(Avatar, {name: 'Ada Lovelace'})
    .on('click', handleClick)
    .on('launch', handleLaunch);
</script>

<Render of={avatar} />

Installation

$ npm i -D svelte-render

API

Svelte Render was primarily built to support complex rendering definitions for Svelte Headless Table. You can find full documentation on createRender on the documentation site.

<Render />

<Render /> handles props and automatically registers the event handlers defined with .on as well as slot data defined with .slot.

of accepts:

  • primitive data such as number and string
  • Writable<number> and Writable<string> for dynamic primitive data
  • ComponentRenderConfig returned by createRender
<script>
  const avatar = createRender(Avatar, {name: 'Ada Lovelace'});
</script>

<Render of={avatar} />

becomes

<Avatar name="Ada Lovelace" />

createRender: (component, props)

createRender accepts a Svelte component and its props as arguments.

props can be omitted if the component does not receive props but must be included otherwise.

const icon = createRender(TickIcon); // ✅
const avatar = createRender(Avatar); // ❌ Type error.
const avatar = createRender(Avatar, {name: 'Ada Lovelace'}); // ✅

If you need prop reactivity, props must be a Svelte store.

const avatarProps = writable({name: 'Ada Lovelace'});
const avatar = createRender(Avatar, avatarProps);

.on(event, handler)

Svelte Render supports the Svelte event system by chaining .on calls on createRender(). Multiple event handlers can be registered for the same event type like the Svelte on: directive.

const button = createRender(Button)
  .on('click', handleClick)
  .on('click', (ev) => console.log(ev));

<Render of={button} /> becomes:

<Button on:click={handleClick} on:click={(ev) => console.log(ev)} />

However, note that the callback handler passed into .on(event, handler) is not dynamic and will only capture references to variables as they were when the render configuration is created.

If you need a handler to access dynamic data, use a dynamic system like Svelte Stores.

const counter = writable(0);
const button = createRender(Button)
  .on('click', (ev) => counter.update(c => c + 1));

.slot(...config)

Svelte Render also supports Svelte's default slot system.

.slot receives any number of arguments with the same type as of, including ComponentRenderConfig returned by createRender, primitive data, and Writable. This makes it useful for rendering wrapper components such as <Button /> and <Label />.

Due to technical limitations with Svelte 4, it is not possible to assign render configurations to named slots.

const button = createRender(Button).slot(createRender(Icon, {name: 'user'}), 'Log in');

<Render of={button} /> becomes:

<Button>
  <Icon name="user" />
  Log in
</Button>