usePrevFocus.ts 495 B

12345678910111213141516
  1. import { useState, useEffect } from 'react';
  2. import { getActiveElement } from '../index';
  3. import { get, isFunction } from 'lodash';
  4. export function usePrevFocus() {
  5. const [prevFocusElement, setPrevFocus] = useState<HTMLElement>(getActiveElement());
  6. useEffect(() => {
  7. return function cleanup() {
  8. const blur = get(prevFocusElement, 'blur');
  9. isFunction(blur) && blur();
  10. };
  11. }, [prevFocusElement]);
  12. return [prevFocusElement, setPrevFocus];
  13. }