Skip to content

useEventListener doesn't work with window 'message' events from iframes #8126

@daedalus28

Description

@daedalus28

Dependencies check up

  • I have verified that I use latest version of all @mantine/* packages

What version of @mantine/* packages do you have in package.json?

8.2.2

What package has an issue?

@mantine/hooks

What framework do you use?

Vite

In which browsers you can reproduce the issue?

All

Describe the bug

I'm attempting to migrate from usehooks-ts to @mantine/hooks (I was directed here from this comment which seems like many devs are migrating here from usehooks-ts: juliencrn/usehooks-ts#675 (comment))

useEventListener doesn't seem to support window message events. I'm using useEventListener currently to support an iframe embedding use case where the frame content posts messages back to the parent (in my case, to embed Metabase and be notified of changes that happen within it)

The mantine implementation is limited to HTMLElementEventMap:
https://github.com/mantinedev/mantine/blob/dd33a2c6fc4c9c5b4c72fcf8e2b8cc4ad9b698a1/packages/%40mantine/hooks/src/use-event-listener/use-event-listener.ts#L3C50-L3C69

The usehooks-ts implementation can handle window events, svg events, and more:
https://github.com/juliencrn/usehooks-ts/blob/master/packages/usehooks-ts/src/useEventListener/useEventListener.ts#L77-L79

It seems like a bug to me, but if this isn't something the mantine team has any desire to implement, feel free to close this 👍

If possible, include a link to a codesandbox with a minimal reproduction

No response

Possible fix

Don't restrict to only HTMLElementEventMap and support window events.

Self-service

  • I would be willing to implement a fix for this issue

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