USWDS - Date Picker: prevent focusout out-of-order issue in safari #6450
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes Safari issues where clicking the calendar button a second time would not close calendar.
Breaking change
Related issue
Closes #6449
Problem statement
The Date Picker in Safari doesn't work properly when trying to close the calendar via the calendar button.
Solution
Sets a "preventFocusOut" flag during the mousedown event to capture the event prior to focusout or click.
The order is as follows in Safari from my testing:
Adding the mousedown event listener allows for the ability to intercept the click on the button before focusout is fired.
In Chromium browsers/Firefox, the order of events is click and then focusout, which is why those browsers don't have issues. The click toggles the calendar off, and the focusout will try to hide the calendar again, which is fine since it's already closed.
Testing and review
The date picker should be tested with all browsers that clicking the calendar button once opens the calendar, and clicking it again closes it. Clicking outside of the element should close the calendar as well.
It didn't seem as though the calendar button was keyboard accessible, so I'm wondering if there is anything to be done in that regard.