-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Attachment using createAttachmentKey
re-runs when other spread state changes
#16044
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
Comments
createAttachmentKey
re-runs when other state changescreateAttachmentKey
re-runs when other spread state changes
You create a new function in the derived, so it definitely will re-attach. But the issue is real. |
Then it should be mentioned in the docs |
or could |
You may want to create multiple attachments. But talking about not re-attaching fn even the key changes, wouldn't it be more convenient? |
it seems you need both the key and the fn body to be external to the const buttonAttachment = {
[createAttachmentKey()]: (node: HTMLElement) => {
console.log('createButtonAttachment', faq.id)
buttonNode = node;
on(node, 'click', () => expanded = !expanded)
on(node, 'focus', () => node.classList.add('focus'))
on(node, 'blur', () => node.classList.remove('focus'))
}
}
const button = $derived({
type: 'button' as const,
'aria-expanded': expanded,
'aria-controls': `faq-${faq.id}`,
...buttonAttachment,
}) |
Mh i don't know if that would work but I think is worth exploring. @Conduitry will reopen this since I think documenting this could still be worth it, I'll try to see if it's fixable, if it's not will document |
So this is doable...however I think it will have a performance impact both because we need to call I'll open a PR and we can discuss. |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
Looks like a similar issue to #15949 but if other spread props are updated the attachment still re-runs.
I would expect to be able to attach behaviour to an element using
createAttachmentKey
while also setting attributes on the element (such as wai-aria).Reproduction
https://svelte.dev/playground/542b5db158e148feafde1451c6cbd900?version=5.33.10
(click button, notice console log output showing attachment re-runs)
Logs
System Info
Severity
blocking an upgrade
The text was updated successfully, but these errors were encountered: