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

Document dynamic bind:this #2422

Closed
Conduitry opened this issue Apr 15, 2019 · 4 comments
Closed

Document dynamic bind:this #2422

Conduitry opened this issue Apr 15, 2019 · 4 comments

Comments

@Conduitry
Copy link
Member

As far as I can tell, there are no docs or examples yet where bind:this is given a dynamic expression depending on e.g. each block iteration. This should be mentioned somewhere, as pre-v3 this was one of the things that kept coming up that people wanted with refs.

@Conduitry Conduitry added the docs label Apr 15, 2019
@CanRau
Copy link

CanRau commented Nov 3, 2019

By dynamic you mean something like

let reference = null
<div
bind:this={condition ? reference : null} />

or like

let reference = []
<div
bind:this={reference[index]} />

like mentioned in #2333 ?

I'd love to get the first one running 😍 Any possibility?
Or any other way to avoid document.querySelector to get the one selected element?

Update: Sadly the following doesn't work either even though it's at least compiling

<div {...index === item ? { 'bind:this': selectedItem } : {}}>

@Conduitry
Copy link
Member Author

I mean the second one - I don't know what you'd want the first one would do. If you bound to reference and then had a reactive assignment $: reference2 = condition ? reference : null, then reference2 would be either the DOM node or null depending on the value of condition. Is that what you mean?

The third other syntax in your update compiles because Svelte doesn't see that as a conditional bind:this, but as an attribute called bind:this that is conditionally applied. All directives need to be visible at compile time.

@CanRau
Copy link

CanRau commented Nov 3, 2019

Yea just that the condition has to happen in the {#each} loop I guess 🤔

The third other syntax in your update compiles because Svelte doesn't see that as a conditional bind:this, but as an attribute called bind:this that is conditionally applied. All directives need to be visible at compile time.

That's what I thought might be the case, thanks for clarifying 🙏

So sticking to document.querySelector for now..

@dummdidumm
Copy link
Member

Closing due to no activity and no plans to do so, as I think the behavior is intuitive. This was mostly a "oh I can do this now?" for Svelte 2 users I guess.

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

No branches or pull requests

5 participants