Socket
Socket
Sign inDemoInstall

@aashu-dubey/capacitor-statusbar-safe-area

Package Overview
Dependencies
2
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @aashu-dubey/capacitor-statusbar-safe-area

Get Status bar height and Safe area insets on Android & iOS.


Version published
Maintainers
1
Install size
59.9 kB
Created

Changelog

Source

v2.1.1 - (18 Jun 2023)

Breaking Change

  • Make safe-area custom element opt-in to register only if needed and to also to prevent breaking evaluating SafeAreaController in SSR environments. by @jjang16 in #9

If earlier you were using the safe-area tag by simple (Empty) import, you now have to import and call function registerSafeAreaElement

// root.component.ts or your-component.ts

import { registerSafeAreaElement } '@aashu-dubey/capacitor-statusbar-safe-area';

registerSafeAreaElement();
  • SafeAreaType type has been renamed to SafeAreaInset (changelog)

Readme

Source

capacitor-statusbar-safe-area

Get Status bar height and Safe area insets on Android & iOS.

npm npm Install Size

Install

npm install @aashu-dubey/capacitor-statusbar-safe-area
npx cap sync

Usage

import { SafeArea } from '@aashu-dubey/capacitor-statusbar-safe-area';

const getStatusBarHeight = async () => {
  const { height } = await SafeArea.getStatusBarHeight();
  return height; // Ex. 29.090909957885742
};

const getSafeAreaInsets = async () => {
  const insets = await SafeArea.getSafeAreaInsets();
  return insets; // Ex. { "bottom":34, "top":47, "right":0, "left":0 }
};

CSS Variables

Package also exposes CSS variables, for that you need to call injectCSSVariables method in your platform.ready() function or whenever app System UI visibility is changed

import { SafeAreaController } from '@aashu-dubey/capacitor-statusbar-safe-area';

const injectSafeAreaVariables = () => {
  SafeAreaController.injectCSSVariables();
};

then you can use them in your CSS files

.myContainer {
  // '--status-bar-height' & '--safe-area-inset-top' would most probably be same
  margin-top: var(--status-bar-height);
}

.myElement {
  padding-top: var(--safe-area-inset-top);
  padding-left: var(--safe-area-inset-left);
  padding-right: var(--safe-area-inset-right);
  padding-bottom: var(--safe-area-inset-bottom);
}

HTML Tag

Other than the above options, you can also use safe-area web component exported by the plugin.

Angular

For Angular users, you will get an error warning that this web component is unknown to the Angular compiler. This is resolved by modifying the module that declares your component to allow for custom web components.

// your-component.module.ts
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

@NgModule({
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})

You also have to register the custom element before using the tag

// app.component.ts or your-component.ts

import { registerSafeAreaElement } '@aashu-dubey/capacitor-statusbar-safe-area';

registerSafeAreaElement();

then just wrap the part you want to apply safe area padding on with safe-area tag as below

<safe-area>
  <!-- Other content -->
</safe-area>
Others

You will have to import the plugin in your component in order to make the web component work.

React
import { registerSafeAreaElement } '@aashu-dubey/capacitor-statusbar-safe-area';

registerSafeAreaElement();

const MyComponent = () => {
  return (
    <safe-area>
      // Other content
    </safe-area>
  );
}
Vue
<template>
  <safe-area>
    <!-- Other content -->
  </safe-area>
</template>

<script setup lang="ts">
import { registerSafeAreaElement } '@aashu-dubey/capacitor-statusbar-safe-area';

registerSafeAreaElement();
</script>
Attributes

There are two attributes, that can be used with the safe-area web component to control it's behaviour, mode & edges.

<safe-area mode="margin" edges="top,left,right"></safe-area>

more details here.

Capacitor version support

capacitorplugin version
v5.x2.1.0
v4.x>= 1.1.0 && <= 2.0.0
v3.x<= 1.0.1

API

getStatusBarHeight()

getStatusBarHeight() => Promise<{ height: number; }>

Get the Status bar height on Android and iOS, and on Web it returns 0.

Returns: Promise<{ height: number; }>


getSafeAreaInsets()

getSafeAreaInsets() => Promise<SafeAreaInset>

Get the Safe area insets for Android and iOS, and on Web it returns 0 for all.

Returns: Promise<SafeAreaInset>


Interfaces

SafeAreaInset
PropTypeDescription
topnumberSafe Area inset value at top.
bottomnumberSafe Area inset value at bottom.
leftnumberSafe Area inset value at left.
rightnumberSafe Area inset value at right.
SafeAreaHTMLProps
PropTypeDescription
mode'padding' | 'margin'Whether to apply safe area insets as padding or margin. default padding.
edgesstringSpecify the edges you want to apply safe area padding on, separated by comma.

For example, to apply padding only on top, left and right, edges="top,left,right". default to all edges.

Keywords

FAQs

Last updated on 17 Jun 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc