UNPKG

@elastic/eui

Version:

Elastic UI Component Library

146 lines (141 loc) 6.13 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.SIZES = exports.EuiButtonIcon = exports.DISPLAYS = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _react = _interopRequireDefault(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _services = require("../../../services"); var _icon = require("../../icon"); var _loading = require("../../loading"); var _button = require("../../../themes/amsterdam/global_styling/mixins/button"); var _button_display = require("../button_display/_button_display"); var _button_icon = require("./button_icon.styles"); var _react2 = require("@emotion/react"); var _excluded = ["className", "iconType", "iconSize", "color", "isDisabled", "disabled", "href", "type", "display", "target", "rel", "size", "buttonRef", "isSelected", "isLoading"]; /* * 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. */ var SIZES = ['xs', 's', 'm']; exports.SIZES = SIZES; var DISPLAYS = ['base', 'empty', 'fill']; exports.DISPLAYS = DISPLAYS; var EuiButtonIcon = function EuiButtonIcon(props) { var className = props.className, iconType = props.iconType, _props$iconSize = props.iconSize, iconSize = _props$iconSize === void 0 ? 'm' : _props$iconSize, _props$color = props.color, _color = _props$color === void 0 ? 'primary' : _props$color, _isDisabled = props.isDisabled, disabled = props.disabled, href = props.href, _props$type = props.type, type = _props$type === void 0 ? 'button' : _props$type, _props$display = props.display, display = _props$display === void 0 ? 'empty' : _props$display, target = props.target, rel = props.rel, _props$size = props.size, size = _props$size === void 0 ? 'xs' : _props$size, buttonRef = props.buttonRef, isSelected = props.isSelected, isLoading = props.isLoading, rest = (0, _objectWithoutProperties2.default)(props, _excluded); var euiThemeContext = (0, _services.useEuiTheme)(); var isDisabled = (0, _button_display.isButtonDisabled)({ isDisabled: _isDisabled || disabled, href: href, isLoading: isLoading }); var ariaHidden = rest['aria-hidden']; var isAriaHidden = ariaHidden === 'true' || ariaHidden === true; if (!rest['aria-label'] && !rest['aria-labelledby'] && !isAriaHidden) { console.warn("EuiButtonIcon requires aria-label or aria-labelledby to be specified because icon-only\n buttons are screen-reader-inaccessible without them."); } var color = isDisabled ? 'disabled' : _color === 'ghost' ? 'text' : _color; var buttonColorStyles = (0, _button.useEuiButtonColorCSS)({ display: display }); var buttonFocusStyle = (0, _button.useEuiButtonFocusCSS)(); var styles = (0, _button_icon.euiButtonIconStyles)(euiThemeContext); var emptyHoverStyles = (0, _button_icon._emptyHoverStyles)(euiThemeContext, color); var cssStyles = [styles.euiButtonIcon, styles[size], buttonColorStyles[color], buttonFocusStyle, display === 'empty' && emptyHoverStyles, isDisabled && styles.isDisabled]; var classes = (0, _classnames.default)('euiButtonIcon', className); if (_color === 'ghost') { // INCEPTION: If `ghost`, re-implement with a wrapping dark mode theme provider return (0, _react2.jsx)(_services.EuiThemeProvider, { colorMode: "dark", wrapperProps: { cloneElement: true } }, (0, _react2.jsx)(EuiButtonIcon, (0, _extends2.default)({}, props, { color: "text" }))); } // Add an icon to the button if one exists. var buttonIcon; if (iconType && !isLoading) { buttonIcon = (0, _react2.jsx)(_icon.EuiIcon, { className: "euiButtonIcon__icon", type: iconType, size: iconSize, "aria-hidden": "true", color: "inherit" // forces the icon to inherit its parent color }); } if (iconType && isLoading) { // `original` size doesn't exist in `EuiLoadingSpinner` // when the `iconSize` is `original` we don't pass any size to the `EuiLoadingSpinner` // so it gets the default size var loadingSize = iconSize === 'original' ? undefined : iconSize; // When the button is disabled the text gets gray // and in some buttons the background gets a light gray // for better contrast we want to change the border of the spinner // to have the same color of the text. This way we ensure the borders // are always visible. The default spinner color could be very light. var loadingSpinnerColor = isDisabled ? { border: 'currentcolor' } : undefined; buttonIcon = (0, _react2.jsx)(_loading.EuiLoadingSpinner, { size: loadingSize, color: loadingSpinnerColor }); } // <a> elements don't respect the `disabled` attribute. So if we're disabled, we'll just pretend // this is a button and piggyback off its disabled styles. if (href && !isDisabled) { var secureRel = (0, _services.getSecureRelForTarget)({ href: href, target: target, rel: rel }); return (0, _react2.jsx)("a", (0, _extends2.default)({ css: cssStyles, tabIndex: isAriaHidden ? -1 : undefined, className: classes, href: href, target: target, rel: secureRel, ref: buttonRef }, rest), buttonIcon); } var buttonType; return (0, _react2.jsx)("button", (0, _extends2.default)({ css: cssStyles, tabIndex: isAriaHidden ? -1 : undefined, disabled: isDisabled, className: classes, "aria-pressed": isSelected, type: type, ref: buttonRef }, rest), buttonIcon); }; exports.EuiButtonIcon = EuiButtonIcon;