Skip to content

Use of deprecated keyCode event prop for Overlay close detection #1011

@cjpmedius

Description

@cjpmedius

Describe the bug

The useRootClose hook relies on the deprecated keyCode event prop to trigger the escape key close behaviour.

Certain modern testing setups (such as v14 of @testing-library/user-event) no longer populate this prop, so tests of the close behaviour will fail.

To Reproduce

Steps to reproduce the behavior:

  • Render an Overlay control in a jest test, setting open=true, rootClose=true, and adding onHide event handler
  • using @testing-library/user-event, call userEvent.keyboard("{Escape}")
  • onHide event handler is not called.

Expected behavior

onClose handler is called.

Additional Info

While I appreciate this is not a bug in some respects, since browsers still populate the keyCode prop, it should be possible to amend this code to allow for both legacy and modern compatibility.

e.g. amend line 81 of src/useRootClose.ts
if (e.keyCode === escapeKeyCode) {
to
if ((e.keyCode && e.keyCode === escapeKeyCode) || e.key === "Escape") {

This issue is related to a previous change to the same code to solve an IE11 compatibility issue:
#211

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions