@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
43 lines (35 loc) • 1 kB
JavaScript
import * as React from "react";
import styled from "styled-components";
const Inner = styled.div.withConfig({
displayName: "ClickOutside__Inner",
componentId: "sc-1rfu7d2-0"
})(["width:100%;"]);
class ClickOutside extends React.PureComponent {
constructor(...args) {
super(...args);
this.node = /*#__PURE__*/React.createRef();
this.handleClickOutside = ev => {
const {
onClickOutside
} = this.props;
if (onClickOutside && this.node.current && ev.target instanceof Node && !this.node.current.contains(ev.target)) {
onClickOutside(ev);
}
};
}
componentDidMount() {
document.addEventListener("click", this.handleClickOutside, true);
}
componentWillUnmount() {
document.removeEventListener("click", this.handleClickOutside, true);
}
render() {
const {
children
} = this.props;
return /*#__PURE__*/React.createElement(Inner, {
ref: this.node
}, children);
}
}
export default ClickOutside;