-
-
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: dynamic event bindings #6876
feat: dynamic event bindings #6876
Conversation
Could you explain what the use case for this feature is and give some examples? |
@dummdidumm yeah sure... OverviewAt the company I'm working for, we are developing a dynamic UI/UX system to read data from a meta data system and data engine. We also have data roles/permissions/policies which provides a map of what the user can do with the data. This presents a few problems.
Typically when you build anything dynamically the process of deriving the structure of the UI and the data and event flow means that everything is loaded just in time. The link between the parent/child components is done using a container component which maps the props, events and loads and get's needed for the child component to load. Senario/ExampleFor example an admin might use DataEditAdmin.svelte and a normal user might use DataEditStd.svelte. DataEditAdmin.svelte has access to properties on the metadata system that the Std component does not. If the Admin user modifies their layout, those admin components broadcast events which the standard components do not in order to capture the modified child layout. If svelte can do Code exampleWhen we click the window, we want to show some options... We don't know what those options are or will do, we also don't know what component those options are built from...but when we click one of them, we need to be able to pass this event into the container component so we can handle it. reusableFunction.ts
AdjustmentsContainer.svelte
InteractableListItem.svelte
|
Thanks for explanation! So this is kind of the other end of #4599 . I'm not sure about the API proposal but I definitely agree that this makes sense to be able to react to arbitrary events. If #4599 would be implemented as |
Precisely :) During dev this morning tho, this method does present some problems. If I want data inside my handled event, if the functionality is dynamically imported this presents some interesting architecture challenges. I would also assume that, passing a spread operator is always preferable to on:* - I can't really think of a time where I might want on:* if you could just pass config in. This might be useful for dev, to see which events are being broadcast but if you want to handle all events, you can simply pass in a * into the event config. So here I'm working on the basis that my itemListClick handler is imported dynamically, we don't want to pollute the CustomEvent. Ive looked at the use cases for on:* Personally, I feel on: is special and Id downvote the on:* because it has some checking and consistency. CustomEvents however handled like this allow the dev to bypass the safety of sveltes event checking. If you take this example...
Container.svelte
If you were to add on:* this would then add in bubbling and event handling when there wasn't any for the second list item. I would argue an assumption that in most cases, svelte:component is being used in a similar manner to handle multiple items and in cases where you handle a list you end up hardcoding a value into a dynamic component. This is generally a bad idea AFAIC - for DOM components, maybe it's fine but Im concerned with svelte:component here not other types of use so I might be wrong. |
…ndings to init function
You can pass in data using this method, just provide it in the binding. |
@flipkickmedia thank you so much for your work on this! Do you have any clue on whether it will be accepted and/or released soon? |
@flipkickmedia Great work on this so far. As the author of one of the duplicate issues, #7548, I just had one question— Allowing event keys to be included in JSON data as the string counterparts of their literal spelt notation, it certainly keeps the syntax familiar, but wouldn't is also open the door to some dangerous situations where 3rd parties could pass in "event props" and thus take control of your app to some capacity? To avoid this, a user would have to sanitize all JSON data being used for props. The two options I see are…
|
In the example, the |
Any update on the discussions @dummdidumm ? |
If you import the component The only reason to use a the <sveltecomponent ...> method is so you can dynmically load the component at runtime to make use of the meta data. If you want to pass spread props/events to a component <Test {...props} {...events}> I can't really see a good reason to do this since you are statically generating the component and therefore know what props/events you need. |
@flipkickmedia Believe me— I want a method for spreading event props into a component as much as anyone. It's why I created an issue quite like this one). However, allowing any arbitrary JSON to be spread into special event/transition props is too unsafe if always done. 3rd-party providers and packages could easily abuse this and include such props in their JSON export which would put your site at risk of an XSS-like attack. Even if you know the initial structure, there's no way to know that such props wouldn't be added later. |
I think we possibly are thinking the same thing. The checking in svelte means when you import a component using a name e.g. This process depends on validating the data at runtime to stop the things you mention. The reason for my comment about not using a spread operator when statically defining the component name is because Svelte can then do its checks when it compiles. You could use the Svelte can then ignore the checks when compiling and the event/prop data can be (and should be) validated at runtime. |
Closing as spreading events is supported in Svelte 5 — thank you |
This PR provides a means to pass a {...spread} operator with bindings so they can be handled dynamically.
It's highly likely this will need rework and some guidance. It's POC and my first contribution towards svelte.
Ill write tests once this is implemented with agreement.
test.svelte
App.svelte
Before submitting the PR, please make sure you do the following
[feat]
,[fix]
,[chore]
, or[docs]
.Tests
npm test
and lint the project withnpm run lint