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

中文文档
5.0.0发布于 2026/08/27MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4854

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

原始使用文档
文档目录9 节
  1. Install
  2. Usage
  3. CSS Variables
  4. HTML Tag
  5. Capacitor version support
  6. API
  7. getStatusBarHeight()
  8. getSafeAreaInsets()
  9. Interfaces

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, The plugin also exports a <safe-area> custom web component.

Register and Use

Register the custom element in your app initialization (e.g. main.ts, app.component.ts, or root component) or only inside the component it's being used:

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

registerSafeAreaElement();

Then wrap the content you want to apply safe area spacing on with the <safe-area> tag:

<safe-area>
  <!-- Content here -->
</safe-area>

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.

Framework Examples

Angular

You will get an error that <safe-area> is an unknown element to the Angular compiler. This is resolved by adding CUSTOM_ELEMENTS_SCHEMA to your standalone component or module schemas.

1. Standalone Component (Angular 14+)
// your.component.ts
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { registerSafeAreaElement } from '@aashu-dubey/capacitor-statusbar-safe-area';

registerSafeAreaElement();

@Component({
  selector: 'app-your-component',
  standalone: true,
  template: `
    <safe-area>
      <!-- Other content -->
    </safe-area>
  `,
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class YourComponent {}
2. NgModule

Modify the module that declares your component to allow custom elements:

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

@NgModule({
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class YourComponentModule {}

Then register the custom element in your component:

// app.component.ts or your-component.ts
import { registerSafeAreaElement } from '@aashu-dubey/capacitor-statusbar-safe-area';

registerSafeAreaElement();
<!-- your.component.html -->
<safe-area>
  <!-- Other content -->
</safe-area>

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

registerSafeAreaElement();

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

You may need to create a *.d.ts file for the custom element in React:

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

declare module 'react' {
  namespace JSX {
    interface IntrinsicElements {
      'safe-area': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement> & SafeAreaHTMLProps;
    }
  }
}

export {};

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

  registerSafeAreaElement();
</script>

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

Make sure you enable recognize native custom elements in vite.config.ts:

// vite.config.ts > plugins
vue({
  template: {
    compilerOptions: {
      isCustomElement: (tag) => tag === 'safe-area',
    },
  },
}),

Also, for proper typing support in .vue <template>, create a *.d.ts file and add the following code:

import type { HTMLAttributes, PublicProps } from 'vue';
import type { SafeAreaHTMLProps } from '@aashu-dubey/capacitor-statusbar-safe-area';

type DefineCustomElement<
  ElementType extends HTMLElement,
  SelectedAttributes extends keyof ElementType = keyof ElementType,
> = new () => ElementType & {
  $props: HTMLAttributes & Partial<Pick<ElementType, SelectedAttributes>> & PublicProps;
};

type SafeAreaAttributes = 'mode' | 'edges';

declare module 'vue' {
  interface GlobalComponents {
    'safe-area': DefineCustomElement<SafeAreaHTMLProps, SafeAreaAttributes>;
  }
}

export {};
With SSR

The plugin and its functionalities are client-specific and might throw an error when used on the server side like #10 and #11, so always make sure to access the plugin on the client side only.

Here are some examples for a possible solution to use the plugin in:

TypeScript / DOM API

The package automatically registers 'safe-area' in HTMLElementTagNameMap. Standard DOM methods like document.querySelector('safe-area') and document.createElement('safe-area') will automatically be typed as SafeAreaElement:

const el = document.querySelector('safe-area'); // typed as SafeAreaElement | null
if (el) {
  el.mode = 'margin';
  el.edges = 'top,bottom';
}

Capacitor version support

capacitorplugin version
v8.x5.0.0
v7.x4.0.0
v6.x3.0.0
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

PropTypeDefaultDescription
mode'padding' | 'margin''padding'Whether to apply safe area insets as padding or margin.
edgesstringall edgesComma-separated list of edges to apply insets on.

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

版本与发布记录

@aashu-dubey/capacitor-statusbar-safe-area 共发布 10 个版本,首次发布于 2022/03/20;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.0.0latest2026/08/272 个月前
4.0.02025/02/271.7 年前
3.0.02024/06/032 年前
2.1.22024/06/022 年前
2.1.12023/06/183 年前
2.1.02023/05/143 年前

关于 @aashu-dubey/capacitor-statusbar-safe-area 的常见问题

@aashu-dubey/capacitor-statusbar-safe-area 是什么?

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

如何安装 @aashu-dubey/capacitor-statusbar-safe-area?

在项目目录运行 npm install @aashu-dubey/capacitor-statusbar-safe-area,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aashu-dubey/capacitor-statusbar-safe-area 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aashu-dubey/capacitor-statusbar-safe-area 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aashu-dubey/capacitor-statusbar-safe-area 可以商用吗?

@aashu-dubey/capacitor-statusbar-safe-area 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aashu-dubey/capacitor-statusbar-safe-area 的下载量有多少?

根据 npm 官方下载统计,@aashu-dubey/capacitor-statusbar-safe-area 在 2026/10/05 – 2026/11/03 这 30 天内被下载 4,854 次,日均约 162 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aashu-dubey/capacitor-statusbar-safe-area 还在维护吗?

最新版本 5.0.0 发布于 2026/08/27(2 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/03/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。