// // Copyright 2026 DXOS.org // import { Steps as StepsPrimitive, useStepsContext } from '@ark-ui/react/steps'; import React, { type CSSProperties, forwardRef, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { mx } from '@dxos/ui-theme'; import { type ThemedClassName } from '@dxos/ui-types'; import { translationKey } from '#translations'; import { recipes } from '../../recipes.ts'; import { type CSSVariables } from '../Container/Container.tsx'; /** One stage of a plan that has identity: a stage the caller can address and select. */ export type Step = { id: string; label?: string; }; /** Where a stage sits relative to the run: ahead of it, in flight, behind it, or where it failed. */ export type StepState = 'pending' | 'active' | 'complete' | 'error'; export type StepsProps = ThemedClassName< Omit > & { /** The plan: a count when the stages are anonymous, or the stages themselves; every stage is drawn. */ steps: number | Step[]; /** * Zero-based index of the stage in flight; absent means none has started, and a value at or past the last stage * means the run is over. */ active?: number; /** How far through the stage in flight, 0..1, drawn on the line leaving it. */ fraction?: number; /** The stage in flight cannot be counted, so it spins rather than filling its line. */ indeterminate?: boolean; /** The run stopped on the stage in flight; every stage it reached is drawn in the error colour. */ error?: boolean; /** Index of a stage the caller has singled out. */ selected?: number; /** Makes every stage a toggle button. */ onSelect?: (step: { index: number; id: string }) => void; /** Milliseconds a line takes to fill, and so how long an advance is held back for it to arrive. */ duration?: number; }; /** * The machine has no "not started" step (it throws on an index outside `0..count`), so the plan is declared one stage * longer and every stage shifted up by one: the machine resting on index 0 means nothing has started. */ const PHANTOM = 1; /** * A fixed plan drawn as icon-sized circles joined by lines that flex, so the gaps stay even however many stages there * are. Which stage is in flight says where the run is in its plan; the fill of the line leaving it says how far through * that stage, so a counted run needs no separate bar (`Progress` is the planless form). */ export const Steps = forwardRef( ( { classNames, steps, active, fraction = 0, indeterminate, error, selected, onSelect, duration = 500, style, ...props }, forwardedRef, ) => { const count = stepCount(steps); const { shown, handover } = useHandover(active, duration); const rootStyle: CSSProperties & CSSVariables = { ...style, '--dx-steps-duration': `${duration}ms` }; return ( ); }, ); Steps.displayName = 'Steps'; type StepsItemsProps = Pick & { count: number; fraction: number; handover: boolean; }; const StepsItems = ({ steps, count, fraction, handover, indeterminate, error, selected, onSelect, }: StepsItemsProps) => { const api = useStepsContext(); const { t } = useTranslation(translationKey); return ( <> {Array.from({ length: count }, (_unused, index) => { const step = stepAt(steps, index); const { current, completed } = api.getItemState({ index: index + PHANTOM }); const state = stepState(current, completed, handover, error); const last = index === count - 1; // A circle carries no text and an anonymous plan supplies no label, so its position is the only name it has. const label = step.label?.trim() || t('steps.step.label', { index: index + 1 }); const indicator = { 'index': index + PHANTOM, 'aria-label': label, // Ark hides its indicator from assistive tech; here the circle is the stage's only representation. 'aria-hidden': false, 'data-state': state, 'data-selected': selected === index ? '' : undefined, 'data-spinning': state === 'active' && indeterminate ? '' : undefined, } as const; return ( {onSelect ? ( // A real button so it takes focus and answers the keyboard; selection toggles, which `aria-pressed` // describes. Not the machine's trigger, which is a `tab` for a panel this component never renders.