Skip to content

Commit

Permalink
feat: add getting theme from cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Givanyan committed Dec 13, 2023
1 parent 3e73bf7 commit d941087
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build/globals.webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ module.exports = {

DETECT_USER_PREFERENCES: s(config.theme.detectUserPreferences()),

POST_PROCESS_THEME: s(config.theme.postProcessor),

DS: runtime.passDesignSystem && pzlr.designSystem ?
s(getDS()) :
null,
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ declare const DETECT_USER_PREFERENCES: CanUndef<
}>>
>;

declare const POST_PROCESS_THEME: CanUndef<boolean>;

declare const DS: CanUndef<DesignSystem>;
declare const DS_COMPONENTS_MODS: CanUndef<{
[name: string]: Nullable<Array<string | boolean | number>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import symbolGenerator from 'core/symbol';
import { syncLocalStorage } from 'core/kv-storage/engines/cookie';

import type iBlock from 'components/super/i-block/i-block';
import type iStaticPage from 'components/super/i-static-page/i-static-page';
Expand Down Expand Up @@ -50,7 +51,9 @@ export default class ThemeManager extends Friend {

let theme = THEME;

if (Object.isDictionary(DETECT_USER_PREFERENCES)) {
if (POST_PROCESS_THEME) {
theme = syncLocalStorage.get('colorTheme') ?? theme;
} else if (Object.isDictionary(DETECT_USER_PREFERENCES)) {
const
prefersColorSchemeEnabled = Object.get<boolean>(DETECT_USER_PREFERENCES, 'prefersColorScheme.enabled') ?? false,
darkTheme = Object.get<string>(DETECT_USER_PREFERENCES, 'prefersColorScheme.aliases.dark') ?? 'dark',
Expand Down Expand Up @@ -97,6 +100,7 @@ export default class ThemeManager extends Friend {
const oldValue = this.currentStore;

this.currentStore = value;
syncLocalStorage.set('colorTheme', value);
document.documentElement.setAttribute(this.themeAttribute, value);

void this.component.lfc.execCbAtTheRightTime(() => {
Expand Down

0 comments on commit d941087

Please sign in to comment.