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

BUG: click outside <Dialog /> should be allowed #170

Open
rreckonerr opened this issue Nov 16, 2022 · 6 comments
Open

BUG: click outside <Dialog /> should be allowed #170

rreckonerr opened this issue Nov 16, 2022 · 6 comments

Comments

@rreckonerr
Copy link
Contributor

rreckonerr commented Nov 16, 2022

Usecase

I'm working on adding notifications to the app, and it should be possible to click/close the notification without closing the current <Dialog />

Problem

At the moment any click outside <Dialog /> will trigger @onclose. The only exception is child dialogs spawned by the modal (handled by dialog-stack-provider service).

This behavior used to be the same in @headlessui/[email protected], but since @headlessui/[email protected] it's possible to click outside the dialog without triggering the @onclose action IF the "outsider" is wrapped with headlessui <Portal /> component.

Workaround

As a workaround, I extended the dialog.js component as follows

import { action } from '@ember/object';
import EmberHeadlessUiDialog from 'ember-headlessui/components/dialog.js';

export default class extends EmberHeadlessUiDialog {
  @action
  allowOutsideClick(e) {
    const target = e.target;

    if (target && target.tagName !== 'BODY') {
      this.outsideClickedElement = target;
    }

    // 👇
    // this.onClose();

    return true;
  }
}

Resources

@far-fetched
Copy link
Contributor

Hello @rreckonerr 👋

So you expect that clicking on blue box it will keep modal opened ?
E.g. in a scenario when we use external lib which appends toast as last body element

simplescreenrecorder-2022-12-16_10.10.01.mp4

@far-fetched
Copy link
Contributor

far-fetched commented Dec 16, 2022

I found a relevant test which you described:

...without triggering the @onclose action IF the "outsider" is wrapped with headlessui component.

https://github.com/tailwindlabs/headlessui/blob/main/packages/%40headlessui-react/src/components/dialog/dialog.test.tsx#L1097

Looking at what others folks in topic said, I am not sure that this would be a solution for you issue 🤔
Imagine that you cannot wrap Toast component in .hbs template and someone just calls toast in .js class e.g.

...
this.toastService.success('message...');
...

And magic goes inside service provided by 3th lib. Then still it will close dialog when clicking on toast box 🤔

Your workaround will fail with this test:
https://github.com/GavinJoyce/ember-headlessui/blob/master/test-app/tests/integration/components/dialog-test.js#L478

as we control visibility of modal by passing isOpen and onClose cb, what about passing onClose more adjusted to your needs:

onClose(event) {
   if (event.doesNotComeFromYourToastBox) {
      this.isModalOpen = false;
   }
}

Any chance does it work ?

@rreckonerr
Copy link
Contributor Author

Hi, @far-fetched!
Thanks for your investigation, it totally makes sense. Yeah, passing the click event to onClose should work just fine!

@roomman
Copy link

roomman commented Apr 4, 2023

I also have a use case that requires the "click outside" to be disabled for specific events. The event isn't currently being passed to the onClose action. @far-fetched would you accept a PR to add the event as an overload?

@far-fetched
Copy link
Contributor

According to the Official API it does not pass event in callback either.

Can you show your example (might be simplified) in some sandbox code ? Maybe we can resolve it differently.

@roomman
Copy link

roomman commented Apr 4, 2023

Can you show your example

Yes, I'll put something together, thanks. I'm away for a few days now.

It's a very basic implementation that renders a form. One of the form inputs is a select that renders its options into a portal. When you select an option it closes the modal prematurely. The only way I can see to prevent this is to check the event - as you suggested above - but I don't have access to it.

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

3 participants