@carbon/react
Version:
React components for the Carbon Design System
165 lines (163 loc) • 6.7 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.
*/
import { usePrefix } from "../../internal/usePrefix.js";
import { Text } from "../Text/Text.js";
import useIsomorphicEffect from "../../internal/useIsomorphicEffect.js";
import { useId } from "../../internal/useId.js";
import { deprecate } from "../../prop-types/deprecate.js";
import { isComponentElement } from "../../internal/utils.js";
import { useMergedRefs } from "../../internal/useMergedRefs.js";
import { DefinitionTooltip } from "../Tooltip/DefinitionTooltip.js";
import { AILabel } from "../AILabel/index.js";
import { isEllipsisActive } from "./isEllipsisActive.js";
import classNames from "classnames";
import React, { cloneElement, useRef, useState } from "react";
import PropTypes from "prop-types";
import { jsx, jsxs } from "react/jsx-runtime";
import { Close } from "@carbon/icons-react";
//#region src/components/Tag/Tag.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.
*/
const TYPES = {
red: "Red",
magenta: "Magenta",
purple: "Purple",
blue: "Blue",
cyan: "Cyan",
teal: "Teal",
green: "Green",
gray: "Gray",
"cool-gray": "Cool-Gray",
"warm-gray": "Warm-Gray",
"high-contrast": "High-Contrast",
outline: "Outline"
};
const SIZES = {
sm: "sm",
md: "md",
lg: "lg"
};
const Tag = React.forwardRef(({ children, className, decorator, id, type, filter, renderIcon: CustomIconElement, title = "Clear filter", disabled, onClose, size, as: BaseComponent, slug, ...other }, forwardRef) => {
const prefix = usePrefix();
const tagRef = useRef(null);
if (filter) console.warn("The `filter` prop for Tag has been deprecated and will be removed in the next major version. Use DismissibleTag instead.");
if (onClose) console.warn("The `onClose` prop for Tag has been deprecated and will be removed in the next major version. Use DismissibleTag instead.");
const ref = useMergedRefs([forwardRef, tagRef]);
const generatedTagId = useId();
const tagId = id ?? `tag-${generatedTagId}`;
const [isEllipsisApplied, setIsEllipsisApplied] = useState(false);
useIsomorphicEffect(() => {
const newElement = tagRef.current?.getElementsByClassName(`${prefix}--tag__label`)[0];
setIsEllipsisApplied(isEllipsisActive(newElement));
}, [prefix, tagRef]);
const isInteractiveTag = [
`${prefix}--tag--selectable`,
`${prefix}--tag--filter`,
`${prefix}--tag--operational`
].some((el) => className?.includes(el));
const tagClasses = classNames(`${prefix}--tag`, className, {
[`${prefix}--tag--disabled`]: disabled,
[`${prefix}--tag--filter`]: filter,
[`${prefix}--tag--${size}`]: size,
[`${prefix}--layout--size-${size}`]: size,
[`${prefix}--tag--${type}`]: type,
[`${prefix}--tag--interactive`]: other.onClick && !isInteractiveTag && isEllipsisApplied
});
const typeText = type !== void 0 && type in Object.keys(TYPES) ? TYPES[type] : "";
const handleClose = (event) => {
if (onClose) {
event.stopPropagation();
onClose(event);
}
};
const candidate = slug ?? decorator;
const normalizedDecorator = isComponentElement(candidate, AILabel) && !isInteractiveTag ? cloneElement(candidate, {
size: "sm",
kind: "inline"
}) : null;
if (filter) return /* @__PURE__ */ jsxs(BaseComponent ?? "div", {
className: tagClasses,
id: tagId,
...other,
children: [
CustomIconElement && size !== "sm" ? /* @__PURE__ */ jsx("div", {
className: `${prefix}--tag__custom-icon`,
children: /* @__PURE__ */ jsx(CustomIconElement, {})
}) : "",
/* @__PURE__ */ jsx(Text, {
title: typeof children === "string" ? children : void 0,
className: `${prefix}--tag__label`,
children: children !== null && children !== void 0 ? children : typeText
}),
normalizedDecorator,
/* @__PURE__ */ jsx("button", {
type: "button",
className: `${prefix}--tag__close-icon`,
onClick: handleClose,
disabled,
"aria-label": title,
title,
children: /* @__PURE__ */ jsx(Close, {})
})
]
});
const ComponentTag = BaseComponent ?? (other.onClick || className?.includes(`${prefix}--tag--operational`) ? "button" : "div");
const labelClasses = classNames({ [`${prefix}--tag__label`]: !isInteractiveTag });
return /* @__PURE__ */ jsxs(ComponentTag, {
ref,
disabled,
className: tagClasses,
id: tagId,
type: ComponentTag === "button" ? "button" : void 0,
...other,
children: [
CustomIconElement && size !== "sm" ? /* @__PURE__ */ jsx("div", {
className: `${prefix}--tag__custom-icon`,
children: /* @__PURE__ */ jsx(CustomIconElement, {})
}) : "",
isEllipsisApplied && !isInteractiveTag ? /* @__PURE__ */ jsx(DefinitionTooltip, {
openOnHover: false,
definition: children !== null && children !== void 0 ? children : typeText,
className: `${prefix}--definition--tooltip--tag`,
children: /* @__PURE__ */ jsx(Text, {
title: children !== null && children !== void 0 && typeof children === "string" ? children : typeText,
className: labelClasses,
children: children !== null && children !== void 0 ? children : typeText
})
}) : /* @__PURE__ */ jsx(Text, {
title: children !== null && children !== void 0 && typeof children === "string" ? children : typeText,
className: labelClasses,
children: children !== null && children !== void 0 ? children : typeText
}),
slug ? normalizedDecorator : decorator ? /* @__PURE__ */ jsx("div", {
className: `${prefix}--tag__decorator`,
children: normalizedDecorator
}) : ""
]
});
});
Tag.propTypes = {
as: PropTypes.elementType,
children: PropTypes.node,
className: PropTypes.string,
decorator: PropTypes.node,
disabled: PropTypes.bool,
filter: deprecate(PropTypes.bool, "The `filter` prop has been deprecated and will be removed in the next major version. Use DismissibleTag instead."),
id: PropTypes.string,
onClose: deprecate(PropTypes.func, "The `onClose` prop has been deprecated and will be removed in the next major version. Use DismissibleTag instead."),
renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
size: PropTypes.oneOf(Object.keys(SIZES)),
slug: deprecate(PropTypes.node, "The `slug` prop has been deprecated and will be removed in the next major version. Use the decorator prop instead."),
title: deprecate(PropTypes.string, "The `title` prop has been deprecated and will be removed in the next major version. Use DismissibleTag instead."),
type: PropTypes.oneOf(Object.keys(TYPES))
};
//#endregion
export { SIZES, TYPES, Tag as default };