@carbon/ibm-products
Version:
Carbon for IBM Products
88 lines (86 loc) • 3.7 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 uuidv4 from "../../global/js/utils/uuidv4.js";
import React, { Children, createElement, forwardRef, useRef, useState } from "react";
import { node, shape, string } from "prop-types";
import { Button, OverflowMenu, OverflowMenuItem } from "@carbon/react";
import { ChevronDown, ChevronUp } from "@carbon/react/icons";
//#region src/components/ComboButton/ComboButton.tsx
/**
* Copyright IBM Corp. 2020, 2024
*
* 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}--combo-button`;
const componentName = "ComboButton";
/**
* The combo button consolidates similar actions, while exposing the most commonly used one.
*/
const ComboButton = forwardRef((props, ref) => {
const { children, className, overflowMenu, ...rest } = props;
const { current: instanceId } = useRef(uuidv4());
const [isOpen, setIsOpen] = useState(false);
const actions = Children.toArray(children).filter(Boolean).map((child) => {
if (React.isValidElement(child)) {
const { props } = child;
return {
...props,
children: /* @__PURE__ */ React.createElement("span", { className: `${blockClass}__action` }, child)
};
}
return null;
});
const primaryAction = actions.slice(0, 1);
const secondaryActions = actions.slice(1);
return /* @__PURE__ */ React.createElement("div", {
...rest,
ref,
className: (0, import_classnames.default)(blockClass, className),
"data-floating-menu-container": true
}, /* @__PURE__ */ React.createElement(Button, primaryAction), secondaryActions.length > 0 && /* @__PURE__ */ React.createElement(OverflowMenu, {
...overflowMenu,
className: `${blockClass}__overflow-menu`,
menuOptionsClass: `${blockClass}__overflow-menu__list`,
onClick: () => !isOpen && setIsOpen(true),
onClose: () => setIsOpen(false),
renderIcon: () => createElement(isOpen ? (props) => /* @__PURE__ */ React.createElement(ChevronUp, {
size: 16,
...props
}) : (props) => /* @__PURE__ */ React.createElement(ChevronDown, {
size: 16,
...props
}), { className: `${blockClass}__overflow-menu__icon` }),
flipped: true
}, secondaryActions.map(({ children, renderIcon: Icon, ...action }, index) => /* @__PURE__ */ React.createElement(OverflowMenuItem, {
...action,
key: `${blockClass}--${instanceId}__overflow-menu__item__${index}`,
className: `${blockClass}__overflow-menu__item`,
itemText: /* @__PURE__ */ React.createElement(React.Fragment, null, children, Icon && /* @__PURE__ */ React.createElement("span", { className: `${blockClass}__overflow-menu__item__icon` }, /* @__PURE__ */ React.createElement(Icon, null)))
}))));
});
/**@ts-ignore*/
ComboButton.deprecated = {
level: "warn",
details: `Please replace ${componentName} with Carbon's ComboButton`
};
ComboButton.displayName = componentName;
ComboButton.propTypes = {
/** Provide the contents of the `ComboButton` */
children: node.isRequired,
/** Provide an optional class to be applied to the containing node */
className: string,
/** Provide the [props of the `OverflowMenu`](https://react.carbondesignsystem.com/?path=/docs/overflowmenu) */
/**@ts-ignore*/
overflowMenu: shape(OverflowMenu.propTypes)
};
//#endregion
export { ComboButton };