melt-ui icon indicating copy to clipboard operation
melt-ui copied to clipboard

🌟Feature request: Expose `handleClose` & `activeTrigger` from `createDialog`

Open maarekj opened this issue 1 year ago • 4 comments

To improve extensibility, it would be nice to expose some functions/properties.

For example, the handleClose function of createDialog can be very useful when you want to manage the closing of a modal in a way that is not expected by melt-ui, but while taking advantage of focus management during closure.

Same with the activeTrigger variable of createDialog.

Additional context For example, I have a dialog with inputs. I want to have fine management of when I close the modal (when the user validates the input with Enter, and after having done some verification on the server, I close the dialog).

Today I cannot easily restore focus to the trigger that opened the dialog. Since I don't have access to activeTrigger, nor handleClose

Describe alternatives you've considered

In the case of createDialog, we could have, as in other builders, helpers.

  • helpers.handleOpen
  • helpers.handleClose

And add activeTrigger in the states.

maarekj avatar Feb 06 '24 09:02 maarekj

Thank you for your library, it's very good quality work, and very pleasant to use

maarekj avatar Feb 06 '24 09:02 maarekj

How do we want to approach this @TGlide?

handleClose is simple to expose.

handleOpen is a bit different since it's used as an event handler and sets the activeTrigger to the event's currentTarget.

I think it's a good idea to expose both, we just have to ensure that bit is documented.

Let's say we expose these two functions, what would the use-case then be to return the activeTrigger as part of states?

huntabyte avatar Mar 03 '24 19:03 huntabyte

what would the use-case then be to return the activeTrigger as part of states

To set it when doing handleOpen, I assume.

Alternatively, we can use effects and trigger the open and close side-effects when doing so, and just expose activeTrigger so that it can be modified. Or not expose it, since we have openFocus and closeFocus

TGlide avatar Mar 03 '24 19:03 TGlide

do we actually need to expose all these additional things? It seems from the example provided that instead of focusing the trigger in handleClose, we should focus the trigger in effect(open) when open is false (and make sure to skip the first mount).

anatolzak avatar Mar 12 '24 12:03 anatolzak