Try to solve it as follows:
When the user clicks on the file selection input, set its focus: $(el).focus()
.
Then, anytime the user presses ESC, see if the $ (': focus') element is a file selection element. In this case, blur () introduces and does not close the modal. Worst case - the user wants to close the modal, presses ESC and nothing happens [1]. Thinks "wtf", presses ESC again and closes modally, as it should. Just make sure that the input for selecting the file is focused in all possible cases - using tabs, etc. If you use a third-party loader, and what I said does not work - look how this loader wraps the file selection in a custom link or button and what actually receives the click event in different cases (for example, when you insert a tab, you can get an event when you click, it could be a link). All in all, it is possible for this to work with the caution I described.
This works with extended ones that are too similar (just make sure the selection is not focused when ESC is pressed).
You will not be able to detect all cases when you need blur()
input to select a file. This is not a cross browser solution. Even FF needs adjustments to work. I tested webkit with positive results, in other browsers it may not work.
source share