← All proposals

LabeledDivider

Net-new LabeledDivider: a horizontal rule with a pill-shaped label centered on it.

Net-new componentOpen in Storybook

proposals/LabeledDivider/LabeledDivider.tsx118 lines · View on GitHub
import { ComponentPropsWithoutRef, ReactNode } from 'react';

import { Icon, IconProps } from '../Icon/Icon';

import './labeled-divider.css';

export type LabeledDividerSize = 'xs' | 's';

export type LabeledDividerTone = 'neutral' | 'agentic';

export interface LabeledDividerProps extends ComponentPropsWithoutRef<'div'> {
	/**
	 * Swaps the label for any control, centered on the line (for example a
	 * "Show more" Button that unfolds more content under the divider, or a
	 * `MenuButton` that jumps to another point in time). The screen owns the
	 * control and what it does, including `aria-expanded` and the content it
	 * reveals. When set, `label`, `prefixIcon` and `suffixIcon` are ignored,
	 * and the divider is a plain group, not a separator. The `tone` still
	 * colors the lines either side.
	 */
	action?: ReactNode;
	/**
	 * Additional class names
	 */
	className?: string;
	/**
	 * Text on the divider (for example "Yesterday" or "Zappy engaged").
	 * Announced to screen readers. Set this or `action`: with neither, the
	 * divider has no name and a warning is logged in development.
	 */
	label?: string;
	/**
	 * Optional icon shown before the label.
	 */
	prefixIcon?: IconProps['iconName'];
	/**
	 * Size. Sets the text size, the icon size and the padding of the label.
	 */
	size?: LabeledDividerSize;
	/**
	 * Optional icon shown after the label.
	 */
	suffixIcon?: IconProps['iconName'];
	/**
	 * Visual tone. `agentic` gives the label the AI gradient border, the lines
	 * the gradient and the shimmer.
	 */
	tone?: LabeledDividerTone;
}

// Stand-in for the v2 `mergeClasses` util, which the published package does
// not export. The port uses the real one.
const mergeClasses = (classes: Array<string | null | undefined>) =>
	classes.filter(Boolean).join(' ');

// Warn once, not on every render.
let hasWarnedNoName = false;

/**
 * LabeledDivider UI component. A horizontal rule with a pill label centered on
 * it, to mark a point in a stream: a date, a jump in time or a state change.
 */
export const LabeledDivider = ({
	action,
	className,
	label,
	prefixIcon,
	size = 's',
	suffixIcon,
	tone = 'neutral',
	...props
}: LabeledDividerProps) => {
	const baseClass = 'pds-labeled-divider';

	if (!label && !action && process.env.NODE_ENV !== 'production') {
		if (!hasWarnedNoName) {
			hasWarnedNoName = true;
			console.warn(
				'[LabeledDivider] Set either the `label` or the `action` prop. Without one the divider has no accessible name.',
			);
		}
	}

	const renderIcon = (iconName: IconProps['iconName']) => (
		<Icon className={`${baseClass}__icon`} iconName={iconName} size={size} />
	);

	const classes = mergeClasses([
		baseClass,
		`pds-copy-${size}`,
		size !== 's' ? `${baseClass}--${size}` : null,
		tone !== 'neutral' ? `${baseClass}--${tone}` : null,
		className,
	]);

	if (action) {
		return (
			<div className={classes} role='group' {...props}>
				<span aria-hidden='true' className={`${baseClass}__line`} />
				<div className={`${baseClass}__action`}>{action}</div>
				<span aria-hidden='true' className={`${baseClass}__line`} />
			</div>
		);
	}

	return (
		<div aria-label={label} className={classes} role='separator' {...props}>
			<span aria-hidden='true' className={`${baseClass}__line`} />
			<span aria-hidden='true' className={`${baseClass}__label`}>
				{prefixIcon && renderIcon(prefixIcon)}
				<span>{label}</span>
				{suffixIcon && renderIcon(suffixIcon)}
			</span>
			<span aria-hidden='true' className={`${baseClass}__line`} />
		</div>
	);
};