Chip
A small pill for options, filters and parameters, with a brand ring when selected.
Install
$ npx shadcn@latest add @nudgekit/chipOr with no config: npx shadcn@latest add https://ui.nudgekit.app/r/chip.json
Usage
usage.tsx
import { Chip } from "@/components/ui/chip"
<Chip dot selected onClick={() => setRegion("bom")}>Mumbai</Chip>
<Chip dot onClick={() => setRegion("fra")}>Frankfurt</Chip>
<Chip mono>format=webp</Chip>Do
- Use for filters, regions and parameters.
- Use mono for API parameters.
- Mark the chosen one with selected.
Don't
- Use a chip as a button for the main action.
- Fill a chip with a colour.
Code
What the CLI writes into your project. It is yours to change.
components/ui/chip.tsx
import * as React from "react";
import { cn } from "@/lib/utils";
export interface ChipProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
/** Marks the chip as chosen: a 1.5px brand ring and a filled dot. */
selected?: boolean;
/** Shows a leading dot. */
dot?: boolean;
/** Uses the mono font, for parameters such as format=webp. */
mono?: boolean;
}
/** A small pill for options, filters and parameters. Selectable when given onClick. */
function Chip({ selected = false, dot = false, mono = false, className, children, ...props }: ChipProps) {
return (
<button
type="button"
data-slot="chip"
aria-pressed={props.onClick ? selected : undefined}
className={cn(
"inline-flex h-8 items-center gap-2 rounded-full border border-border bg-background px-3 text-[13px] text-foreground transition-colors duration-150",
"hover:border-line-strong focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring",
mono && "font-mono text-xs",
selected && "border-transparent ring-[1.5px] ring-brand",
!props.onClick && "cursor-default",
className,
)}
{...props}
>
{dot && <span aria-hidden className={cn("size-1.5 rounded-full", selected ? "bg-brand" : "bg-line-strong")} />}
{children}
</button>
);
}
export { Chip };