-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat: attachments #15000
Open
Rich-Harris
wants to merge
19
commits into
main
Choose a base branch
from
attachments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+365
−18
Open
feat: attachments #15000
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
531e75d
parse attachments
Rich-Harris b29e1e3
basic attachments working
Rich-Harris 2329284
working
Rich-Harris 0c914eb
rename to attach
Rich-Harris 1988ba4
fix
Rich-Harris e1b940c
restrict which symbols are recognised as attachment keys
Rich-Harris ed3bf01
merge main
Rich-Harris 0690ba2
allow cleanup to be returned directly
Rich-Harris e0620a1
changeset
Rich-Harris 2ae3aa0
fix
Rich-Harris 7046427
lint
Rich-Harris 85cc9bc
remove createAttachmentKey/isAttachmentKey
Rich-Harris bec5708
fix spreading of symbol properties onto component
Rich-Harris afab150
types
Rich-Harris 7e5d4d9
fix
Rich-Harris c599e90
update name
Rich-Harris 8699771
reserve ability to use sequence expressions in future
Rich-Harris 1664fd8
Update packages/svelte/src/internal/client/dom/elements/attachments.js
Rich-Harris 6402161
actually let's do this instead
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': minor | ||
--- | ||
|
||
feat: attachments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/svelte/src/compiler/phases/3-transform/client/visitors/AttachTag.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** @import { Expression } from 'estree' */ | ||
/** @import { AST } from '#compiler' */ | ||
/** @import { ComponentContext } from '../types' */ | ||
import * as b from '../../../../utils/builders.js'; | ||
|
||
/** | ||
* @param {AST.AttachTag} node | ||
* @param {ComponentContext} context | ||
*/ | ||
export function AttachTag(node, context) { | ||
context.state.init.push( | ||
b.stmt( | ||
b.call( | ||
'$.attach', | ||
context.state.node, | ||
b.thunk(/** @type {Expression} */ (context.visit(node.expression))) | ||
) | ||
) | ||
); | ||
context.next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/svelte/src/internal/client/dom/elements/attachments.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { effect } from '../../reactivity/effects.js'; | ||
|
||
/** | ||
* @param {Element} node | ||
* @param {() => (node: Element) => void} get_fn | ||
*/ | ||
export function attach(node, get_fn) { | ||
effect(() => { | ||
return get_fn()(node); | ||
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/svelte/tests/parser-modern/samples/attachments/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div {@attach (node) => {}} {@attach (node) => {}}></div> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an interesting one, as we probably want the users function to be non-tracking like we have with actions. Otherwise any reads inside their attachment function will cause it to re-run over and over.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the whole point, no? It re-runs when the attachment changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's when the attachment changes and when something inside the attachment changes. I don't think we want the attachment to re-run if something inside the attachment function changes. People should create their own
$effect
inside the attachment to handle reactivity.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not obvious to others either it seems: #15000 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is explicitly one of the flaws of actions that attachments are designed to overcome. If I had to write the tooltip example like this...
...I would be very unhappy. (And in fact it's not equivalent, because if
tooltip
itself changed, nothing would happen.) It would cause much more broken code, not less.I don't think people should be setting state directly inside attachments any more than they should be setting state inside normal effects. But if they really need to for some reason,
untrack
exists to prevent it from causing an infinite loop.