@elastic/eui
Version:
Elastic UI Component Library
107 lines (104 loc) • 8.23 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
var _excluded = ["iconType", "color", "fill", "shape", "size", "style", "className", "title", "aria-label", "aria-labelledby", "aria-describedby"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
/*
* 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 from 'react';
import classNames from 'classnames';
import { useEuiTheme, isColorDark, hexToRgb } from '../../services';
import { EuiIcon } from '../icon';
import { TOKEN_MAP } from './token_map';
import { COLORS } from './token_types';
import { euiTokenStyles } from './token.styles';
import { jsx as ___EmotionJSX } from "@emotion/react";
var isTokenColor = function isTokenColor(color) {
return COLORS.includes(color);
};
export var EuiToken = function EuiToken(_ref) {
var iconType = _ref.iconType,
color = _ref.color,
fill = _ref.fill,
shape = _ref.shape,
_ref$size = _ref.size,
size = _ref$size === void 0 ? 's' : _ref$size,
_ref$style = _ref.style,
style = _ref$style === void 0 ? {} : _ref$style,
className = _ref.className,
title = _ref.title,
ariaLabel = _ref['aria-label'],
ariaLabelledby = _ref['aria-labelledby'],
ariaDescribedby = _ref['aria-describedby'],
rest = _objectWithoutProperties(_ref, _excluded);
// Set the icon size to the same as the passed size
// unless they passed `xs` which IconSize doesn't support
var finalSize = size === 'xs' ? 's' : size;
// When displaying at the small size, the token specific icons
// should actually be displayed at medium size
if (typeof iconType === 'string' && iconType.indexOf('token') === 0 && size === 's') {
finalSize = 'm';
}
// If the iconType passed is one of the prefab token types,
// grab its properties
var tokenDefaults = typeof iconType === 'string' && iconType in TOKEN_MAP ? TOKEN_MAP[iconType] : {};
var finalColor = color || tokenDefaults.color || 'gray';
var finalShape = shape || tokenDefaults.shape || 'circle';
var finalFill = fill || 'light';
var euiTheme = useEuiTheme();
var styles = euiTokenStyles(euiTheme, finalFill);
var cssStyles = [styles.euiToken, styles[finalShape], styles[finalFill], styles[size]];
var finalStyle = style;
if (isTokenColor(finalColor)) {
cssStyles = [].concat(_toConsumableArray(cssStyles), [styles[finalColor]]);
} else if (finalFill === 'none') {
// When a custom HEX color is passed and the token doesn't have any fill (no background),
// the icon gets that passed color
cssStyles = [].concat(_toConsumableArray(cssStyles), [styles.customColor]);
finalStyle = _objectSpread({
color: finalColor
}, style);
} else {
// When a custom HEX color is passed and the token has a fill (light or dark),
// the background gets the custom color and the icon gets white or black based on the passed color
// The fill='light' (lightened background) will always be overridden by fill='dark' (opaque background)
// to better handle custom colors
var isFinalColorDark = isColorDark.apply(void 0, _toConsumableArray(hexToRgb(finalColor)));
var lightOrDarkColor = isFinalColorDark ? '#FFFFFF' : '#000000';
cssStyles = [].concat(_toConsumableArray(cssStyles), [styles.customColor]);
finalFill = 'dark';
finalStyle = _objectSpread({
color: lightOrDarkColor,
backgroundColor: finalColor
}, style);
}
var classes = classNames('euiToken', className);
return ___EmotionJSX("span", _extends({
className: classes,
css: cssStyles,
style: finalStyle
}, rest), ___EmotionJSX(EuiIcon, {
type: iconType,
size: finalSize,
title: title,
"aria-label": ariaLabel,
"aria-labelledby": ariaLabelledby,
"aria-describedby": ariaDescribedby
}));
};