Dark Mode Persistence

If your app offers a dark/light mode, you might be wondering how to “transfer” that mode to your login theme. This ensures that if users are browsing your app in dark mode, then click “Login,” they’ll be redirected to a Keycloakify login UI that’s rendered in dark mode.

Example of a Keycloakify theme implementation that carries over dark mode

This is somewhat of a niche use case, but it illustrates how you can pass state from your application to your Keycloak UIs.

In Your Web Application

Typically, when your user clicks your “Login” button in the header, your application will redirect them to a URL that looks something like this:

https://<your-keycloak-url>/realms/protocol/openid-connect/auth?client_id=<your-client>

What we want to do is append, for example, &dark=true or &dark=false to that URL so it can be retrieved on the other side by your Keycloak theme.

How you do that depends on your stack. Let’s look at an example with:

  • A React SPA

  • oidc-spa, a modern alternative to keycloak-js

  • MUI, a popular React component library

The following snippet is a React component typically placed in the header of your application for displaying Login and Register buttons.

In Your Login Theme

Within your Keycloak theme, you can now create a utility to read your custom &dark=true|false parameter.

How you use this utility depends heavily on your framework and UI library. As an example, here’s what it might look like with React/MUI:

Last updated

Was this helpful?