usePrevFocus.ts 522 B

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