Skip to content
Merged
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
16 changes: 12 additions & 4 deletions apps/tauri/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

const shell = useShellStore();
const motion = provideQueryaMotion();
const { level: motionLevel, springsOn } = motion;
const { level: motionLevel, springsOn, manual: motionManual } = motion;
const backend = ref<string>("…");
const theme = ref<QueryaThemeMode>("system");
const panelOpen = ref(false);
Expand Down Expand Up @@ -104,23 +104,31 @@ function setMotion(level: QueryaMotionLevel) {
<button
type="button"
class="btn"
:class="{ active: motionLevel === 'full' }"
:class="{ active: !motionManual }"
@click="motion.followSystem()"
>
System
</button>
<button
type="button"
class="btn"
:class="{ active: motionManual && motionLevel === 'full' }"
@click="setMotion('full')"
>
Full
</button>
<button
type="button"
class="btn"
:class="{ active: motionLevel === 'reduced' }"
:class="{ active: motionManual && motionLevel === 'reduced' }"
@click="setMotion('reduced')"
>
Reduced
</button>
<button
type="button"
class="btn"
:class="{ active: motionLevel === 'off' }"
:class="{ active: motionManual && motionLevel === 'off' }"
@click="setMotion('off')"
>
Off
Expand Down
73 changes: 62 additions & 11 deletions apps/tauri/src/motion/useQueryaMotion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { computed, inject, provide, ref, type InjectionKey, type Ref } from "vue";
import {
computed,
inject,
onScopeDispose,
provide,
ref,
type InjectionKey,
type Ref,
} from "vue";
import {
effectiveDurationMs,
effectiveEasing,
Expand All @@ -13,37 +21,72 @@ const STORAGE_KEY = "querya.motion";

type MotionApi = {
level: Ref<QueryaMotionLevel>;
/** True when the user picked Full/Reduced/Off (not following OS). */
manual: Ref<boolean>;
setLevel: (level: QueryaMotionLevel) => void;
/** Clear override and follow prefers-reduced-motion again. */
followSystem: () => void;
duration: (token?: QueryaMotionToken | number) => number;
easing: (name?: keyof typeof QueryaMotionEasing) => string;
springsOn: Ref<boolean>;
};

const MotionKey: InjectionKey<MotionApi> = Symbol("querya-motion");

function readStoredLevel(): QueryaMotionLevel {
function prefersReducedMotion(): boolean {
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
}

function levelFromSystem(): QueryaMotionLevel {
return prefersReducedMotion() ? "reduced" : "full";
}

function readStoredLevel(): QueryaMotionLevel | null {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw === "full" || raw === "reduced" || raw === "off") return raw;
return "full";
return null;
}

function applyDom(next: QueryaMotionLevel) {
document.documentElement.dataset.motion = next;
}

export function provideQueryaMotion(
initial: QueryaMotionLevel = readStoredLevel(),
initial?: QueryaMotionLevel,
): MotionApi {
const level = ref<QueryaMotionLevel>(initial);

function applyDom(next: QueryaMotionLevel) {
document.documentElement.dataset.motion = next;
localStorage.setItem(STORAGE_KEY, next);
}
const stored = readStoredLevel();
const manual = ref(stored !== null);
const level = ref<QueryaMotionLevel>(
initial ?? stored ?? levelFromSystem(),
);

applyDom(level.value);

function setLevel(next: QueryaMotionLevel) {
manual.value = true;
level.value = next;
applyDom(next);
localStorage.setItem(STORAGE_KEY, next);
}

function followSystem() {
manual.value = false;
localStorage.removeItem(STORAGE_KEY);
const next = levelFromSystem();
level.value = next;
applyDom(next);
}

const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
const onMq = () => {
if (manual.value) return;
const next = levelFromSystem();
level.value = next;
applyDom(next);
};
mq.addEventListener("change", onMq);
onScopeDispose(() => mq.removeEventListener("change", onMq));

function duration(token: QueryaMotionToken | number = "standard"): number {
const ms = typeof token === "number" ? token : QueryaMotionMs[token];
return effectiveDurationMs(level.value, ms);
Expand All @@ -55,7 +98,15 @@ export function provideQueryaMotion(

const springsOn = computed(() => springsEnabled(level.value));

const api: MotionApi = { level, setLevel, duration, easing, springsOn };
const api: MotionApi = {
level,
manual,
setLevel,
followSystem,
duration,
easing,
springsOn,
};
provide(MotionKey, api);
return api;
}
Expand Down
Loading