UNPKG

@elastic/eui

Version:

Elastic UI Component Library

116 lines (114 loc) 4.59 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["levels", "max", "min", "showTicks", "showRange", "trackWidth"], _excluded2 = ["color", "className", "min", "max"]; /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import React, { useMemo, useEffect } from 'react'; import classNames from 'classnames'; import { useEuiTheme } from '../../../services'; import { logicalStyles } from '../../../global_styling'; import { isNamedLevelColor } from './range_levels_colors'; import { euiRangeLevelsStyles, euiRangeLevelStyles } from './range_levels.styles'; import { calculateThumbPosition, EUI_THUMB_SIZE } from './utils'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var EuiRangeLevels = function EuiRangeLevels(_ref) { var _ref$levels = _ref.levels, levels = _ref$levels === void 0 ? [] : _ref$levels, max = _ref.max, min = _ref.min, showTicks = _ref.showTicks, showRange = _ref.showRange, trackWidth = _ref.trackWidth, rest = _objectWithoutProperties(_ref, _excluded); var euiTheme = useEuiTheme(); var styles = euiRangeLevelsStyles(euiTheme); var cssStyles = [styles.euiRangeLevels, showTicks && styles.hasTicks, showRange && styles.hasRange]; return ___EmotionJSX("div", _extends({ className: "euiRangeLevels", css: cssStyles }, rest), levels.map(function (level, index) { return ___EmotionJSX(EuiRangeLevelElement, { key: index, level: level, min: min, max: max, trackWidth: trackWidth }); })); }; // Internal subcomponent var EuiRangeLevelElement = function EuiRangeLevelElement(_ref2) { var level = _ref2.level, min = _ref2.min, max = _ref2.max, trackWidth = _ref2.trackWidth; var color = level.color, className = level.className, levelMin = level.min, levelMax = level.max, levelRest = _objectWithoutProperties(level, _excluded2); var isNamedColor = useMemo(function () { return isNamedLevelColor(color); }, [color]); useEffect(function () { validateLevelIsInRange({ min: levelMin, max: levelMax }, { min: min, max: max }); }, [levelMin, levelMax, min, max]); var styles = useMemo(function () { var left = 0; var right = 0; var leftOffset = 0; var rightOffset = 0; if (trackWidth > 0) { left = levelMin === min ? 0 : calculateThumbPosition(levelMin, min, max, trackWidth); leftOffset = calculateOffset(left, levelMin, min); right = levelMax === max ? 100 : calculateThumbPosition(levelMax, min, max, trackWidth); rightOffset = calculateOffset(right, levelMax, max); } return logicalStyles({ left: "calc(".concat(left, "% + ").concat(leftOffset, "px)"), right: "calc(".concat(100 - right, "% - ").concat(rightOffset, "px)"), backgroundColor: !isNamedColor ? color : undefined }); }, [levelMin, levelMax, min, max, trackWidth, isNamedColor, color]); var levelClasses = classNames('euiRangeLevel', className); var euiTheme = useEuiTheme(); var levelStyles = euiRangeLevelStyles(euiTheme); var cssLevelStyles = [levelStyles.euiRangeLevel, isNamedColor ? levelStyles[color] : levelStyles.customColor]; return ___EmotionJSX("span", _extends({ style: styles, className: levelClasses, css: cssLevelStyles }, levelRest)); }; var validateLevelIsInRange = function validateLevelIsInRange(level, _ref3) { var min = _ref3.min, max = _ref3.max; if (level.min < min) { throw new Error("The level min of ".concat(level.min, " is lower than the min value of ").concat(min, ".")); } if (level.max > max) { throw new Error("The level max of ".concat(level.max, " is higher than the max value of ").concat(max, ".")); } }; var calculateOffset = function calculateOffset(position, value, bound) { var threshold = 30; var offset = value === bound ? 0 : EUI_THUMB_SIZE / 2; if (offset !== 0) { // Estimating offset by eye. Trying to account for range scaling at both ends. offset = position <= threshold ? offset + 1 / position * threshold : offset; offset = position >= 100 - threshold ? offset - 1 / (100 - position) * threshold : offset; } return offset; };