Skip to content

Instantly share code, notes, and snippets.

@RobinMalfait

RobinMalfait/Badge.tsx Secret

Last active Jun 23, 2021
Embed
What would you like to do?
import React, { ReactNode } from "react";
import { classNames } from "../utils/class-names";
enum Variant {
GRAY,
RED,
ORANGE,
YELLOW,
GREEN,
TEAL,
BLUE,
INDIGO,
PURPLE,
PINK
}
type Props = {
variant: Variant;
children?: ReactNode;
};
const VARIANT_MAPS: Record<Variant, string> = {
[Variant.GRAY]: "bg-gray-100 text-gray-800",
[Variant.RED]: "bg-red-100 text-red-800",
[Variant.ORANGE]: "bg-orange-100 text-orange-800",
[Variant.YELLOW]: "bg-yellow-100 text-yellow-800",
[Variant.GREEN]: "bg-green-100 text-green-800",
[Variant.TEAL]: "bg-teal-100 text-teal-800",
[Variant.BLUE]: "bg-blue-100 text-blue-800",
[Variant.INDIGO]: "bg-indigo-100 text-indigo-800",
[Variant.PURPLE]: "bg-purple-100 text-purple-800",
[Variant.PINK]: "bg-pink-100 text-pink-800"
};
export function Badge(props: Props) {
const { children, variant } = props;
return (
<span
className={classNames(
"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium leading-4 whitespace-no-wrap",
VARIANT_MAPS[variant]
)}
>
{children}
</span>
);
}
Badge.defaultProps = {
variant: Variant.GRAY
};
Badge.variant = Variant;
<Badge />
<Badge variant={Badge.variant.GRAY} />

Why this is bad (you might think)

  1. The file is big
  2. The file might look unmaintainable:
  • You have to update the class map & the enum (spoiler: TypeScript will ensure this is in sync)
  • There is a lot of duplicate code (the classNames per variant are almost the same except for the color)

Why this is good

  1. You know the exact states your component can be in
  2. There is no cleverness / dynamic classes going on -> More maintable
  3. There is no className prop, this means that we know what states / variants our component can be in, we don't introduce new implicit states (a large badge if we add className="text-xl")
  4. If one of the values need an extra class, you can add it, no need for clever tricks. (E.g.: Let's say a yellow color needs a darker text color)
  5. PurgeCSS can easily remove non-used css classes

Here is a bad example: https://gist.github.com/RobinMalfait/7cf7a0498039027a1d591f5f2b908a95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment