FocusToGlobalSearch.jsx 786 B

12345678910111213141516171819202122232425262728293031323334
  1. import { useEffect } from 'react';
  2. import { useIsEditable } from '~/stores/context';
  3. import { useGlobalSearchFormRef } from '~/stores/ui';
  4. const FocusToGlobalSearch = (props) => {
  5. const { data: isEditable } = useIsEditable();
  6. const { data: globalSearchFormRef } = useGlobalSearchFormRef();
  7. // setup effect
  8. useEffect(() => {
  9. if (!isEditable) {
  10. return;
  11. }
  12. // ignore when dom that has 'modal in' classes exists
  13. if (document.getElementsByClassName('modal in').length > 0) {
  14. return;
  15. }
  16. globalSearchFormRef.current.focus();
  17. // remove this
  18. props.onDeleteRender();
  19. }, [globalSearchFormRef, isEditable, props]);
  20. return null;
  21. };
  22. FocusToGlobalSearch.getHotkeyStrokes = () => {
  23. return [['/']];
  24. };
  25. export default FocusToGlobalSearch;