COMPONENTS/DATA DISPLAY
Badge.
Small signals with useful meaning.
In practice
PublishedDraftAvailablev0.2In review
Variants: default, secondary, outline, success, warning and destructive. Communicate status in text as well as color.
Installation
Install Coal once. Components and TypeScript declarations are included. React 19 and React DOM 19 are peer dependencies.
Terminal · local package
npm install http://localhost:3100/downloads/chlohal-coal-ui-0.5.0.tgzThis installs the local package. It is not yet published on npm. See installation for stylesheet setup and integration.
Usage
example.tsx
"use client";
import * as React from "react";
import "@chlohal/coal-ui/styles.css";
import { Badge } from "@chlohal/coal-ui";
export default function Example() {
return (<div>
<Badge>Published</Badge>
<Badge variant="secondary">Draft</Badge>
<Badge variant="success">Available</Badge>
<Badge variant="outline">v0.2</Badge>
<Badge variant="warning">In review</Badge>
</div>);
}
Source
The original Coal implementation. Use the package export to integrate it.
packages/react/src/badge.tsx
import * as React from "react";
import { type Intent, intentSymbols } from "./intent.js";
import { cn } from "./internal.js";
export type BadgeProps = React.ComponentPropsWithRef<"span"> & {
intent?: Intent;
variant?:
| "default"
| "secondary"
| "destructive"
| "outline"
| "success"
| "warning";
};
export function badgeVariants({
variant = "default",
className,
}: Pick<BadgeProps, "variant" | "className"> = {}) {
return cn("coal-badge", `coal-badge--${variant}`, className);
}
export function Badge({
className,
variant,
intent,
children,
...props
}: BadgeProps) {
const tone =
intent ??
(variant === "destructive"
? "danger"
: variant === "success" || variant === "warning"
? variant
: "neutral");
return (
<span
className={cn(
badgeVariants({ variant, className }),
`coal-intent-${tone}`,
)}
{...props}
>
<span className="coal-intent-symbol" aria-hidden="true">
{intentSymbols[tone]}
</span>
{children}
</span>
);
}
Accessibility checklist
- Provide an accessible name for controls, including icon-only buttons.
- Check keyboard navigation, visible focus and disabled states in your application.
- Do not rely on color alone to communicate status.