Skip to content
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

Event Handling #18

Closed
MentalGear opened this issue Aug 15, 2022 · 8 comments
Closed

Event Handling #18

MentalGear opened this issue Aug 15, 2022 · 8 comments

Comments

@MentalGear
Copy link

MentalGear commented Aug 15, 2022

Thank you for this great library! It's much more idiomatic than svelte-simple-modal!
However, there's no mention of event handling, and the standard svelte way doesn't seem to work.

Is your feature request related to a problem? Please describe.
I try to dispatch an event from my modal component to the <Modals on:message={handleModalDialogButton}> tag in the overlying component.

Describe the solution you'd like
Ideally, the normal svelte event dispatch would work out of the box.

Describe alternatives you've considered
Using a store.

@mattjennings
Copy link
Owner

Unfortunately the only way to pass data up is through callback props. It is indirectly shown in the examples for the Managing your Modals section, but as also mentioned in #7, I should have a more clear page explaining this.

If svelte ever supports a way to forward all events up this would be an easy feature to add. But until that day comes (if it does), callback props are the only way for now.

@MentalGear
Copy link
Author

MentalGear commented Aug 15, 2022 via email

@mattjennings
Copy link
Owner

Could you elaborate on what you mean?

@MentalGear
Copy link
Author

MentalGear commented Aug 17, 2022

Yeah, a pity with event forwarding. Gone is the ease and simplicity when trying to open multiple modals. :/
There's also a whole spec discussion going on in: sveltejs/rfcs#60

I was wondering if it would be possible to forward an event (or global event object), if you were to simply on:message on all intermediary components of your Modals system.

A bit like the svelte example were an inner elements forwards an event with on:message to the topmost level.
https://svelte.dev/tutorial/event-forwarding

@mattjennings
Copy link
Owner

Can you share your use case for wanting to handle events in <Modals />? IMO it doesn't make sense to have event handlers there, they should be on a per-modal basis. In which case #17 would be a good solution until there's a way to provide event handlers at the time of openModal.

@mattjennings
Copy link
Owner

I might have a solution here, should have a PR this week. It requires creating a custom event dispatcher, but it would allow you to handle events like so:

openModal(MyModal, props, {
   on: {
        message: (event) =>  { ... }
   }
})

The modal would have to dispatch the event using a custom dispatch method, but effectively works the same as normal event dispatching:

<script>
import { createModalEventDispatcher } from 'svelte-modals'

const dispatch = createModalEventDispatcher()

dispatch('message', { ... })
</script>

@mattjennings
Copy link
Owner

you can try it here https://svelte.dev/repl/40c3ee5b548f4c75bacf88b088259c66?version=3.49.0

@MentalGear
Copy link
Author

This is great, thank you so much @mattjennings for this and your overall work on my favorite modal solution for svelte! (and sorry for the late reply!)

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

No branches or pull requests

2 participants