Skip to content

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

Open
CaptainCodeman opened this issue May 30, 2025 · 8 comments · May be fixed by #16048
Open

Attachment using createAttachmentKey re-runs when other spread state changes #16044

CaptainCodeman opened this issue May 30, 2025 · 8 comments · May be fixed by #16048

Comments

@CaptainCodeman
Copy link

CaptainCodeman commented May 30, 2025

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

System:
    OS: macOS 15.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 1.68 GB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.12.0 - ~/Library/pnpm/node
    npm: 11.1.0 - ~/Library/pnpm/npm
    pnpm: 10.11.0 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 136.0.7103.116
    Safari: 18.5
  npmPackages:
    svelte: ^5.0.0 => 5.33.10

Severity

blocking an upgrade

@CaptainCodeman CaptainCodeman changed the title Attachment using createAttachmentKey re-runs when other state changes Attachment using createAttachmentKey re-runs when other spread state changes May 30, 2025
@7nik
Copy link
Contributor

7nik commented May 30, 2025

You create a new function in the derived, so it definitely will re-attach. But the issue is real.

@PatrickG
Copy link
Member

@7nik You create a new attachment key in the derived.
Works.

@7nik
Copy link
Contributor

7nik commented May 30, 2025

Then it should be mentioned in the docs

@CaptainCodeman
Copy link
Author

or could createAttachmentKey possibly return the same key based on the scope it's in?

@Conduitry Conduitry closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2025
@7nik
Copy link
Contributor

7nik commented May 30, 2025

You may want to create multiple attachments.

But talking about not re-attaching fn even the key changes, wouldn't it be more convenient?

@CaptainCodeman
Copy link
Author

it seems you need both the key and the fn body to be external to the $derived, so I'll probably go with something like this:

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,
})

@paoloricciuti
Copy link
Member

You may want to create multiple attachments.

But talking about not re-attaching fn even the key changes, wouldn't it be more convenient?

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

@paoloricciuti paoloricciuti reopened this May 30, 2025
@paoloricciuti
Copy link
Member

But talking about not re-attaching fn even the key changes, wouldn't it be more convenient?

So this is doable...however I think it will have a performance impact both because we need to call Object.getOwnPropertySymbols on the previous values, find the function in it (and we need to do that for every attachment) and we also need to call delete effects[that_symbol] (I know delete is veeeeery slow)...that said we should gain by not recreating an effect so maybe worth it?

I'll open a PR and we can discuss.

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

Successfully merging a pull request may close this issue.

5 participants