UNPKG

@geist-ui/react

Version:

Modern and minimalist React UI library.

141 lines (121 loc) 8.67 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _excluded = ["children"]; function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } 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; } import React, { forwardRef } from 'react'; import { ScaleableContext } from './scaleable-context'; import useTheme from '../use-theme'; import { isCSSNumberValue } from '../utils/collections'; var reduceScaleCoefficient = function reduceScaleCoefficient(scale) { if (scale === 1) return scale; var diff = Math.abs((scale - 1) / 2); return scale > 1 ? 1 + diff : 1 - diff; }; var withScaleable = function withScaleable(Render) { var ScaleableFC = /*#__PURE__*/forwardRef(function (_ref, ref) { var _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9, _ref10, _ref11, _ref12, _ref13, _ref14, _ref15, _ref16, _ref17, _ref18, _ref19, _ref20, _ref21, _ref22, _ref23, _ref24, _ref25, _ref26, _ref27, _ref28, _ref29, _ref30, _ref31, _ref32, _ref33; var children = _ref.children, props = _objectWithoutProperties(_ref, _excluded); var _useTheme = useTheme(), layout = _useTheme.layout; var paddingLeft = props.paddingLeft, pl = props.pl, paddingRight = props.paddingRight, pr = props.pr, paddingTop = props.paddingTop, pt = props.pt, paddingBottom = props.paddingBottom, pb = props.pb, marginTop = props.marginTop, mt = props.mt, marginRight = props.marginRight, mr = props.mr, marginBottom = props.marginBottom, mb = props.mb, marginLeft = props.marginLeft, ml = props.ml, px = props.px, py = props.py, mx = props.mx, my = props.my, width = props.width, height = props.height, font = props.font, w = props.w, h = props.h, margin = props.margin, padding = props.padding, _props$unit = props.unit, unit = _props$unit === void 0 ? layout.unit : _props$unit, _props$scale = props.scale, scale = _props$scale === void 0 ? 1 : _props$scale; var makeScaleHandler = function makeScaleHandler(attrValue) { return function (scale1x, defaultValue) { // 0 means disable scale and the default value is 0 if (scale1x === 0) { scale1x = 1; defaultValue = defaultValue || 0; } var factor = reduceScaleCoefficient(scale) * scale1x; if (typeof attrValue === 'undefined') { if (typeof defaultValue !== 'undefined') return "".concat(defaultValue); return "calc(".concat(factor, " * ").concat(unit, ")"); } if (!isCSSNumberValue(attrValue)) return "".concat(attrValue); var customFactor = factor * Number(attrValue); return "calc(".concat(customFactor, " * ").concat(unit, ")"); }; }; var getScaleableProps = function getScaleableProps(keyOrKeys) { if (!Array.isArray(keyOrKeys)) return props[keyOrKeys]; var value = undefined; var _iterator = _createForOfIteratorHelper(keyOrKeys), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var key = _step.value; var currentValue = props[key]; if (typeof currentValue !== 'undefined') { value = currentValue; } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return value; }; var value = { unit: unit, SCALES: { pt: makeScaleHandler((_ref2 = (_ref3 = paddingTop !== null && paddingTop !== void 0 ? paddingTop : pt) !== null && _ref3 !== void 0 ? _ref3 : py) !== null && _ref2 !== void 0 ? _ref2 : padding), pr: makeScaleHandler((_ref4 = (_ref5 = paddingRight !== null && paddingRight !== void 0 ? paddingRight : pr) !== null && _ref5 !== void 0 ? _ref5 : px) !== null && _ref4 !== void 0 ? _ref4 : padding), pb: makeScaleHandler((_ref6 = (_ref7 = paddingBottom !== null && paddingBottom !== void 0 ? paddingBottom : pb) !== null && _ref7 !== void 0 ? _ref7 : py) !== null && _ref6 !== void 0 ? _ref6 : padding), pl: makeScaleHandler((_ref8 = (_ref9 = paddingLeft !== null && paddingLeft !== void 0 ? paddingLeft : pl) !== null && _ref9 !== void 0 ? _ref9 : px) !== null && _ref8 !== void 0 ? _ref8 : padding), px: makeScaleHandler((_ref10 = (_ref11 = (_ref12 = (_ref13 = px !== null && px !== void 0 ? px : paddingLeft) !== null && _ref13 !== void 0 ? _ref13 : paddingRight) !== null && _ref12 !== void 0 ? _ref12 : pl) !== null && _ref11 !== void 0 ? _ref11 : pr) !== null && _ref10 !== void 0 ? _ref10 : padding), py: makeScaleHandler((_ref14 = (_ref15 = (_ref16 = (_ref17 = py !== null && py !== void 0 ? py : paddingTop) !== null && _ref17 !== void 0 ? _ref17 : paddingBottom) !== null && _ref16 !== void 0 ? _ref16 : pt) !== null && _ref15 !== void 0 ? _ref15 : pb) !== null && _ref14 !== void 0 ? _ref14 : padding), mt: makeScaleHandler((_ref18 = (_ref19 = marginTop !== null && marginTop !== void 0 ? marginTop : mt) !== null && _ref19 !== void 0 ? _ref19 : my) !== null && _ref18 !== void 0 ? _ref18 : margin), mr: makeScaleHandler((_ref20 = (_ref21 = marginRight !== null && marginRight !== void 0 ? marginRight : mr) !== null && _ref21 !== void 0 ? _ref21 : mx) !== null && _ref20 !== void 0 ? _ref20 : margin), mb: makeScaleHandler((_ref22 = (_ref23 = marginBottom !== null && marginBottom !== void 0 ? marginBottom : mb) !== null && _ref23 !== void 0 ? _ref23 : my) !== null && _ref22 !== void 0 ? _ref22 : margin), ml: makeScaleHandler((_ref24 = (_ref25 = marginLeft !== null && marginLeft !== void 0 ? marginLeft : ml) !== null && _ref25 !== void 0 ? _ref25 : mx) !== null && _ref24 !== void 0 ? _ref24 : margin), mx: makeScaleHandler((_ref26 = (_ref27 = (_ref28 = (_ref29 = mx !== null && mx !== void 0 ? mx : marginLeft) !== null && _ref29 !== void 0 ? _ref29 : marginRight) !== null && _ref28 !== void 0 ? _ref28 : ml) !== null && _ref27 !== void 0 ? _ref27 : mr) !== null && _ref26 !== void 0 ? _ref26 : margin), my: makeScaleHandler((_ref30 = (_ref31 = (_ref32 = (_ref33 = my !== null && my !== void 0 ? my : marginTop) !== null && _ref33 !== void 0 ? _ref33 : marginBottom) !== null && _ref32 !== void 0 ? _ref32 : mt) !== null && _ref31 !== void 0 ? _ref31 : mb) !== null && _ref30 !== void 0 ? _ref30 : margin), width: makeScaleHandler(width !== null && width !== void 0 ? width : w), height: makeScaleHandler(height !== null && height !== void 0 ? height : h), font: makeScaleHandler(font) }, getScaleableProps: getScaleableProps }; return /*#__PURE__*/React.createElement(ScaleableContext.Provider, { value: value }, /*#__PURE__*/React.createElement(Render, _extends({}, props, { ref: ref }), children)); }); ScaleableFC.displayName = "Scaleable".concat(Render.displayName || 'Wrapper'); return ScaleableFC; }; export default withScaleable;