npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-orientation-manager

v1.1.2

Published

A React Native module to retrieve interface/device orientation, listen to orientation changes, and lock screen to a specific orientation.

Downloads

13,807

Readme

react-native-orientation-manager

A React Native module to retrieve interface/device orientation, listen to orientation changes, and lock screen to a specific orientation.

Supported platforms

  • [x] iOS
  • [x] Android
  • [x] Windows

Installation

Using npm

npm install react-native-orientation-manager

Using yarn

yarn add react-native-orientation-manager

iOS Only:

Make sure pods are installed by navigating to the iOS project and running:

bundle exec pod install

Additional Configuration

To ensure that the module is fully initialized during app startup, include this import statement in your app's entry file (index.* or App.*):

import "react-native-orientation-manager";

For example:

import "react-native-orientation-manager";
import { AppRegistry } from "react-native";
import App from "./src/App";
import { name as appName } from "./app.json";

AppRegistry.registerComponent(appName, () => App);

Android

Add the following code to MainApplication.java which is probably located at android/app/src/main/java/<your package name>/MainApplication.java

// ... code
import com.kroosx4v.orientationmanager.OrientationManagerActivityLifecycleCallbacks;

public class MainApplication extends Application implements ReactApplication
{
    // ...code

    @Override
    public void onCreate()
    {
        // ...code
        registerActivityLifecycleCallbacks(new OrientationManagerActivityLifecycleCallbacks());
    }
}

iOS

Add the following code to your project's AppDelegate.mm

// ...code
#import "RNOrientationManager.h"

@implementation AppDelegate

// ...code

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return [RNOrientationManager getSupportedInterfaceOrientations];
}

@end

Usage

Retrieve current interface and device orientations:

import Orientations from "react-native-orientation-manager";
import type { InterfaceOrientation, DeviceOrientation } from "react-native-orientation-manager";

const currentInterfaceOrientation: InterfaceOrientation = Orientations.interfaceOrientation;
const currentDeviceOrientation: DeviceOrientation = Orientations.deviceOrientation;

Use current interface and device orientations as state hooks:

import { Text } from "react-native";
import { useInterfaceOrientation, useDeviceOrientation } from "react-native-orientation-manager";

function OrientationValues(): React.JSX.Element
{
    const interfaceOrientation = useInterfaceOrientation();
    const deviceOrientation = useDeviceOrientation();

    return (
        <>
            <Text style={{ color: "#841584" }}>
                Interface orientation: {interfaceOrientation.getName()}
            </Text>
            <Text style={{ color: "#841584" }}>
                Device orientation: {deviceOrientation.getName()}
            </Text>
        </>
    );
}

Lock interface orientation to a specific orientation:

Using functions:

import { lockToLandscape, lockToPortrait, unlockAllOrientations, resetInterfaceOrientationSetting } from "react-native-orientation-manager";

lockToLandscape();
// ...or
lockToPortrait();
// ...or
unlockAllOrientations();

// remove lock
resetInterfaceOrientationSetting();

Using OrientationLocker component:

import { View } from "react-native";
import { OrientationLocker } from "react-native-orientation-manager";

function LandscapeOnlyScreen(): React.JSX.Element
{
    return (
        <View style={{ flex: 1 }}>
            <OrientationLocker lock="Landscape" />
        </View>
    );
}

Listen to orientation changes:

import { addInterfaceOrientationChangeListener, addDeviceOrientationChangeListener } from "react-native-orientation-manager";
import type { InterfaceOrientation, DeviceOrientation } from "react-native-orientation-manager";

const listenerRemover = addInterfaceOrientationChangeListener((interfaceOrientation: InterfaceOrientation) => {
    if (interfaceOrientation.isPortrait())
    {
        // ...do something
    }
});

const listenerRemover2 = addDeviceOrientationChangeListener((deviceOrientation: DeviceOrientation) => {
    if (deviceOrientation.isPortrait())
    {
        // ...do something
    }
});

Do something every time the interface orientation changes while a screen is focused (requires @react-navigation/native 5x or higher):

import { useInterfaceOrientationWhenFocusedEffect } from "react-native-orientation-manager";
import type { InterfaceOrientation } from "react-native-orientation-manager";
import { useNavigation } from "@react-navigation/native";

