Stackflow

A stack-based navigation flow component for managing sequential views with animations

Installation

Terminal
pnpm add @choice-ui/stackflow

Import

Component.tsx
import { Stackflow, type StackflowProps, useStackflowContext } from "@choice-ui/stackflow"

Basic

Stackflow is a **non-linear stack navigation** component for managing multi-page views.
### Core Concepts
- **Stackflow**: Container component, manages navigation state - **Stackflow.Item**: Page component, each `id` corresponds to one page - **Stackflow.Prefix**: Fixed header area, does not scroll with pages - **Stackflow.Suffix**: Fixed footer area, does not scroll with pages - **useStackflowContext**: Hook to access navigation methods and state
### Navigation Methods
- `push(id)`: Navigate to a specified page and record in history - `back()`: Return to the previous page - `canGoBack`: Whether there is a previous page to return to - `current`: Current page information

Non Linear Navigation

Unlike traditional linear step navigation, Stackflow supports **jumping to any page** at any time.
### Additional Features
- `history`: Complete navigation history array - `clearHistory()`: Clear all history records
### Usage Scenario
- Wizard flows that allow jumping between steps - Settings pages with multiple sub-pages - Any scenario requiring page stack management
### Key Difference from Linear Navigation
- Linear: Page 1 → Page 2 → Page 3 (can only go forward/backward in order) - Non-linear: Can jump from any page to any other page, history still tracks all visited pages
History:
home
💡 Stackflow.Prefix and Stackflow.Suffix are always fixed and do not scroll with page content

Use Stackflow Context

`useStackflowContext` is a hook that provides access to the navigation state and methods.
### Return Values
| Property | Type | Description | |----------|------|-------------| | `push` | `(id: string) => void` | Navigate to a page by id | | `back` | `() => void` | Go back to the previous page | | `clearHistory` | `() => void` | Clear history and return to the first page | | `canGoBack` | `boolean` | Whether there's a previous page | | `current` | `{ id, content } \| undefined` | Current page info | | `history` | `string[]` | Array of visited page ids | | `direction` | `"forward" \| "backward"` | Current navigation direction | | `isInitial` | `boolean` | Whether it's the initial render |
### Usage
```tsx const { push, back, canGoBack, current, history, direction, isInitial, clearHistory } = useStackflowContext() ```
current?.id:
undefined
canGoBack:
false
direction:
forward
isInitial:
true
history: ["step1"]