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
5 changes: 5 additions & 0 deletions .changeset/yellow-pianos-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workflow/web-shared": patch
---

Add reduced motion for the trace viewer
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useRef,
useState,
} from 'react';
import { useReducedMotion } from '../../hooks/use-reduced-motion';
import { ErrorBoundary } from '../error-boundary';
import {
EntityDetailPanel,
Expand Down Expand Up @@ -51,6 +52,7 @@ function useAnimatedViewport(initial: Viewport) {
} | null>(null);
const currentRef = useRef(initial);
currentRef.current = viewport;
const reducedMotion = useReducedMotion();

const cancel = useCallback(() => {
if (animRef.current) {
Expand All @@ -62,6 +64,13 @@ function useAnimatedViewport(initial: Viewport) {
const animateTo = useCallback(
(target: Viewport) => {
cancel();

if (reducedMotion) {
currentRef.current = target;
setViewportState(target);
return;
}

const from = currentRef.current;
const anim = { raf: 0, from, to: target, start: performance.now() };

Expand All @@ -79,7 +88,7 @@ function useAnimatedViewport(initial: Viewport) {
animRef.current = anim;
anim.raf = requestAnimationFrame(tick);
},
[cancel]
[cancel, reducedMotion]
);

const setViewport = useCallback(
Expand Down
34 changes: 34 additions & 0 deletions packages/web-shared/src/hooks/use-reduced-motion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use client';

import { useEffect, useState } from 'react';

const QUERY = '(prefers-reduced-motion: reduce)';

/**
* Hook that detects whether the user has requested reduced motion and reacts
* to changes. Mirrors the `prefers-reduced-motion: reduce` media query so
* components can skip or shorten animations for users with motion
* sensitivities (e.g. vestibular disorders).
*
* @returns `true` if the user prefers reduced motion, `false` otherwise
*/
export const useReducedMotion = (): boolean => {
const [reduced, setReduced] = useState(() => {
if (typeof window === 'undefined' || !window.matchMedia) return false;
return window.matchMedia(QUERY).matches;
});

useEffect(() => {
if (typeof window === 'undefined' || !window.matchMedia) return;

const media = window.matchMedia(QUERY);
const onChange = (): void => setReduced(media.matches);

Comment thread
mitul-s marked this conversation as resolved.
media.addEventListener('change', onChange);
onChange();

return () => media.removeEventListener('change', onChange);
}, []);

return reduced;
};
Loading