Skip to content

Commit

Permalink
feat: disableTransitions prop (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte authored Mar 12, 2024
1 parent 4822c7f commit 920f231
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 7 deletions.
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Hunter Johnston <https://github.com/huntabyte>",
"main": "index.js",
"scripts": {
"test": "pnpm -r test",
"test": "pnpm -F mode-watcher test",
"dev": "pnpm -r --parallel dev",
"build": "pnpm -r build",
"build:packages": "pnpm -F \"./packages/**\" --parallel build",
Expand Down
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,
};
20 changes: 16 additions & 4 deletions packages/mode-watcher/src/lib/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export const systemPrefersMode = createSystemMode();
* Theme colors for light and dark modes.
*/
export const themeColors = writable<ThemeColors>(undefined);

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

/**
* Derived store that represents the current mode (`"dark"`, `"light"` or `undefined`)
*/
Expand Down Expand Up @@ -114,13 +120,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 +142,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;
};
7 changes: 7 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,12 @@ export type ModeWatcherProps = {
* @defaultValue `undefined`
*/
themeColors?: ThemeColors;

/**
* Whether to disable transitions when the mode changes.
*
* @defaultValue `true`
*/
disableTransitions?: boolean;
};
```
1 change: 0 additions & 1 deletion sites/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"preview": "vite preview",
"check": "pnpm build:content && svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"test": "vitest",
"lint": "prettier --check . && eslint .",
"format": "prettier --write ."
},
Expand Down

0 comments on commit 920f231

Please sign in to comment.