All files / src/components/ui/hooks use-ime-is-composing.ts

88.88% Statements 16/18
50% Branches 2/4
60% Functions 3/5
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3212x   12x     7x   7x 7x   7x 4x   4x 4x 4x     4x 4x 4x       4x         7x    
import { RefObject, useCallback, useEffect, useState } from "react";
 
export const useIMEIsComposing = (
  inputRef: RefObject<HTMLInputElement | null>,
) => {
  const [isComposing, setComposition] = useState(false);
 
  const onCompositionStart = useCallback(() => setComposition(true), []);
  const onCompositionEnd = useCallback(() => setComposition(false), []);
 
  useEffect(() => {
    const inputElement = inputRef.current;
 
    Eif (inputElement) {
      inputElement.addEventListener("compositionstart", onCompositionStart);
      inputElement.addEventListener("compositionend", onCompositionEnd);
    }
 
    return () => {
      Eif (inputElement) {
        inputElement.removeEventListener(
          "compositionstart",
          onCompositionStart,
        );
        inputElement.removeEventListener("compositionend", onCompositionEnd);
      }
    };
  }, [inputRef, onCompositionStart, onCompositionEnd]);
 
  return isComposing;
};