Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Gatsby, in Firefox, all click events close calendar #274

Closed
glen0071 opened this issue Jun 5, 2020 · 11 comments
Closed

In Gatsby, in Firefox, all click events close calendar #274

glen0071 opened this issue Jun 5, 2020 · 11 comments
Assignees
Labels
bug Something isn't working stale

Comments

@glen0071
Copy link

glen0071 commented Jun 5, 2020

When react-date-picker is used in a Gatsby app within a Firefox browser all click events (whether on the calendar days or the navigation arrows at the top) all close the calendar instantly. When I click on a day, the calendar closes without inputting a date. All click events seem to trigger react-date-picker--open class to change to react-date-picker--closed before anything else can happen.

To reproduce, spin up a new gatsby project and drop the datepicker in anywhere. I did not see this problem with a create-react-app.

If I mount the react-date-picker component on the DOM via ReactDOM, this fixes the problem, but that doesn't seem like a good workaround in my use case.

@wojtekmaj
Copy link
Owner

Sorry for the delay. Been super busy in commercial projects.

Found the reason. The reason is that we have a method, onOutsideClick.

onOutsideAction = (event) => {
if (this.wrapper && !this.wrapper.contains(event.target)) {
this.closeCalendar();
}
}

If the method detects a click outside React-Date-Picker's wrapper, it closes the calendar. The thing is, in Gatsby, clicking on the calendar causes focusin to be triggered on #gatsby-focus-wrapper element. This is detected as a focus event outside React-Date-Picker and thus is closing the calendar.

We could do this awful, awful hack:

import React, { useEffect, useRef, useState } from "react"
import DatePicker from "react-date-picker"

import Layout from "../components/layout"

function MyApp() {
  const [value, onChange] = useState();
  const datePicker = useRef();

  useEffect(() => {
    if (!datePicker.current) {
      return;
    }

    // Replace DatePicker's onOutsideAction with our own that takes Gatsby's focus wrapper into account

    const _this = datePicker.current;

    _this.onOutsideAction = (event) => {
      if (
        _this.wrapper
        && !_this.wrapper.contains(event.target)
        // This is the line that fixes it
        && event.target.id !== 'gatsby-focus-wrapper'
      ) {
        _this.closeCalendar();
      }
    }
  });

  return (
    <DatePicker
      ref={datePicker}
      onChange={onChange}
      value={value}
    />
  );
};

export default MyApp;

When you're done vomiting, it should work just fine :D

@wojtekmaj wojtekmaj changed the title in Gatsby, in Firefox, all click events close calendar In Gatsby, in Firefox, all click events close calendar Jun 26, 2020
@wojtekmaj wojtekmaj self-assigned this Jun 26, 2020
@wojtekmaj wojtekmaj added the bug Something isn't working label Jun 26, 2020
@pedrofsantoscom
Copy link

pedrofsantoscom commented Aug 27, 2020

#15 (comment)
The fix above didn't worked for me. I'm using v8.0.1

@wojtekmaj
Copy link
Owner

wojtekmaj commented Aug 27, 2020

I suggest placing a breakpoint in line with event.target.id !== 'gatsby-focus-wrapper' and investigate what was broken b y the development environment you use. Perhaps Gatsby is not adding this ID anymore, or used a different one...

@glen0071
Copy link
Author

@wojtekmaj I'm no longer working on the project that was running into this bug. I think your fix worked, but I never finished up the PR there before moving onto other features. I gave them a link to this Issue in case they want to implement it themselves, but otherwise you could close this issue if you want.

@pedrofsantoscom
Copy link

@wojtekmaj on my end, setting the condition as event.target.id !== '' makes the calendar work without closing itself.
i'll be testing it further but it seems it's working fine this way.

@pedrofsantoscom
Copy link

@wojtekmaj The issue happens when the date picker is inside the MUI Dialog, this isn't related with gatsby. Check the demo below:
https://codesandbox.io/s/react-date-picker-forked-1ee7e?file=/index.js

You have the normal working date picker on the page, and then you have the date picker inside the MUI Dialog. Click the button, open the calendar, click on the "month" to select the month on the month view:
image
then click on any month to see the issue happening.

Any idea what is happening here?
image

@pedrofsantoscom
Copy link

Adding disableEnforceFocus={true} to Material UI Dialog fixed it:

import { Dialog } from '@material-ui/core'

<Dialog disableEnforceFocus={true} ></Dialog>

@marcoancona
Copy link

Facing a similar issue with a custom calendar implementation. Maybe a shouldCalendarClose optional prop that, given the event triggering the close, can return true/false to change the behavior?

@wojtekmaj
Copy link
Owner

Good idea, I think it's worth implementing.

@github-actions
Copy link
Contributor

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this issue will be closed in 14 days.

@github-actions github-actions bot added the stale label Nov 15, 2021
@github-actions
Copy link
Contributor

This issue was closed because it has been stalled for 14 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

4 participants