diff --git a/packages/grafana-ui/src/components/ClickOutsideWrapper/ClickOutsideWrapper.tsx b/packages/grafana-ui/src/components/ClickOutsideWrapper/ClickOutsideWrapper.tsx index c435be0f384..6836f56a69e 100644 --- a/packages/grafana-ui/src/components/ClickOutsideWrapper/ClickOutsideWrapper.tsx +++ b/packages/grafana-ui/src/components/ClickOutsideWrapper/ClickOutsideWrapper.tsx @@ -2,7 +2,14 @@ import { PureComponent } from 'react'; import ReactDOM from 'react-dom'; export interface Props { + /** + * When clicking outside of current element + */ onClick: () => void; + /** + * Runs the 'onClick' function when pressing a key outside of the current element. Defaults to true. + */ + includeButtonPress: boolean; } interface State { @@ -10,16 +17,26 @@ interface State { } export class ClickOutsideWrapper extends PureComponent { + static defaultProps = { + includeButtonPress: true, + }; state = { hasEventListener: false, }; componentDidMount() { window.addEventListener('click', this.onOutsideClick, false); + if (this.props.includeButtonPress) { + // Use keyup since keydown already has an eventlistener on window + window.addEventListener('keyup', this.onOutsideClick, false); + } } componentWillUnmount() { window.removeEventListener('click', this.onOutsideClick, false); + if (this.props.includeButtonPress) { + window.addEventListener('keyup', this.onOutsideClick, false); + } } onOutsideClick = (event: any) => {