-
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 4 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,32 @@ | ||
| <script lang="ts"> | ||
| import { onDestroy, onMount } from "svelte"; | ||
| import { themeStore } from "$lib"; | ||
| import { LOCALSTORAGE_THEME_KEY } from "$lib/utils/theme.utils.js"; | ||
daviddecentage marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| export let nnsOnChange: undefined | ((isDarkMode: boolean) => void); | ||
daviddecentage marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Set up our MediaQueryList | ||
| const prefersDarkMode = window.matchMedia("(prefers-color-scheme: dark)"); | ||
daviddecentage marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Update the store if OS preference changes | ||
| const updateThemeOnChange = () => { | ||
| if (nnsOnChange) { | ||
| nnsOnChange(prefersDarkMode.matches); | ||
| } else { | ||
| // Only reset to system theme if no specific preference has been selected | ||
| if (window.localStorage.getItem(LOCALSTORAGE_THEME_KEY) !== null) return; | ||
daviddecentage marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| themeStore.resetToSystemSettings(); | ||
| } | ||
| }; | ||
|
|
||
| // Register change event on mount | ||
| onMount(() => | ||
| prefersDarkMode.addEventListener("change", updateThemeOnChange), | ||
peterpeterparker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| // Clean up if this component is destroyed | ||
| onDestroy(() => | ||
| prefersDarkMode.removeEventListener("change", updateThemeOnChange), | ||
| ); | ||
| </script> | ||
daviddecentage marked this conversation as resolved.
Show resolved
Hide resolved
|
||
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
| # 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 which overrides the default behavior | ||
|
|
||
| ```javascript | ||
| <SystemThemeListener | ||
| nnsOnChange={(prefersDarkMode) => | ||
| console.log(`User selected ${prefersDarkMode ? "dark" : "light"} mode`) | ||
| } | ||
| /> | ||
| ``` | ||
|
|
||
| ## Properties | ||
|
|
||
| | Property | Description | Type | Default | | ||
| | ------------- | ------------------------------------------------------------------- | ------------------------------------ | ------- | | ||
| | `nnsOnChange` | Override default behavior of savng the selected theme to themeStore | `(prefersDarkMode: boolean) => void` | | |
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.