@artsy/fresnel
Version:
An SSR compatible approach to CSS media query based responsive layouts for React.
106 lines (77 loc) • 5.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Interactions = exports.InteractionKey = void 0;
var _Utils = require("./Utils");
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys.push.apply(ownKeys, Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
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; }
var InteractionKey;
/**
* Encapsulates all interaction data needed by the Media component. The data is
* generated on initialization so no further runtime work is necessary.
*/
exports.InteractionKey = InteractionKey;
(function (InteractionKey) {
InteractionKey["interaction"] = "interaction";
})(InteractionKey || (exports.InteractionKey = InteractionKey = {}));
var Interactions = /*#__PURE__*/function () {
_createClass(Interactions, null, [{
key: "validKeys",
value: function validKeys() {
return [InteractionKey.interaction];
}
}]);
function Interactions(interactions) {
_classCallCheck(this, Interactions);
_defineProperty(this, "_interactions", void 0);
this._interactions = interactions;
}
_createClass(Interactions, [{
key: "toRuleSets",
value: function toRuleSets() {
return Object.entries(this._interactions).reduce(function (acc, _ref) {
var _ref2 = _slicedToArray(_ref, 2),
name = _ref2[0],
query = _ref2[1];
return _toConsumableArray(acc).concat([(0, _Utils.createRuleSet)((0, _Utils.createClassName)(InteractionKey.interaction, name), query)]);
}, []);
}
}, {
key: "shouldRenderMediaQuery",
value: function shouldRenderMediaQuery(interaction, onlyMatch) {
return !!(onlyMatch && onlyMatch.includes(interaction));
}
}, {
key: "interactions",
get: function get() {
return Object.keys(this._interactions);
}
}, {
key: "dynamicResponsiveMediaQueries",
get: function get() {
return Object.entries(this._interactions).reduce(function (acc, _ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
name = _ref4[0],
query = _ref4[1];
return _objectSpread({}, acc, _defineProperty({}, name, query));
}, {});
}
}]);
return Interactions;
}();
exports.Interactions = Interactions;
//# sourceMappingURL=Interactions.js.map