1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var react_1 = tslib_1.__importDefault(require("react"));
|
5 | var events_1 = require("@shopify/javascript-utilities/events");
|
6 |
|
7 | var EventListener = (function (_super) {
|
8 | tslib_1.__extends(EventListener, _super);
|
9 | function EventListener() {
|
10 | return _super !== null && _super.apply(this, arguments) || this;
|
11 | }
|
12 | EventListener.prototype.componentDidMount = function () {
|
13 | this.attachListener();
|
14 | };
|
15 | EventListener.prototype.componentDidUpdate = function (_a) {
|
16 | var passive = _a.passive, detachProps = tslib_1.__rest(_a, ["passive"]);
|
17 | this.detachListener(detachProps);
|
18 | this.attachListener();
|
19 | };
|
20 | EventListener.prototype.componentWillUnmount = function () {
|
21 | this.detachListener();
|
22 | };
|
23 | EventListener.prototype.render = function () {
|
24 | return null;
|
25 | };
|
26 | EventListener.prototype.attachListener = function () {
|
27 | var _a = this.props, event = _a.event, handler = _a.handler, capture = _a.capture, passive = _a.passive;
|
28 | events_1.addEventListener(window, event, handler, { capture: capture, passive: passive });
|
29 | };
|
30 | EventListener.prototype.detachListener = function (prevProps) {
|
31 | var _a = prevProps || this.props, event = _a.event, handler = _a.handler, capture = _a.capture;
|
32 | events_1.removeEventListener(window, event, handler, capture);
|
33 | };
|
34 | return EventListener;
|
35 | }(react_1.default.PureComponent));
|
36 | exports.EventListener = EventListener;
|