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.

133 lines (112 loc) 4.33 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.isFirst = exports.calculateLeftPosition = void 0; var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _transition = _interopRequireDefault(require("../../../utils/transition")); var _rtl = require("../../../utils/rtl"); var _defaultTheme = _interopRequireDefault(require("../../../defaultTheme")); const _excluded = ["left", "theme", "onTop"]; const calculateLeftPosition = (valueNow, valueMin, valueMax, isFirst, isSimple) => { // if first, stick it to the left edge if (isFirst) { // if simple (one handle and without histogram) if (isSimple) { return +((valueNow - valueMin) / (valueMax - valueMin) * 100).toFixed(1); } return +((valueNow - valueMin) / (valueMax - valueMin + 1) * 100).toFixed(1); } // for every other handle stick on the right edge return +((valueNow - valueMin + 1) / (valueMax - valueMin + 1) * 100).toFixed(1); }; exports.calculateLeftPosition = calculateLeftPosition; const isFirst = (value, valueNow, index, hasHistogram) => { if (Array.isArray(value)) { const max = Math.max(...value); const min = Math.min(...value); const maxEqualsMin = max === min; const minEqualsValueNow = min === valueNow; if (index !== 0 && maxEqualsMin) return false; return maxEqualsMin || minEqualsValueNow; } return !hasHistogram; }; exports.isFirst = isFirst; const StyledHandle = (0, _styledComponents.default)((_ref) => { let { left, theme, onTop } = _ref, props = (0, _objectWithoutProperties2.default)(_ref, _excluded); return /*#__PURE__*/React.createElement("div", props); }).attrs(({ left, onTop, theme }) => { return { style: { // TODO: use token for deducting the half size of the Handle [(0, _rtl.left)({ theme })]: `calc(${left}% - 12px)`, zIndex: onTop ? 20 : undefined } }; }).withConfig({ displayName: "Handle__StyledHandle", componentId: "sc-1k7rls9-0" })(["display:flex;position:absolute;right:0;bottom:0;flex:0 0 24px;align-items:center;justify-content:center;box-sizing:border-box;width:24px;height:24px;border-radius:24px;box-shadow:", ";background-color:", ";cursor:pointer;transition:", ";-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;-webkit-user-select:none;:after{content:\"\";display:block;width:8px;height:8px;background-color:", ";border-radius:8px;}:hover,:focus,:active{outline:none;box-shadow:", ";}"], ({ theme }) => theme.orbit.boxShadowAction, ({ theme }) => theme.orbit.paletteWhite, (0, _transition.default)(["box-shadow"], "fast", "ease-in-out"), ({ theme }) => theme.orbit.paletteBlueNormal, ({ theme }) => theme.orbit.boxShadowActionActive); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledHandle.defaultProps = { theme: _defaultTheme.default }; const Handle = ({ tabIndex, onMouseDown, onFocus, onTouchStart, valueMax, valueMin, onTop, value, index, ariaValueText, ariaLabel, hasHistogram, dataTest }) => { const valueNow = Array.isArray(value) ? value[index] : value; const first = isFirst(value, valueNow, index, hasHistogram); const isSimple = !hasHistogram && !Array.isArray(value); const left = calculateLeftPosition(valueNow, valueMin, valueMax, first, isSimple); return /*#__PURE__*/React.createElement(StyledHandle, { "data-test": dataTest, tabIndex: tabIndex, onTop: onTop, role: "slider", onMouseDown: onMouseDown, onFocus: onFocus, onTouchStart: onTouchStart, "aria-valuemax": valueMax, "aria-valuemin": valueMin, "aria-valuenow": valueNow, "aria-label": Array.isArray(ariaLabel) && typeof index !== "undefined" ? ariaLabel[index] : ariaLabel, "aria-valuetext": ariaValueText, left: left }); }; var _default = Handle; exports.default = _default;