Shadow root in server-side Element

Elements contain support for adding a shadow root for Element types that support it. This enables for the creation of serverside webcomponents. Elements without shadow root support

A shadow root can be created simply with the command:

Element element = new Element("custom-element");
ShadowRoot shadowRoot = element.attachShadow();

A ShadowRoot is not an actual element and only supports child element handling and getting the host element which contains the shadow root.

Note
When adding a shadow root, any element added to the ShadowRoot parent will not be visible if the ShadowRoot doesn’t contain a <slot></slot> element. See Components in slot for more information.

All new elements should be added to the ShadowRoot element so that they will be encapsulated in the shadow tree of the hosting element.

@Tag("my-label")
public class MyLabel extends Component {

    public MyLabel() {
        ShadowRoot shadowRoot = getElement().attachShadow();
        Label textLabel = new Label("In the shadow");
        shadowRoot.appendChild(textLabel.getElement());
    }
}

Missing shadow root support

  • The browser already hosts its own internal shadow DOM for the element (<textarea>, <input>).

  • It doesn’t make sense for the element to host a shadow DOM (<img>).