@carbon/react
Version:
React components for the Carbon Design System
84 lines (82 loc) • 3.15 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_usePrefix = require("../../internal/usePrefix.js");
const require_useMergedRefs = require("../../internal/useMergedRefs.js");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let react_jsx_runtime = require("react/jsx-runtime");
//#region src/components/ListBox/ListBoxMenuItem.tsx
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* 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 = (0, react.useRef)(null);
const mergedRef = require_useMergedRefs.useMergedRefs([...forwardedRef ? [forwardedRef] : [], localRef]);
const [isTruncated, setIsTruncated] = (0, react.useState)(false);
(0, 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 = (0, react.forwardRef)(({ children, isActive = false, isHighlighted = false, title, ...rest }, forwardedRef) => {
const prefix = require_usePrefix.usePrefix();
const { isTruncated, ref: menuItemOptionRef } = useIsTruncated(forwardedRef && typeof forwardedRef !== "function" ? forwardedRef.menuItemOptionRef : void 0, [children]);
const className = (0, classnames.default)(`${prefix}--list-box__menu-item`, {
[`${prefix}--list-box__menu-item--active`]: isActive,
[`${prefix}--list-box__menu-item--highlighted`]: isHighlighted
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", {
...rest,
className,
title: isTruncated ? title : void 0,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: `${prefix}--list-box__menu-item__option`,
ref: menuItemOptionRef,
children
})
});
});
ListBoxMenuItem.displayName = "ListBoxMenuItem";
ListBoxMenuItem.propTypes = {
children: prop_types.default.node,
disabled: prop_types.default.bool,
isActive: prop_types.default.bool,
isHighlighted: prop_types.default.bool,
title: prop_types.default.string
};
//#endregion
exports.default = ListBoxMenuItem;