OneCX IconService
This service provides a convenient way to request icons by name and get a CSS class to apply in templates. It is available via @onecx/angular-integration-interface and coordinates icon loading through the integration topic system.
Overview
This document explains how to:
-
Request an icon and receive a CSS class for use in templates
-
Await icon availability with an async API
-
Choose the desired rendering style (
IconClassType) -
Understand the icon loading flow and cache semantics
-
Topics Used IconTopic
Usage
Follow these steps to use the IconService:
-
Inject the
IconServiceinto your component.import { inject } from '@angular/core' import { IconService } from '@onecx/angular-integration-interface' class MyComponent { private readonly iconService = inject(IconService) } -
Request an icon:
-
Synchronous: Returns a CSS class immediately while the icon assets are loaded asynchronously in the background.
Ideal when:-
The icon is not critical for initial layout
-
It can pop in shortly after rendering
-
You want fast template binding with no await
homeIcon: string = ''; async ngOnInit() { homeIcon = this.iconService.requestIcon('mdi:home', 'svg') }
-
-
Asynchronous: Await the icon; resolves to a class string when available or
nullif missing. The method returns a Promise and resolves only when the icon asset is available (ornullif missing).
Ideal when:-
The icon must render immediately with no flicker
-
Rendering or layout logic depends on the icon
-
You need to know whether the icon actually exists
accountIcon: string = ''; async ngOnInit() { const icon = await this.iconService.requestIconAsync('mdi:account', 'svg') if (icon) { this.accountIcon = icon } }
-
-
-
Apply the icon classes in the template.
<span [class]="homeIcon"></span> <span [class]="accountIcon"></span>
API
| Name | Signature | Description |
|---|---|---|
|
|
Requests an icon and returns a CSS class name to apply. Triggers loading if the icon is not cached. |
|
|
Requests an icon and resolves when the icon becomes available. Returns |
| Type | Behavior |
|---|---|
|
Applies an inline mask based on the SVG and uses |
|
Sets the icon as a |
|
Inserts the icon via |
|
Use |
Loading Flow and Cache
The icon system uses a global cache and topics for coordination:
-
Global cache:
window.onecxIcons: Record<string, IconCache | null | undefined>-
undefined→ requested, not yet loaded -
null→ not found -
IconCache→ available (contains SVG body and metadata)
-
-
Requests publish
IconRequestedvia IconTopic; the Shell listens, loads missing icons, injects CSS rules, then publishesIconsReceived. -
Class names are generated as
onecx-theme-icon-<type>-<normalized-name>.Exampleconst className = this.iconService.requestIcon('mdi:account', 'background-before') // yields e.g. "onecx-theme-icon-background-before-mdi-account"
Topics and Models
| See Icon Topic for platform-level details and message flow. |
|
Publishes |
|
|
|
|
|
|