UNPKG

react-bps

Version:

Create breakpoints to your component props

94 lines (80 loc) 2.69 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react-sizes')) : typeof define === 'function' && define.amd ? define(['exports', 'react-sizes'], factory) : (factory((global.ReactBps = {}),global.ReactSizes)); }(this, (function (exports,withSizes) { 'use strict'; withSizes = withSizes && withSizes.hasOwnProperty('default') ? withSizes['default'] : withSizes; var keys = function keys(obj) { return Object.keys(obj); }; var map = function map(fn) { return function (array) { return array.map(fn); }; }; var filter = function filter(fn) { return function (array) { return array.filter(fn); }; }; var sort = function sort(fn) { return function (array) { return array.sort(fn); }; }; var head = function head(array) { return array[0]; }; var gte = function gte(num1) { return function (num2) { return num1 >= num2; }; }; var lte = function lte(num1) { return function (num2) { return num1 <= num2; }; }; var asc = function asc(a, b) { return a - b; }; var desc = function desc(a, b) { return b - a; }; var pipe = function pipe() { for (var _len = arguments.length, props = Array(_len), _key = 0; _key < _len; _key++) { props[_key] = arguments[_key]; } return function () { for (var _len2 = arguments.length, passProps = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { passProps[_key2] = arguments[_key2]; } return props.reduce.apply(props, [function (acc, fn) { return fn(acc); }].concat(passProps)); }; }; var getBreakpointsProps = function getBreakpointsProps(width, breakpoints, mobileFirst) { var getPropsByKey = function getPropsByKey(key) { return key && breakpoints[key] || {}; }; return pipe(keys, map(Number), filter((mobileFirst ? gte : lte)(width)), sort(mobileFirst ? desc : asc), head, getPropsByKey)(breakpoints); }; var withBps = function withBps() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref$propName = _ref.propName, propName = _ref$propName === undefined ? 'bps' : _ref$propName, _ref$mobileFirst = _ref.mobileFirst, mobileFirst = _ref$mobileFirst === undefined ? false : _ref$mobileFirst; return withSizes(function (_ref2, _ref3) { var width = _ref2.width; var _ref3$propName = _ref3[propName], breakpoints = _ref3$propName === undefined ? {} : _ref3$propName; return getBreakpointsProps(width, breakpoints, mobileFirst); }); }; var index = { withBps: withBps }; exports.withBps = withBps; exports['default'] = index; Object.defineProperty(exports, '__esModule', { value: true }); })));