简体   繁体   中英

How to decorate a component in angular.dart

I would like to provide a possibility to show my components in a bit different look and feel and thought using the decorator for it. Something like:

<body>
    <my-component my-decorator></my-component>
</body>

.

@Component(
  selector: 'my-component',
  templateUrl: '.../my-component.html',
  cssUrl: '.../my-component.css',
  publishAs: 'comp',
)
class MyComponent {

  MyComponent(final Element element) {
    Logger.root.fine("MyComponent(): element = $element, element.attributes = ${element.attributes.keys}");
  }

}


@Decorator(selector: '[my-decorator]')
class MyDecorator {

  final Element element;

  @NgOneWay('my-decorator')
  var model; // is not used

  MyDecorator(this.element) {
    Logger.root.fine("MyDecorator(): element = $element, element.nodeName = ${element.nodeName}");
    Logger.root.fine("MyDecorator(): element.shadowRoot = ${element.shadowRoot}, element.parent = ${element.parent}");
  }

}

Unfortunately, it seems that my-decorator is processed before my-component so it is getting null shadowRoot property in the injected Element object.

It would be possible to check on existence of the my-decorator attribute within the my-component backing class, but that is clearly polluting the design.


UPDATE: Thanks to replay from Marko Vuksanovic, the following is now returning the :

@Decorator(selector: '[my-decorator]')
class MyDecorator extends AttachAware {

  final Element element;

  @NgOneWay('my-decorator')
  var model; // is not used

  MyDecorator(this.element) {
    Logger.root.fine("MyDecorator(): element = $element, element.nodeName = ${element.nodeName}");
    Logger.root.fine("MyDecorator(): element.shadowRoot = ${element.shadowRoot}, element.parent = ${element.parent}");
  }

  void attach() {
    Logger.root.fine("attach(): element.shadowRoot = ${element.shadowRoot}");
  }

}

The question still remains how to modify the styling of the shadow DOM.

Thanks in advance for any comments/ideas/solutions.

You can try using AttachAware and it's attach method. You should implement AttachAware interface in your decorator and/or component.

Here's link to Angular.dart docs - https://docs.angulardart.org/#angular-core-annotation.AttachAware

To change the styling of a ShadowDom component you can use element.shadowRoot to get the root of your web component. Shadow root is almost like 'document' object. You can use shadow root to get reference to any element and then you can easily modify it by applying styles as needed.

You could use something like this.element.shadowRoot.querySelector('[some-attr]').innerHtml = "Modified by decorator" // disclaimer: not tested, but I hope you get the idea.

You can add a style tag to the shadowDom programmatically:

shadowRoot.append(new StyleElement()..text = ':host{background: red;}');

or

shadowRoot.append(new StyleElement()..text = "@import url('some.css')");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM