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

Component's ShadowRoot is null on attach when navigating from a Typescript view. #12080

Closed
tulioag opened this issue Oct 18, 2021 · 4 comments · Fixed by #12228
Closed

Component's ShadowRoot is null on attach when navigating from a Typescript view. #12080

tulioag opened this issue Oct 18, 2021 · 4 comments · Fixed by #12228

Comments

@tulioag
Copy link
Contributor

tulioag commented Oct 18, 2021

Description of the bug / feature

Consider this view

@Route(value = "shadow")
public class ShadowView extends VerticalLayout {

    public ShadowView() {
        TextField field = new TextField();
        field.addAttachListener(ui -> field.getElement().executeJs("alert(this.shadowRoot)"));
        add(field);
    }
}

If you navigate to this view from a typescript view, the shadowRoot will be null. Otherwise it will be available.

A more complex example:

  1. Download the project from https://start.vaadin.com/?preset=all&preset=partial-prerelease
  2. Go to one of typescript views
  3. Go to the collaborative-master-detail-view
  4. See that an error is shown.

ce-issue

The errors are caused by the field-highlighter trying to access the components shadow roots.

Minimal reproducible example

my-app.zip

  1. Run the application
  2. Click in "Shadow".
  3. Verify that the alert shows [object ShadowRoot]
  4. Click in "Hello TS"
  5. Click in "Shadow".
  6. Verify that the alert shows null

Expected behavior

The alert should show [object ShadowRoot] in all cases.

Versions:

- Vaadin / Flow version: 22.0.0.alpha8/9.0.0.alpha10
- Java version: 8, 17
- OS version: Linux, MacOs
@Artur-
Copy link
Member

Artur- commented Oct 19, 2021

The test case can actually be simplified to

public class ShadowView extends VerticalLayout {
    public ShadowView() {
        getElement().executeJs("alert(this.shadowRoot)");
    }
}

@Artur-
Copy link
Member

Artur- commented Oct 19, 2021

The problem is that the view element is not attached to the DOM when the executeJS command is run.

You can see this more clearly if you change to

alert(this.parentElement.parentElement)

When you open the Java view directly, you get
[object HTMLBodyElement]
and when you navigate from TS to Java, you get
null

@platosha platosha added hilla Issues related to Hilla investigation labels Oct 19, 2021
@platosha
Copy link
Contributor

We need to investigate this. Let’s do a 1-day investigation here.

My gut feeling is that this is related to Flow / vaadin-router navigation lifecycle.

@platosha platosha self-assigned this Oct 29, 2021
platosha added a commit that referenced this issue Nov 1, 2021
Fixes #12080

The client-side Vaadin Router does not attach components until
the `onBeforeEnter` callback is run. However, when the Flow client
initializes a server-side view, it assumes its container and the UI root
are connected to the document. This concerns constructor timing as well
as `AttachEvent` listeners.

This fixes the issue by pre-attaching the hidden container element created in
the `Flow.ts` (the adapter for the client-side router) to the body.
platosha added a commit that referenced this issue Nov 1, 2021
Fixes #12080

The client-side Vaadin Router does not attach components until
the `onBeforeEnter` callback is run. However, when the Flow client
initializes a server-side view, it assumes its container and the UI root
are connected to the document. This concerns constructor timing as well
as `AttachEvent` listeners.

This fixes the issue by pre-attaching the hidden container element created in
the `Flow.ts` (the adapter for the client-side router) to the body.
platosha added a commit that referenced this issue Nov 2, 2021
Fixes #12080

The client-side Vaadin Router does not attach components until
the `onBeforeEnter` callback is run. However, when the Flow client
initializes a server-side view, it assumes its container and the UI root
are connected to the document. This concerns constructor timing as well
as `AttachEvent` listeners.

This fixes the issue by pre-attaching the hidden container element created in
the `Flow.ts` (the adapter for the client-side router) to the body.
platosha added a commit that referenced this issue Nov 8, 2021
Fixes #12080

The client-side Vaadin Router does not attach components until
the `onBeforeEnter` callback is run. However, when the Flow client
initializes a server-side view, it assumes its container and the UI root
are connected to the document. This concerns constructor timing as well
as `AttachEvent` listeners.

This fixes the issue by pre-attaching the hidden container element created in
the `Flow.ts` (the adapter for the client-side router) to the body.
Haprog pushed a commit that referenced this issue Nov 8, 2021
Fixes #12080

The client-side Vaadin Router does not attach components until
the `onBeforeEnter` callback is run. However, when the Flow client
initializes a server-side view, it assumes its container and the UI root
are connected to the document. This concerns constructor timing as well
as `AttachEvent` listeners.

This fixes the issue by pre-attaching the hidden container element created in
the `Flow.ts` (the adapter for the client-side router) to the body.
@vaadin-bot
Copy link
Collaborator

This ticket/PR has been released with platform 22.0.0.beta2 and is also targeting the upcoming stable 22.0.0 version.

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

Successfully merging a pull request may close this issue.

4 participants