Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

3.3 KB
Raw Blame
import { Pill as MantinePill } from "@mantine/core";
import type {
  CSSProperties,
  HTMLAttributes,
  KeyboardEvent,
  ReactNode,
} from "react";
import "@editor/ui/Chip.css";

/** Same accent dial as Button. */
export type ChipAccent =
  | "default"
  | "neutral"
  | "brand"
  | "ai"
  | "premium"
  | "danger"
  | "success"
  | "warning";
export type ChipSize = "xs" | "sm" | "md" | "lg";
/** primary = solid fill; secondary = soft tinted tag (the default tag look). */
export type ChipVariant = "primary" | "secondary";

export interface ChipProps extends Omit<
  HTMLAttributes<HTMLElement>,
  "onClick"
> {
  accent?: ChipAccent;
  variant?: ChipVariant;
  size?: ChipSize;
  leadingIcon?: ReactNode;
  trailingIcon?: ReactNode;
  /** Shows a spinner and dims the chip. */
  loading?: boolean;
  onRemove?: () => void;
  onClick?: () => void;
  /** Leading status dot. Use for status-style chips. */
  showDot?: boolean;
  /** Dashed "box-cut" outline with no fill - for add/placeholder affordances. */
  dashed?: boolean;
  style?: CSSProperties;
  children?: ReactNode;
  className?: string;
}

/** Open-ended chip/tag (Mantine Pill-backed). For semantic status use StatusBadge. */
export function Chip({
  accent = "default",
  variant = "secondary",
  size = "md",
  leadingIcon,
  trailingIcon,
  loading = false,
  onRemove,
  onClick,
  showDot,
  dashed,
  style,
  children,
  className,
  ...rest
}: ChipProps) {
  const classes = [
    "sui-chip",
    `sui-acc-${accent}`,
    `sui-chip--${variant}`,
    onClick ? "sui-chip--interactive" : "",
    loading ? "sui-chip--loading" : "",
    dashed ? "sui-chip--dashed" : "",
    className ?? "",
  ]
    .filter(Boolean)
    .join(" ");

  // Interactive chips are a <span>, so add button semantics + Enter/Space activation.
  const interactive = !!onClick && !loading;
  const removable = !!onRemove && !loading;
  const buttonProps = interactive
    ? {
        role: "button",
        tabIndex: 0,
        onKeyDown: (e: KeyboardEvent<HTMLElement>) => {
          if (e.key === "Enter" || e.key === " ") {
            e.preventDefault();
            onClick();
          }
        },
      }
    : {};
  // A removable chip carries Mantine's remove <button> inside the pill root, and
  // a control may not nest inside a control — so the button semantics move to
  // the label, leaving the two as siblings. The click handler stays on the root
  // either way, so the whole chip remains the pointer target.
  const rootProps = {
    ...(interactive ? { onClick } : {}),
    ...(removable ? {} : buttonProps),
  };

  return (
    <MantinePill
      {...rest}
      className={classes}
      style={style}
      size={size}
      withRemoveButton={removable}
      onRemove={onRemove}
      {...rootProps}
    >
      {showDot && <span className="sui-chip__dot" aria-hidden />}
      {loading ? (
        <span className="sui-chip__spinner" aria-hidden />
      ) : leadingIcon ? (
        <span className="sui-chip__icon" aria-hidden>
          {leadingIcon}
        </span>
      ) : null}
      <span className="sui-chip__label" {...(removable ? buttonProps : {})}>
        {children}
      </span>
      {trailingIcon && !onRemove && (
        <span className="sui-chip__icon" aria-hidden>
          {trailingIcon}
        </span>
      )}
    </MantinePill>
  );
}