Skip to content
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

feat: add defaultMode to DarkMode component #1384

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib/darkmode/DarkMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export let btnClass: string = 'text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:outline-none rounded-lg text-sm p-2.5';
export let size: 'sm' | 'md' | 'lg' = 'md';
export let ariaLabel: string = 'Dark mode';
export let defaultMode: 'light' | 'dark' | undefined = undefined;

const sizes = {
sm: 'w-4 h-4',
Expand All @@ -25,8 +26,10 @@
if ('color-theme' in localStorage) {
// explicit preference - overrides author's choice
localStorage.getItem('color-theme') === 'dark' ? window.document.documentElement.classList.add('dark') : window.document.documentElement.classList.remove('dark');
} else if (defaultMode) {
defaultMode === 'dark' ? window.document.documentElement.classList.add('dark') : window.document.documentElement.classList.remove('dark');
} else {
// browser preference - does not overrides
// browser preference - does not override
if (window.matchMedia('(prefers-color-scheme: dark)').matches) window.document.documentElement.classList.add('dark');
}
</script>
Expand Down