UNPKG

@carbon/react

Version:

React components for the Carbon Design System

200 lines (188 loc) 7.22 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var PropTypes = require('prop-types'); var React = require('react'); var cx = require('classnames'); require('../Tooltip/DefinitionTooltip.js'); var Tooltip = require('../Tooltip/Tooltip.js'); var useId = require('../../internal/useId.js'); var usePrefix = require('../../internal/usePrefix.js'); var ButtonBase = require('../Button/ButtonBase.js'); var deprecateValuesWithin = require('../../prop-types/deprecateValuesWithin.js'); var index = require('../BadgeIndicator/index.js'); var mapPopoverAlign = require('../../tools/mapPopoverAlign.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes); var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx); const IconButtonKinds = ['primary', 'secondary', 'ghost', 'tertiary']; const IconButton = /*#__PURE__*/React__default["default"].forwardRef(function IconButton({ align, autoAlign = false, badgeCount, children, className, closeOnActivation = true, defaultOpen = false, disabled, dropShadow = false, enterDelayMs = 100, highContrast = true, kind, label, leaveDelayMs = 100, wrapperClasses, size, isSelected, ...rest }, ref) { const prefix = usePrefix.usePrefix(); const tooltipClasses = cx__default["default"](wrapperClasses, `${prefix}--icon-tooltip`, { [`${prefix}--icon-tooltip--disabled`]: disabled }); if (badgeCount && (kind !== 'ghost' || size !== 'lg')) { console.warn("The prop BadgeCount must be used with hasIconOnly=true, kind='ghost' and size='lg'"); } const badgeId = useId.useId('badge-indicator'); return /*#__PURE__*/React__default["default"].createElement(Tooltip.Tooltip, { align: align, autoAlign: autoAlign, closeOnActivation: closeOnActivation, className: tooltipClasses, defaultOpen: defaultOpen, dropShadow: dropShadow, enterDelayMs: enterDelayMs, highContrast: highContrast, label: label, leaveDelayMs: leaveDelayMs }, /*#__PURE__*/React__default["default"].createElement(ButtonBase["default"], _rollupPluginBabelHelpers["extends"]({}, rest, { disabled: disabled, kind: kind, ref: ref, size: size, isSelected: isSelected, hasIconOnly: true, className: className, "aria-describedby": rest['aria-describedby'] || badgeCount && badgeId }), children, !disabled && badgeCount !== undefined && /*#__PURE__*/React__default["default"].createElement(index.BadgeIndicator, { id: badgeId, count: badgeCount > 0 ? badgeCount : undefined }))); }); IconButton.propTypes = { /** * Specify how the trigger should align with the tooltip */ align: deprecateValuesWithin["default"](PropTypes__default["default"].oneOf(['top', 'top-left', // deprecated use top-start instead 'top-right', // deprecated use top-end instead 'bottom', 'bottom-left', // deprecated use bottom-start instead 'bottom-right', // deprecated use bottom-end instead 'left', 'left-bottom', // deprecated use left-end instead 'left-top', // deprecated use left-start instead 'right', 'right-bottom', // deprecated use right-end instead 'right-top', // deprecated use right-start instead // new values to match floating-ui 'top-start', 'top-end', 'bottom-start', 'bottom-end', 'left-end', 'left-start', 'right-end', 'right-start']), ['top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end'], mapPopoverAlign.mapPopoverAlign), /** * **Experimental**: Will attempt to automatically align the tooltip */ autoAlign: PropTypes__default["default"].bool, /** * **Experimental**: Display a badge on the button. An empty/dot badge if 0, a numbered badge if > 0. * Must be used with size="lg", kind="ghost" and hasIconOnly=true */ badgeCount: PropTypes__default["default"].number, /** * Optionally specify an href for your IconButton to become an `<a>` element */ href: PropTypes__default["default"].string, /** * Provide an icon or asset to be rendered inside of the IconButton */ children: PropTypes__default["default"].node, /** * Specify an optional className to be added to your Button */ className: PropTypes__default["default"].string, /** * Determines whether the tooltip should close when inner content is activated (click, Enter or Space) */ closeOnActivation: PropTypes__default["default"].bool, /** * Specify whether the tooltip should be open when it first renders */ defaultOpen: PropTypes__default["default"].bool, /** * Specify whether a drop shadow should be rendered on the tooltip */ dropShadow: PropTypes__default["default"].bool, /** * Specify whether the Button should be disabled, or not */ disabled: PropTypes__default["default"].bool, /** * Specify the duration in milliseconds to delay before displaying the tooltip */ enterDelayMs: PropTypes__default["default"].number, /** * Specify whether the IconButton is currently selected */ isSelected: PropTypes__default["default"].bool, /** * Render the tooltip using the high-contrast theme */ highContrast: PropTypes__default["default"].bool, /** * Specify the type of button to be used as the base for the IconButton */ kind: PropTypes__default["default"].oneOf(IconButtonKinds), /** * Provide the label to be rendered inside of the Tooltip. The label will use * `aria-labelledby` and will fully describe the child node that is provided. * If the child node already has an `aria-label`, the tooltip will not apply * `aria-labelledby`. If the child node has `aria-labelledby`, that value will * be used instead. Otherwise, the tooltip will use its own ID as the label. * This means that if you have text in the child node it will not be * announced to the screen reader. * If using `badgeCount={0}`, make sure the label explains that there is a * new notification. */ label: PropTypes__default["default"].node.isRequired, /** * Specify the duration in milliseconds to delay before hiding the tooltip */ leaveDelayMs: PropTypes__default["default"].number, /** * Optionally specify a `rel` when using an `<a>` element. */ rel: PropTypes__default["default"].string, /** * Specify the size of the Button. */ size: PropTypes__default["default"].oneOf(['sm', 'md', 'lg']), /** * Optionally specify a `target` when using an `<a>` element. */ target: PropTypes__default["default"].string, /** * Specify an optional className to be added to your Tooltip wrapper */ wrapperClasses: PropTypes__default["default"].string }; exports.IconButton = IconButton; exports.IconButtonKinds = IconButtonKinds;