Skip to content

Commit ce9d6eb

Browse files
committed
Observe the admin policy regarding dark mode allowance set by the Keycloak admin
1 parent f312574 commit ce9d6eb

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ If you are deploying Keycloak on Kubernetes using Helm, here's how to configure
4747
...
4848
```
4949
50-
### Dark Mode Persistence
50+
### Dark Mode
5151
52-
If you want to ensure the color scheme preference from your app to be persisted when navigating to the Keycloak pages
52+
If you want to ensure the color scheme preference from your app to be carried when navigating to the Keycloak pages
5353
you need to add `dark=true` or `dark=false` when redirecting to the login or account page.
5454

5555
With [oidc-spa](https://oidc-spa.dev) and [react-dsfr](https://github.com/codegouvfr/react-dsfr) for the login theme:
@@ -71,6 +71,10 @@ For the Account page:
7171
https://<your keycloak url>/realms/<your realm>/account?referrer={encodeURIComponent(location.href))}l&dark=true
7272
```
7373

74+
Otherwise the themes will render in dark mode if the user prefers it.
75+
You can also prevent the login theme (not supported with the account theme) to ever render in dark mode
76+
by disabling "dark mode" in the Keycloak Admin Console in the `Realm Configuration` -> `Themes` section.
77+
7478
# License
7579

7680
This project is licensed under the [MIT License](LICENSE), courtesy of the Direction interministérielle du numérique.

src/login/KcContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type KcContextExtension = {
1010
client: {
1111
baseUrl?: string;
1212
};
13+
darkMode?: boolean;
1314
};
1415

1516
export type KcContextExtensionPerPage = {};

src/main.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createRoot } from "react-dom/client";
22
import { StrictMode } from "react";
33
import { KcPage } from "./kc.gen";
4+
import { setKcContextDarkModePolicy } from "./shared/getColorScheme";
45

56
// The following block can be uncommented to test a specific page with `yarn dev`
67
// Don't forget to comment back or your bundle size will increase
@@ -14,6 +15,12 @@ import { KcPage } from "./kc.gen";
1415
// });
1516
// }
1617

18+
if (window.kcContext !== undefined && window.kcContext.themeType === "login") {
19+
setKcContextDarkModePolicy({
20+
kcContextDarkMode: window.kcContext.darkMode
21+
});
22+
}
23+
1724
createRoot(document.getElementById("root")!).render(
1825
<StrictMode>
1926
{!window.kcContext ? (

src/shared/getColorScheme.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
const SESSION_STORAGE_KEY = "keycloak-theme-dsfr:isDark";
22

3+
let kcContextDarkMode: boolean | undefined = undefined;
4+
5+
export function setKcContextDarkModePolicy(params: {
6+
kcContextDarkMode: boolean | undefined;
7+
}): void {
8+
kcContextDarkMode = params.kcContextDarkMode;
9+
}
10+
311
function getIsDark(): boolean | undefined {
4-
from_url: {
12+
from_admin_policy: {
13+
if (kcContextDarkMode === undefined || kcContextDarkMode === true) {
14+
break from_admin_policy;
15+
}
16+
return false;
17+
}
518

19+
from_url: {
620
const url = new URL(window.location.href);
721

822
const value = url.searchParams.get("dark");
@@ -24,28 +38,24 @@ function getIsDark(): boolean | undefined {
2438
}
2539

2640
from_session_storage: {
27-
2841
const value = sessionStorage.getItem(SESSION_STORAGE_KEY);
2942

3043
if (value === null) {
3144
break from_session_storage;
3245
}
3346

3447
return value === "true";
35-
3648
}
3749

3850
return undefined;
3951
}
4052

41-
export function getColorScheme(){
42-
53+
export function getColorScheme() {
4354
const isDark = getIsDark();
4455

45-
if( isDark === undefined ) {
56+
if (isDark === undefined) {
4657
return undefined;
4758
}
4859

4960
return isDark ? "dark" : "light";
50-
5161
}

0 commit comments

Comments
 (0)