Git Product home page Git Product logo

Comments (3)

Quernest avatar Quernest commented on August 16, 2024 1

How can I close dialog on back button pressed? and prevent back action in navigator?

Not the best way to use modals and this question is more related to StackOverflow 🙂

For a native solution, you can use popstate event or beforeunload I think
https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event

window.addEventListener('beforeunload', (event) => {
  event.preventDefault();
  event.returnValue = '';

  // close your modal
  modal.close();
});

OR

If you are using react-router & history check
https://medium.com/@subwaymatch/disabling-back-button-in-react-with-react-router-v5-34bb316c99d7


⭐️ The main idea is to catch this event, cancel it, and at that moment close the modal window using modal.close();

from mui-modal-provider.

Quernest avatar Quernest commented on August 16, 2024 1

@Quernest maybe we can add wrapper function for showModal function in the useModal hook. This wrapper will track modal ids which were created in some component. And in useModal hook we will use useEffect to find out when component which created some modals is destroyed. And when it is destroyed we can use destroyModal to close modals created by this component?

Because if we do not close modals manually when browser back button it is pressed and some component which created modals is destroyed we can have different issues.

Great idea, I'll do it soon, thanks!

from mui-modal-provider.

pavelspichonak avatar pavelspichonak commented on August 16, 2024

@Quernest maybe we can add wrapper function for showModal function in the useModal hook. This wrapper will track modal ids which were created in some component. And in useModal hook we will use useEffect to find out when component which created some modals is destroyed. And when it is destroyed we can use destroyModal to close modals created by this component?

Because if we do not close modals manually when browser back button it is pressed and some component which created modals is destroyed we can have different issues.

from mui-modal-provider.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.