UNPKG

@carbon/react

Version:

React components for the Carbon Design System

128 lines (126 loc) 4.43 kB
/** * 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 { useId } from "../../internal/useId.js"; import { deprecate } from "../../prop-types/deprecate.js"; import { IconButton } from "../IconButton/index.js"; import Toggletip, { ToggletipActions, ToggletipButton, ToggletipContent } from "../Toggletip/index.js"; import classNames from "classnames"; import React from "react"; import PropTypes from "prop-types"; import { jsx, jsxs } from "react/jsx-runtime"; import { Undo } from "@carbon/icons-react"; //#region src/components/AILabel/index.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 AILabelContent = ({ className, children }) => { const prefix = usePrefix(); return /* @__PURE__ */ jsx(ToggletipContent, { className: classNames(className, { [`${prefix}--ai-label-content`]: true, [`${prefix}--ai-label-content--with-actions`]: false }), children }); }; AILabelContent.displayName = "AILabelContent"; AILabelContent.propTypes = { children: PropTypes.node, className: PropTypes.string }; const AILabelActions = ({ className, children }) => { return /* @__PURE__ */ jsx(ToggletipActions, { className: classNames(className, { [`${usePrefix()}--ai-label-actions`]: true }), children }); }; AILabelActions.displayName = "AILabelActions"; AILabelActions.propTypes = { children: PropTypes.node, className: PropTypes.string }; const AILabel = React.forwardRef(function AILabel({ aiText = "AI", aiTextLabel, textLabel, align, autoAlign = true, children, className, kind = "default", onRevertClick, revertActive, revertLabel = "Revert to AI input", slugLabel = "Show information", ["aria-label"]: ariaLabel = "Show information", size = "xs", ...rest }, ref) { const prefix = usePrefix(); const id = useId("AILabel"); const aiLabelClasses = classNames(className, { [`${prefix}--ai-label`]: true, [`${prefix}--ai-label--revert`]: revertActive }); const aiLabelButtonClasses = classNames({ [`${prefix}--ai-label__button`]: true, [`${prefix}--ai-label__button--${size}`]: size, [`${prefix}--ai-label__button--${kind}`]: kind, [`${prefix}--ai-label__button--inline-with-content`]: kind === "inline" && (aiTextLabel || textLabel) }); const handleOnRevertClick = (evt) => { if (onRevertClick) onRevertClick(evt); }; const ariaLabelText = !aiTextLabel && !textLabel ? `${aiText} ${slugLabel || ariaLabel}` : `${aiText} ${aiTextLabel || textLabel}`; const isSmallIcon = [ "xs", "2xs", "mini" ].includes(size); return /* @__PURE__ */ jsx("div", { className: aiLabelClasses, ref, id, children: revertActive ? /* @__PURE__ */ jsx(IconButton, { onClick: handleOnRevertClick, kind: "ghost", size: "sm", label: revertLabel, ...rest, children: /* @__PURE__ */ jsx(Undo, {}) }) : /* @__PURE__ */ jsxs(Toggletip, { align, autoAlign, alignmentAxisOffset: isSmallIcon ? -24 : 0, ...rest, children: [/* @__PURE__ */ jsxs(ToggletipButton, { className: aiLabelButtonClasses, label: kind === "inline" ? "" : ariaLabelText, children: [/* @__PURE__ */ jsx("span", { className: `${prefix}--ai-label__text`, children: aiText }), kind === "inline" && (aiTextLabel || textLabel) && /* @__PURE__ */ jsx("span", { className: `${prefix}--ai-label__additional-text`, children: aiTextLabel || textLabel })] }), children] }) }); }); AILabel.displayName = "AILabel"; AILabel.propTypes = { ...Toggletip.propTypes, AILabelContent: PropTypes.node, aiText: PropTypes.string, aiTextLabel: deprecate(PropTypes.string, "`aiTextLabel` on `AILabel` has been deprecated - Please use the `textLabel` prop instead"), "aria-label": PropTypes.string, kind: PropTypes.oneOf(["default", "inline"]), onRevertClick: PropTypes.func, revertActive: PropTypes.bool, revertLabel: PropTypes.string, size: PropTypes.oneOf([ "mini", "2xs", "xs", "sm", "md", "lg", "xl" ]), slugLabel: deprecate(PropTypes.string, "`slugLabel` on `AILabel` has been deprecated - Please use the `ariaLabel` prop instead"), textLabel: PropTypes.string }; //#endregion export { AILabel, AILabelActions, AILabelContent };