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-extras

v2.0.2

Published

Extra methods for Svelte components

Downloads

1,187

Readme

svelte-extras

Extra methods for Svelte components.

Usage

Install with npm or yarn...

npm install --save svelte-extras

...then add to your component methods:

<input bind:value='newTodo'>
<button on:click='push("todos", newTodo)'>add todo</button>

<ul>
  {{#each todos as todo, i}}
    <li>
      <button on:click='splice("todos", i, 1)'>x</button>
      {{todo}}
    </li>
  {{/each}}
</ul>

<script>
  import { push, splice } from 'svelte-extras';

  export default {
    data: function () {
      return {
        newTodo: '',
        todos: ['add some more todos']
      };
    },

    methods: {
      push,
      splice
    }
  };
</script>

Available methods

Array methods (live demo)

  • push
  • pop
  • shift
  • unshift
  • splice
  • sort
  • reverse

These all work exactly as their Array.prototype counterparts, except that the first argument must be the keypath that points to the array. The following are all examples of keypaths:

component.push('todos', 'finish writing this documentation');
component.push('foo.bar.baz', 42);
component.push('rows[4]', cell);

tween(key, end, options?) (live demo)

Smoothly tweens key from its current value to the end value. Numerical values (and non-cyclical objects and arrays, as long as their leaf properties are numerical) are automatically interpolated, or you can supply a custom function.

The available options (and default values) are:

  • delay (0) — the delay in milliseconds before the tween starts
  • duration (400) — the duration of the tween
  • easing (x => x) — which easing function to use (see e.g. eases-jsnext))
  • interpolate (see above) — a function that generators a custom interpolator, for e.g. transitioning strings representing colors. Must take arguments a and b and return a function that takes a value t between 0 and 1

This method returns a promise with an additional abort method. The tween will be aborted automatically if key is updated separately, either by a second tween or via component.set(...). The promise will not resolve if the tween is aborted.

spring(key, end, options) (live demo)

Similar to tween, except it uses a spring physics simulation rather than a pre-defined easing curve, which gives more natural results in some situations. The end value can be anything you could pass to tween.

The following options must be provided:

  • stiffness — the spring constant, a value between 0 and 1
  • damping — the damping coefficient, again between 0 and 1

Figuring out the optimal combination of stiffness and damping typically takes a bit of trial and error. The higher the stiffness, the quicker the motion will be; the lower the damping, the 'springier' it will be.

This method returns a promise that resolves when the simulation is complete — or not at all, if the simulation is aborted by another call to spring(...) or a call to set(...).

observe(key, callback, options?)

Runs the callback function with two arguments, newValue and oldValue, every time the value of key changes. options can contain two booleans — init, which determines whether to fire the callback immediately (the default) instead of waiting for a change, and defer, which determines whether the callback fires before or after the DOM has updated.

This method used to be built in to Svelte; it's now recommended that you use the onstate and onupdated lifecycle hooks instead.

observeDeep(keypath, callback, options?) (live demo)

Exactly the same as observe method, except that it observes nested properties of objects and arrays, rather than the objects themselves. The keypath option is a string like foo.bar (observe the bar property of the foo object) or baz[0] (observe the first member of the baz array).

observeMany(keys, callback, options?) (live demo)

Observes multiple keys, without firing multiple times when they change simultaneously. keys is an array of keys, while the callback receives two arguments — an array of the new values corresponding to those keys, and an array of the old values. options can include init and defer, like the built-in observe method.

getDeep(keypath) (live demo)

Similar to the built-in get method, except that it gets nested properties of objects and arrays, rather than the objects themselves. The keypath option is a string like foo.bar (get the bar property of the foo object) or baz[0] (get the first member of the baz array).

setDeep(keypath, value) (live demo)

Similar to the built-in set method, except that it sets nested properties of objects and arrays, rather than the objects themselves. The keypath option is a string like foo.bar (set the bar property of the foo object) or baz[0] (set the first member of the baz array).

Tree-shaking

If you're using a module bundler that supports tree-shaking, such as Rollup, only the methods your components use will be included in your app.

Universal module definition

If you really need it, a UMD build is available at svelte-extras/dist/svelte-extras.umd.js, and will register itself as svelte.extras. We recommend using a module bundler instead, however.

License

MIT