GithubHelp home page GithubHelp logo

bamlab / flow-navigator Goto Github PK

View Code? Open in Web Editor NEW
36.0 36.0 1.0 4.41 MB

A flow navigator for react-navigation

License: MIT License

TypeScript 61.10% JavaScript 2.63% Ruby 6.56% Java 20.83% Objective-C 6.70% Objective-C++ 2.18%
flows react-native react-navigation

flow-navigator's Introduction

Alt text

Flow Navigator for React Navigation

Simplifying Flow Navigation in React Native

Flow Navigator provides a simplified API for managing navigation flows in your React Native applications with React Navigation. It abstracts the complexity of flow management, allowing individual screens to navigate through the flow using simple methods like goToNextStep and goToPreviousStep, without the need to understand the entire navigation stack, or knowing which page exactly is the next one.

Features

  • Simplified Flow Management: Get a comprehensive overview of your flow in a single location. This allows new developers to quickly understand the entire flow without the need to examine each page individually.
  • Separation of responsibility: Screens are not aware of their specific position in the flow. They don't need to know which page is next; they simply navigate to the next page in the flow.
  • Declarative Screen Ordering: Define the order of screens in your navigation flow declaratively, ensuring a clear and maintainable navigation structure.

Installation

The flow navigator is built on top of react-navigation's native stack navigator. So it requires both react-navigation and the native stack navigator installed.

To install the flow navigator, run:

yarn add @bam.tech/flow-navigator
# or
npm install @bam.tech/flow-navigator

Usage

Basic usage

import { createFlowNavigator } from '@bam.tech/flow-navigator';

const FlowNavigator = createFlowNavigator();

export const FlowNavigatorExample = () => {
   // Define your screens and their order in the flow
  return (
    <FlowNavigator.Navigator>
      <FlowNavigator.Screen name="Step1" component={Step1Page} />
      <FlowNavigator.Screen name="Step2" component={Step2Page} />
      <FlowNavigator.Screen name="Step3" component={Step2Page} />
    </FlowNavigator.Navigator>
  );
};

In each screen component, you can navigate through the flow using:

import { useFlow } from '@bam.tech/flow-navigator';

const Step1Page = () => {
  const { goToNextStep, goToPreviousStep, currentStep } = useFlow();

  return (
    <Button title="Go to next page" onPress={() => goToNextStep()} />
  )
};

You can find a fully working example in the example folder.

Alt text

Define conditional steps

In certain scenarios, a flow may include steps that are conditional. These steps might be dependent on user-specific conditions or based on whether certain actions have already been completed. You can manage such conditional steps declaratively in your navigation flow.

Here's an example where "Step 2" is conditionally displayed based on the hasToPassStep2 variable. This variable could be a piece of data fetched from the backend or a state within your application. In our case, we use jotai to store our user data locally.

import { createFlowNavigator } from '@bam.tech/flow-navigator';

export const flagAtom = atom(false);

const FlowNavigator = createFlowNavigator();

export const FlowNavigatorExample = () => {
  const [flag] = useAtom(flagAtom);

  return (
    <FlowNavigator.Navigator>
      <FlowNavigator.Screen name="Step1" component={Step1Page} />
      {flag && <FlowNavigator.Screen name="Step2" component={Step2Page} />}
      <FlowNavigator.Screen name="Step3" component={Step3Page} />
    </FlowNavigator.Navigator>
  );
};

In this example, the Step2 screen is only included in the flow if hasToPassStep2 evaluates to true.

You can enable or disable routes at anytime in your flow by setting your boolean state: setFlag(false)

import { useFlow } from '@bam.tech/flow-navigator';

export const Step1Page = () => {
  const {goBack, goToNextStep} = useFlow();
  const [flag] = useAtom(flagAtom);

  const onNextPress = async () => {
    setFlag(false);
    goToNextStep();
  };

  const onBackPress = () => {
    goBack();
  };

  return (
    <View style={styles.container}>
      <Text style={styles.pageTitle}>Current page: 1</Text>
      <FlowInfos />
      <Button title="next" onPress={onNextPress} />
      <Button title="back" onPress={onBackPress} />
    </View>
  );
};

You can check out a fully working example in the example folder

Define steps with several screens

In some scenarios, a single step in a flow may encompass several screens. To group these screens within one step, you have a couple of options: using Groups or Nested navigators. Examples of both approaches can be found in the example folder. We recommend using groups if they suit your use-case. However, one limitation to note is that the currentStep will reflect the name of the screen that is currently focused, not the group name. So all the screens in the step won't have the same currentStep value. With nested navigator, currentStep is the name of the subnavigator, which provides a more cohesive representation of the step. Note that all the pages of a subnavigator will correspond to one increment of the progress index, that will stay constant throughout the whole step.

Use cases

Flows are sequences of pages with a pre-defined order, guiding users through a specific process within your app. Whether complex or straightforward, flows are a fundamental part of the user experience in many applications. Here are some common examples where Flow Navigator can be particularly useful:

  • Onboarding flow
  • Post publication flow
  • Subscription flow
  • Shopping cart checkout process
  • Survey of feedback flow
  • Profile setup flow

API definition

FlowNavigator

The Flow Navigator is built upon the foundation of the native stack, it inherits the same API.

useFlow

Inside a screen defined below a Flow Navigator, you can use the useFlow useFlow provides the following helpers:

  • goToNextStep: To navigate to the next step in the flow, based on the order of the screens in the navigation flow.
  • goToPreviousStep: To navigate to the previous step in the flow, based on the order of the screens in the navigation flow.
  • quitFlow: To exit the flow.

useFlow also provides information about the current step of the flow. It contains the following properties:

  • currentStep: A string representing the identifier of the current step in the flow. Based on the name of the screen.
  • progress: A number indicating the progress through the flow. It is calculated as the ratio of the current index to the total number of routes.
  • canGoToPreviousStep: A boolean indicating whether navigation to a previous step is possible.
  • canGoToNextStep: A boolean indicating whether navigation to the next step is possible.

Contributing

Pull requests and feature suggestions are more than welcome!

Testing your changes

You can try out your changes in the example folder. Use yarn link to try out your local library version:

  1. At the root directory of @bam.tech/flow-navigator, run
  cd packages/lib
  yarn link
  1. Link in the example project:
  cd packages/example
  yarn link "@bam.tech/flow-navigator"
  1. Make your changes in the lib, and transpile the code from TS to JS, to be able to try out the changes in the example. You can run this command at the root of the repo
  yarn transpile:lib
  1. Unlink when done, once the changes are published:
  cd packages/example
  yarn unlink "@bam.tech/flow-navigator"
  yarn install

Publishing the package

  1. Increment the package.json in ./packages/lib/package.json
  2. Commit the change git commit -m "chore: bump version"
  3. Add a tag matching the version
   git tag vx.x.x && git push --tags
  1. Then publish the package: run this command at the root of the repo
  yarn publish:lib

flow-navigator's People

Contributors

charlotteisambert avatar ellouzeskandercs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.