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

[Bug]: Accessing value of sp-menu-item before render throws an error #3626

Closed
1 task done
charlessuh opened this issue Sep 6, 2023 · 5 comments · Fixed by #3717
Closed
1 task done

[Bug]: Accessing value of sp-menu-item before render throws an error #3626

charlessuh opened this issue Sep 6, 2023 · 5 comments · Fixed by #3717
Labels
bug Something isn't working Component: Menu Discovery

Comments

@charlessuh
Copy link
Collaborator

charlessuh commented Sep 6, 2023

Code of conduct

  • I agree to follow this project's code of conduct.

Impacted component(s)

sp-menu-item

Expected behavior

No response

Actual behavior

Accessing a MenuItem's value before it has rendered can throw the following error:

Uncaught TypeError: Cannot read properties of null (reading 'assignedElements')
    at get itemChildren [as itemChildren] (MenuItem.dev.js:140:32)
    at get itemText [as itemText] (MenuItem.dev.js:128:17)
    at get value [as value] (MenuItem.dev.js:111:32)

Screenshots

No response

What browsers are you seeing the problem in?

No response

How can we reproduce this issue?

  1. Go to https://studio.webcomponents.dev/edit/BvbfsRX2m4AMWTsc2zTe/src/index.ts?p=stories

Sample code that illustrates the problem

No response

Logs taken while reproducing problem

No response

@charlessuh charlessuh added bug Something isn't working triage An issue needing triage labels Sep 6, 2023
@charlessuh charlessuh changed the title [Bug]: Accessing value of sp-menu-item throws an error [Bug]: Accessing value of sp-menu-item before render throws an error Sep 6, 2023
@charlessuh charlessuh mentioned this issue Sep 6, 2023
21 tasks
@hunterloftis
Copy link
Contributor

hunterloftis commented Sep 12, 2023

I'm throwing a similar error stack when I call Menu.focus(). This may be a race where the menu hasn't had a chance to render once and collect its assigned elements before I focus it:

image

@hunterloftis
Copy link
Contributor

A quick test provides more evidence that it might be a race to the first render/update cycle. I changed this:

    private updateFocus(): void {
        this.activeMenu()?.focus({ preventScroll: true });
    };

to this:

    private async updateFocus(): Promise<void> {
        const menu = this.activeMenu();
        if (menu == null) return;

        await menu.updateComplete;
        menu.focus({ preventScroll: true });
    };

...and it resolved the issue.

@najikahalsema najikahalsema added Component: Menu Discovery and removed triage An issue needing triage labels Sep 19, 2023
@ettchan618
Copy link

Hey @Westbrook, I tried this in the webcomponents studio and it looks like the error is still appearing.

https://studio.webcomponents.dev/edit/fdpCdZN6Khei2agAcWud/src/index.ts?p=stories

@Westbrook
Copy link
Contributor

Looks like we addressed a similar context elsewhere, and then erroneously marked this as addressed for it. @najikahalsema is looking into a fix for this, soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Component: Menu Discovery
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants