hooks#useKeyEvent JavaScript Examples

The following examples show how to use hooks#useKeyEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AvatarGroupCounterTooltipHelper.js    From monday-ui-react-core with MIT License 4 votes vote down vote up
export function useTooltipContentTabNavigation({
  counterContainerRef = undefined,
  tooltipContentContainerRef,
  focusPrevPlaceholderRef,
  focusNextPlaceholderRef,
  setShouldUpdate,
  setIsTooltipVisible
}) {
  const hideTooltip = useCallback(() => {
    // Tricky way to close the tooltip
    setTimeout(() => {
      setIsTooltipVisible(false);
      setIsTooltipVisible(true);
    });
  }, [setIsTooltipVisible]);

  // For Counter
  useKeyEvent({
    keys: KEYS,
    withoutAnyModifier: true,
    ref: counterContainerRef,
    callback: useCallback(
      e => {
        if (e.target === counterContainerRef.current) {
          e.preventDefault();
          tooltipContentContainerRef?.current && tooltipContentContainerRef.current.focus();
          setShouldUpdate(prev => !prev);
        }
      },
      [counterContainerRef.current, setShouldUpdate, tooltipContentContainerRef]
    )
  });

  // For Counter
  useKeyEvent({
    keys: KEYS,
    modifier: useKeyEvent.modifiers.SHIFT,
    ref: counterContainerRef,
    callback: useCallback(
      e => {
        if (e.target === counterContainerRef.current) {
          focusPrevPlaceholderRef?.current && focusPrevPlaceholderRef.current.focus();
          hideTooltip();
        }
      },
      [counterContainerRef.current, focusPrevPlaceholderRef, hideTooltip]
    )
  });

  // For Tooltip content
  useKeyEvent({
    keys: KEYS,
    ref: tooltipContentContainerRef,
    withoutAnyModifier: true,
    callback: useCallback(
      e => {
        if (e.target === tooltipContentContainerRef.current) {
          focusNextPlaceholderRef?.current && focusNextPlaceholderRef.current.focus();
          hideTooltip();
        }
      },
      [focusNextPlaceholderRef, hideTooltip, tooltipContentContainerRef.current]
    )
  });

  // For Tooltip content
  useKeyEvent({
    keys: KEYS,
    ref: tooltipContentContainerRef,
    modifier: useKeyEvent.modifiers.SHIFT,
    callback: useCallback(
      e => {
        if (e.target === tooltipContentContainerRef.current) {
          e.preventDefault();
          counterContainerRef?.current && counterContainerRef.current.focus();
        }
      },
      [counterContainerRef, tooltipContentContainerRef.current]
    )
  });
}