sonner-native

repository·main·Indexed 23 days ago

https://github.com/gunnartorfis/sonner-native-toasts

An opinionated toast component library for React Native, providing a port of the sonner web library. It offers high-performance, customizable, and gesture-enabled notifications with support for various variants including success, error, warning, and promise-based toasts. The library requires peer dependencies such as react-native-reanimated and react-native-gesture-handler.

Tokens
9.4K
Snippets
29
Records
46
Agent score
79%

What's inside sonner-native

  1. Setup the Toaster component

    main

    The Toaster is the central context component that manages toast notifications. To ensure it works correctly across all screens, you must render it at the root level of your app, after the NavigationContainer.

    Crucial Requirement: The Toaster must be rendered within both a SafeAreaProvider and a GestureHandlerRootView.

    import { Toaster } from 'sonner-native';
    import { NavigationContainer } from '@react-navigation/native';
    import { SafeAreaProvider } from 'react-native-safe-area-context';
    import { GestureHandlerRootView } from 'react-native-gesture-handler';
    
    export default function App() {
      return (
        <SafeAreaProvider>
          <GestureHandlerRootView>
            <NavigationContainer>{/* App content */}</NavigationContainer>
            <Toaster />
          </GestureHandlerRootView>
        </SafeAreaProvider>
      );
    }
  2. Configure Toaster with Expo Router

    main

    When using Expo Router, place the <Toaster /> component inside your root layout file (app/_layout.tsx). It should be wrapped within a <GestureHandlerRootView> to ensure gesture-based dismissal works correctly.

    import { Toaster } from 'sonner-native';
    import { Stack } from 'expo-router';
    import { GestureHandlerRootView } from "react-native-gesture-handler";
    
    export default function RootLayout() {
      return (
        <GestureHandlerRootView>
          <Stack>
            <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
            <Stack.Screen name="+not-found" />
          </Stack>
          <Toaster />
        </GestureHandlerRootView>
      );
    }
  3. Configure the Toaster component

    main

    To display toasts, you must include the <Toaster /> component in your application's entry point or root layout. This ensures the toast container is available globally.

    import { Toaster } from 'sonner-native';
    
    function App() {
      return (
        <View>
          <NavigationContainer>...</NavigationContainer>
          <Toaster />
        </View>
      );
    }
  4. Setup Toaster with Expo Router

    main

    When using Expo Router, place the <Toaster /> component in your root layout file (app/_layout.tsx) to ensure toasts are visible across all screens in your application.

    import { Toaster } from 'sonner-native';
    import { Stack } from 'expo-router';
    
    export default function RootLayout() {
      return (
        <>
          <Stack>
            <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
            <Stack.Screen name="+not-found" />
          </Stack>
          <Toaster />
        </>
      );
    }
  5. Configure web support for sonner-native

    main

    While sonner-native works on the web, it is recommended to use the original sonner package for web platforms. You can achieve platform-specific imports by creating two files, sonner.ts and sonner.web.ts, and importing from those instead of the library directly.

    // sonner.ts
    export * from 'sonner-native';
    
    // sonner.web.ts
    export * from 'sonner';
  6. Install sonner-native

    main

    Install the package using yarn. Note that you must also install several peer dependencies for the library to function correctly: react-native-reanimated, react-native-gesture-handler, react-native-safe-area-context, react-native-svg, react-native-screens, and react-native-worklets.

    yarn add sonner-native
  7. Implement cross-platform toast support (Web vs Native)

    main
    While sonner-native works on the web, it is recommended to use the original sonner package for web environments. You can achieve this by creating abstraction files (sonner.ts and sonner.web.ts) and importing from those instead of the library directly.