diff --git a/src-ui/e2e/settings/settings.spec.ts b/src-ui/e2e/settings/settings.spec.ts index 51210acdd..1ae9afa06 100644 --- a/src-ui/e2e/settings/settings.spec.ts +++ b/src-ui/e2e/settings/settings.spec.ts @@ -46,10 +46,10 @@ test('should warn on unsaved changes', async ({ page }) => { test('should apply appearance changes when set', async ({ page }) => { await page.routeFromHAR(REQUESTS_HAR, { notFound: 'fallback' }) await page.goto('/settings') - await expect(page.locator('body')).toHaveClass(/color-scheme-system/) + await expect(page.locator('html')).toHaveAttribute('data-bs-theme', /auto/) await page.getByLabel('Use system setting').click() await page.getByLabel('Enable dark mode').click() - await expect(page.locator('body')).toHaveClass(/color-scheme-dark/) + await expect(page.locator('html')).toHaveAttribute('data-bs-theme', /dark/) }) test('should toggle saved view options when set & saved', async ({ page }) => { diff --git a/src-ui/src/app/services/settings.service.spec.ts b/src-ui/src/app/services/settings.service.spec.ts index 560b67ae8..fac41ee6c 100644 --- a/src-ui/src/app/services/settings.service.spec.ts +++ b/src-ui/src/app/services/settings.service.spec.ts @@ -147,13 +147,14 @@ describe('SettingsService', () => { ).toEqual('') const addClassSpy = jest.spyOn(settingsService.renderer, 'addClass') - const removeClassSpy = jest.spyOn(settingsService.renderer, 'removeClass') + const setAttributeSpy = jest.spyOn(settingsService.renderer, 'setAttribute') settingsService.updateAppearanceSettings(true, true, '#fff000') expect(addClassSpy).toHaveBeenCalledWith(document.body, 'primary-light') - expect(addClassSpy).toHaveBeenCalledWith( - document.body, - 'color-scheme-system' + expect(setAttributeSpy).toHaveBeenCalledWith( + document.documentElement, + 'data-bs-theme', + 'auto' ) expect( document.body.style.getPropertyValue('--pngx-primary-lightness') @@ -161,21 +162,23 @@ describe('SettingsService', () => { settingsService.updateAppearanceSettings(false, false, '#000000') expect(addClassSpy).toHaveBeenCalledWith(document.body, 'primary-light') - expect(removeClassSpy).toHaveBeenCalledWith( - document.body, - 'color-scheme-system' + expect(setAttributeSpy).toHaveBeenCalledWith( + document.documentElement, + 'data-bs-theme', + 'light' ) + expect( document.body.style.getPropertyValue('--pngx-primary-lightness') ).toEqual('0%') settingsService.updateAppearanceSettings(false, true, '#ffffff') expect(addClassSpy).toHaveBeenCalledWith(document.body, 'primary-dark') - expect(removeClassSpy).toHaveBeenCalledWith( - document.body, - 'color-scheme-system' + expect(setAttributeSpy).toHaveBeenCalledWith( + document.documentElement, + 'data-bs-theme', + 'dark' ) - expect(addClassSpy).toHaveBeenCalledWith(document.body, 'color-scheme-dark') expect( document.body.style.getPropertyValue('--pngx-primary-lightness') ).toEqual('100%') diff --git a/src-ui/src/app/services/settings.service.ts b/src-ui/src/app/services/settings.service.ts index 134bb3ebb..1081bede1 100644 --- a/src-ui/src/app/services/settings.service.ts +++ b/src-ui/src/app/services/settings.service.ts @@ -106,19 +106,19 @@ export class SettingsService { themeColor ??= this.get(SETTINGS_KEYS.THEME_COLOR) if (darkModeUseSystem) { - this._renderer.addClass(this.document.body, 'color-scheme-system') - this._renderer.removeClass(this.document.body, 'color-scheme-dark') + this._renderer.setAttribute( + this.document.documentElement, + 'data-bs-theme', + 'auto' + ) } else { - this._renderer.removeClass(this.document.body, 'color-scheme-system') - darkModeEnabled - ? this._renderer.addClass(this.document.body, 'color-scheme-dark') - : this._renderer.removeClass(this.document.body, 'color-scheme-dark') + this._renderer.setAttribute( + this.document.documentElement, + 'data-bs-theme', + darkModeEnabled ? 'dark' : 'light' + ) } - // remove these in case they were there - this._renderer.removeClass(this.document.body, 'primary-dark') - this._renderer.removeClass(this.document.body, 'primary-light') - if (themeColor) { const hsl = hexToHsl(themeColor) const bgBrightnessEstimate = estimateBrightnessForColor(themeColor) @@ -142,6 +142,16 @@ export class SettingsService { `${hsl.l * 100}%`, RendererStyleFlags2.DashCase ) + + /** + * Fix for not reflecting changed variables. (--bs-primary is at :root while here we set them to body) + */ + this._renderer.setStyle( + document.body, + '--bs-primary', + 'hsl(var(--pngx-primary), var(--pngx-primary-lightness))', + RendererStyleFlags2.DashCase + ) } else { this._renderer.removeStyle( document.body, @@ -153,6 +163,11 @@ export class SettingsService { '--pngx-primary-lightness', RendererStyleFlags2.DashCase ) + this._renderer.removeStyle( + document.body, + '--bs-primary', + RendererStyleFlags2.DashCase + ) } } diff --git a/src-ui/src/index.html b/src-ui/src/index.html index c01df6243..118b9deff 100644 --- a/src-ui/src/index.html +++ b/src-ui/src/index.html @@ -1,5 +1,5 @@ - +