@@ -10,7 +10,6 @@ import {
10
10
} from "/_content/Microsoft.FluentUI.AspNetCore.Components/Microsoft.FluentUI.AspNetCore.Components.lib.module.js" ;
11
11
12
12
const currentThemeCookieName = "currentTheme" ;
13
- const themeSettingSystem = "System" ;
14
13
const themeSettingDark = "Dark" ;
15
14
const themeSettingLight = "Light" ;
16
15
const darkThemeLuminance = 0.19 ;
@@ -31,11 +30,11 @@ export function updateTheme(specifiedTheme) {
31
30
}
32
31
33
32
/**
34
- * Returns the value of the currentTheme cookie, or System if the cookie is not set .
33
+ * Returns the value of the currentTheme cookie.
35
34
* @returns {string }
36
35
*/
37
36
export function getThemeCookieValue ( ) {
38
- return getCookieValue ( currentThemeCookieName ) ?? themeSettingSystem ;
37
+ return getCookieValue ( currentThemeCookieName ) ;
39
38
}
40
39
41
40
export function getCurrentTheme ( ) {
@@ -61,7 +60,15 @@ function getSystemTheme() {
61
60
* @param {string } theme
62
61
*/
63
62
function setThemeCookie ( theme ) {
64
- document . cookie = `${ currentThemeCookieName } =${ theme } ` ;
63
+ if ( theme == themeSettingDark || theme == themeSettingLight ) {
64
+ // Cookie will expire after 1 year. Using a much larger value won't have an impact because
65
+ // Chrome limits expiration to 400 days: https://developer.chrome.com/blog/cookie-max-age-expires
66
+ // The cookie is reset when the dashboard loads to creating a sliding expiration.
67
+ document . cookie = `${ currentThemeCookieName } =${ theme } ; Path=/; expires=${ new Date ( new Date ( ) . getTime ( ) + 1000 * 60 * 60 * 24 * 365 ) . toGMTString ( ) } ` ;
68
+ } else {
69
+ // Delete cookie for other values (e.g. System)
70
+ document . cookie = `${ currentThemeCookieName } =; Path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC;` ;
71
+ }
65
72
}
66
73
67
74
/**
@@ -260,6 +267,12 @@ function initializeTheme() {
260
267
const effectiveTheme = getEffectiveTheme ( themeCookieValue ) ;
261
268
262
269
applyTheme ( effectiveTheme ) ;
270
+
271
+ // If a theme cookie has been set then set it again on page load.
272
+ // This updates the cookie expiration date and creates a sliding expiration.
273
+ if ( themeCookieValue ) {
274
+ setThemeCookie ( themeCookieValue ) ;
275
+ }
263
276
}
264
277
265
278
createAdditionalDesignTokens ( ) ;
0 commit comments