Slots

What are Slots ?

Slots are essentially placeholders within a component that can be filled with custom content. They provide a way to dynamically inject content into a component without affecting its original template.
This makes it possible to create components that can be easily adapted to different scenarios without having to duplicate code or create a new component from scratch.

Why use them ?

Slots facilitate the creation of more versatile and reusable components. They allow developers to design components with predefined structures while leaving room for variation in content.
This separation of structure and content enhances code reusability and promotes a cleaner, more modular codebase.

AgnosUI Slot

AgnosUI components include slots as properties. This allows to specify the projected content in multiple manners, depending on the framework.

Angular applications usually handle slots using content projection.
We support this behavior, while going further.

The AgnosUI Angular slots can be set using:

  • a simple string
  • a function (props: Props) => string
  • a TemplateRef
  • an Angular component
  • a ComponentTemplate, an AgnosUI utility allowing to use an Angular component without the host element

To illustrate the basic usage, let's see in action how we can use a simple slot in the Bootstrap flavour of the Alert component:

Sample not found, make sure to fill the samples.ts file.

Context

Slots have access to a context, which for most cases is the component state.
It is possible however to extend the context, which enables powerful customization. Here is an example with the Bootstrap flavour of the Pagination component:

Sample not found, make sure to fill the samples.ts file.

Integration with Configuration

As explained above, AgnosUI slots are inherently properties, thus benefit from the Configuration.
For instance, we may configure the slotStructure of the Alert to use a custom component, allowing to fully customize the component.

Sample not found, make sure to fill the samples.ts file.

Headless Usage

AgnosUI provides utilities to manage slots for each framework, as frameworks have differences in their implementations of slots / templates / snippets.
To learn more about the specificies of each framework, go here:

Headless example

You can check out the following example, re-writing the Bootstrap flavour of the Rating component as readonly:

Sample not found, make sure to fill the samples.ts file.