UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

104 lines (90 loc) 3.03 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.StyledBarPart = exports.calculateBarPosition = void 0; var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _rtl = require("../../../utils/rtl"); var _defaultTheme = _interopRequireDefault(require("../../../defaultTheme")); const _excluded = ["width", "left", "theme", "active"]; const StyledBar = _styledComponents.default.div.withConfig({ displayName: "Bar__StyledBar", componentId: "sc-1i9jxww-0" })(["display:block;position:relative;width:100%;height:24px;cursor:pointer;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;-webkit-user-select:none;"]); const calculateBarPosition = (value, max, min, hasHistogram) => { if (Array.isArray(value)) { return { left: +((value[0] - min) / (max - min + 1) * 100).toFixed(1), width: +((value[value.length - 1] - value[0] + 1) / (max - min + 1) * 100).toFixed(1) }; } const addition = hasHistogram ? 1 : 0; return { left: 0, width: +((value - min + addition) / (max - min + addition) * 100).toFixed(1) }; }; exports.calculateBarPosition = calculateBarPosition; const StyledBarPart = (0, _styledComponents.default)((_ref) => { let { width, left, theme, active } = _ref, props = (0, _objectWithoutProperties2.default)(_ref, _excluded); return /*#__PURE__*/React.createElement("div", props); }).attrs(({ width, left, theme }) => { return { style: { width: `${width}%`, [(0, _rtl.left)({ theme })]: `${left}%` } }; }).withConfig({ displayName: "Bar__StyledBarPart", componentId: "sc-1i9jxww-1" })(["position:absolute;height:4px;top:10px;border-radius:4px;background-color:", ";"], ({ theme, active }) => active ? theme.orbit.paletteBlueNormal : theme.orbit.paletteCloudDarker); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 exports.StyledBarPart = StyledBarPart; StyledBarPart.defaultProps = { theme: _defaultTheme.default }; const Bar = /*#__PURE__*/React.forwardRef(({ onMouseDown, value, max, min, hasHistogram }, ref) => { const { left, width } = calculateBarPosition(value, max, min, hasHistogram); return /*#__PURE__*/React.createElement(StyledBar, { ref: ref, onMouseDown: onMouseDown }, /*#__PURE__*/React.createElement(StyledBarPart, { width: 100, left: 0 }), /*#__PURE__*/React.createElement(StyledBarPart, { active: true, left: left, width: width })); }); Bar.displayName = "Bar"; var _default = Bar; exports.default = _default;