MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useExitAnimation

Function useExitAnimation

packages/react-aria/src/utils/animation.ts:67–105  ·  view source on GitHub ↗
(
  ref: RefObject<HTMLElement | null>,
  isOpen: boolean,
  onExit?: (element: HTMLElement) => void | Promise<void>
)

Source from the content-addressed store, hash-verified

65}
66
67export function useExitAnimation(
68 ref: RefObject<HTMLElement | null>,
69 isOpen: boolean,
70 onExit?: (element: HTMLElement) => void | Promise<void>
71): boolean {
72 let [exitState, setExitState] = useState<'closed' | 'open' | 'exiting'>(
73 isOpen ? 'open' : 'closed'
74 );
75
76 switch (exitState) {
77 case 'open':
78 // If isOpen becomes false, set the state to exiting.
79 if (!isOpen) {
80 setExitState('exiting');
81 }
82 break;
83 case 'closed':
84 case 'exiting':
85 // If we are exiting and isOpen becomes true, the animation was interrupted.
86 // Reset the state to open.
87 if (isOpen) {
88 setExitState('open');
89 }
90 break;
91 }
92
93 let isExiting = exitState === 'exiting';
94 useAnimation(
95 ref,
96 isExiting,
97 onExit,
98 useCallback(() => {
99 // Set the state to closed, which will cause the element to be unmounted.
100 setExitState(state => (state === 'exiting' ? 'closed' : state));
101 }, [])
102 );
103
104 return isExiting;
105}
106
107function useAnimation(
108 ref: RefObject<HTMLElement | null>,

Callers 6

Popover.tsxFile · 0.90
Tooltip.tsxFile · 0.90
Tabs.tsxFile · 0.90
ThreadScrollButtonFunction · 0.90
ActionBar.tsxFile · 0.90

Calls 1

useAnimationFunction · 0.85

Tested by

no test coverage detected