-
-
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
feat: attachments (alternative syntax) #15045
Conversation
Co-authored-by: Leonidaz <[email protected]>
🦋 Changeset detectedLatest commit: 0d7f022 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
preview: https://svelte-dev-git-preview-svelte-15045-svelte.vercel.app/ this is an automated message |
|
This seems like a bad idea, because:
|
Yeah i'm sorry you did the work to revert stuff and add the new syntax but this is not the right idea. Thanks for contributing anyway. I'm gonna close this 😄 |
@Ocean-OS @paoloricciuti That's not true. This is similar to how event listeners work. By convention, all props beginning with “on” are event listeners, but you don't have to follow that convention. https://svelte.dev/playground/b71cb484016143ea8a0146b9cd220117?version=pr-15045
Because they are not. That's the point :)
I feel like most of the discussion in #15000 is about syntax. The most recent comments are about changing syntax to use a different character. I wanted to make a proposal too, but since it's a bigger change, I actually implemented a POC to make sure I don't waste anybody's time with something that wouldn't work.
You're already attaching event handlers with plain attributes like |
This PR has a good point. I'll make the floatiing-ui POC using this PR.... |
Btw |
Agreed. This works because whatever property name we choose ("attachments" currently) is not an existing attribute name in any HTML element in existence right now. What if it appears in the future? But, @paoloricciuti, isn't this the same with events? They are set up by name convention currently. What makes events different? I'm having a hard time pinpointing the difference. |
Events are actually already attributes on html element and they are basically used to do the same thing. If tomorrow a brand new browser api comes out and |
Hmm, ok. What about |
@paoloricciuti That's a valid point, but I think there is a solution to that. Due to how HTML works, all HTML attributes must be strings, and attachments must be JS functions. If such attribute was added to HTML, we could check |
Technically not even the |
The <div {@attach thing}></div> ...would be interpreted by the browser as... <div {@attach="" thing}=""></div> And lambdas would be even worse: <div {@attach="" (node)="" =="">{...attachment stuff...}></div> <!--something like that--> |
Well, if Maybe Svelte can start reserving property names that start with "@"? For future |
Alternative to #15000, with simplified syntax.
In Svelte 5, event handler syntax was changed from special
on:
directives to normal props, which allowed them to be forwarded from a component in more explicit and understandable way, while also simplifying the syntax. I think the same treatment can be applied to actions/attachments:This syntax makes some edge cases mentioned in discussion, as #15000 (comment), trivial to solve:
It also makes it easier to correctly type props of a component that accepts attachments (the
[key: symbol]
syntax isn't super common, I wouldn't be surprised if some devs weren't familiar with it):Since this version doesn't introduce new syntax, it will require no changes to language tools and Prettier / ESLint plugins.
Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
pnpm test
and lint the project withpnpm lint