mattjennings / svelte-modals

A simple, flexible, zero-dependency modal stack manager for Svelte.
https://svelte-modals.mattjennings.io
154 stars 8 forks source link

Emit events to parent from modal opened using openModal()? #9

Closed hardingjam closed 2 weeks ago

hardingjam commented 2 years ago

Is it possible to dispatch events from a modal back to the component that called openModal()?

For example:

    const handleClick = () => {
        openModal(ListingModal, on:eventDisaptch={handleEvent});
    };
mattjennings commented 2 years ago

Unfortunately no, you'll have to use callback props to communicate back up. See this example: https://svelte.dev/repl/2f2d171a9ea7458191904cb2feb5a54c?version=3.38.2

I'm open to figuring out some way to handle events like this though, as that would be much more svelte-y.

daevid66 commented 1 year ago

Hey, started to use svelte-modals (thanks for creating this!), and while I think the callback method works fine, I think it would be possible to add event listeners by adding them to $on, in a similar same way as in this example: https://svelte.dev/repl/5b495a6d61e64d0cabdb3657f100837c?version=3.18.2

Not sure if it's possible to infer the types that way, though.

mattjennings commented 1 year ago

@daevid66 oh this is very interesting, I'll definitely take a look at this approach!

mattjennings commented 2 weeks ago

Svelte 5 now handles events with callback props, so nothing to do here.