UNPKG

@carbon/react

Version:

React components for the Carbon Design System

110 lines (102 loc) 3.72 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 cx = require('classnames'); var React = require('react'); var PropTypes = require('prop-types'); var usePrefix = require('../../internal/usePrefix.js'); var useMergedRefs = require('../../internal/useMergedRefs.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx); var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes); /** * Determines if the content of an element is truncated. * * Merges a forwarded ref with a local ref to check the element's dimensions. * * @template T * @param forwardedRef - A ref passed from the parent component. * @param deps - Dependencies to re-run the truncation check. * @returns An object containing the truncation state and the merged ref. */ const useIsTruncated = (forwardedRef, deps = []) => { const localRef = React.useRef(null); const mergedRef = useMergedRefs.useMergedRefs([...(forwardedRef ? [forwardedRef] : []), localRef]); const [isTruncated, setIsTruncated] = React.useState(false); React.useEffect(() => { const element = localRef.current; if (element) { const { offsetWidth, scrollWidth } = element; setIsTruncated(offsetWidth < scrollWidth); } }, [localRef, ...deps]); return { isTruncated, ref: mergedRef }; }; /** * `ListBoxMenuItem` is a helper component for managing the container class * name, alongside any classes for any corresponding states, for a generic list * box menu item. */ const ListBoxMenuItem = /*#__PURE__*/React.forwardRef(({ children, isActive = false, isHighlighted = false, title, ...rest }, forwardedRef) => { const prefix = usePrefix.usePrefix(); const menuItemOptionRefProp = forwardedRef && typeof forwardedRef !== 'function' ? forwardedRef.menuItemOptionRef : undefined; const { isTruncated, ref: menuItemOptionRef } = useIsTruncated(menuItemOptionRefProp, [children]); const className = cx__default["default"](`${prefix}--list-box__menu-item`, { [`${prefix}--list-box__menu-item--active`]: isActive, [`${prefix}--list-box__menu-item--highlighted`]: isHighlighted }); return /*#__PURE__*/React__default["default"].createElement("li", _rollupPluginBabelHelpers["extends"]({}, rest, { className: className, title: isTruncated ? title : undefined }), /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--list-box__menu-item__option`, ref: menuItemOptionRef }, children)); }); ListBoxMenuItem.displayName = 'ListBoxMenuItem'; ListBoxMenuItem.propTypes = { /** * Specify any children nodes that should be rendered inside of the ListBox * Menu Item */ children: PropTypes__default["default"].node, /** * Specify if the item should be disabled */ disabled: PropTypes__default["default"].bool, /** * Specify whether the current menu item is "active". */ isActive: PropTypes__default["default"].bool, /** * Specify whether the current menu item is "highlighted". */ isHighlighted: PropTypes__default["default"].bool, /** * Provide an optional tooltip for the ListBoxMenuItem */ title: PropTypes__default["default"].string }; exports["default"] = ListBoxMenuItem;