react-native-onboarding

repository·main·Indexed 19 days ago

https://github.com/software-mansion-labs/react-native-onboarding

A library for creating customizable onboarding and tutorial flows in React Native with smooth animations powered by Reanimated. It supports iOS, Android, and Web, featuring a 'spill' animation effect, customizable themes for colors and fonts, and support for both default and custom step components.

Tokens
6.2K
Snippets
18
Records
25
Agent score
65%

What's inside @blazejkustra/react-native-onboarding

  1. How to use custom components in Onboarding

    main

    For complete control over the UI, you can replace default elements with custom components.

    • introPanel: Pass a function that receives onPressStart.
    • steps: Pass a component function that receives onNext, onBack, and isLast.
    • background: Pass a function that returns a ReactNode to render behind the content.
    • skipButton: Pass a function that receives onPress to customize the close/skip button.
    <Onboarding
      introPanel={({ onPressStart }) => (
        <CustomWelcomeScreen onStart={onPressStart} />
      )}
      background={() => (
        <Image 
          source={require('./assets/background.png')} 
          style={StyleSheet.absoluteFillObject} 
        />
      )}
      skipButton={({ onPress }) => (
        <TouchableOpacity onPress={onPress}>
          <Text>✕</Text>
        </TouchableOpacity>
      )}
      steps={[
        {
          component: ({ onNext, onBack, isLast }) => (
            <CustomStepComponent 
              onNext={onNext} 
              onBack={onBack} 
              isLast={isLast} 
            />
          ),
          image: require('./assets/step1.png'),
          position: 'top',
        },
      ]}
    />
  2. Install @blazejkustra/react-native-onboarding

    main

    To use this library, install the core package along with its required animation and safe area dependencies.

    Required dependencies:

    • react-native-reanimated
    • react-native-safe-area-context

    Optional dependencies for image support:

    • expo-image OR react-native-svg
    npm install @blazejkustra/react-native-onboarding
    
    npm install react-native-reanimated react-native-safe-area-context
  3. Configure Onboarding colors and fonts

    main

    You can customize the visual identity of the onboarding flow using the colors and fonts props.

    // Custom Colors
    colors={{
      background: {
        primary: '#FFFFFF',
        secondary: '#F8F9FA',
        label: '#E9ECEF',
        accent: '#007AFF'
      },
      text: {
        primary: '#1C1C1E',
        secondary: '#8E8E93',
        contrast: '#FFFFFF'
      }
    }}
    
    // Custom Fonts
    // Option 1: Single font for all text
    fonts="Inter"
    
    // Option 2: Detailed configuration
    fonts={{
      introTitle: 'Inter-Bold',
      introSubtitle: 'Inter-Medium',
      stepTitle: 'Inter-SemiBold',
      stepDescription: 'Inter-Regular',
      primaryButton: 'Inter-Medium'
    }}
  4. Define onboarding steps using OnboardingStep

    main

    An OnboardingStep can be defined in two ways:

    1. Default Step: A text-based step where you provide title, description, buttonLabel, image, and position. You must not provide a component property.
    2. Custom Step: A fully custom renderer where you provide a component function. This function receives onNext, onBack, and isLast props to allow manual control over the step lifecycle. Custom steps also require an image and position.
    // Default Step
    const defaultStep: OnboardingStep = {
      title: 'Default Step',
      description: 'This is a standard step',
      buttonLabel: 'Continue',
      image: require('./step1.png'),
      position: 'top',
    };
    
    // Custom Step
    const customStep: OnboardingStep = {
      image: require('./step2.png'),
      position: 'bottom',
      component: ({ onNext, onBack, isLast }) => (
        <View>
          <Text>Custom Content</Text>
          <Button title={isLast ? 'Finish' : 'Next'} onPress={onNext} />
        </View>
      ),
    };
  5. Basic usage of the Onboarding component

    main

    You can implement a standard onboarding flow by passing an introPanel object and an array of steps to the Onboarding component. This uses the library's default styles and animations.

    import Onboarding from '@blazejkustra/react-native-onboarding';
    
    function MyOnboarding() {
      return (
        <Onboarding
          introPanel={{
            title: 'Welcome to My App',
            subtitle: 'Let\'s get you started',
            button: 'Get Started',
            image: require('./assets/logo.png'),
          }}
          steps={[
            {
              title: 'Step 1',
              description: 'This is the first step of your journey',
              buttonLabel: 'Next',
              image: require('./assets/step1.png'),
              position: 'top',
            },
            {
              title: 'Step 2', 
              description: 'Learn about our amazing features',
              buttonLabel: 'Continue',
              image: require('./assets/step2.png'),
              position: 'bottom',
            },
          ]}
          onComplete={() => {
            // Handle completion (e.g., save to AsyncStorage)
            console.log('Onboarding completed!')
          }}
          onSkip={() => console.log('Onboarding skipped')}
          onStepChange={(step) => console.log('Current step:', step)}
        />
      );
    }
  6. Customize the onboarding theme

    main

    The react-native-onboarding library uses a theme object to define colors and fonts. You can provide a custom theme to the onboarding component to match your application's design. The defaultTheme provides a baseline for background colors (bg), text colors (text), and font families (fonts).

    When providing a custom theme, ensure you satisfy the ThemeColors structure. Note that the internal Theme type extends ThemeColors by adding insets (of type EdgeInsets from react-native-safe-area-context), which are handled internally by the component.

    import { defaultTheme } from '@blazejkustra/react-native-onboarding';
    
    const myCustomTheme = {
      ...defaultTheme,
      bg: {
        ...defaultTheme.bg,
        primary: '#FF5733', // Custom primary background color
      },
      text: {
        ...defaultTheme.text,
        primary: '#000000',
      },
    };
  7. OnboardingProps reference

    main

    The Onboarding component accepts the following props to control the flow, appearance, and behavior of the tutorial.

    interface OnboardingProps {
      /** The welcome screen. Can be an object {title, subtitle, button, image} or a custom component receiving onPressStart */
      introPanel: OnboardingIntroPanel;
    
      /** Array of steps. Each can be a default object or a custom component receiving {onNext, onBack, isLast} */
      steps: OnboardingStep[];
    
      /** Callback fired when user completes the final step. Required. */
      onComplete: () => void;
    
      /** Callback fired when user skips onboarding. Optional. */
      onSkip?: () => void;
    
      /** Callback fired when the active step changes. Receives the current step index. */
      onStepChange?: (stepIndex: number) => void;
    
      /** Whether to show the close button in the header. Default: true */
      showCloseButton?: boolean;
    
      /** Whether to show back button on steps (except first step). Default: true */
      showBackButton?: boolean;
    
      /** Whether to wrap the onboarding in a modal on web. Default: true */
      wrapInModalOnWeb?: boolean;
    
      /** Animation duration in milliseconds for step transitions. Default: 500 */
      animationDuration?: number;
    
      /** Custom color configuration for background and text */
      colors?: OnboardingColors;
    
      /** Custom font configuration. Can be a string (applies to all) or an object specifying fonts for different elements. Default: 'System' */
      fonts?: OnboardingFonts | string;
    
      /** Custom background element rendered behind content */
      background?: () => ReactNode;
    
      /** Custom close button renderer receiving onPress */
      skipButton?: ({ onPress: () => void }) => ReactNode;
    }
  8. Create a custom Intro Panel

    main

    The introPanel prop can accept a function instead of a configuration object. This allows you to implement a completely custom entry screen. The function receives an object with an onPressStart method, which you should call to transition from the intro panel to the first onboarding step.

    <SpillOnboarding
      introPanel={({ onPressStart }) => (
        <View>
          <Text>Welcome to the App!</Text>
          <Button title="Get Started" onPress={onPressStart} />
        </View>
      )}
      steps={steps}
    />
  9. Configure the theme with ThemeProvider

    main

    Wrap your application (or the onboarding component tree) with ThemeProvider to provide custom colors and fonts to the onboarding experience. The provider automatically integrates react-native-safe-area-context insets into the theme.

    You can pass colors (of type OnboardingColors) and fonts (either a single string applied to all font roles or an OnboardingFonts object for granular control) as props.

    import ThemeProvider from './path-to/ThemeProvider';
    
    function App() {
      return (
        <ThemeProvider 
          colors={{ 
            background: { primary: '#FFFFFF' },
            text: { primary: '#000000' }
          }}
          fonts={{ 
            introTitle: 'System Font' 
          }}
        >
          <YourOnboardingComponent />
        </ThemeProvider>
      );
    }
  10. Use the Onboarding component

    main

    The Onboarding component is the main entry point for the library. It wraps the onboarding flow in a ThemeProvider for consistent styling and uses SafeAreaProvider (on non-web platforms) to ensure content respects device boundaries.

    To use it, import the default export and provide the required OnboardingProps, including colors, fonts, and the array of steps.

    import Onboarding from '@blazejkustra/react-native-onboarding';
    
    // Example usage (requires implementation of props)
    <Onboarding
      colors={myColors}
      fonts={myFonts}
      steps={mySteps}
    />
  11. Configure the Onboarding component with OnboardingProps

    main

    The Onboarding component is configured using the OnboardingProps interface. It requires an introPanel to show before steps begin, an array of steps, and an onComplete callback. You can also customize the appearance using colors, fonts, and background elements, or control navigation behavior via onSkip, onStepChange, and button visibility props.

    import { Onboarding } from '@blazejkustra/react-native-onboarding';
    
    <Onboarding
      introPanel={{
        title: 'Welcome',
        subtitle: 'Let\'s get started',
        button: 'Start'
      }}
      steps={[
        {
          title: 'Step 1',
          description: 'Description here',
          buttonLabel: 'Next',
          image: require('./img.png'),
          position: 'top'
        }
      ]}
      onComplete={() => console.log('Done!')}
    />
  12. Use the SpillOnboarding component

    main

    The SpillOnboarding component is the main entry point for implementing an onboarding flow. It manages a sequence of steps, an intro panel, and transitions between them using a 'spill' animation effect.

    Key features:

    • Intro Panel: Displays initial content before the first step.
    • Steps: An array of step objects defining the content of each onboarding screen.
    • Navigation: Supports back button (hardware and UI), next button, and skip functionality.
    • Platform Adaptation: Automatically wraps in a modal on Web if wrapInModalOnWeb is enabled.
    • Customization: Allows for custom intro panels and custom step components via function props.
    import SpillOnboarding from './spill-onboarding';
    
    // Example usage structure
    <SpillOnboarding
      steps={[
        { title: 'Step 1', description: 'Welcome', image: require('./img.png') },
        { title: 'Step 2', description: 'More info', image: require('./img2.png') }
      ]}
      onComplete={() => console.log('Done!')}
      onSkip={() => console.log('Skipped')}
    />