Display

Tag

Label with a logo and a name, for technologies and tools

Preview

Vue
size
icon
children

Code

import css from "./Tag.module.css";

type Size = "sm" | "md";

interface ResponsiveSize {
  base: Size;
  mobile?: Size;
  tablet?: Size;
  desktop?: Size;
  wide?: Size;
}

interface TagProps {
  size: Size | ResponsiveSize;
  icon: string;
  children: React.ReactNode;
}

function getSizeClasses(size: Size | ResponsiveSize): string {
  if (typeof size === "string") {
    return css[size];
  }

  const classes = [css[`${size.base}_base`]];

  if (size.mobile) classes.push(css[`${size.mobile}_mobile`]);
  if (size.tablet) classes.push(css[`${size.tablet}_tablet`]);
  if (size.desktop) classes.push(css[`${size.desktop}_desktop`]);
  if (size.wide) classes.push(css[`${size.wide}_wide`]);

  return classes.join(" ");
}

export default function Tag({
  size,
  icon,
  children,
}: TagProps) {
  return (
    <span
      className={`
        ${css.tag}
        ${getSizeClasses(size)}
      `}
    >
      <img src={icon} alt="" className={css.tag_icon} />
      {children}
    </span>
  );
}

Props

PropTypeRequiredDescription
size"sm" | "md" | ResponsiveSizerequiredTag size (height, icon and padding). Fixed or responsive.
iconstringrequiredPath of the icon image.
childrenReact.ReactNoderequiredTag content.