@carbon/react
Version:
React components for the Carbon Design System
65 lines (60 loc) • 2.06 kB
JavaScript
/**
* 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 = id => defaultTranslations[id];
/**
* `ListBoxTrigger` is used to orient the icon up or down depending on the
* state of the menu for a given `ListBox`
*/
const ListBoxTrigger = /*#__PURE__*/React.forwardRef(function ListBoxTrigger({
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,
/**
* i18n hook used to provide the appropriate description for the given menu
* icon. This function takes in an id defined in `translationIds` and should
* return a string message for that given message id.
*/
translateWithId: PropTypes.func
};
export { ListBoxTrigger as default, translationIds };