Skip to content

Instantly share code, notes, and snippets.

@jpzwarte
Last active July 30, 2022 02:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpzwarte/6dacea6a51a4e7afca9b80014b376e3f to your computer and use it in GitHub Desktop.
Save jpzwarte/6dacea6a51a4e7afca9b80014b376e3f to your computer and use it in GitHub Desktop.
PoC scoped custom element registries in Angular

PoC scoped custom element registries in Angular

  1. Render the AppComponent as a custom element
export class AppModule {
  ngDoBootstrap(): void {
    customElements.define('mg-root', createCustomIvyElement(AppComponent));
  }
}
  1. Set AppComponent encapsulation to ShadowDom
@Component({
  ...
  encapsulation: ViewEncapsulation.ShadowDom
})
  1. Add @ElementDefinitions decorator to AppComponent
@Component({
  ...
})
@ElementDefinitions({ 'dna-page-header': PageHeader })
export class AppComponent {
  ...
}
  1. Add @ElementDefinitions to any component using web components
@Component({
  selector: 'mg-foo',
  template: `
    <dna-alert>Foo works!</dna-alert>
    <p>
      foo works!
    </p>
  `,
  styles: [`
    :host { display: block; }
    p { color: hotpink; }
  `]
})
@ElementDefinitions({ 'dna-alert': Alert })
export class FooComponent {}
  1. Use the root custom element to render your angular app in index.html
<body>
  <mg-root></mg-root>
</body>
  1. Add polyfill to polyfills.ts
import '@webcomponents/scoped-custom-element-registry/scoped-custom-element-registry.min.js';
  1. Disable zone.js
  • Remove zone.js polyfill
  • Set ngZone to noop
platformBrowserDynamic().bootstrapModule(AppModule, { ngZone: 'noop' })
  .catch(err => console.error(err));
  1. Pizza

Screenshot 2021-11-29 at 12 24 48

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