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

Uncaught TypeError: anchor.addEventListener is not a function. #1976

Open
ZeraiGR opened this issue Dec 2, 2024 · 0 comments
Open

Uncaught TypeError: anchor.addEventListener is not a function. #1976

ZeraiGR opened this issue Dec 2, 2024 · 0 comments

Comments

@ZeraiGR
Copy link

ZeraiGR commented Dec 2, 2024

Gravity Tooltip is crashing, when it's children is a legacy class react component.

This is because we have only one check inside useTooltipVisible hook to ensure that anchor exist.

But anchor may exist but not to be instance of HTMLElement and in this case application will be crashed.

My offer to handle this case is the following:

  1. If the legacy class react component have a ref, we can try to resolve it inside setRef helper. It may be written something like this:
export function setRef(ref, value) {

    if (!(value instanceof HTMLElement)) {
        // try to extract ref from legacy class react components

        if (value?.ref?.current instanceof HTMLElement) {
            value = value.ref.current;
        }
    }

    if (typeof ref === 'function') {
        ref(value);
    }
    else if (ref) {
        ref.current = value;
    }
}

Of course it should be refactored, may be moved to separate helper (in separate file) to resolve value for ref.

  1. If the legacy class react component haven't a ref. We shouldn't crash the app, but print warning. It can be implemented inside useTooltipVisible hook, something like this:
if (!(value instanceof HTMLElement)) {
       console.warn("Tooltip won't be work! Because it's children can't be resolved to html element, try to wrap your component inside <div>...<div>") // the text of error just for example. It also needs to be corrected.
       return undefined;
    }

Repo for reproduce the error: https://github.com/ZeraiGR/gravity-tooltip-issue-minimal-example-to-reproduce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant