Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow spring/tweened values to be initially undefined #3761

Closed
Rich-Harris opened this issue Oct 21, 2019 · 0 comments · Fixed by #3762
Closed

Allow spring/tweened values to be initially undefined #3761

Rich-Harris opened this issue Oct 21, 2019 · 0 comments · Fixed by #3762

Comments

@Rich-Harris
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants