-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Conditional bind:this (allow undefined and conditional targets) #4570
Comments
If the expression inside the |
As I said, the second option, that lets you swap target variables, can be vague. I have yet to find a feasible use case for this feature, tbh.
This example is a bit artificial, but I would say the second option should work. |
I am trying to do the following, any ideas for other solutions?
|
@davidcallanan Simplest solution for now would be to store all the refs and only use the one you need. <script>
const items = [...];
let selectedIndex = 3;
let refs = [];
$: refs[selectedIndex]?.getClientBoundingRect().doSomeStuff();
</script>
{#each items as item, i}
<div class="item" class:selected={selectedIndex == i} bind:this={ref[i]}/>
{/each} |
Does anyone know if anything has changed since then? |
it haven't. trying to bind |
I have the following use case and workaround: <script>
export let paras
let curSegSpan;
$: currentSeg = ... // some logic to find the current segment
$: if (curSegSpan) {
// some dom javascript to apply to current segment
}
</script>
<div class="stream">
{#each paras as segments}
<p>
{#each segments as segment}
{@const current = segment === currentSeg}
{#if current}
<Segment {segment} {current} bind:span={curSegSpan} />
{:else}
<Segment {segment} {current} />
{/if}
{/each}
</p>
{/each}
</div>
I could't use @dkzlv's workaround (array of bound elements) because the segments, one of which may be current, don't come from a flat array, but from an array of arrays. I wish there was some shortcut syntax for |
This can be handled in v5 with function bindings. |
Is your feature request related to a problem? Please describe.
It has a lot of possible applications. Here's the latest problem I faced today.
I have a big paginated list, that has page buttons in the bottom of the list, and I want to scroll the list to the first item whenever I change the page. I want to use
scrollIntoView
method, so I need a ref to the first item of the list.Describe the solution you'd like
It seems to be quite safe to allow
undefined
as a possible value for ref — it would just mean to not bind this element/component to any variable.It also seems legit from developer point of view to allow dynamic ref target with code like this:
Describe alternatives you've considered
Right now I do this in multiple ways.
Array of refs. REPL. In two words:
and then use
refs[0]
orrefs[refs.length - 1]
to get certain elements.I think:
Custom class name. Assign a conditional class via
class:custom-name={index === 0}
, and then usedocument.querySelector
to get the element with this class. It is bad because of two reasons: class name clashes (need to come up with unique name) and class name duplication (one in<script>
and one in layout).How important is this feature to you?
Not that much, it just seems like a good feature to have. I'm not aware of the internals of Svelte, but I assume adding any kind of dynamic code in element binding would be a lot of pain, because it most probably happens during compile time and not in the runtime.
Additional context
Same ideas were mentioned in this comment.
The text was updated successfully, but these errors were encountered: