question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dropdown: When clicking to close a dropdown, it fires "click" and "blur" events

See original GitHub issue

This issue happens when controlling manually (via states) the status of a dropdown.

Steps

If you click the “Not ok button” to close the drop-down you will notice that it triggers the click and blur events (It shouldn’t fire blur cause I’m still inside the component).

Now, if you click the “ok button” repeatedly, you will notice that only the click event is fired, regardless if the dropdown is opening or closing, and the blur happens only when you actually click outside of the related button, as expected.

Expected Result

When clicking the “not ok button” to close a drop-down, it should fire only a click event.

Actual Result

When clicking the “not ok button” to close a drop-down, it fires a click and blur event.

Version

0.78.3 OS: High Sierra

Testcase

https://codesandbox.io/s/3q781mpvw1

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
willb335commented, May 7, 2018

I deleted

if (!hasSearchFocus) {
    this.ref.blur()
  }

from Dropdown.js and it fires only the click event. Also, #627 seems to be unaffected. I’ll write some tests and issue a pull request

1reaction
levithomasoncommented, May 11, 2018

Thanks for the report and test case. Here’s our problem in Dropdown.js, the close handler is explicitly calling blur

handleClose = () => {
  debug('handleClose()')
  const hasSearchFocus = document.activeElement === this.searchRef
  const hasDropdownFocus = document.activeElement === this.ref
  const hasFocus = hasSearchFocus || hasDropdownFocus
  // https://github.com/Semantic-Org/Semantic-UI-React/issues/627
  // Blur the Dropdown on close so it is blurred after selecting an item.
  // This is to prevent it from re-opening when switching tabs after selecting an item.
  if (!hasSearchFocus) {
    this.ref.blur()
  }

  // We need to keep the virtual model in sync with the browser focus change
  // https://github.com/Semantic-Org/Semantic-UI-React/issues/692
  this.setState({ focus: hasFocus })
}

I believe the culprit is #627 (see inline comments in code above). Rather than blurring the Dropdown to prevent the issue noted, it seems we should be detecting when the browser tab is switched back and not reopen.

I’d investigate which methods are being called when the browser tab is switched away and back. That process needs to not re-open the Dropdown AND we should retain its focus.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Need to fire an event when a select menu closes or hides ...
I have the following dropdown select menu. I am able to fire event onChange or onBlur. But I want to be able to...
Read more >
X-On
In the above example, after showing the dropdown contents by clicking the "Toggle" button, you can close the dropdown by clicking anywhere on...
Read more >
Element: blur event - Web APIs | MDN
The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does...
Read more >
onblur Event
The onblur event occurs when an element loses focus. The onblur event is often used on input fields. The onblur event is often...
Read more >
Events - Action Commands
Firing the focus or click event on the input form field may cause the drop-down box to open, but the blur event may...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found