How to use the react-use/lib/useClickAway function in react-use

To help you get started, we’ve selected a few react-use 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 grafana / grafana / packages / grafana-ui / src / components / Segment / SegmentSelect.tsx View on Github external
export function SegmentSelect({
  value,
  options = [],
  onChange,
  onClickOutside,
  width,
  noOptionsMessage = '',
  allowCustomValue = false,
}: React.PropsWithChildren>) {
  const ref = useRef(null);

  useClickAway(ref, () => {
    onClickOutside();
  });

  return (
    <div>
      <select> 120 ? width : 120}px;
          `
        )}
        noOptionsMessage={() =&gt; noOptionsMessage}
        placeholder=""
        autoFocus={true}
        isOpen={true}
        onChange={onChange}</select></div>
github gremlin / chaoskit / src / components / OffCanvas.js View on Github external
openOffCanvas();
    } else {
      onReverseComplete();
    }
  }, [renderOffCanvas]);

  const classes = cx(
    'offCanvas',
    {
      'offCanvas--right': align === 'right',
    },
    className
  );

  useClickAway(offCanvasPanelRef, () =&gt; onOffCanvasToggle());
  useLockBodyScroll(renderOffCanvas);

  return (
    renderOffCanvas &amp;&amp;
    ReactDOM.createPortal(
      <div>
        <div>
          
          {children}
        </div>
      </div>,
      document.body
    )
  );
};
github grafana / grafana / packages / grafana-ui / src / components / ContextMenu / ContextMenu.tsx View on Github external
const rect = menuElement.getBoundingClientRect();
      const OFFSET = 5;
      const collisions = {
        right: window.innerWidth &lt; x + rect.width,
        bottom: window.innerHeight &lt; rect.bottom + rect.height + OFFSET,
      };

      setPositionStyles({
        position: 'fixed',
        left: collisions.right ? x - rect.width - OFFSET : x - OFFSET,
        top: collisions.bottom ? y - rect.height - OFFSET : y + OFFSET,
      });
    }
  }, [menuRef.current]);

  useClickAway(menuRef, () =&gt; {
    if (onClose) {
      onClose();
    }
  });

  const styles = getContextMenuStyles(theme);
  return (
    
      <div style="{positionStyles}">
        {renderHeader &amp;&amp; <div>{renderHeader()}</div>}
         {
            return (
              &lt;&gt;
                </div>
github grafana / grafana / packages / grafana-ui / src / components / DataLinks / DataLinkSuggestions.tsx View on Github external
export const DataLinkSuggestions: React.FC = ({ suggestions, ...otherProps }) =&gt; {
  const ref = useRef(null);
  const theme = useContext(ThemeContext);
  useClickAway(ref, () =&gt; {
    if (otherProps.onClose) {
      otherProps.onClose();
    }
  });

  const groupedSuggestions = useMemo(() =&gt; {
    return _.groupBy(suggestions, s =&gt; s.origin);
  }, [suggestions]);

  const styles = getStyles(theme);
  return (
    <div>
      {Object.keys(groupedSuggestions).map((key, i) =&gt; {
        const indexOffset =
          i === 0
            ? 0</div>
github gremlin / chaoskit / src / components / Modal.js View on Github external
openModal();
    } else {
      onReverseComplete();
    }
  }, [renderModal]);

  const modalClasses = cx(
    'modal',
    {
      'modal--small': size === 'small',
      'modal--large': size === 'large',
    },
    className
  );

  useClickAway(modalDialogRef, () =&gt; onOutsideModalClick());
  useLockBodyScroll(renderModal);

  return (
    renderModal &amp;&amp;
    ReactDOM.createPortal(
      <div>
        <div>
          {children}
        </div>
      </div>,
      document.body
    )
  );
};
github apollographql / gatsby-theme-apollo / packages / gatsby-theme-apollo-docs / src / components / select.js View on Github external
const wrapperRef = useRef(null);
  const [open, setOpen] = useState(false);

  const optionKeys = useMemo(() =&gt; Object.keys(options), [options]);
  const labelHeight = useMemo(() =&gt; {
    switch (props.size) {
      case 'small':
        return 20;
      case 'large':
        return 27;
      default:
        return 22;
    }
  }, [props.size]);

  useClickAway(wrapperRef, () =&gt; {
    setOpen(false);
  });

  function handleClick() {
    setOpen(prevOpen =&gt; !prevOpen);
  }

  return (
    
      <button>
        
          {optionKeys.map(key =&gt; (
            <spacer>{options[key]}</spacer>
          ))}
          <label>{options[value]}</label>
        </button>
github grafana / grafana / packages / grafana-ui / src / components / Segment / SegmentInput.tsx View on Github external
export function SegmentInput({
  value: initialValue,
  onChange,
  Component,
  className,
  placeholder,
  autofocus = false,
}: React.PropsWithChildren&gt;) {
  const ref = useRef(null);
  const [value, setValue] = useState(initialValue);
  const [inputWidth, setInputWidth] = useState(measureText((initialValue || '').toString(), FONT_SIZE).width);
  const [Label, , expanded, setExpanded] = useExpandableLabel(autofocus);

  useClickAway(ref, () =&gt; {
    setExpanded(false);
    onChange(value);
  });

  if (!expanded) {
    return (
      <label placeholder="">
              {initialValue || placeholder}
            
          )
        }
      /&gt;
    );</label>