import { useEffect, useRef } from 'react';

/**
 * Returns a ref and will focus on the element that ref is attached to
 * if the `autoFocus` boolean parameter is truthy.
 */
export var useAutofocus = function useAutofocus(autoFocus) {
  var ref = useRef();
  useEffect(function () {
    var _ref$current;
    if (autoFocus && (_ref$current = ref.current) !== null && _ref$current !== void 0 && _ref$current.focus) {
      var _ref$current2;
      (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : _ref$current2.focus();
    }
  }, [ref, autoFocus]);
  return ref;
};
export default useAutofocus;