Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add get/set neutral base color in wc and in theme storage
  • Loading branch information
vnbaaij committed Mar 16, 2025
commit 4199befbd461a0bf2acea0aeb168088ab03a3e19
6 changes: 4 additions & 2 deletions src/Core.Assets/src/Design/ThemeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ThemeStorage {
}


public updateLocalStorage(mode: string | null, primaryColor: string | null): void {
public updateLocalStorage(mode: string | null, primaryColor: string | null, neutralBaseColor: string | null): void {

// If LocalStorage is not available, do nothing.
if (localStorage == null) {
Expand All @@ -41,10 +41,11 @@ class ThemeStorage {
localStorage.setItem(this.storageName, JSON.stringify({
mode: ThemeStorage.getValueOrNull(mode),
primaryColor: ThemeStorage.getValueOrNull(primaryColor),
neutralBaseColor: ThemeStorage.getValueOrNull(neutralBaseColor),
}));
}

public readLocalStorage(): { mode: string | null, primaryColor: string | null } | null {
public readLocalStorage(): { mode: string | null, primaryColor: string | null, neutralBaseColor: string | null } | null {

// If LocalStorage is not available, do nothing.
if (localStorage == null) {
Expand All @@ -69,6 +70,7 @@ class ThemeStorage {
return {
mode: ThemeStorage.getValueOrNull(storageItems?.mode),
primaryColor: ThemeStorage.getValueOrNull(storageItems?.primaryColor),
neutralBaseColor: ThemeStorage.getValueOrNull(storageItems?.neutralBaseColor),
}
}

Expand Down
42 changes: 40 additions & 2 deletions src/Core.Assets/src/DesignTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,32 @@ class DesignTheme extends HTMLElement {
this._synchronization.synchronizeOtherComponents("primary-color", value);
}

/**
* Gets the current neutral base color attribute value.
*/
get neutralColor(): string | null {
return this.getAttribute("neutral-base-color");
}

/**
* Sets the current neutral base color attribute value.
*/
set neutralColor(value: string | null) {
this.updateAttribute("neutral-base-color", value);

// Apply the color
if (value != null) {
const rgb = parseColorHexRGB(value);
if (rgb != null) {
const swatch = SwatchRGB.from(rgb);
neutralBaseColor.withDefault(swatch);
}

// Synchronization
this._synchronization.synchronizeOtherComponents("neutral-color", value);
}
}

/**
* Gets the current storage-name key, to persist the theme/color between sessions.
*/
Expand Down Expand Up @@ -147,6 +173,7 @@ class DesignTheme extends HTMLElement {
if (existingComponent != null) {
const mode = ThemeStorage.getValueOrNull(existingComponent.getAttribute("mode"));
const color = ThemeStorage.getValueOrNull(existingComponent.getAttribute("primary-color"))
const neutralColor = ThemeStorage.getValueOrNull(existingComponent.getAttribute("neutral-base-color"));

// Mode can be null in other components
this.attributeChangedCallback("mode", this.mode, mode);
Expand All @@ -155,6 +182,11 @@ class DesignTheme extends HTMLElement {
if (color != null) {
this.attributeChangedCallback("primary-color", this.primaryColor, color);
}

// Neutral color cannot be null in other components
if (neutralColor != null) {
this.attributeChangedCallback("neutral-base-color", this.neutralColor, neutralColor);
}
}

// Load from LocalStorage
Expand All @@ -164,6 +196,7 @@ class DesignTheme extends HTMLElement {
if (theme != null) {
this.attributeChangedCallback("mode", this.mode, theme.mode);
this.attributeChangedCallback("primary-color", this.primaryColor, theme.primaryColor);
this.attributeChangedCallback("neutral-base-color", this.neutralColor, theme.neutralBaseColor);
}
}

Expand Down Expand Up @@ -202,7 +235,7 @@ class DesignTheme extends HTMLElement {

// Attributes to observe
static get observedAttributes() {
return ["mode", "primary-color", "storage-name"];
return ["mode", "primary-color", "neutral-base-color", "storage-name"];
}

// Attributes has changed.
Expand Down Expand Up @@ -234,6 +267,11 @@ class DesignTheme extends HTMLElement {
this.primaryColor = newValue;
break;

case "neutral-base-color":
this.dispatchAttributeChanged(name, oldValue, newValue);
this.neutralColor = newValue;
break;

case "storage-name":
this.storageName = newValue;
break;
Expand Down Expand Up @@ -298,7 +336,7 @@ class DesignTheme extends HTMLElement {
}
}
if (this.storageName != null) {
this._themeStorage.updateLocalStorage(this.mode, this.primaryColor);
this._themeStorage.updateLocalStorage(this.mode, this.primaryColor, this.neutralColor);
}

this._isInternalChange = false;
Expand Down