-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Init dark mode with feature flag and body class (#4687)
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { computed } from "#imports" | ||
|
||
import { useFeatureFlagStore } from "~/stores/feature-flag" | ||
|
||
export const DARK_MODE_CLASS = "dark-mode" | ||
export const LIGHT_MODE_CLASS = "light-mode" | ||
|
||
/** | ||
* TODO: Replace with the user's actual dark mode preference. | ||
* Dark mode detection will be based on user preference, | ||
* overwritten by the "force_dark_mode" feature flag. | ||
*/ | ||
export function useDarkMode() { | ||
const featureFlagStore = useFeatureFlagStore() | ||
|
||
const isDarkMode = computed(() => featureFlagStore.isOn("force_dark_mode")) | ||
const cssClass = computed(() => | ||
isDarkMode.value ? DARK_MODE_CLASS : LIGHT_MODE_CLASS | ||
) | ||
|
||
return { | ||
isDarkMode, | ||
/** The CSS class representing the current color mode. */ | ||
cssClass, | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
frontend/test/unit/specs/composables/use-dark-mode.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { describe, it, expect } from "vitest" | ||
|
||
import { | ||
DARK_MODE_CLASS, | ||
LIGHT_MODE_CLASS, | ||
useDarkMode, | ||
} from "~/composables/use-dark-mode" | ||
import { OFF, ON } from "~/constants/feature-flag" | ||
import { useFeatureFlagStore } from "~/stores/feature-flag" | ||
|
||
describe("useDarkMode", () => { | ||
it(`should report isDarkMode as true and cssClass as ${DARK_MODE_CLASS} when the feature flag is enabled`, () => { | ||
const featureFlagStore = useFeatureFlagStore() | ||
featureFlagStore.toggleFeature("force_dark_mode", ON) | ||
|
||
// Call the composable | ||
const { isDarkMode, cssClass } = useDarkMode() | ||
|
||
// Assert the computed properties | ||
expect(isDarkMode.value).toBe(true) | ||
expect(cssClass.value).toBe(DARK_MODE_CLASS) | ||
}) | ||
|
||
it(`should report isDarkMode as false and cssClass as ${LIGHT_MODE_CLASS} when the feature flag is disabled`, () => { | ||
const featureFlagStore = useFeatureFlagStore() | ||
featureFlagStore.toggleFeature("force_dark_mode", OFF) | ||
|
||
// Call the composable | ||
const { isDarkMode, cssClass } = useDarkMode() | ||
|
||
// Assert the computed properties | ||
expect(isDarkMode.value).toBe(false) | ||
expect(cssClass.value).toBe(LIGHT_MODE_CLASS) | ||
}) | ||
}) |