Hacker0x01 / react-datepicker

A simple and reusable datepicker component for React
https://reactdatepicker.com/
MIT License
7.89k stars 2.23k forks source link

Fix #4844: 🔨 Retain the open state of the calendar popup on document visibility change #4878

Closed balajis-qb closed 3 weeks ago

balajis-qb commented 3 weeks ago

Closes #4844

Description

Retain the open state of the calendar popup on document visibility change.

Problem As mentioned in the issue description, the calendar gets auto-reopen whenever we switch the browser tab and switch back to the original tab in any of the below 2 cases

  1. Switching the browser tab after we enter the date manually into the calendar input by dismissing the calendar popup
  2. Switching the tab immediately after selecting any date via calendar popup (In this case, we could have blurred focus from the input, but when I inspect the code, we intentionally refocus the calendar input, upon selection of any date from the calendar popup as a fix for the issue https://github.com/Hacker0x01/react-datepicker/issues/628)

In any of the above cases the input is focused while switching the browser tab and when we switch back to the original tab, the browser refocus the calendar input, which will trigger our handleFocus handler in our code and we open the calendar popup regardless of it's previous open state

Changes As a fix, I listened for the visibilitychange event and note whenever the tab gots hidden. Whenever the input gets refocused, if it gets auto re-focused because of the browser visibilitychange, I used the previous open state to decide whether to re-open the calendar popup or not.

Basically the solution I suggested helps to retain the previous open state of the calendar popup whenever the browser gets re-opened.

Contribution checklist

codecov[bot] commented 3 weeks ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 96.73%. Comparing base (8e7dbce) to head (59a42e3).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #4878 +/- ## ========================================== + Coverage 96.72% 96.73% +0.01% ========================================== Files 28 28 Lines 2990 3004 +14 Branches 1293 1293 ========================================== + Hits 2892 2906 +14 Misses 98 98 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.