@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.
48 lines (37 loc) • 1.32 kB
JavaScript
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import * as React from "react";
import styled from "styled-components";
const Inner = styled.div.withConfig({
displayName: "ClickOutside__Inner",
componentId: "sc-8mz6b9-0"
})(["width:100%;"]);
class ClickOutside extends React.PureComponent {
constructor(...args) {
super(...args);
_defineProperty(this, "handleClickOutside", ev => {
var _this$node;
const {
onClickOutside
} = this.props;
if (onClickOutside && ((_this$node = this.node) === null || _this$node === void 0 ? void 0 : _this$node.current) && ev.target instanceof Node && !this.node.current.contains(ev.target)) {
onClickOutside(ev);
}
});
_defineProperty(this, "node", React.createRef());
}
componentDidMount() {
document.addEventListener("click", this.handleClickOutside, true);
}
componentWillUnmount() {
document.removeEventListener("click", this.handleClickOutside, true);
}
render() {
const {
children
} = this.props;
return React.createElement(Inner, {
ref: this.node
}, children);
}
}
export default ClickOutside;