How to use the @react-md/utils.useTimeout function in @react-md/utils

To help you get started, we’ve selected a few @react-md/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mlaursen / react-md / packages / tooltip / src / useHandlers.ts View on Github external
export function useKeyboardState({
  mode,
  showTooltip,
  hideTooltip,
  delay,
  initiated,
  setInitiated,
  onFocus,
  onBlur,
  onKeyDown,
  setEstimatedPosition,
}: KeyboardOptions): KeyboardResult {
  const handlers = useRefCache({ onFocus, onBlur, onKeyDown });
  const isWindowBlurred = useRef(false);

  const [start, stop] = useTimeout(() => {
    if (initiated.current === "keyboard") {
      showTooltip();
    }
  }, delay);

  const handleFocus = useCallback(
    (event: React.FocusEvent) => {
      const { onFocus } = handlers.current;
      if (onFocus) {
        onFocus(event);
      }

      // if the entire browser window was blurred, we don't want to show the tooltip
      // on the next focus event since it is confusing to see a tooltip appear again
      // after re-focusing a window.
      if (isWindowBlurred.current) {
github mlaursen / react-md / packages / tooltip / src / useTooltipHoverMode.ts View on Github external
export function useTooltipHoverModeState(
  defaultDelay: number,
  delayTimeout: number
): TooltipHoverModeState {
  const [delay, setDelay] = useState(defaultDelay);
  const delayRef = useRefCache(delay);

  const disable = useCallback(() => {
    if (delayRef.current === 0) {
      setDelay(defaultDelay);
    }
    // disabled since useRefCache
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [defaultDelay]);

  const [start, stop] = useTimeout(disable, delayTimeout);
  const enable = useCallback(() => {
    stop();
    if (delayRef.current !== 0) {
      setDelay(0);
    }
    // disabled since useRefCache
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [stop]);

  return {
    delay,
    enable,
    startDisableTimer: start,
  };
}
github mlaursen / react-md / packages / alert / src / useMessageQueue.ts View on Github external
},
    [duplicates]
  );

  const popMessageDispatch = useCallback(() => {
    dispatch(popMessage());
  }, []);

  const resetQueueDispatch = useCallback>(() => {
    dispatch(resetQueue());
    return queueRef.current;
  }, []);
  const [visible, showMessage, hideMessage] = useToggle(
    defaultQueue.length > 0
  );
  const [startTimer, stopTimer, restartTimer] = useTimeout(
    hideMessage,
    timeout
  );

  useEffect(() => {
    // this effect will handle all the "logic" for transitioning between each message along with the
    // message priority updates.
    const [message, nextMessage] = queue;
    if (!message) {
      return;
    }

    const prevQueue = queueRef.current;
    const [prevMessage] = prevQueue;
    if (
      message.messagePriority !== "immediate" &&
github mlaursen / react-md / packages / tooltip / src / useTooltipDelay.tsx View on Github external
export function useTooltipDelayState(
  defaultDelay: number,
  delayTimeout: number
) {
  const [delay, setDelay] = useState(defaultDelay);
  const delayRef = useRef(delay);
  useEffect(() => {
    delayRef.current = delay;
  });

  const disable = useCallback(() => {
    if (delayRef.current === 0) {
      setDelay(defaultDelay);
    }
  }, [defaultDelay]);
  const { start, stop } = useTimeout(disable, delayTimeout);
  const startTimer = useCallback(() => {
    start();
  }, []);
  const enable = useCallback(() => {
    stop();
    if (delayRef.current !== 0) {
      setDelay(0);
    }
  }, []);

  return {
    delay,
    enable,
    disable,
    startTimer,
  };
github mlaursen / react-md / packages / tooltip / src / useHandlers.ts View on Github external
}: MouseOptions): MouseResult {
  const handlers = useRefCache({ onMouseEnter, onMouseLeave });

  let isHoverModeable = useTooltipHoverModeEnabled();
  if (typeof disableHoverMode === "boolean") {
    isHoverModeable = !disableHoverMode;
  }

  let delay = useTooltipHoverModeDelay();
  if (!isHoverModeable) {
    delay = propDelay;
  }

  const hoverModeActions = useTooltipHoverModeActions();

  const [start, stop] = useTimeout(() => {
    if (initiated.current === "mouse") {
      showTooltip();

      if (isHoverModeable) {
        hoverModeActions.enable();
      }
    }
  }, delay);

  const handleMouseEnter = useCallback(
    (event: React.MouseEvent) => {
      const { onMouseEnter } = handlers.current;
      if (onMouseEnter) {
        onMouseEnter(event);
      }
github mlaursen / react-md / packages / documentation / components / Demos / Form / AsyncSwitchExample.tsx View on Github external
const AsyncSwitchExample: FC = () => {
  const [loading, setLoading] = useState(false);
  const [checked, setChecked] = useState(false);
  const [fail, handleFailChange] = useCheckboxState(false);
  const [start] = useTimeout(() => {
    setLoading(false);
    if (fail) {
      setChecked(prevChecked => !prevChecked);
    }
  }, 5000);

  return (
github mlaursen / react-md / packages / tooltip / src / useHandlers.ts View on Github external
export function useTouchState({
  mode,
  visible,
  showTooltip,
  hideTooltip,
  delay,
  setInitiated,
  onTouchStart,
  onTouchMove,
  onContextMenu,
  setEstimatedPosition,
}: TouchOptions): TouchResult {
  const touched = useRef(false);
  const handlers = useRefCache({ onTouchStart, onTouchMove, onContextMenu });

  const [start, stop] = useTimeout(() => {
    touched.current = false;
    hideTooltip();
  }, delay);

  useEffect(() => {
    if (!visible) {
      return;
    }

    if (mode !== "touch") {
      touched.current = false;
      return;
    }

    const cb = (): void => {
      start();