docs

Setting Themes

You define which theme is used by your app either by using the theme configuration parameter or the sap/ui/core/Theming.setTheme method.


Using Custom Themes

To load an external custom theme, you can either declare it statically on the page or by setting the theme-root configuration parameter.


Theme Origin Allowlist

When configuring a theme with a themeRoot URL via the theme parameter, security restrictions apply. Absolute URLs to an origin other than the current page are cut off by default. The path segment will be resolved relative to the current page origin.

In order to allow other origins to be used via the URL parameter, the sap-allowed-theme-origins configuration option can be used:

<meta name="sap-allowed-theme-origins" content="https://example.com">

This allows you to load a theme from https://example.com, which is provided via the URL parameter:

https://myserver.com/sap/myapp/?sap-theme=my_theme@https://example.com/custom-themes/

Note:

sap-allowed-theme-origins can only be provided using bootstrap or the <meta> tag. Origins provided via configuration must contain the same protocol, host, and port as the origin provided in the theme parameter. Multiple allowed origins can be separated by a comma.

A general wildcard (*) can also be used to allow all origins. However, this should only be used in combination with additional security mechanisms, such as CSP style-src directives. Wildcards to allow sub-domains are not supported.


Listening to the Theming.applied Event

Whenever the theme is switched, an event is fired indicating that a theme switch has been triggered. If your application needs to take theming into consideration, attach an event handler, like this:

sap.ui.require([
    "sap/ui/core/Theming"
], (
    Theming
) => {
    "use strict";
     
    // Whenever the theme is switched, an event is fired,
    // indicating that a theme switch has been triggered.
    Theming.attachApplied((oEvent) => {
        // Note: The event callback has no longer a <this> context,
        // thus we use an arrow function here
 
        // Note: the Event object now has a different API than on the Core facade:
        // no more getParameters(), but simple properties like the Web API events.
        // Therefore, you can access the new "theme" like so:
        const sTheme = oEvent.theme;
    });
    ...
});

Note:

The handler of the applied event will be executed immediately once if all *.css files are loaded and there are no further requests pending for the theme.

After that, it will only be executed in case of new *.css files, which may happen for a complete theme change or the loading of additional libraries. Keep in mind that the onThemeChanged hook is not executed initially if the theme has already been applied.

For more information, see the API Reference.

Related Information

Available Configuration Options

API Reference: sap/ui/core/Theming.setTheme