Rich-Harris / svelte-knobby

MIT License
201 stars 16 forks source link

How to cross-reference control values? #20

Closed braebo closed 3 years ago

braebo commented 3 years ago

I'm not sure the best way to do this. I tried a few things like using this or anon functions and spreads to no avail. Right now I get:

const controls = knobby.panel({
    a: 20,
    b: a * 2    // a is not defined
})

And I'm not sure what the best way to consume them elsewhere is. Perhaps:

const stuff = (c) => constrols.a * c + controls.b   // or $controls.a/b?
// or
const { a, b } = $controls
const stuff = (c) => a * c + b

Any clarification would be much appreciated! Sorry if I'm overlooking the obvious 😅

Rich-Harris commented 3 years ago

It sounds like you want derived: https://svelte.dev/tutorial/derived-stores

In some cases, like the min and max of a range value, you can also pass a function that takes the current store value as its input. Demo here: https://svelte.dev/repl/ecb3b08487b54182b58d19c465be6564?version=3.40.3

braebo commented 3 years ago

Ah yes- good idea! This should cover my use case, I'll try it next time I've got the project open. Thanks!