Mapped Components Limitations

Out-of-sync Methods

Example: MainPage JavaScript Polymer template file.

js

  1. class MainPage extends LitElement {
  2. render() {
  3. return html`
  4. <div id="header">Main page</div>
  5. <div id="content"></div>
  6. <hr>
  7. <div id="footer">
  8. <a href="mailto:someone@example.com?Subject=Hello" target="_top">Send Mail</a>
  9. </div>`;
  10. }
  11. }
  12. customElements.define("main-page", MainPage);

Example: Mapped Java template class.

Java

  1. @Tag("main-page")
  2. @JsModule("./com/example/main-page.js")
  3. public class MainPage extends LitTemplate {
  4. @Id("content")
  5. private Div content;
  6. public void setContent(Component content) {
  7. this.content.removeAll();
  8. this.content.add(content);
  9. }
  10. }

In the template class example above, you could additionally map the div element with a "footer" identifier using the Div component and @Id("footer") annotation. However, the hierarchical structure will not be available on the server side using the Java API.

The injected Div instance does not have a server-side child, even though the a (anchor) element is available on the client side. The getChildren() method in the injected instance returns an empty Stream. Similarly, the getText() method of the Div instance injected using the @Id("header") annotation, returns an empty string.

To summarize:

  • Server-side Component or Element read methods are not always in sync with the client side.

  • You can still use mutation API methods, like appendChild, setProperty or setAttribute from the server side, without issue.

  • Getter methods return values that are set from the server side only.

Enabled State

Since hierarchical structure in the template is not reflected on the server side the setEnabled method called on @Id injected component doesn’t disable any other @Id injected component even though such component may be a descendant in the shadow DOM (see also Component Enabled State).

Example: Disable @Id injected component.

Java

  1. @Tag("main-layout")
  2. @JsModule("./com/example/main-layout.js")
  3. public class MainLayout extends LitTemplate {
  4. @Id("layout")
  5. private VerticalLayout layout;
  6. @Id("textfield")
  7. private TextField textField;
  8. @Id("button")
  9. private Button button;
  10. public void onSomeAction() {
  11. layout.setEnabled(false);
  12. System.out.println(textField.isEnabled()); // prints "true"
  13. System.out.println(button.isEnabled()); // prints "true"
  14. // call explicitly setEnabled(false) to disable a component
  15. button.setEnabled(false);
  16. System.out.println(button.isEnabled()); // prints "false"
  17. }
  18. }

js

  1. class MainLayout extends LitElement {
  2. render() {
  3. return html`
  4. <div>
  5. <vaadin-vertical-layout id="layout">
  6. <vaadin-text-field id="textfield"></vaadin-text-field>
  7. <vaadin-button id="button"></vaadin-button>
  8. </vaadin-vertical-layout>
  9. </div>`;
  10. }
  11. }
  12. customElements.define("main-layout", MainLayout);

Template Disabled Attribute

LitTemplate doesn’t support using the disabled attribute for an id-mapped component and throws an IllegalAttributeException with information on where it is used. Id-mapped components should always be disabled from the server side using the setEnabled(false) method.

Faulty sample

js

  1. class MainLayout extends LitElement {
  2. render() {
  3. return html`
  4. <div>
  5. <vaadin-button id="button" disabled></vaadin-button>
  6. </div>
  7. `;
  8. }
  9. }
  10. customElements.define("main-layout", MainLayout);

Java

  1. @Tag("main-layout")
  2. @JsModule("./com/example/main-layout.js")
  3. public class MainLayout extends LitTemplate {
  4. @Id("button")
  5. private Button button;
  6. }

This throws an IllegalAttributeException with the message:

  1. Lit template 'com.example.MainLayout' injected element 'vaadin-button' with id 'button' uses the disabled attribute.
  2. Mapped components should instead be disabled using the 'setEnabled(false)' method on the server side.
Note
PolymerTemplate will not throw for using the disabled attribute, but only store it as a property for the element leaving the element as enabled on the server side.

Removing Mapped Elements

A virtually-mapped Element is connected to the ShadowRoot of the LitTemplate, even if it actually resides deeper in the shadow tree. You cannot remove virtually mapped components from the DOM by removing them on the server side.

Note
You can detect whether a component is used in a LitTemplate using the isTemplateMapped method. See the Detecting Component Mappings for more.
Note
The same limitations apply to Polymer template class.