function SomeComponent(): React.JSX.Element
{
    const navigation = useNavigation();

    useInterfaceOrientationWhenFocusedEffect((interfaceOrientation: InterfaceOrientation) => {
        if (!interfaceOrientation.isLandscape())
        {
            // Only allow landscape orientation in this screen with the possibility of leaving it when orientation changes to portrait
            navigation.goBack();
            return;
        }

        // optionally return a cleanup function
        return () => {
            // ...do something
        };
    });
}

API

Class InterfaceOrientation

| Function | Return Type | | --- | --- | | isUnknown() | boolean | | isPortrait() | boolean | | isPortraitUpsideDown() | boolean | | isLandscapeLeft() | boolean | | isLandscapeRight() | boolean | | isEitherPortrait() | boolean | | isLandscape() | boolean | | getName() | string | | equals(interfaceOrientation: InterfaceOrientation) | boolean |

Class DeviceOrientation

| Function | Return Type | | --- | --- | | isUnknown() | boolean | | isPortrait() | boolean | | isPortraitUpsideDown() | boolean | | isLandscapeLeft() | boolean | | isLandscapeRight() | boolean | | isFaceUp() | boolean | | isFaceDown() | boolean | | isEitherPortrait() | boolean | | isLandscape() | boolean | | isFace() | boolean | | getName() | string | | equals(deviceOrientation: DeviceOrientation) | boolean |

Default Export: An object with the following properties:

| Property | Type | Represents | | --- | --- | --- | | interfaceOrientation | InterfaceOrientation | Current interface orientation | | deviceOrientation | DeviceOrientation | Current device orienation |

Listener Registrar Functions:

| Function | Returns | | --- | --- | | addInterfaceOrientationChangeListener((interfaceOrientation: InterfaceOrientation) => void) | Listener remover function | | addDeviceOrientationChangeListener((deviceOrientation: DeviceOrientation) => void) | Listener remover function |

Interface Orientation Locking Functions:

| Function | Return Type | Notes | | --- | --- | --- | | async lockToPortrait() | Promise<void> || | async lockToPortraitUpsideDown() | Promise<void> || | async lockToLandscapeLeft() | Promise<void> || | async lockToLandscapeRight() | Promise<void> || | async lockToLandscape() | Promise<void> || | async lockToAllOrientationsButUpsideDown() | Promise<void> | Not supported on Android | | async unlockAllOrientations() | Promise<void> ||

To remove interface orientation lock:

| Function | Return Type | | --- | --- | | async resetInterfaceOrientationSetting() | Promise<void> |

Component OrientationLocker

| Prop | Type | Value | | --- | --- | --- | | lock | string | Can be one of the following:"Portrait": Locks to portrait only. Same as lockToPortrait()"PortraitUpsideDown": Locks to portrait upside down only. Same as lockToPortraitUpsideDown()"LandscapeLeft": Locks to landscape left only. Same as lockToLandscapeLeft()"LandscapeRight": Locks to landscape right only. Same as lockToLandscapeRight()"Landscape": Locks to landscape left or right only. Same as lockToLandscape()"AllButUpsideDown": Locks to all orientations except portrait upside down. Same as lockToAllOrientationsButUpsideDown(). Not supported on Android"All": Unlocks all orientations. Same as unlockAllOrientations() |

Hooks:

| Hook | Return Type | Functionality | --- | --- | --- | | useInterfaceOrientation() | InterfaceOrientation | Uses current interface orientation as a state | | useDeviceOrientation() | DeviceOrientation | Uses current device orientation as a state | | useInterfaceOrientationEffect(        effect: (interfaceOrientation: InterfaceOrientation) => CleanupFunction \| undefined,        dependencies: any[] = [],) | void | This runs on mount and subsequently whenever the interface orientation changes or when dependencies change during a re-render | | useDeviceOrientationEffect(        effect: (deviceOrientation: DeviceOrientation) => CleanupFunction \| undefined,        dependencies: any[] = [],) | void | This runs on mount and subsequently whenever the device orientation changes or when dependencies change during a re-render | | useInterfaceOrientationWhenFocusedEffect(        effect: (interfaceOrientation: InterfaceOrientation) => CleanupFunction \| undefined,        dependencies: any[] = [],) | void | This hook runs under three conditions:When screen has just been focusedWhen interface orientation changes while screen is focusedWhen a re-render occurs and dependencies change while screen is focusedSecond argument (dependencies) is optional. When it's not given or an empty array is given, the third condition does not apply.You can provide a cleanup function that gets called before the effect function must run again or when screen loses focus |