@elastic/eui
Version:
Elastic UI Component Library
186 lines (182 loc) • 8.58 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "color", "iconType", "iconSide", "className", "isDisabled", "onClick", "iconOnClick", "onClickAriaLabel", "iconOnClickAriaLabel", "closeButtonProps", "href", "rel", "target", "style"];
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; }
/*
* 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 } from 'react';
import classNames from 'classnames';
import { useEuiTheme, getSecureRelForTarget, wcagContrastMin } from '../../services';
import { EuiInnerText } from '../inner_text';
import { EuiIcon } from '../icon';
import { validateHref } from '../../services/security/href_validator';
import { getTextColor, getColorContrast, getIsValidColor } from './color_utils';
import { euiBadgeStyles } from './badge.styles';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var ICON_SIDES = ['left', 'right'];
export var COLORS = ['default', 'hollow', 'primary', 'success', 'accent', 'warning', 'danger'];
export var EuiBadge = function EuiBadge(_ref) {
var children = _ref.children,
_ref$color = _ref.color,
color = _ref$color === void 0 ? 'default' : _ref$color,
iconType = _ref.iconType,
_ref$iconSide = _ref.iconSide,
iconSide = _ref$iconSide === void 0 ? 'left' : _ref$iconSide,
className = _ref.className,
_isDisabled = _ref.isDisabled,
onClick = _ref.onClick,
iconOnClick = _ref.iconOnClick,
onClickAriaLabel = _ref.onClickAriaLabel,
iconOnClickAriaLabel = _ref.iconOnClickAriaLabel,
closeButtonProps = _ref.closeButtonProps,
href = _ref.href,
rel = _ref.rel,
target = _ref.target,
style = _ref.style,
rest = _objectWithoutProperties(_ref, _excluded);
var euiTheme = useEuiTheme();
var isHrefValid = !href || validateHref(href);
var isDisabled = _isDisabled || !isHrefValid;
var isNamedColor = COLORS.includes(color);
var customColorStyles = useMemo(function () {
// Named colors set their styles via Emotion CSS and not inline styles
if (isNamedColor) return style;
// Do our best to ensure custom colors provide sufficient contrast
try {
// Set dark or light text color based upon best contrast
var textColor = getTextColor(euiTheme, color);
// Check the contrast ratio. If it's low contrast, emit a console awrning
var contrastRatio = getColorContrast(textColor, color);
if (contrastRatio < wcagContrastMin) {
console.warn("Warning: ".concat(color, " badge has a low contrast of ").concat(contrastRatio.toFixed(2), ". Should be above ").concat(wcagContrastMin, "."));
}
return _objectSpread({
backgroundColor: color,
color: textColor
}, style);
} catch (err) {
if (!getIsValidColor(color)) {
console.warn('EuiBadge expects a valid color. This can either be a three or six ' + "character hex value, rgb(a) value, hsv value, hollow, or one of the following: ".concat(COLORS, ". ") + "Instead got ".concat(color, "."));
}
}
}, [color, isNamedColor, style, euiTheme]);
var styles = euiBadgeStyles(euiTheme);
var cssStyles = [styles.euiBadge, isNamedColor && styles[color], (onClick || href) && !iconOnClick && styles.clickable, isDisabled && styles.disabled];
var textCssStyles = [styles.text.euiBadge__text, (onClick || href) && !isDisabled && styles.text.clickable];
var iconCssStyles = [styles.icon.euiBadge__icon, styles.icon[iconSide]];
var iconButtonCssStyles = [styles.iconButton.euiBadge__iconButton, styles.iconButton[iconSide]];
var classes = classNames('euiBadge', className);
var closeClassNames = classNames('euiBadge__icon', closeButtonProps === null || closeButtonProps === void 0 ? void 0 : closeButtonProps.className);
var Element = href && !isDisabled ? 'a' : 'button';
var relObj = {};
if (href && !isDisabled) {
relObj.href = href;
relObj.target = target;
relObj.rel = getSecureRelForTarget({
href: href,
target: target,
rel: rel
});
}
if (onClick) {
relObj.onClick = onClick;
}
var optionalIcon = null;
if (iconType) {
if (iconOnClick) {
if (!iconOnClickAriaLabel) {
console.warn('When passing the iconOnClick props to EuiBadge, you must also provide iconOnClickAriaLabel');
}
optionalIcon = ___EmotionJSX("button", {
type: "button",
className: "euiBadge__iconButton",
css: iconButtonCssStyles,
"aria-label": iconOnClickAriaLabel,
disabled: isDisabled,
title: iconOnClickAriaLabel,
onClick: iconOnClick
}, ___EmotionJSX(EuiIcon, _extends({
type: iconType,
size: "s",
color: "inherit" // forces the icon to inherit its parent color
}, closeButtonProps, {
className: closeClassNames,
css: [].concat(iconCssStyles, [closeButtonProps === null || closeButtonProps === void 0 ? void 0 : closeButtonProps.css])
})));
} else {
optionalIcon = ___EmotionJSX(EuiIcon, {
type: iconType,
size: children ? 's' : 'm',
className: "euiBadge__icon",
css: iconCssStyles,
color: "inherit" // forces the icon to inherit its parent color
});
}
}
if (onClick && !onClickAriaLabel) {
console.warn('When passing onClick to EuiBadge, you must also provide onClickAriaLabel');
}
var content = ___EmotionJSX("span", {
className: "euiBadge__content",
css: styles.euiBadge__content
}, iconSide === 'left' && optionalIcon, children && ___EmotionJSX("span", {
className: "euiBadge__text",
css: textCssStyles
}, children), iconSide === 'right' && optionalIcon);
if (iconOnClick) {
return onClick || href ? ___EmotionJSX("span", {
className: classes,
css: cssStyles,
style: customColorStyles
}, ___EmotionJSX("span", {
className: "euiBadge__content",
css: styles.euiBadge__content
}, iconSide === 'left' && optionalIcon, ___EmotionJSX(EuiInnerText, null, function (ref, innerText) {
return ___EmotionJSX(Element, _extends({
className: "euiBadge__childButton",
css: styles.euiBadge__childButton,
disabled: isDisabled,
"aria-label": onClickAriaLabel,
ref: ref,
title: innerText
}, relObj, rest), children);
}), iconSide === 'right' && optionalIcon)) : ___EmotionJSX(EuiInnerText, null, function (ref, innerText) {
return ___EmotionJSX("span", _extends({
className: classes,
css: cssStyles,
style: customColorStyles,
ref: ref,
title: innerText
}, rest), content);
});
} else if (onClick || href) {
return ___EmotionJSX(EuiInnerText, null, function (ref, innerText) {
return ___EmotionJSX(Element, _extends({
disabled: isDisabled,
"aria-label": onClickAriaLabel,
className: classes,
css: cssStyles,
style: customColorStyles,
ref: ref,
title: innerText
}, relObj, rest), content);
});
} else {
return ___EmotionJSX(EuiInnerText, null, function (ref, innerText) {
return ___EmotionJSX("span", _extends({
className: classes,
css: cssStyles,
style: customColorStyles,
ref: ref,
title: innerText
}, rest), content);
});
}
};