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

Is there any way to trigger use: action after rendering finished, not before? #47

Closed
taw opened this issue Sep 28, 2021 · 4 comments
Closed

Comments

@taw
Copy link

taw commented Sep 28, 2021

OK so this code in Svelte REPL and Malina REPL does different things:

<script>
let output = ""
function action(el) {
  output = el.innerText
}
</script>

<div use:action>
{#each [1,2,3,4] as i}
{i}
{/each}
</div>

DIV CONTAINS: {output}

Malina runs it before children update so it gets "". Svelte runs it after children update so it gets "1 2 3 4".

I don't mind whichever one is the default, but I don't know how to get Svelte-like behavior in Malina, and I need it.

Svelte even has await tick() I could use for this if it wasn't already default to wait for full render, but I don't think Malina has anything like that.

(for my use case I need parent's scrollHeight etc. to see which elements can be seen and do some conditional rendering logic, and that won't be ready until all children are rendered).

@AlexxNB
Copy link
Member

AlexxNB commented Sep 28, 2021

Svelte even has await tick() I could use for this if it wasn't already default to wait for full render, but I don't think Malina has anything like that.

You can use await $tick() (no need to import in component or import it from malinajs/runtime.js when use it in external function.

@taw
Copy link
Author

taw commented Sep 28, 2021

Oh so there is a $tick? Might be a good idea to add it to the docs.

Weirdly this solves the original example, but this doesn't work in Malina repl.

App.xht:

<script>
let output = ""
async function action(el) {
  await $tick()
  output = el.innerText
}
</script>

<div use:action>
{#each [1,2,3,4] as i}
  <Comp {i} />
{/each}
</div>

DIV CONTAINS: {output}

Comp.xht:

<script>
export let i
</script>
{i}

So maybe my theory that tick would be sufficient was wrong. Any other suggestions?

@taw
Copy link
Author

taw commented Sep 28, 2021

Just tried another thing. onMount behaves like use: + await tick(). It waits for render of component's own stuff, but it does not wait for its children to render.

@lega911
Copy link
Member

lega911 commented Oct 1, 2021

fixed: https://malinajs.github.io/repl/#/share/hYh6qd8eQ2K?version=0.6.45
$tick works different way, I will fix it later

@lega911 lega911 closed this as completed Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants