import {
ComponentPropsWithoutRef,
CSSProperties,
ReactNode,
useEffect,
useRef,
useState,
} from 'react';
import './card-sequence.css';
export interface CardSequenceCard {
/**
* Content of the card. Any text in it shimmers while the card is working.
* Use `CardSequenceText` for a title and subtitle.
*/
content: ReactNode;
/**
* Icon shown in a tile at the start of the card.
*/
icon?: ReactNode;
}
export interface CardSequenceProps extends Omit<
ComponentPropsWithoutRef<'div'>,
'children'
> {
/**
* The cards, in order, 2 to 4. They appear one after the other with a line
* between them. Cards after the fourth are not shown.
*/
cards: CardSequenceCard[];
/**
* Additional class names
*/
className?: string;
/**
* Headline and screen-reader text once everything is done.
*/
completeLabel: string;
/**
* Set to `true` when the real work is done. The last card keeps shimmering
* until then. The animation always plays in full first (about 3 seconds per
* card after the first, plus 2), so if this is `true` early, the landing
* waits for it.
*/
isComplete?: boolean;
/**
* Called once, 2 seconds after the landing starts, so "all set" can be read.
* Redirect from here.
*/
onComplete?: () => void;
/**
* Headline and screen-reader text while the work is running. A trailing
* full stop is drawn in the brand color.
*/
workingLabel: string;
}
export interface CardSequenceTextProps extends Omit<
ComponentPropsWithoutRef<'span'>,
'title'
> {
/**
* Additional class names
*/
className?: string;
/**
* Smaller secondary text under the title.
*/
subtitle?: ReactNode;
/**
* Main text of the card.
*/
title: ReactNode;
}
const baseClass = 'pds-card-sequence';
// Each card takes this long: it appears, its text settles at 1.5 s, the line
// to the next card is drawn from 2.2 s and the next card appears at 3 s. The
// CSS carries the same numbers.
const STEP_MS = 3000;
// The last card appears and shimmers for this long before the landing can
// come, so two cards give a minimum of 5 seconds.
const TAIL_MS = 2000;
const MAX_CARDS = 4;
// How long "all set" stays on screen after the landing starts, before
// `onComplete` is called.
const LANDING_MS = 2000;
// 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>) =>
classes.filter(Boolean).join(' ');
/**
* A title and subtitle for the content of a card.
*/
export const CardSequenceText = ({
className = '',
subtitle,
title,
...props
}: CardSequenceTextProps) => (
<span className={mergeClasses([`${baseClass}__text`, className])} {...props}>
<span className={`${baseClass}__title`}>{title}</span>
{subtitle && (
<span className={`${baseClass}__subtitle pds-copy-xs`}>{subtitle}</span>
)}
</span>
);
/**
* CardSequence UI component. Cards appear one after the other with a line
* between them while a piece of work runs, their text shimmers, and it lands
* on "all set" when the work is done.
*/
export const CardSequence = ({
cards,
className = '',
completeLabel,
isComplete = false,
onComplete,
workingLabel,
...props
}: CardSequenceProps) => {
const shownCards = cards.slice(0, MAX_CARDS);
const minimumPlayMs = (shownCards.length - 1) * STEP_MS + TAIL_MS;
// The full animation has played. With reduced motion there is no
// animation to wait for.
const [hasPlayed, setHasPlayed] = useState(false);
useEffect(() => {
const prefersReducedMotion =
typeof window.matchMedia === 'function' &&
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (prefersReducedMotion) {
setHasPlayed(true);
return undefined;
}
const timer = setTimeout(() => setHasPlayed(true), minimumPlayMs);
return () => clearTimeout(timer);
}, [minimumPlayMs]);
const isLanded = isComplete && hasPlayed;
// Keep the latest callback without restarting anything when it changes.
const onCompleteRef = useRef(onComplete);
useEffect(() => {
onCompleteRef.current = onComplete;
}, [onComplete]);
useEffect(() => {
if (!isLanded) {
return undefined;
}
const timer = setTimeout(() => onCompleteRef.current?.(), LANDING_MS);
return () => clearTimeout(timer);
}, [isLanded]);
// Draw a trailing full stop in the brand color.
const hasStop = workingLabel.endsWith('.');
const workingText = hasStop ? workingLabel.slice(0, -1) : workingLabel;
const landedClass = isLanded ? `${baseClass}--landed` : null;
return (
<div
className={mergeClasses([baseClass, landedClass, className])}
{...props}
>
{/* One polite live region: the working text, then the complete text. */}
<p className='visually-hidden' role='status'>
{isLanded ? completeLabel : workingLabel}
</p>
<div aria-hidden='true' className={`${baseClass}__headline`}>
<span className={`${baseClass}__working`}>
{workingText}
{hasStop && <span className={`${baseClass}__stop`}>.</span>}
</span>
<span className={`${baseClass}__complete`}>{completeLabel}</span>
</div>
{shownCards.map((card, index) => {
const isLast = index === shownCards.length - 1;
const cardClass = isLast ? `${baseClass}__card--last` : null;
return (
<div className={`${baseClass}__step`} key={index}>
{index > 0 && (
<svg
aria-hidden='true'
className={`${baseClass}__link`}
focusable='false'
style={
{
'--card-start': `${(index - 1) * STEP_MS}ms`,
} as CSSProperties
}
viewBox='0 0 12 48'
>
<path
className={`${baseClass}__line`}
d='M6 4V44'
pathLength='1'
/>
<circle
className={`${baseClass}__dot ${baseClass}__dot--start`}
cx='6'
cy='4'
r='3'
/>
<circle
className={`${baseClass}__dot ${baseClass}__dot--end`}
cx='6'
cy='44'
r='3'
/>
</svg>
)}
<div
className={mergeClasses([`${baseClass}__card`, cardClass])}
style={
{ '--card-start': `${index * STEP_MS}ms` } as CSSProperties
}
>
{card.icon && (
<span aria-hidden='true' className={`${baseClass}__tile`}>
{card.icon}
</span>
)}
<div className={`${baseClass}__content`}>{card.content}</div>
</div>
</div>
);
})}
</div>
);
};
Card Sequence
A new component, CardSequence, in the loading indicators family.
Net-new componentOpen in Storybook