Skip to content

Menu does not close when using Preact #240

Closed
@christianjuth

Description

@christianjuth

The issue

When using preact, the onBlur event does not seem to fire. I think this is related to the fact that React bubbles blur events up to the parent element, but Preact doesn't (see this issue). I think this can be easily fixed by using focusout instead of onBlur.

Example fix

This is just a rough example to demon straight my point. Obviously, I'm leaving some things out. I already implemented this change in my own project, but it would be better if it was done at the library level.

function Menu() {
  const ref = useRef(null)

  useEffect(() => {
    const container = ref.current

    const handleClose = (e) => {
      // Same logic as https://github.com/szhsin/react-menu/blob/6ef8d95a82af27a15023d3f5a5f035a387e9a2df/src/components/ControlledMenu.js#L109
      // ...
    }

    if (container) {
      container.addEventListener('focusout', handleClose)
      return () => {
        container.removeEventListener('focusout', handleClose)
      }
    }
  }, [])

  return (
    <ControlledMenu ref={ref}/>
  )
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions