-
Notifications
You must be signed in to change notification settings - Fork 725
/
Presence.tsx
135 lines (115 loc) · 4.68 KB
/
Presence.tsx
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as React from 'react';
import { useComposedRefs } from '@radix-ui/react-compose-refs';
import { useStateMachine } from './useStateMachine';
type PresenceProps = {
present: boolean;
children: React.ReactElement | ((props: { present: boolean }) => React.ReactElement);
};
const Presence: React.FC<PresenceProps> = (props) => {
const { present, children } = props;
const presence = usePresence(present);
const child = (typeof children === 'function'
? children({ present: presence.isPresent })
: React.Children.only(children)) as React.ReactElement;
const ref = useComposedRefs(presence.ref, (child as any).ref);
const forceMount = typeof children === 'function';
return forceMount || presence.isPresent ? React.cloneElement(child, { ref }) : null;
};
Presence.displayName = 'Presence';
/* -------------------------------------------------------------------------------------------------
* usePresence
* -----------------------------------------------------------------------------------------------*/
function usePresence(present: boolean) {
const [node, setNode] = React.useState<HTMLElement>();
const stylesRef = React.useRef<CSSStyleDeclaration>({} as any);
const prevPresentRef = React.useRef(present);
const prevAnimationNameRef = React.useRef<string>();
const initialState = present ? 'mounted' : 'unmounted';
const [state, send] = useStateMachine(initialState, {
mounted: {
UNMOUNT: 'unmounted',
ANIMATION_OUT: 'unmountSuspended',
},
unmountSuspended: {
MOUNT: 'mounted',
ANIMATION_END: 'unmounted',
},
unmounted: {
MOUNT: 'mounted',
},
});
React.useEffect(() => {
const styles = stylesRef.current;
const wasPresent = prevPresentRef.current;
const hasPresentChanged = wasPresent !== present;
if (hasPresentChanged) {
const prevAnimationName = prevAnimationNameRef.current;
const currentAnimationName = getAnimationName(styles);
if (present) {
send('MOUNT');
} else if (currentAnimationName === 'none' || styles?.display === 'none') {
// If there is no exit animation or the element is hidden, animations won't run
// so we unmount instantly
send('UNMOUNT');
} else {
/**
* When `present` changes to `false`, we check changes to animation-name to
* determine whether an animation has started. We chose this approach (reading
* computed styles) because there is no `animationrun` event and `animationstart`
* fires after `animation-delay` has expired which would be too late.
*/
const isAnimating = prevAnimationName !== currentAnimationName;
if (wasPresent && isAnimating) {
send('ANIMATION_OUT');
} else {
send('UNMOUNT');
}
}
prevPresentRef.current = present;
}
}, [present, send]);
React.useEffect(() => {
if (node) {
/**
* Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`
* event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we
* make sure we only trigger ANIMATION_END for the currently active animation.
*/
const handleAnimationEnd = (event: AnimationEvent) => {
const currentAnimationName = getAnimationName(stylesRef.current);
const isCurrentAnimation = currentAnimationName.includes(event.animationName);
if (event.target === node && isCurrentAnimation) {
send('ANIMATION_END');
}
};
const handleAnimationStart = (event: AnimationEvent) => {
if (event.target === node) {
// if animation occurred, store its name as the previous animation.
prevAnimationNameRef.current = getAnimationName(stylesRef.current);
}
};
node.addEventListener('animationstart', handleAnimationStart);
node.addEventListener('animationcancel', handleAnimationEnd);
node.addEventListener('animationend', handleAnimationEnd);
return () => {
node.removeEventListener('animationstart', handleAnimationStart);
node.removeEventListener('animationcancel', handleAnimationEnd);
node.removeEventListener('animationend', handleAnimationEnd);
};
}
}, [node, send]);
return {
isPresent: ['mounted', 'unmountSuspended'].includes(state),
ref: React.useCallback((node: HTMLElement) => {
if (node) {
stylesRef.current = getComputedStyle(node);
setNode(node);
}
}, []),
};
}
/* -----------------------------------------------------------------------------------------------*/
function getAnimationName(styles?: CSSStyleDeclaration) {
return styles?.animationName || 'none';
}
export { Presence };