UNPKG

@carbon/react

Version:

React components for the Carbon Design System

107 lines (101 loc) 3.49 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'); /** * 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, // eslint-disable-next-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452 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); } // eslint-disable-next-line react-hooks/exhaustive-deps -- https://github.com/carbon-design-system/carbon/issues/20452 }, [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(`${prefix}--list-box__menu-item`, { [`${prefix}--list-box__menu-item--active`]: isActive, [`${prefix}--list-box__menu-item--highlighted`]: isHighlighted }); return /*#__PURE__*/React.createElement("li", _rollupPluginBabelHelpers.extends({}, rest, { className: className, title: isTruncated ? title : undefined }), /*#__PURE__*/React.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.node, /** * Specify if the item should be disabled */ disabled: PropTypes.bool, /** * Specify whether the current menu item is "active". */ isActive: PropTypes.bool, /** * Specify whether the current menu item is "highlighted". */ isHighlighted: PropTypes.bool, /** * Provide an optional tooltip for the ListBoxMenuItem */ title: PropTypes.string }; exports.default = ListBoxMenuItem;