// ~/src/siteplane/components/badge-group/badge-group.tsx
"use client";
import { ArrowRightIcon } from "@phosphor-icons/react/dist/ssr";
import * as React from "react";
import { Badge, BadgeDot, type BadgeProps, type BadgeVariant } from "../badge";
import "./badge-group.css";
/**
* The tint of the group, by the Badge variant that draws it: `secondary` is
* the neutral one (gray), `info` the accent tint (the brand),
* `success`, `warning` and `error` the status tints, and `slate`, `sky`,
* `blue`, `indigo`, `purple`, `pink` and `orange` the colour tints. In the
* modern appearance it names the colour of the dot.
*/
export type BadgeGroupVariant = Extract<
BadgeVariant,
| "secondary"
| "info"
| "success"
| "warning"
| "error"
| "slate"
| "sky"
| "blue"
| "indigo"
| "purple"
| "pink"
| "orange"
>;
/**
* `light` tints the whole pill and sets the addon on it as a white pill of its
* own. `modern` is a white, hairline group whose addon is a white badge with a
* dot in the variant's colour.
*/
export type BadgeGroupAppearance = "light" | "modern";
/**
* `md` is a 30 px group with 12 px type around a 22 px addon, `lg` a 32 px
* group with 14 px type around a 24 px addon. The addon
* is the Badge at `sm` and `md`.
*/
export type BadgeGroupSize = "md" | "lg";
/** Where the addon stands: before the message, or after it. */
export type BadgeGroupAddonPosition = "start" | "end";
export interface BadgeGroupProps extends Omit<
BadgeProps,
| "light"
| "onRemove"
| "profile"
| "removeLabel"
| "shape"
| "size"
| "variant"
> {
/**
* The small badge that joins the message, such as "New" or "Error". It is a
* Badge of its own, so text and an icon lay out as in any Badge.
*/
addon: React.ReactNode;
/**
* `start`, the default, puts the addon before the message and the icon after
* it; `end` puts the message first and the addon, with the icon inside it, at
* the end of the group.
*/
addonPosition?: BadgeGroupAddonPosition | undefined;
/** `light`, the default, or `modern`. */
appearance?: BadgeGroupAppearance | undefined;
/**
* Makes the group a link to this address: it shows the pointer, a hover and
* a pressed tint in its own colour and the keyboard focus ring, in every
* appearance and colour. For a router's link component or a button, pass
* `render` instead. Without either the group is static and has no hover.
*/
href?: string | undefined;
/**
* The message is the children, one line that ends with an ellipsis where the
* group is full.
*/
children?: React.ReactNode;
/**
* The icon after the message, or inside the addon when it stands at the end:
* an arrow by default. Pass `null` for none. Decoration, so give it
* `aria-hidden`.
*/
icon?: React.ReactNode;
/** `md`, the default, or `lg`. */
size?: BadgeGroupSize | undefined;
/** The tint, `info` by default. */
variant?: BadgeGroupVariant | undefined;
}
/** Whether a node has anything to show, so its gap and its margin stay out. */
function hasContent(node: React.ReactNode): boolean {
return !(
node === undefined ||
node === null ||
node === false ||
node === ""
);
}
/**
* One pill that joins a small badge, the addon, and a message: a release note,
* a warning or an announcement above a heading, with an arrow that says it
* goes somewhere. It is a Badge, so the tint, the pill geometry and the
* behaviour of a link or a button (`href`, or `render`: the pointer, the
* hover and pressed tints and the focus ring) are the Badge's; the addon is a
* Badge inside it. The group is static unless `href` or `render` makes it a
* link or a button.
*/
export function BadgeGroup({
addon,
addonPosition = "start",
appearance = "light",
children,
className,
href,
icon,
render,
size = "md",
variant = "info",
...props
}: BadgeGroupProps): React.ReactElement {
const modern = appearance === "modern";
const end = addonPosition === "end";
const iconNode =
icon === undefined ? : icon;
const hasMessage = hasContent(children);
const hasIcon = hasContent(iconNode);
// The dot of the modern appearance leads the addon, or the group when the
// addon stands at the end.
const dot = modern ? : null;
const addonBadge = (
{end ? null : dot}
{addon}
{end ? iconNode : null}
);
return (
)}
shape={modern ? "default" : "pill"}
size={size}
variant={modern ? "default" : variant}
{...props}
>
{/* Three children, not a fragment: the Badge makes the text among its
own children the label that ends with an ellipsis, and does not look
inside a fragment. */}
{end ? dot : addonBadge}
{children}
{end ? addonBadge : iconNode}
);
}
// ~/src/siteplane/components/badge-group/badge-group.css
@layer theme, base, components, siteplane, utilities;
@layer siteplane {
/* A badge group is a Badge (the pill block, the tints, the link behaviour in
* design-rework.css) around a Badge, the addon, and a message:
* a 4 px frame around the addon, 8 px between the addon and the
* message and an icon 4 px after the message. Only the sizes the pill block
* does not have are named here: the group is as high as its addon and 4 px
* above and below it (30 px at md around a 22 px addon, 32 px at lg around a
* 24 px one), is as wide as its content and no wider than its space (a flex
* column does not stretch it, and the Badge's max-width stays), and sets its
* own type. Every rule names the Badge foundation
* and the group, so it stands above the pill block whichever stylesheet
* comes later. */
:root .sp-base-badge.sp-badge-group {
--sp-badge-group-lead: var(--sp-rw-space-1);
--sp-badge-group-trail: var(--sp-rw-space-2);
--sp-badge-group-message-gap: var(--sp-rw-space-2);
--sp-badge-group-icon-gap: var(--sp-rw-space-1);
--sp-badge-group-dot-gap: var(--sp-rw-space-1-5);
width: max-content;
height: auto;
min-width: 0;
gap: 0;
padding-block: calc(var(--sp-rw-space-1) - var(--sp-rw-border-default));
padding-inline-start: calc(
var(--sp-badge-group-lead) - var(--sp-rw-border-default)
);
padding-inline-end: calc(
var(--sp-badge-group-trail) - var(--sp-rw-border-default)
);
}
:root .sp-base-badge.sp-badge-group[data-size="md"] {
--sp-pill-font: var(--sp-rw-font-12);
--sp-pill-line: var(--sp-rw-line-18);
}
:root .sp-base-badge.sp-badge-group[data-size="lg"] {
--sp-pill-font: var(--sp-rw-font-14);
--sp-pill-line: var(--sp-rw-line-20);
--sp-badge-group-dot-gap: var(--sp-rw-space-2);
}
/* The addon at the start has the frame at its start and the end of the group
* 8 px after the message or the icon; with neither, 4 px, as at its start.
* At the end the message starts 12 px in, or 10 px where the modern dot
* leads it, and the addon has the 4 px frame at its end. */
:root
.sp-base-badge.sp-badge-group[data-addon-position="start"]:not(
[data-message],
[data-icon]
) {
--sp-badge-group-trail: var(--sp-rw-space-1);
}
:root .sp-base-badge.sp-badge-group[data-addon-position="end"] {
--sp-badge-group-lead: var(--sp-rw-space-3);
--sp-badge-group-trail: var(--sp-rw-space-1);
}
:root
.sp-base-badge.sp-badge-group[data-addon-position="end"][data-appearance="modern"][data-size="md"] {
--sp-badge-group-lead: var(--sp-rw-space-2-5);
}
/* The modern group is a white rounded rectangle, one provider radius, which
* its addon's own corner (6 px) is concentric with; a theme that rounds
* every badge (--sp-rw-radius-badge) rounds it too. Its text is the
* secondary colour and its icon the tertiary one. */
:root .sp-base-badge.sp-badge-group[data-appearance="modern"] {
--sp-badge-radius: var(--radius);
color: var(--sp-rw-fg-secondary);
}
/* The addon takes the group's text colour, which is the tint's on the light
* group, and stands 8 px from the message it joins. */
:root .sp-base-badge.sp-badge-group > .sp-badge-group-addon {
color: inherit;
}
:root
.sp-base-badge.sp-badge-group[data-message][data-addon-position="start"]
> .sp-badge-group-addon {
margin-inline-end: var(--sp-badge-group-message-gap);
}
:root
.sp-base-badge.sp-badge-group[data-message][data-addon-position="end"]
> .sp-badge-group-addon {
margin-inline-start: var(--sp-badge-group-message-gap);
}
/* The icon after the message is 16 px and 4 px from it. At the end it is the
* addon's, a 12 px icon like any Badge icon. */
:root .sp-base-badge.sp-badge-group > svg:not([class*="size-"]) {
width: var(--sp-rw-space-4);
height: var(--sp-rw-space-4);
margin-inline-start: var(--sp-badge-group-icon-gap);
}
:root .sp-base-badge.sp-badge-group[data-appearance="modern"] > svg {
color: var(--sp-rw-fg-tertiary);
}
/* The dot that leads a group with its addon at the end: 8 px, 6 px or 8 px
* before the message, with no margin of its own at its start. */
:root .sp-base-badge.sp-badge-group > .sp-badge-dot {
margin: 0;
margin-inline-end: var(--sp-badge-group-dot-gap);
}
}
// ~/src/siteplane/components/badge-group/index.ts
export * from "./badge-group";