Skip to content

capacitor-community/safe-area

Repository files navigation


Safe Area

@capacitor-community/safe-area

Capacitor Plugin that exposes the safe area insets from the native iOS/Android device to your web project.


Introduction

On web and iOS the safe area insets work out of the box. That is, on these platforms the env(safe-area-inset-*) CSS variables will have the correct values by default. On Android (in combination with Capacitor), however, those CSS variables will not have the correct values when Edge-to-Edge mode is enabled. So we need to work some magic in order to have access to the correct values. This plugin does that by detecting the safe area insets on Android, and injecting them as CSS variables to the browser. On web and iOS it will just fallback to the given values (which, again, are working out of the box).

There's one small but important quirck though, since we cannot override the native env(safe-area-inset-*) variables, the values are instead written to custom var(--safe-area-inset-*) variables.

Edge-to-Edge

You'll only need access to the safe area insets when you want to display your app edge-to-edge, by drawing your app content behind the system bars. The system bars are the status bar and the navigation bar. See the comparison table below for clarification:

Edge-to-Edge mode: Disabled Enabled Enabled
Safe Area Plugin installed: N/A.* Plugin not installed Plugin installed
Screenshot:
*Plugin not needed when not in Edge-to-Edge mode. Because the safe area insets will always be 0px in this mode. So it wouldn't have any effect.

This plugin, therefore, also provides utilities for styling those system bars and its content. It enables you to set the color of the bars to any color (e.g. black, white or transparent). It also enables you to set the color of the content (i.e. icon color) to either light or dark.

Installation

npm install @capacitor-community/safe-area
npx cap sync

Usage

This plugin can be enabled either by using the API or by using the Configuration. It's also possible to combine those two.

Using the API

import { SafeArea } from '@capacitor-community/safe-area';

SafeArea.enable({
  config: {
    customColorsForSystemBars: true,
    statusBarColor: '#00000000', // transparent
    statusBarContent: 'light',
    navigationBarColor: '#00000000', // transparent
    navigationBarContent: 'light',
  },
});

Using the Configuration

See the Configuration documentation below for examples.

Important

If you're enabling this plugin by using the configuration only and not by calling any of the API methods, you should still import the plugin like so:

import '@capacitor-community/safe-area';

Using the CSS variables

Having done this, the plugin will inject the correct safe area insets as CSS variables to the browser. This enables you to use those variables inside your CSS. You're now able to do this for example:

#header {
  padding-top: var(--safe-area-inset-top);
}

Or maybe you want to do something like this:

#header {
  padding-top: calc(var(--safe-area-inset-top) + 1rem);
}

Important

It's important to note that the used CSS variables are var(--safe-area-inset-*) and not env(safe-area-inset-*). Unfortunately it's not (yet) possible to override the native env( variables. So therefore custom variables are injected instead.

API

enable(...)

enable(options: { config: Config; }) => Promise<void>
Param Type
options { config: Config; }

disable(...)

disable(options: { config: Config; }) => Promise<void>
Param Type
options { config: Config; }

Interfaces

Config

Prop Type Description Default
customColorsForSystemBars boolean Flag indicating that you are responsible for drawing the background color for the system bars. If false it will fallback to the default colors for the system bars. true
statusBarColor string Specifies the background color of the status bar. Should be in the format #RRGGBB or #AARRGGBB. Will only have effect if customColorsForSystemBars is set to true. '#000000'
statusBarContent 'light' | 'dark' Specifies the color of the content (i.e. icon color) in the status bar. 'light'
navigationBarColor string Specifies the background color of the navigation bar. Should be in the format #RRGGBB or #AARRGGBB. Will only have effect if customColorsForSystemBars is set to true. '#000000'
navigationBarContent 'light' | 'dark' Specifies the color of the content (i.e. icon color) in the navigation bar. 'light'
offset number Specifies the offset to be applied to the safe area insets. This means that if the safe area top inset is 30px, and the offset specified is 10px, the safe area top inset will be exposed as being 40px. Usually you don't need this, but on iOS the safe area insets are mostly offset a little more by itself already. So you might want to compensate for that on Android. It's totally up to you. The offset will be applied if Edge-to-Edge mode is enabled only. 0

Configuration

For ease of use and speed, these configuration values are available:

Prop Type Description Default
enabled boolean Flag indicating whether of not the plugin should be enabled from startup. false
customColorsForSystemBars boolean Flag indicating that you are responsible for drawing the background color for the system bars. If false it will fallback to the default colors for the system bars. true
statusBarColor string Specifies the background color of the status bar. Should be in the format #RRGGBB or #AARRGGBB. Will only have effect if customColorsForSystemBars is set to true. '#000000'
statusBarContent 'light' | 'dark' Specifies the color of the content (i.e. icon color) in the status bar. 'light'
navigationBarColor string Specifies the background color of the navigation bar. Should be in the format #RRGGBB or #AARRGGBB. Will only have effect if customColorsForSystemBars is set to true. '#000000'
navigationBarContent 'light' | 'dark' Specifies the color of the content (i.e. icon color) in the navigation bar. 'light'
offset number Specifies the offset to be applied to the safe area insets. This means that if the safe area top inset is 30px, and the offset specified is 10px, the safe area top inset will be exposed as being 40px. Usually you don't need this, but on iOS the safe area insets are mostly offset a little more by itself already. So you might want to compensate for that on Android. It's totally up to you. The offset will be applied if Edge-to-Edge mode is enabled only. 0

Examples

In capacitor.config.json:

{
  "plugins": {
    "SafeArea": {
      "enabled": true,
      "customColorsForSystemBars": true,
      "statusBarColor": '#000000',
      "statusBarContent": 'light',
      "navigationBarColor": '#000000',
      "navigationBarContent": 'light',
      "offset": 0
    }
  }
}

In capacitor.config.ts:

/// <reference types="@capacitor-community/safe-area" />

import { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  plugins: {
    SafeArea: {
      enabled: true,
      customColorsForSystemBars: true,
      statusBarColor: '#000000',
      statusBarContent: 'light',
      navigationBarColor: '#000000',
      navigationBarContent: 'light',
      offset: 0,
    },
  },
};

export default config;