UI state¶
UI state, app state or domain data are big terms, and we do not want to implement our own state management. For client side state management we recommend Flux / Redux / NgRx based state management frameworks like NgRx.
To separate and define these terms we use the definitions from Redux.
- Domain data: data that the application needs to show, use, or modify (such as "all the Todos retrieved from the server")
- App state: data that is specific to the application's behavior (such as "Todo #5 is currently selected", or "there is a request in progress to fetch Todos")
- UI state: data that represents how the UI is currently displayed (such as "The EditTodo modal dialog is currently open")
However, we provide a lightweight UI state service to persist some UI states, which many users like to keep through reloading the web page. The supported components are
Expanded and split size states are persisted in e.g. the localStorage of the browser and re-applied on reload.
How to enable the UI state storage¶
The storing of UI State is disabled by default. It requires two steps to enable it for a specific component.
- Include
provideSiUiServicein your app configuration /AppModuleto enable it in general:
import { ApplicationConfig } from '@angular/core';
import { provideSiUiState } from '@siemens/element-ng/common';
const config: ApplicationConfig = {
providers: [provideSiUiState()]
};
- Set the
stateIdon supported components like the
<si-navigation-vertical stateId="navVertical" />
With those two changes, the expand/collapse state will now be saved.
Using a custom storage¶
If enabled, the UI state is stored by default in the localstorage. Applications can override this by implementing a custom store:
import { UIStateStorage } from '@siemens/element-ng/common';
@Injectable()
class MyUIStateStorage implements UIStateStorage {
save(stateId: string, data: string): void | Promise<void> {
// Save the for the provided stateId. Override any existing data for that ID.
// Handle errors here. Otherwise the will just be retrhown.
}
load(stateId: string): string | undefined | null | Promise<string | undefined | null> {
// Load the data by stateId. Return undefined or null if nothing was stored previously.
// Handle errors here. Otherwise the will just be retrhown.
}
}
The custom storage needs to be configured on initialization of the SiUIState:
import { ApplicationConfig } from '@angular/core';
import { provideSiUiState } from '@siemens/element-ng/common';
const config: ApplicationConfig = {
providers: [
provideSiUiState({
store: MyUIStateStorage
})
]
};
Enables the automatic storage of UI state for enabled components. Enables the automatic storage of UI state for enabled components. config ? : provideSiUiState(config?: { store?: Type<UIStateStorage>; }): Provider[]
{ store?: Type<UIStateStorage>; } ¶
Types Documentation¶
Interface that defines a UIStateStore. It must be provided via provideSiUiState. load Loads data for a specific stateId. Can be asynchronous. Errors should be handled by the implementation. The Returns: The data or undefined if the data does not exist. Asynchronous implementations must return a Promise containing the data. save Saves data under a specific stateId. Already existing data for that stateId should be overridden. Asynchronous implementations must return a Promise and resolve it once saving was completed. Errors should be handled by the implementation. The
(stateId: string) => string | Promise<string | null | undefined> | null | undefined ¶ SiUIStateService does not handle errors.(stateId: string, data: string) => void | Promise<void> ¶ SiUIStateService does not handle errors.
Except where otherwise noted, content on this site is licensed under MIT License.