Skip to content

Allow spring/tweened values to be initially undefined #3761

Closed
@Rich-Harris

Description

@Rich-Harris

Is your feature request related to a problem? Please describe.
When using springs and tweens to describe values that are set from props, I find myself doing this sort of thing:

<script>
  import { spring } from 'svelte/motion';

  export let big = false;
  
  const size = spring(big ? 100 : 20);
  $: size.set(big ? 100 : 20);
</script>

In this situation I want the value of size to be static when the component is first created, until the prop changes. But it's a nuisance to have to specify big ? 100 : 20 twice. You could create a function for it...

const get_size = big => big ? 100 : 20;
const size = spring(get_size(big));
$: size.set(get_size(big));

...but that's arguably worse.

Describe the solution you'd like
If it were possible to call spring without an initial value, we could initialise it later:

const size = spring();
$: size.set(big ? 100 : 20);

Of course, it would be nice if we could initialise the store right there in the reactive declaration, as happens for non-store values, but I'm not sure what that would look like. Might be cool to be able to do this sort of thing...

$: <spring>$size = big ? 100 : 20;

...but it's obviously invalid JS. Maybe worth coming back round to that one day, but for the meantime allowing undefined/null initial values solves the DRY problem.

How important is this feature to you?
I'm encountering this situation a lot in my current project. I think it's important that using motion be as frictionless as possible.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions