@carbon/ibm-products
Version:
Carbon for IBM Products
113 lines (111 loc) • 4.13 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 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.
*/
import { __toESM } from "../../../_virtual/_rolldown/runtime.js";
import { require_classnames } from "../../../node_modules/classnames/index.js";
import { pkg } from "../../../settings.js";
import { getDevtoolsProps } from "../../../global/js/utils/devtools.js";
import React, { Children, forwardRef, useEffect, useMemo, useRef, useState } from "react";
import PropTypes from "prop-types";
import { FeatureFlags, OverflowMenu, OverflowMenuItem } from "@carbon/react";
import { createOverflowHandler } from "@carbon/utilities";
//#region src/components/Card/next/CardActions.tsx
/**
* Copyright IBM Corp. 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.
*/
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--card-next`;
const componentName = "CardActions";
/**
* CardActions is a container for action buttons in the card header.
* Positioned in the top-right corner with 8px gap between actions.
* When actions exceed 50% of available header space, overflow menu is shown.
*/
let CardActions = forwardRef(({ children, className, overflowMenuLabel = "More actions", ...rest }, ref) => {
const containerRef = useRef(null);
const [hiddenItems, setHiddenItems] = useState([]);
const classes = (0, import_classnames.default)(`${blockClass}__actions`, className);
const actionItems = useMemo(() => {
const items = [];
Children.forEach(children, (child, index) => {
if (React.isValidElement(child)) {
const actionProps = child.props;
const button = actionProps.children;
const buttonProps = React.isValidElement(button) ? button.props : null;
const label = actionProps.label ?? buttonProps?.label ?? buttonProps?.iconDescription ?? (typeof buttonProps?.children === "string" ? buttonProps.children : `Action ${index + 1}`);
const id = `${label}-${index}`;
items.push({
id,
element: child,
label
});
}
});
return items;
}, [children]);
useEffect(() => {
if (!containerRef.current || actionItems.length === 0) return;
const handler = createOverflowHandler({
container: containerRef.current,
onChange: (_visible, hidden) => {
const hiddenIds = hidden.map((el) => el.dataset.id);
setHiddenItems(actionItems.filter((item) => hiddenIds.includes(item.id)));
}
});
return () => handler.disconnect();
}, [children]);
return /* @__PURE__ */ React.createElement("div", {
ref: (node) => {
containerRef.current = node;
if (typeof ref === "function") ref(node);
else if (ref) ref.current = node;
},
className: classes,
...rest,
...getDevtoolsProps(componentName)
}, actionItems.map((item) => /* @__PURE__ */ React.createElement("div", {
key: item.id,
"data-id": item.id
}, item.element)), /* @__PURE__ */ React.createElement("div", {
"data-offset": true,
"data-hidden": true,
"data-floating-menu-container": true
}, /* @__PURE__ */ React.createElement(FeatureFlags, { enableV12Overflowmenu: true }, /* @__PURE__ */ React.createElement(OverflowMenu, {
size: "sm",
"aria-label": overflowMenuLabel
}, hiddenItems.map((item) => /* @__PURE__ */ React.createElement(OverflowMenuItem, {
key: item.id,
itemText: item.label || "Action",
onClick: () => {
const button = item.element.props.children;
if (button && React.isValidElement(button)) {
const buttonProps = button.props;
if (buttonProps.onClick) buttonProps.onClick();
}
}
}))))));
});
CardActions.propTypes = {
/**
* Provide the contents of the CardActions.
*/
children: PropTypes.node,
/**
* Provide an optional class to be applied to the containing node.
*/
className: PropTypes.string,
/**
* Aria label for the overflow menu
*/
overflowMenuLabel: PropTypes.string
};
CardActions = pkg.checkComponentEnabled(CardActions, componentName);
CardActions.displayName = componentName;
//#endregion
export { CardActions };