File size: 1,212 Bytes
3c56493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { derived, readable } from 'svelte/store';
import { browser } from '$app/environment';
import { userPreferencesStore } from './userPreferences';
import { type PerformanceLevel } from '$lib/utils/performance';

/**
 * Derived store that provides the effective performance level
 * based on user preferences.
 */
export const effectivePerformanceLevel = derived(
	userPreferencesStore,
	($prefs): PerformanceLevel => {
		return $prefs.performanceMode as PerformanceLevel;
	}
);

/**
 * Store that tracks if user prefers reduced motion
 */
export const reducedMotion = readable(false, (set) => {
	if (!browser) {
		return;
	}

	const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)');

	set(mediaQuery.matches);

	const handler = (e: MediaQueryListEvent) => {
		set(e.matches);
	};

	mediaQuery.addEventListener('change', handler);

	return () => {
		mediaQuery.removeEventListener('change', handler);
	};
});

/**
 * Derived store that determines if animations should be enabled
 */
export const animationsEnabled = derived(
	[effectivePerformanceLevel, reducedMotion],
	([$perfLevel, $reducedMotion]) => {
		if ($reducedMotion) {
			return false;
		}

		return $perfLevel !== 'low';
	}
);