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.

83 lines (80 loc) 2.39 kB
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["width", "left", "theme", "active"]; import * as React from "react"; import styled from "styled-components"; import { left as leftRight } from "../../../utils/rtl"; import defaultTheme from "../../../defaultTheme"; const StyledBar = styled.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;"]); export 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) }; }; export const StyledBarPart = styled((_ref) => { let { width, left, theme, active } = _ref, props = _objectWithoutProperties(_ref, _excluded); return /*#__PURE__*/React.createElement("div", props); }).attrs(({ width, left, theme }) => { return { style: { width: `${width}%`, [leftRight({ 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 StyledBarPart.defaultProps = { theme: defaultTheme }; 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"; export default Bar;