Skip to content

FlowDefinition

A flow definition describes a complete tour as a finite state machine.

Type

ts
interface FlowDefinition<TContext = Record<string, unknown>> {
  id: string
  initial: string
  context?: TContext
  states: Record<string, FlowState<TContext>>
}

FlowState

ts
interface FlowState<TContext> {
  steps: Step[]
  on?: Record<string, string | TransitionConfig<TContext>>
  onEntry?: (ctx: TContext) => void
  onExit?: (ctx: TContext) => void
  final?: boolean
}

TransitionConfig

ts
interface TransitionConfig<TContext> {
  target: string
  guard?: (ctx: TContext) => boolean
}

Fields

FieldTypeRequiredDescription
idstringYesUnique flow identifier
initialstringYesName of the starting state
contextTContextNoInitial context data
statesRecord<string, FlowState>YesMap of state names to state definitions

FlowState Fields

FieldTypeRequiredDescription
stepsStep[]YesSteps shown in this state
onRecord<string, string | TransitionConfig>NoEvent → target state transitions
onEntry(ctx) => voidNoCalled when entering this state
onExit(ctx) => voidNoCalled when leaving this state
finalbooleanNoIf true, the tour ends after this state's steps

Example

ts
const flow: FlowDefinition<{ role: string }> = {
  id: 'onboarding',
  initial: 'welcome',
  context: { role: 'user' },
  states: {
    welcome: {
      steps: [{ id: 'hi', content: { title: 'Hello!' }, target: '#app' }],
      on: {
        NEXT: {
          target: 'admin',
          guard: (ctx) => ctx.role === 'admin',
        },
      },
    },
    admin: {
      steps: [{ id: 'admin', content: { title: 'Admin panel' }, target: '#admin' }],
      final: true,
    },
  },
}

Released under the MIT License.