react-container-query
Version:
Container Query for React Component
169 lines • 6.58 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var ReactDOM = require("react-dom");
var matchQueries_1 = require("container-query-toolkit/lib/matchQueries");
var ContainerQueryCore_1 = require("./ContainerQueryCore");
var isShallowEqual_1 = require("./isShallowEqual");
/*
* const MyComponent = () => {
* const [params, containerRef] = useContainerQuery(query);
* return <div ref={containerRef} className={classnames(params)}>the box</div>;
* };
*/
exports.useContainerQuery = function (query, initialSize) {
// setup a ref callback
// (end user) attaches that ref callback to a container element
// @ts-ignore
var _a = React.useState(function () {
if (!initialSize) {
return {};
}
return matchQueries_1.default(query)(initialSize);
}), params = _a[0], setParams = _a[1];
// @ts-ignore
var _b = React.useState(null), containerRef = _b[0], setContainerRef = _b[1];
// @ts-ignore
var refCallback = React.useCallback(function (node) {
setContainerRef(node); // on unmount, node would be set to null triggering cleanup
}, [setContainerRef]);
// @ts-ignore
React.useEffect(function () {
if (containerRef) {
var cqCore_1 = new ContainerQueryCore_1.default(query, function (params) {
setParams(params);
});
cqCore_1.observe(containerRef);
return function () {
cqCore_1.disconnect();
cqCore_1 = null;
};
}
}, [query, containerRef, setParams]);
return [params, refCallback];
};
/**
* <ContainerQuery query={query} initialSize={{width: 123, height: 456}}>
* {(params) => {
* <div className={classname(params)}></div>
* }}
* </ContainerQuery>
*/
var ContainerQuery = /** @class */ (function (_super) {
__extends(ContainerQuery, _super);
function ContainerQuery(props) {
var _this = _super.call(this, props) || this;
_this.cqCore = null;
_this.state = {
params: props.initialSize
? matchQueries_1.default(props.query)(props.initialSize)
: {},
};
return _this;
}
ContainerQuery.prototype.componentDidMount = function () {
this._startObserving(this.props.query);
};
ContainerQuery.prototype.UNSAFE_componentWillReceiveProps = function (nextProps) {
// componentWillReceiveProps and componentDidMount can potentially run out of order,
// so we need to consider the case where cqCore is not initialized yet.
if (this.cqCore && !isQueriesEqual(this.props.query, nextProps.query)) {
this.cqCore.disconnect();
this.cqCore = null;
this._startObserving(nextProps.query);
}
};
ContainerQuery.prototype.componentDidUpdate = function () {
this.cqCore.observe(ReactDOM.findDOMNode(this));
};
ContainerQuery.prototype.componentWillUnmount = function () {
this.cqCore.disconnect();
this.cqCore = null;
};
ContainerQuery.prototype.render = function () {
return this.props.children(this.state.params);
};
ContainerQuery.prototype._startObserving = function (query) {
var _this = this;
this.cqCore = new ContainerQueryCore_1.default(query, function (params) {
_this.setState({ params: params });
});
this.cqCore.observe(ReactDOM.findDOMNode(this));
};
return ContainerQuery;
}(React.Component));
exports.ContainerQuery = ContainerQuery;
;
function applyContainerQuery(Component, query, initialSize) {
return _a = /** @class */ (function (_super) {
__extends(ContainerQuery, _super);
function ContainerQuery(props) {
var _this = _super.call(this, props) || this;
_this.cqCore = null;
_this.state = {
params: initialSize
? matchQueries_1.default(query)(initialSize)
: {},
};
return _this;
}
ContainerQuery.prototype.componentDidMount = function () {
var _this = this;
this.cqCore = new ContainerQueryCore_1.default(query, function (params) {
_this.setState({ params: params });
});
this.cqCore.observe(ReactDOM.findDOMNode(this));
};
ContainerQuery.prototype.componentDidUpdate = function () {
this.cqCore.observe(ReactDOM.findDOMNode(this));
};
ContainerQuery.prototype.componentWillUnmount = function () {
this.cqCore.disconnect();
this.cqCore = null;
};
ContainerQuery.prototype.render = function () {
return (React.createElement(Component, __assign({}, this.props, { containerQuery: this.state.params })));
};
return ContainerQuery;
}(React.Component)),
_a.displayName = Component.displayName
? "ContainerQuery(" + Component.displayName + ")"
: 'ContainerQuery',
_a;
var _a;
}
exports.applyContainerQuery = applyContainerQuery;
var hasOwnProperty = Object.prototype.hasOwnProperty;
function isQueriesEqual(queryA, queryB) {
var keysA = Object.keys(queryA);
var keysB = Object.keys(queryB);
if (keysA.length !== keysB.length) {
return false;
}
for (var i = 0; i < keysA.length; i++) {
if (!hasOwnProperty.call(queryB, keysA[i]) ||
!isShallowEqual_1.default(queryA[keysA[i]], queryB[keysA[i]])) {
return false;
}
}
return true;
}
//# sourceMappingURL=index.js.map