Skip to content

Instantly share code, notes, and snippets.

@ecklf
Last active July 2, 2024 19:40
Show Gist options
  • Save ecklf/dc8dee4e1c9b8cd8011199416176fb3e to your computer and use it in GitHub Desktop.
Save ecklf/dc8dee4e1c9b8cd8011199416176fb3e to your computer and use it in GitHub Desktop.

Revisions

  1. ecklf revised this gist Aug 3, 2021. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion useStatusBarHeight.ts
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,19 @@
    import { useEffect, useState } from "react";
    import { NativeEventEmitter, NativeModules, StatusBar } from "react-native";
    import {
    NativeEventEmitter,
    NativeModules,
    Platform,
    StatusBar,
    } from "react-native";

    const { StatusBarManager } = NativeModules;

    export default function useStatusBarHeight() {
    const [value, setValue] = useState(StatusBar.currentHeight || 0);

    useEffect(() => {
    if (Platform.OS !== "ios") return;

    const emitter = new NativeEventEmitter(StatusBarManager);

    StatusBarManager.getHeight(({ height }: { height: number }) => {
  2. ecklf revised this gist Aug 3, 2021. No changes.
  3. ecklf created this gist Aug 3, 2021.
    23 changes: 23 additions & 0 deletions useStatusBarHeight.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import { useEffect, useState } from "react";
    import { NativeEventEmitter, NativeModules, StatusBar } from "react-native";

    const { StatusBarManager } = NativeModules;

    export default function useStatusBarHeight() {
    const [value, setValue] = useState(StatusBar.currentHeight || 0);

    useEffect(() => {
    const emitter = new NativeEventEmitter(StatusBarManager);

    StatusBarManager.getHeight(({ height }: { height: number }) => {
    setValue(height);
    });
    const listener = emitter.addListener("statusBarFrameWillChange", (data) =>
    setValue(data.frame.height),
    );

    return () => listener.remove();
    }, []);

    return value;
    }