Fix application of dark mode, tests

This commit is contained in:
shamoon 2023-09-12 19:55:08 -07:00
parent 04b22902f8
commit 527a6edf1a
4 changed files with 22 additions and 34 deletions

View File

@ -46,10 +46,10 @@ test('should warn on unsaved changes', async ({ page }) => {
test('should apply appearance changes when set', async ({ page }) => { test('should apply appearance changes when set', async ({ page }) => {
await page.routeFromHAR(REQUESTS_HAR, { notFound: 'fallback' }) await page.routeFromHAR(REQUESTS_HAR, { notFound: 'fallback' })
await page.goto('/settings') 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('Use system setting').click()
await page.getByLabel('Enable dark mode').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 }) => { test('should toggle saved view options when set & saved', async ({ page }) => {

View File

@ -147,23 +147,14 @@ describe('SettingsService', () => {
).toEqual('') ).toEqual('')
const addClassSpy = jest.spyOn(settingsService.renderer, 'addClass') const addClassSpy = jest.spyOn(settingsService.renderer, 'addClass')
const removeClassSpy = jest.spyOn(settingsService.renderer, 'removeClass')
const setAttributeSpy = jest.spyOn(settingsService.renderer, 'setAttribute') const setAttributeSpy = jest.spyOn(settingsService.renderer, 'setAttribute')
const removeAttributeSpy = jest.spyOn(
settingsService.renderer,
'removeAttribute'
)
settingsService.updateAppearanceSettings(true, true, '#fff000') settingsService.updateAppearanceSettings(true, true, '#fff000')
expect(addClassSpy).toHaveBeenCalledWith(document.body, 'primary-light') expect(addClassSpy).toHaveBeenCalledWith(document.body, 'primary-light')
expect(removeAttributeSpy).toHaveBeenCalledWith(
document.documentElement,
'data-bs-theme'
)
expect(setAttributeSpy).toHaveBeenCalledWith( expect(setAttributeSpy).toHaveBeenCalledWith(
document.documentElement, document.documentElement,
'data-bs-theme', 'data-bs-theme',
'dark' 'auto'
) )
expect( expect(
document.body.style.getPropertyValue('--pngx-primary-lightness') document.body.style.getPropertyValue('--pngx-primary-lightness')
@ -171,11 +162,11 @@ describe('SettingsService', () => {
settingsService.updateAppearanceSettings(false, false, '#000000') settingsService.updateAppearanceSettings(false, false, '#000000')
expect(addClassSpy).toHaveBeenCalledWith(document.body, 'primary-light') expect(addClassSpy).toHaveBeenCalledWith(document.body, 'primary-light')
expect(removeAttributeSpy).toHaveBeenCalledWith( expect(setAttributeSpy).toHaveBeenCalledWith(
document.documentElement, document.documentElement,
'data-bs-theme' 'data-bs-theme',
'light'
) )
expect(document.documentElement.hasAttribute('data-bs-theme')).toBe(false)
expect( expect(
document.body.style.getPropertyValue('--pngx-primary-lightness') document.body.style.getPropertyValue('--pngx-primary-lightness')
@ -183,10 +174,6 @@ describe('SettingsService', () => {
settingsService.updateAppearanceSettings(false, true, '#ffffff') settingsService.updateAppearanceSettings(false, true, '#ffffff')
expect(addClassSpy).toHaveBeenCalledWith(document.body, 'primary-dark') expect(addClassSpy).toHaveBeenCalledWith(document.body, 'primary-dark')
expect(removeAttributeSpy).toHaveBeenCalledWith(
document.documentElement,
'data-bs-theme'
)
expect(setAttributeSpy).toHaveBeenCalledWith( expect(setAttributeSpy).toHaveBeenCalledWith(
document.documentElement, document.documentElement,
'data-bs-theme', 'data-bs-theme',

View File

@ -105,25 +105,27 @@ export class SettingsService {
darkModeEnabled ??= this.get(SETTINGS_KEYS.DARK_MODE_ENABLED) darkModeEnabled ??= this.get(SETTINGS_KEYS.DARK_MODE_ENABLED)
themeColor ??= this.get(SETTINGS_KEYS.THEME_COLOR) themeColor ??= this.get(SETTINGS_KEYS.THEME_COLOR)
const isSystemColorSchemeDark = window.matchMedia( if (darkModeUseSystem) {
'(prefers-color-scheme: dark)' this._renderer.setAttribute(
).matches
// clearing state.
this._renderer.removeAttribute(
this.document.documentElement, this.document.documentElement,
'data-bs-theme' 'data-bs-theme',
'auto'
) )
this._renderer.removeClass(this.document.body, 'primary-dark') } else {
this._renderer.removeClass(this.document.body, 'primary-light') if (!darkModeEnabled) {
this._renderer.setAttribute(
if ((darkModeUseSystem && isSystemColorSchemeDark) || darkModeEnabled) { this.document.documentElement,
'data-bs-theme',
'light'
)
} else {
this._renderer.setAttribute( this._renderer.setAttribute(
this.document.documentElement, this.document.documentElement,
'data-bs-theme', 'data-bs-theme',
'dark' 'dark'
) )
} }
}
if (themeColor) { if (themeColor) {
const hsl = hexToHsl(themeColor) const hsl = hexToHsl(themeColor)

View File

@ -137,7 +137,6 @@ $form-check-radio-checked-bg-image-dark: url("data:image/svg+xml,<svg xmlns='htt
} }
.card { .card {
--bs-border-color-translucent: 0, 0, 0, .175;
background-color: var(--bs-body-bg); background-color: var(--bs-body-bg);
.card-header { .card-header {