| 
									
										
										
										
											2020-10-30 15:34:04 -05:00
										 |  |  | // Copyright 2020 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-17 17:23:19 -05:00
										 |  |  | import * as React from 'react'; | 
					
						
							| 
									
										
										
										
											2020-05-05 15:49:34 -04:00
										 |  |  | import { ActionCreatorsMapObject, bindActionCreators } from 'redux'; | 
					
						
							|  |  |  | import { useDispatch } from 'react-redux'; | 
					
						
							| 
									
										
										
										
											2020-01-17 17:23:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Restore focus on teardown
 | 
					
						
							|  |  |  | export const useRestoreFocus = ( | 
					
						
							|  |  |  |   // The ref for the element to receive initial focus
 | 
					
						
							| 
									
										
										
										
											2020-09-14 14:56:35 -07:00
										 |  |  |   focusRef: React.RefObject<HTMLElement>, | 
					
						
							| 
									
										
										
										
											2020-01-17 17:23:19 -05:00
										 |  |  |   // Allow for an optional root element that must exist
 | 
					
						
							|  |  |  |   root: boolean | HTMLElement | null = true | 
					
						
							| 
									
										
										
										
											2020-09-14 14:56:35 -07:00
										 |  |  | ): void => { | 
					
						
							| 
									
										
										
										
											2020-01-17 17:23:19 -05:00
										 |  |  |   React.useEffect(() => { | 
					
						
							|  |  |  |     if (!root) { | 
					
						
							| 
									
										
										
										
											2020-09-14 14:56:35 -07:00
										 |  |  |       return undefined; | 
					
						
							| 
									
										
										
										
											2020-01-17 17:23:19 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 14:56:35 -07:00
										 |  |  |     const lastFocused = document.activeElement as HTMLElement; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-17 17:23:19 -05:00
										 |  |  |     if (focusRef.current) { | 
					
						
							|  |  |  |       focusRef.current.focus(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return () => { | 
					
						
							| 
									
										
										
										
											2020-02-26 23:09:19 -03:00
										 |  |  |       // This ensures that the focus is returned to
 | 
					
						
							|  |  |  |       // previous element
 | 
					
						
							|  |  |  |       setTimeout(() => { | 
					
						
							|  |  |  |         if (lastFocused && lastFocused.focus) { | 
					
						
							|  |  |  |           lastFocused.focus(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-01-17 17:23:19 -05:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, [focusRef, root]); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-05-05 15:49:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const useBoundActions = <T extends ActionCreatorsMapObject>( | 
					
						
							|  |  |  |   actions: T | 
					
						
							| 
									
										
										
										
											2020-09-14 14:56:35 -07:00
										 |  |  | ): T => { | 
					
						
							| 
									
										
										
										
											2020-05-05 15:49:34 -04:00
										 |  |  |   const dispatch = useDispatch(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return React.useMemo(() => { | 
					
						
							|  |  |  |     return bindActionCreators(actions, dispatch); | 
					
						
							| 
									
										
										
										
											2020-09-14 14:56:35 -07:00
										 |  |  |   }, [actions, dispatch]); | 
					
						
							| 
									
										
										
										
											2020-05-05 15:49:34 -04:00
										 |  |  | }; |