ng-polymorpheus

396

22

Alexander Inkin

Added on December 21, 2024

Polymorpheus is a tiny Angular library, combining interpolation, templates and dynamic components under unified neat API to make view customisation a breeze.

ng-polymorpheus

Polymorpheus is a tiny Angular library, combining interpolation, templates and dynamic components under unified neat API to make view customisation a breeze.

Alexander Inkin

README.md

Polymorpheus

Polymorpheus is a tiny library for polymorphic templates in Angular.

It is 1 KB gzip, dependency free and allows you to make pretty cool things.

Polymorpheus

What does it do?

It abstracts over different ways of view customization in Angular with one simple structural directive:

<ng-container *polymorpheusOutlet="content as text; context: context">{{text}}</ng-container>

Content accepts:

  • primitives like number or string
  • functions that take context as argument and return a primitive
  • templates that get instantiated with given context
  • components that would get context injected through DI

Context is optional when you need your content to adapt to the situation

How to use it?

Typical use case would be a component that accepts visual customization and defines context by itself. Say a menu list where you can configure how each item should look like by passing a template. And context would be item itself and, for example, whether it is focused or not.

Please see extensive demo (wait for packages to install and run npm start).

You can also read about this concept in detail.

Accessing context in dynamic component

If you use component content you can inject context with POLYMORPHEUS_CONTEXT token.

Important! This object is live so if you change it, your component will not be recreated:

@Component({
  template: `
    {{ context.active }}
  `, // <-- this will automatically update
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MyComponent {
  constructor(@Inject(POLYMORPHEUS_CONTEXT) readonly context: {active: boolean}) {}
}

Adding type to template context

You can use polymorpehus directive to add type to template context:

readonly context!: { $implicit: number };
<ng-template
  #template="polymorpheus"
  [polymorpheus]="context"
  let-item
>
  {{ item.toFixed(2) }} <-- type 'number'
</ng-template>

Core team

Alex Inkin
Alex Inkin
Roman Sedov
Roman Sedov

ng-polymorpheus is a part of Taiga UI libraries family which is backed and used by a large enterprise. This means you can rely on timely support and continuous development.

Related Content

Taiga UI

Taiga UI is an Angular components library and a comprehensive UI kit spanning over 100 components and many helpful directives and utils.
GitHubUpdated today

ng-zorro-antd

An enterprise-class Angular UI component library based on Ant Design, all components are open source and free to use under MIT license.
GitHubUpdated yesterday

100-days-of-angular

100-days-of-angular
GitHubUpdated 17 months ago

angular-performance-checklist

angular-performance-checklist
GitHubUpdated 20 months ago

Clarity Design System

Clarity is a scalable, customizable, open source design system that works with Angular. It provides high quality UI components to build your own applications with ease.
GitHubUpdated 24 months ago

We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.