-
Notifications
You must be signed in to change notification settings - Fork 7
feat: System Theme Listener component #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e462440
initial commit
daviddecentage 0304401
Updating formatting
github-actions[bot] fe53343
fix minor typo
daviddecentage 588f076
Merge remote-tracking branch 'origin/feat/system-theme-listener' into…
daviddecentage d0b602f
fix minor typo
daviddecentage b7267f8
Updating formatting
github-actions[bot] a6b8580
fixes
daviddecentage 44691d4
Merge remote-tracking branch 'origin/feat/system-theme-listener' into…
daviddecentage 27bc6fd
Updating formatting
github-actions[bot] a445908
make prop truly optional, added test
daviddecentage 384505c
Merge remote-tracking branch 'origin/feat/system-theme-listener' into…
daviddecentage 27e6519
Updating formatting
github-actions[bot] 0e29a66
adjustments
daviddecentage 78e3a96
Merge remote-tracking branch 'origin/feat/system-theme-listener' into…
daviddecentage 6f41dac
Updating formatting
github-actions[bot] 6bb4c50
fix
daviddecentage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,30 @@ | ||
| <script lang="ts"> | ||
| import { onDestroy, onMount, createEventDispatcher } from "svelte"; | ||
| import { themeStore } from "$lib/stores/theme.store"; | ||
| import { isThemeSelected } from "$lib/utils/theme.utils"; | ||
|
|
||
| const dispatch = createEventDispatcher(); | ||
|
|
||
| // Set up our MediaQueryList | ||
| const matchMediaDark = window.matchMedia("(prefers-color-scheme: dark)"); | ||
|
|
||
| // Update the store if OS preference changes | ||
| const updateThemeOnChange = (e: MediaQueryListEvent) => { | ||
| dispatch("nnsSystemThemeChange", e); | ||
|
|
||
| // Only reset to system theme if no specific preference has been selected | ||
| if (isThemeSelected()) return; | ||
|
|
||
| themeStore.resetToSystemSettings(); | ||
| }; | ||
|
|
||
| // Register change event on mount | ||
| onMount(() => matchMediaDark.addEventListener("change", updateThemeOnChange)); | ||
|
|
||
| // Clean up if this component is destroyed | ||
| onDestroy(() => | ||
| matchMediaDark.removeEventListener("change", updateThemeOnChange), | ||
| ); | ||
| </script> | ||
|
|
||
| <slot /> | ||
This file contains hidden or 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
33 changes: 33 additions & 0 deletions
33
src/routes/(split)/components/system-theme-listener/+page.md
This file contains hidden or 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,33 @@ | ||
| # System Theme Listener | ||
|
|
||
| A component that listens to changes in the users system/OS theme. | ||
|
|
||
| # Usage | ||
|
|
||
| ```javascript | ||
| <SystemThemeListener /> | ||
| ``` | ||
|
|
||
| > By default, the newly selected theme will be saved in themeStore (as long as a theme has not been explicitly selected / system setting is selected). | ||
| > | ||
| > You may also pass a custom event handler to extend/add to this behavior: | ||
|
|
||
| ```javascript | ||
| <SystemThemeListener | ||
| on:nnsSystemThemeChange={(e: CustomEvent<MediaQueryListEvent>) => | ||
| console.log(`User selected ${e.detail.matches ? "dark" : "light"} mode`) | ||
| } | ||
| /> | ||
| ``` | ||
|
|
||
| ## Slots | ||
|
|
||
| | Slot name | Description | | ||
| | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | Default slot | Renders wrapped components<br/><br/>**Note:** Even though you can wrap other elements, the event listener that gets registered is global and not only applied to child elements, so it will always trigger as long as the SystemThemeListener component is mounted. | | ||
|
|
||
| ## Events | ||
|
|
||
| | Event | Description | Detail | | ||
| | ---------------------- | --------------------------------------- | ---------------------------------- | | ||
| | `nnsSystemThemeChange` | Triggered when the system theme changes | `CustomEvent<MediaQueryListEvent>` | |
This file contains hidden or 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,142 @@ | ||
| import { Theme, themeStore } from "$lib"; | ||
| import SystemThemeListener from "$lib/components/SystemThemeListener.svelte"; | ||
| import { render } from "@testing-library/svelte"; | ||
| import { get } from "svelte/store"; | ||
| import { vi } from "vitest"; | ||
|
|
||
| describe("SystemThemeListener", () => { | ||
| // Mock match media window events | ||
| const listeners: { | ||
| [key: string]: ((e: Partial<MediaQueryListEvent>) => void) | undefined; | ||
| } = {}; | ||
|
|
||
| const mockMatchMedia = vi.fn((query) => ({ | ||
| matches: query === "(prefers-color-scheme: dark)", | ||
| media: query, | ||
| addEventListener: ( | ||
| name: string, | ||
| handler: (e: Partial<MediaQueryListEvent>) => void, | ||
| ) => { | ||
| listeners[name] = handler; | ||
| }, | ||
| removeEventListener: ( | ||
| name: string, | ||
| handler: (e: Partial<MediaQueryListEvent>) => void, | ||
| ) => { | ||
| if (listeners[name] === handler) listeners[name] = undefined; | ||
| }, | ||
| dispatchEvent: (event: Partial<MediaQueryListEvent>) => | ||
| listeners[event.type || ""]?.({ matches: true }), | ||
| })); | ||
|
|
||
| vi.stubGlobal("matchMedia", mockMatchMedia); | ||
|
|
||
| it("should set theme to dark if no theme selected", () => { | ||
| render(SystemThemeListener); | ||
|
|
||
| // Init change event | ||
| const changeEvent = new Event("change", { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
|
|
||
| // Modify the event object to simulate the new theme state (e.g., matches = true for dark mode) | ||
| Object.defineProperty(changeEvent, "matches", { | ||
| value: true, // Simulate dark mode | ||
| }); | ||
|
|
||
| const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)"); | ||
| mediaQueryList.dispatchEvent(changeEvent); | ||
|
|
||
| expect(get(themeStore)).toEqual(Theme.DARK); | ||
| }); | ||
|
|
||
| it("should leave theme on light if light theme explicitly selected", () => { | ||
| render(SystemThemeListener); | ||
|
|
||
| // Set theme to light initially | ||
| themeStore.select(Theme.LIGHT); | ||
|
|
||
| // Init change event | ||
| const changeEvent = new Event("change", { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
|
|
||
| // Modify the event object to simulate the new theme state (e.g., matches = true for dark mode) | ||
| Object.defineProperty(changeEvent, "matches", { | ||
| value: true, // Simulate dark mode | ||
| }); | ||
|
|
||
| const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)"); | ||
| mediaQueryList.dispatchEvent(changeEvent); | ||
|
|
||
| expect(get(themeStore)).toEqual(Theme.LIGHT); | ||
| }); | ||
|
|
||
| it("should not change theme and run custom event handler", () => { | ||
| const initialTestValue = "unchanged"; | ||
| const expectedTestValue = "changed"; | ||
| let testValue = initialTestValue; | ||
|
|
||
| const listenerRender = render(SystemThemeListener); | ||
|
|
||
| listenerRender.component.$on("nnsSystemThemeChange", () => { | ||
| testValue = expectedTestValue; | ||
| }); | ||
|
|
||
| // Set theme to light initially | ||
| themeStore.select(Theme.LIGHT); | ||
|
|
||
| // Init change event | ||
| const changeEvent = new Event("change", { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
|
|
||
| // Modify the event object to simulate the new theme state (e.g., matches = true for dark mode) | ||
| Object.defineProperty(changeEvent, "matches", { | ||
| value: true, // Simulate dark mode | ||
| }); | ||
|
|
||
| const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)"); | ||
| mediaQueryList.dispatchEvent(changeEvent); | ||
|
|
||
| expect(get(themeStore)).toEqual(Theme.LIGHT); | ||
| expect(testValue).toEqual(expectedTestValue); | ||
| }); | ||
|
|
||
| it("should run custom event handler and receive media query event data", () => { | ||
| const initialTestValue = ""; | ||
| const expectedTestValue = "dark mode"; | ||
| let testValue = initialTestValue; | ||
|
|
||
| const listenerRender = render(SystemThemeListener); | ||
|
|
||
| listenerRender.component.$on( | ||
| "nnsSystemThemeChange", | ||
| (e: CustomEvent<MediaQueryListEvent>) => { | ||
| testValue = e.detail.matches ? "dark mode" : "light mode"; | ||
| }, | ||
| ); | ||
|
|
||
| // Set theme to light initially | ||
| themeStore.select(Theme.LIGHT); | ||
|
|
||
| // Init change event | ||
| const changeEvent = new Event("change", { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
|
|
||
| // Modify the event object to simulate the new theme state (e.g., matches = true for dark mode) | ||
| Object.defineProperty(changeEvent, "matches", { | ||
| value: true, // Simulate dark mode | ||
| }); | ||
|
|
||
| const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)"); | ||
| mediaQueryList.dispatchEvent(changeEvent); | ||
|
|
||
| expect(testValue).toEqual(expectedTestValue); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.