UNPKG

@carbon/react

Version:

React components for the Carbon Design System

66 lines (61 loc) 2.01 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. */ import { extends as _extends } from '../../../_virtual/_rollupPluginBabelHelpers.js'; import cx from 'classnames'; import React from 'react'; import PropTypes from 'prop-types'; import { ChevronDown } from '@carbon/icons-react'; import { usePrefix } from '../../../internal/usePrefix.js'; var _ChevronDown; const translationIds = { 'close.menu': 'close.menu', 'open.menu': 'open.menu' }; const defaultTranslations = { [translationIds['close.menu']]: 'Close', [translationIds['open.menu']]: 'Open' }; const defaultTranslateWithId = messageId => { return defaultTranslations[messageId]; }; /** * `ListBoxTrigger` is used to orient the icon up or down depending on the * state of the menu for a given `ListBox` */ // eslint-disable-next-line react/display-name -- https://github.com/carbon-design-system/carbon/issues/20452 const ListBoxTrigger = /*#__PURE__*/React.forwardRef(({ isOpen, translateWithId: t = defaultTranslateWithId, ...rest }, ref) => { const prefix = usePrefix(); const className = cx({ [`${prefix}--list-box__menu-icon`]: true, [`${prefix}--list-box__menu-icon--open`]: isOpen }); const description = isOpen ? t('close.menu') : t('open.menu'); return /*#__PURE__*/React.createElement("button", _extends({}, rest, { "aria-label": description, title: description, className: className, type: "button", tabIndex: -1, ref: ref }), _ChevronDown || (_ChevronDown = /*#__PURE__*/React.createElement(ChevronDown, null))); }); ListBoxTrigger.propTypes = { /** * Specify whether the menu is currently open, which will influence the * direction of the menu icon */ isOpen: PropTypes.bool.isRequired, /** * Translates component strings using your i18n tool. */ translateWithId: PropTypes.func }; export { ListBoxTrigger as default };