Skip to content

Commit 8057bb3

Browse files
committed
Add fade animation to reduce motion
1 parent 468c303 commit 8057bb3

File tree

3 files changed

+45
-46
lines changed

3 files changed

+45
-46
lines changed

src/components/toast-bar.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const exitAnimation = (factor: number) => `
1515
100% {transform: translate3d(0,${factor * -150}%,-1px) scale(.6); opacity:0;}
1616
`;
1717

18+
const fadeInAnimation = `0%{opacity:0;} 100%{opacity:1;}`;
19+
const fadeOutAnimation = `0%{opacity:1;} 100%{opacity:0;}`;
20+
1821
const ToastBarBase = styled('div', React.forwardRef)`
1922
display: flex;
2023
align-items: center;
@@ -53,28 +56,25 @@ const getAnimationStyle = (
5356
): React.CSSProperties => {
5457
const top = position.includes('top');
5558
const factor = top ? 1 : -1;
56-
return visible
57-
? {
58-
animation: `${keyframes`${enterAnimation(
59-
factor
60-
)}`} 0.35s cubic-bezier(.21,1.02,.73,1) forwards`,
61-
}
62-
: {
63-
animation: `${keyframes`${exitAnimation(
64-
factor
65-
)}`} 0.4s forwards cubic-bezier(.06,.71,.55,1)`,
66-
};
59+
60+
const [enter, exit] = prefersReducedMotion()
61+
? [fadeInAnimation, fadeOutAnimation]
62+
: [enterAnimation(factor), exitAnimation(factor)];
63+
64+
return {
65+
animation: visible
66+
? `${keyframes(enter)} 0.35s cubic-bezier(.21,1.02,.73,1) forwards`
67+
: `${keyframes(exit)} 0.4s forwards cubic-bezier(.06,.71,.55,1)`,
68+
};
6769
};
6870

6971
export const ToastBar: React.FC<ToastBarProps> = React.memo(
7072
({ toast, position, style, children }) => {
7173
const animationStyle: React.CSSProperties = toast?.height
72-
? prefersReducedMotion()
73-
? {}
74-
: getAnimationStyle(
75-
toast.position || position || 'top-center',
76-
toast.visible
77-
)
74+
? getAnimationStyle(
75+
toast.position || position || 'top-center',
76+
toast.visible
77+
)
7878
: { opacity: 0 };
7979

8080
const icon = <ToastIcon toast={toast} />;

src/components/toaster.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ const getPositionStyle = (
3333
right: 0,
3434
display: 'flex',
3535
position: 'absolute',
36-
transition: `all ${
37-
prefersReducedMotion() ? 0 : 230
38-
}ms cubic-bezier(.21,1.02,.73,1)`,
36+
transition: prefersReducedMotion()
37+
? undefined
38+
: `all 230ms cubic-bezier(.21,1.02,.73,1)`,
3939
transform: `translateY(${offset * (top ? 1 : -1)}px)`,
4040
...verticalStyle,
4141
...horizontalStyle,

src/core/store.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useEffect, useState } from 'react';
22
import { DefaultToastOptions, Toast, ToastType } from './types';
3-
import { prefersReducedMotion } from './utils';
43

54
const TOAST_LIMIT = 20;
65

@@ -16,33 +15,33 @@ export enum ActionType {
1615

1716
type Action =
1817
| {
19-
type: ActionType.ADD_TOAST;
20-
toast: Toast;
21-
}
18+
type: ActionType.ADD_TOAST;
19+
toast: Toast;
20+
}
2221
| {
23-
type: ActionType.UPSERT_TOAST;
24-
toast: Toast;
25-
}
22+
type: ActionType.UPSERT_TOAST;
23+
toast: Toast;
24+
}
2625
| {
27-
type: ActionType.UPDATE_TOAST;
28-
toast: Partial<Toast>;
29-
}
26+
type: ActionType.UPDATE_TOAST;
27+
toast: Partial<Toast>;
28+
}
3029
| {
31-
type: ActionType.DISMISS_TOAST;
32-
toastId?: string;
33-
}
30+
type: ActionType.DISMISS_TOAST;
31+
toastId?: string;
32+
}
3433
| {
35-
type: ActionType.REMOVE_TOAST;
36-
toastId?: string;
37-
}
34+
type: ActionType.REMOVE_TOAST;
35+
toastId?: string;
36+
}
3837
| {
39-
type: ActionType.START_PAUSE;
40-
time: number;
41-
}
38+
type: ActionType.START_PAUSE;
39+
time: number;
40+
}
4241
| {
43-
type: ActionType.END_PAUSE;
44-
time: number;
45-
};
42+
type: ActionType.END_PAUSE;
43+
time: number;
44+
};
4645

4746
interface State {
4847
toasts: Toast[];
@@ -64,7 +63,7 @@ const addToRemoveQueue = (toastId: string) => {
6463
toastId: toastId,
6564
});
6665
},
67-
prefersReducedMotion() ? 0 : 1000
66+
1000
6867
);
6968

7069
toastTimeouts.set(toastId, timeout);
@@ -121,9 +120,9 @@ export const reducer = (state: State, action: Action): State => {
121120
toasts: state.toasts.map((t) =>
122121
t.id === toastId || toastId === undefined
123122
? {
124-
...t,
125-
visible: false,
126-
}
123+
...t,
124+
visible: false,
125+
}
127126
: t
128127
),
129128
};

0 commit comments

Comments
 (0)