useHighlight.ts 516 B

12345678910111213141516171819
  1. import { useEffect, useState } from 'react';
  2. import useMountedState from 'react-use/lib/useMountedState';
  3. export function useHighlight(focusedNodeId?: string) {
  4. const [highlightId, setHighlightId] = useState<string>();
  5. const mounted = useMountedState();
  6. useEffect(() => {
  7. if (focusedNodeId) {
  8. setHighlightId(focusedNodeId);
  9. setTimeout(() => {
  10. if (mounted()) {
  11. setHighlightId(undefined);
  12. }
  13. }, 500);
  14. }
  15. }, [focusedNodeId, mounted]);
  16. return highlightId;
  17. }