capacitor-plugin-safe-area

repository·main·Indexed 40 days ago

https://github.com/alwaysloveme/capacitor-plugin-safe-area

A Capacitor plugin for Android and iOS that provides access to device safe area insets (top, right, bottom, left) and status bar height. Version 5.0.1 includes methods to retrieve insets via getSafeAreaInsets(), get the status bar height via getStatusBarHeight(), and manage immersive navigation bar modes on Android.

Tokens
2.2K
Snippets
11
Records
14
Agent score
47%

What's inside capacitor-plugin-safe-area

  1. Use with TailwindCSS

    main

    To use safe area insets with TailwindCSS, use the tailwindcss-safe-area-capacitor plugin. This allows you to use utility classes like pb-safe to handle bottom safe areas automatically.

    Example integration in a React component:

    import { useEffect } from 'react';
    import { SafeArea } from 'capacitor-plugin-safe-area';
    
    const App = () => {
        useEffect(() => {
            (async function(){
                const { insets } = await SafeArea.getSafeAreaInsets();
                for (const [key, value] of Object.entries(insets)) {
                    document.documentElement.style.setProperty(
                        `--safe-area-inset-${key}`,
                        `${value}px`,
                    );
                }
            })()
        }, []);
    
        return (
            <div className="pb-safe toolbar">
                <div>....</div>
            </div>
        );
    }
  2. Listen for safe-area changes

    main

    To react to device orientation changes or other events that alter the safe area, use addListener with the 'safeAreaChanged' event. It is recommended to call removeAllListeners() before setting up new listeners to avoid duplicates.

    import { SafeArea } from 'capacitor-plugin-safe-area';
    
    // Clear existing listeners
    await SafeArea.removeAllListeners();
    
    // Listen for changes
    await SafeArea.addListener('safeAreaChanged', data => {
      const { insets } = data;
      for (const [key, value] of Object.entries(insets)) {
        document.documentElement.style.setProperty(
          `--safe-area-inset-${key}`,
          `${value}px`,
        );
      }
    });
  3. Manage immersive navigation bar on Android

    main

    On Android, you can control the immersive mode of the navigation bar. Note that these methods are not implemented on iOS. When setting immersive mode, the status bar background is automatically set to transparent.

    • setImmersiveNavigationBar(options): Enables immersive mode.
    • unsetImmersiveNavigationBar(options): Disables immersive mode.
    import { SafeArea } from 'capacitor-plugin-safe-area';
    
    // Enable immersive mode
    await SafeArea.setImmersiveNavigationBar({ statusBarStyle: 'dark' });
    
    // Disable immersive mode
    await SafeArea.unsetImmersiveNavigationBar();
  4. Get SafeArea insets and status bar height

    main

    Use getSafeAreaInsets() to retrieve the padding required to avoid notches and system bars, and getStatusBarHeight() to get the specific height of the status bar.

    Common practice is to map these insets to CSS custom properties so they can be used in your styles.

    import { SafeArea } from 'capacitor-plugin-safe-area';
    
    // Get insets and apply to CSS variables
    SafeArea.getSafeAreaInsets().then(({ insets }) => {
      for (const [key, value] of Object.entries(insets)) {
        document.documentElement.style.setProperty(
          `--safe-area-inset-${key}`,
          `${value}px`,
        );
      }
    });
    
    // Get status bar height
    SafeArea.getStatusBarHeight().then(({ statusBarHeight }) => {
      console.log(statusBarHeight);
    });
  5. Get safe area insets with getSafeAreaInsets()

    main

    Retrieve the current device's safe area insets (top, right, bottom, and left) to ensure your UI does not overlap with system notches, status bars, or navigation bars. Returns a SafeAreaInsets object containing a SafeArea object with numeric values for each side.

    const { insets } = await SafeAreaPlugin.getSafeAreaInsets();
    console.log(insets.top, insets.right, insets.bottom, insets.left);
  6. Access the SafeArea plugin

    main

    To use the SafeArea plugin in your Capacitor project, import the SafeArea constant from the package. This object provides access to all safe area related methods defined in the plugin's interface.

    import { SafeArea } from 'capacitor-plugin-safe-area';
    
    // Example usage:
    // const insets = await SafeArea.getSafeAreaInsets();
  7. Listen for safe area changes with addListener()

    main

    Subscribe to the safeAreaChanged event to react dynamically when the device's safe area insets change (e.g., during device rotation or when system UI elements appear/disappear). The listener receives a SafeAreaInsets object.

    const handler = await SafeAreaPlugin.addListener('safeAreaChanged', (data) => {
      console.log('New insets:', data.insets);
    });
    
    // To clean up later:
    // await handler.remove();