UNPKG

@trail-ui/react

Version:
158 lines (156 loc) 5.66 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/chip/chip.tsx var chip_exports = {}; __export(chip_exports, { Chip: () => _Chip, ChipContext: () => ChipContext }); module.exports = __toCommonJS(chip_exports); var import_icons = require("@trail-ui/icons"); var import_shared_utils = require("@trail-ui/shared-utils"); var import_theme = require("@trail-ui/theme"); var import_utils = require("@react-aria/utils"); var import_react = require("react"); var import_react_aria = require("react-aria"); var import_react_aria_components = require("react-aria-components"); var import_jsx_runtime = require("react/jsx-runtime"); var ChipContext = (0, import_react.createContext)({}); function Chip(props, ref) { [props, ref] = (0, import_react_aria_components.useContextProps)(props, ref, ChipContext); const ctx = props; const { elementType, children, avatar, startContent: startContentProps, endContent: endContentProps, classNames, className, onClose, isBordered, isReadOnly, ...chipProps } = props; const variantProps = (0, import_theme.filterVariantProps)(props, import_theme.chip.variantKeys); const Component = elementType || "div"; let componentProps = chipProps; if (typeof Component === "string") { componentProps = (0, import_utils.filterDOMProps)(chipProps); } const baseStyles = (0, import_shared_utils.clsx)(classNames == null ? void 0 : classNames.base, className); const isCloseable = !!onClose; const isDot = props.variant === "dot"; const { focusProps: closeFocusProps, isFocusVisible: isCloseButtonFocusVisible } = (0, import_react_aria.useFocusRing)(); const { pressProps: closePressProps } = (0, import_react_aria.usePress)({ isDisabled: !!(ctx == null ? void 0 : ctx.isDisabled), onPress: onClose }); const hasStartContent = (0, import_react.useMemo)( () => !!avatar || !!startContentProps, [avatar, startContentProps] ); const hasEndContent = (0, import_react.useMemo)( () => !!endContentProps || isCloseable, [endContentProps, isCloseable] ); const slots = (0, import_react.useMemo)( () => (0, import_theme.chip)({ ...variantProps, hasStartContent, hasEndContent, isCloseable, isCloseButtonFocusVisible, isBordered, isReadOnly }), [ variantProps, hasStartContent, hasEndContent, isCloseable, isCloseButtonFocusVisible, isBordered, isReadOnly ] ); const getAvatarClone = (avatar2) => { if (!(0, import_react.isValidElement)(avatar2)) return null; return (0, import_react.cloneElement)(avatar2, { // @ts-ignore className: slots.avatar({ class: classNames == null ? void 0 : classNames.avatar }) }); }; const getContentClone = (content) => (0, import_react.isValidElement)(content) ? (0, import_react.cloneElement)(content, { // @ts-ignore className: (0, import_shared_utils.clsx)("max-h-[80%]", content.props.className) }) : null; const startContent = getAvatarClone(avatar) || getContentClone(startContentProps); const endContent = getContentClone(endContentProps); const start = (0, import_react.useMemo)(() => { if (isDot && !startContent) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: slots.dot({ class: classNames == null ? void 0 : classNames.dot }) }); } return startContent; }, [isDot, startContent, slots, classNames == null ? void 0 : classNames.dot]); const end = (0, import_react.useMemo)(() => { if (isCloseable) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "span", { role: "button", tabIndex: 0, "aria-label": `Remove ${children}`, className: slots.closeButton({ class: classNames == null ? void 0 : classNames.closeButton }), ...(0, import_react_aria.mergeProps)(closePressProps, closeFocusProps), children: endContent || /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icons.CloseIcon, {}) } ); } return endContent; }, [ classNames == null ? void 0 : classNames.closeButton, closeFocusProps, closePressProps, children, endContent, isCloseable, slots ]); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( Component, { ref, className: `${slots.base({ class: baseStyles })} ${isBordered && slots.border({ class: classNames == null ? void 0 : classNames.border })}`, ...componentProps, children: [ start, /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: slots.content({ class: classNames == null ? void 0 : classNames.content }), children }), end ] } ); } var _Chip = (0, import_react.forwardRef)(Chip); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Chip, ChipContext });