I do not know if this is a stupid question but I just learned about stop propagation and am still getting used to how they work.
I am using a Headless UI dialog element, but I do not want key events to occur in the background. For example, I have made a program that updates the count whenever you press the "a" key. However, when the dialog popup shows up, I do not want the count to update in the background. I have added e.stopPropagation in the Dialog element to prevent key down events. It kind of works because the button is focused when you open the modal. When the button loses focus, the problem happens again and "a" will update the count.
Here is that implementation: https://codesandbox.io/p/sandbox/ecstatic-grass-896xzd
I have another count that updates whenever you press the escape key. There is a conflict because the escape key also hides the dialog, but I do not want the count to update when this happens.
I think I found a similar problem here and I tried to transfer that solution to my own problem, I do not know if I did it wrong or if the problems are comparable: Prevent event propagation on row click and dialog in material ui
I do know of a solution that puts the Dialog element "outside", like this: https://github.com/facebook/react/issues/11387#issuecomment-340019419
I am fine with this solution, but I am still curious if there is something more convenient.
in
handleKeyPress
you need to check if the modal is open. If it is, return and do not proceed further.to be able to check if the modal is open in
KeyPressComponent
, moveisOpen
above toApp
and pass it down to both components as props.demo: https://livecodes.io/?x=id/84rcv8trn5k