Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

2.2 KB
Raw Blame
import { ComponentPropsWithRef, ElementType, type JSX, ReactNode, useRef } from "react";
import { DotLottie, DotLottieReact } from "@lottiefiles/dotlottie-react";
import { twMerge } from "tailwind-merge";

type MenuIconButtonProps<T extends ElementType> = ComponentPropsWithRef<T> & {
  as?: T;
  icon?: string;
  isDisabled?: boolean;
  isSelected?: boolean;
  description?: ReactNode;
  inputRef?: ComponentPropsWithRef<T>["ref"];
  lottieIconMode?: "reverse" | "forward";
};

export const MenuIconButton = <T extends ElementType = "button">({
  children,
  icon,
  className,
  isDisabled,
  isSelected,
  as: Item = "div",
  description,
  // wrapping in forward ref with generic component causes the loss of ts definitions on props
  inputRef,
  lottieIconMode = "forward",
  ...props
}: MenuIconButtonProps<T>): JSX.Element => {
  const iconRef = useRef<DotLottie | null>(null);
  return (
    <div className={!isSelected ? "hover:px-1" : ""}>
      <Item
        type="button"
        role="menuitem"
        className={twMerge(
          "group relative my-1 flex w-full cursor-pointer flex-col items-center justify-center rounded-sm p-2 font-sans text-sm text-foreground-default transition-all duration-150 hover:bg-surface-hover",
          isSelected && "rounded-none bg-page hover:bg-surface-active",
          isDisabled && "cursor-not-allowed hover:bg-transparent",
          className
        )}
        onMouseEnter={() => iconRef.current?.play()}
        onMouseLeave={() => iconRef.current?.stop()}
        ref={inputRef}
        {...props}
      >
        <div
          className={`${
            isSelected ? "opacity-100" : "opacity-0"
          } absolute left-0 h-full w-1 bg-project transition-all duration-150`}
        />
        {icon && (
          <div className="my-auto mb-2 h-6 w-6">
            <DotLottieReact
              dotLottieRefCallback={(el) => {
                iconRef.current = el;
              }}
              src={`/lotties/${icon}.json`}
              loop
              className="h-full w-full"
              mode={lottieIconMode}
            />
          </div>
        )}
        <div className="grow justify-center text-center break-words" style={{ fontSize: "10px" }}>
          {children}
        </div>
      </Item>
    </div>
  );
};