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

add disableTransitions prop #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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: 5 additions & 0 deletions .changeset/empty-turkeys-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mode-watcher": minor
---

feat: `disableTransitions` prop
4 changes: 4 additions & 0 deletions packages/mode-watcher/src/lib/mode-watcher.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
localStorageKey,
mode,
themeColors as themeColorsStore,
disableTransitions as disableTransitionsStore,
setInitialMode,
} from './mode.js';

import type { Mode, ModeWatcherProps, ThemeColors } from './types.js';
import { isValidMode } from './stores.js';

Expand All @@ -16,8 +18,10 @@
export let track = true;
export let defaultMode: Mode = 'system';
export let themeColors: ThemeColors = undefined;
export let disableTransitions = true;

themeColorsStore.set(themeColors);
disableTransitionsStore.set(disableTransitions);

onMount(() => {
const unsubscriber = mode.subscribe(() => {});
Expand Down
10 changes: 9 additions & 1 deletion packages/mode-watcher/src/lib/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
systemPrefersMode,
derivedMode,
themeColors,
disableTransitions,
} from './stores.js';
import type { Mode, ThemeColors } from './types.js';

Expand Down Expand Up @@ -44,4 +45,11 @@ export function setInitialMode(defaultMode: Mode, themeColors?: ThemeColors) {
localStorage.setItem('mode-watcher-mode', mode);
}

export { localStorageKey, userPrefersMode, systemPrefersMode, derivedMode as mode, themeColors };
export {
localStorageKey,
userPrefersMode,
systemPrefersMode,
derivedMode as mode,
themeColors,
disableTransitions,
};
19 changes: 15 additions & 4 deletions packages/mode-watcher/src/lib/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export const themeColors = writable<ThemeColors>(undefined);
*/
export const derivedMode = createDerivedMode();

/**
* Whether to disable transitions when changing the mode.
*/
export const disableTransitions = writable(true);

// derived from: https://github.com/CaptainCodeman/svelte-web-storage
function createUserPrefersMode() {
const defaultValue = 'system';
Expand Down Expand Up @@ -114,13 +119,13 @@ function createSystemMode() {

function createDerivedMode() {
const { subscribe } = derived(
[userPrefersMode, systemPrefersMode, themeColors],
([$userPrefersMode, $systemPrefersMode, $themeColors]) => {
[userPrefersMode, systemPrefersMode, themeColors, disableTransitions],
([$userPrefersMode, $systemPrefersMode, $themeColors, $disableTransitions]) => {
if (!isBrowser) return undefined;

const derivedMode = $userPrefersMode === 'system' ? $systemPrefersMode : $userPrefersMode;

withoutTransition(() => {
function update() {
const htmlEl = document.documentElement;
const themeColorEl = document.querySelector('meta[name="theme-color"]');
if (derivedMode === 'light') {
Expand All @@ -136,7 +141,13 @@ function createDerivedMode() {
themeColorEl.setAttribute('content', $themeColors.dark);
}
}
});
}

if ($disableTransitions) {
withoutTransition(update);
} else {
update();
}

return derivedMode;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/mode-watcher/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ export type ModeWatcherProps = {
* The theme colors to use for each mode.
*/
themeColors?: ThemeColors;

/**
* Whether to disable transitions when updating the mode.
*/
disableTransitions?: boolean;
};
8 changes: 8 additions & 0 deletions sites/docs/content/api-reference/mode-watcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,13 @@ export type ModeWatcherProps = {
* @defaultValue `undefined`
*/
themeColors?: ThemeColors;

/**
* Whether to disable transitions when the mode changes.
*
* @defaultValue `true`
*/
disableTransitions?: boolean;

};
```
Loading