Giter Club home page Giter Club logo

aashu-dubey / capacitor-statusbar-safe-area Goto Github PK

View Code? Open in Web Editor NEW
31.0 2.0 10.0 757 KB

Capacitor Package to get Status bar height and Safe area insets on Android & iOS.

Home Page: https://npmjs.com/package/@aashu-dubey/capacitor-statusbar-safe-area

License: MIT License

Ruby 6.78% Java 24.23% Swift 20.79% Objective-C 2.78% JavaScript 19.92% TypeScript 21.17% CSS 0.16% HTML 4.17%
android capacitor capacitor-plugin cross-platform ionic ios library npm-package package plugin

capacitor-statusbar-safe-area's Introduction

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.

With SSR

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

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

Capacitor version support

capacitor plugin version
v6.x 3.0.0
v5.x 2.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

Prop Type Description
top number Safe Area inset value at top.
bottom number Safe Area inset value at bottom.
left number Safe Area inset value at left.
right number Safe Area inset value at right.

SafeAreaHTMLProps

Prop Type Description
mode 'padding' | 'margin' Whether to apply safe area insets as padding or margin. default padding.
edges string Specify 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.

capacitor-statusbar-safe-area's People

Contributors

aashu-dubey avatar azarz avatar hrdtr avatar jjang16 avatar menardi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

capacitor-statusbar-safe-area's Issues

question: possible collab

Nice plugin!

I was in need of a plugin that allowed me to use the edge-to-edge functionality on Android just as we're able to use it on iOS. I ended up writing this plugin: https://github.com/capacitor-community/safe-area

Only then I discovered your plugin. There's also another similar plugin: https://github.com/AlwaysLoveme/capacitor-plugin-safe-area

Maybe we can collaborate. I asked the author of the other plugin the same thing. See: AlwaysLoveme/capacitor-plugin-safe-area#29

Error when using with Nuxt.js

When using within my Nuxt.js environment, I am getting an error message on npm run dev.

[nuxt] [request error] [unhandled] [500] HTMLElement is not defined
  at Object.<anonymous> (./node_modules/@aashu-dubey/capacitor-statusbar-safe-area/dist/plugin.cjs.js:38:31) 

When I remove "SafeAreaElement" from the plugin code, all is working well.(I do not need SafeSreaElement, only "getSafeAreaInsets"), so I could do a fork, but maybe you can have look into it.

Maybe it has something to do with TypeScript (or the lack of it?)

Add Capacitor v5 support

Hi!
Could you please update your plugin to make it compatible with Capacitor v5?
Thanks in advance.

Swift warning on iOS 11

Hello,

I used this plugin on an Ionic app and my iOS build got a console warning upon calling SafeAreaController.injectCSSVariables() (see screenshot below). After a quick Google search, this seems like a problem with the Swift bindings for the plugin: any UI operations must be forced to run on the main thread with DispatchQueue.main.sync (or .async) - see this issue's answer for an example on how to fix it.

Just to make it clear, the issue doesn't seem to break my build (the CSS variables still work as intended), but it seems to slow down the app's process for a few secs while the error is being thrown.

ios-bug

Seems like a relatively simple fix, but I never touched Swift before so I thought it was better to discuss this before attempting a PR. Here are my package versions, in case it's useful:

  • @ionic/angular: 6.4.2
  • @capacitor/core: 4.7.0
  • @angular/core: 15.1.0
  • @aashu-dubey/capacitor-statusbar-safe-area: 1.1.0

Grats for the great plugin and thanks, it's really useful and I hope you get to fix this minor issue soon!

Not working with insets

I got the insets, but when I paste this to the scss file, I cant get any result for my page, how is it working? Any help please

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.