# react-native-layout-provider

> Getting layout size by top provider instead of Dimensions

Latest version **1.2.1** (published 2018-02-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-native-layout-provider
pnpm add react-native-layout-provider
yarn add react-native-layout-provider
bun add react-native-layout-provider
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2018-02-23 |
| First published | 2016-07-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Jhen |
| Maintainers | jhen0409 |
| Keywords | react, react-native, layout, provider, layout-tester |

## Links

- npm: https://www.npmjs.com/package/react-native-layout-provider
- Repository: https://github.com/jhen0409/react-native-layout-provider
- Issues: https://github.com/jhen0409/react-native-layout-provider/issues
- npm.io page: https://npm.io/package/react-native-layout-provider

## Dependencies (2)

- [shallowequal](https://npm.io/package/shallowequal.md) ^0.2.2
- [hoist-non-react-statics](https://npm.io/package/hoist-non-react-statics.md) ^1.2.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.1 (latest) — 2018-02-23
- 1.2.0 — 2018-02-23
- 1.1.0 — 2016-07-09
- 1.0.1 — 2016-07-08
- 1.0.0 — 2016-07-08

## README

# React Native Layout Provider [![Build Status](https://travis-ci.org/jhen0409/react-native-layout-provider.svg?branch=master)](https://travis-ci.org/jhen0409/react-native-layout-provider)

Getting layout size by top provider instead of `Dimensions`. This is split from [react-native-layout-tester](https://github.com/machadogj/react-native-layout-tester)'s context & `getLayout` decorator. Inspired by Provider / connect of [react-redux](https://github.com/reactjs/react-redux).

## Why?

Originally `react-native-layout-tester` have a problem for `Dimensions`, you can't get a correct layout with Dimensions for each component if you're using LayouTester wrapper, so we made a `getLayout` decorator to resolve it.

It's mean you must be use `getLayout` instead of `Dimensions` for each component, but you can keep the same code in production.

## Installation

```bash
$ npm install --save react-native-layout-provider
```

## Usage

Use `LayoutProvider` to wrap your app container:

```js
import React from 'react';
import LayoutProvider from 'react-native-layout-provider';

export default () => (
  <LayoutProvider
    // Default: "Default"
    label={String}

    // Default: provided by Dimensions
    width={Number},
    height={Number},
    // Default: undefined, you can use react-native-orientation to get portrait
    portrait={Boolean}
  >
    <YourApp />
  </LayoutProvider>
);
```

And use `getLayout` for each component, the component props will update when LayoutProvider props changed:

```js
import React, { Component, PropTypes } from 'react';
import { getLayout, defaultLayoutTypes } from 'react-native-layout-provider';

class Comp extends Component {

  // It's same with defaultLayoutTypes
  static propTypes = {
    label: PropTypes.string.isRequired,
    viewport: PropTypes.shape({
      width: PropTypes.number.isRequired,
      height: PropTypes.number.isRequired,
    }).isRequired,
    portrait: PropTypes.bool,
  };

  render() {
    return (...);
  }
}

export default getLayout(layout => ({
  label: layout.label,
  viewport: {
    width: layout.viewport.width,
    height: layout.viewport.height,
  },
  portrait: layout.portrait,
}))(Comp);
```

Or use [babel-plugin-transform-decorators-legacy](https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy) to get `@getLayout()` work.

## Advanced example

Use [react-native-orientation](https://github.com/yamill/react-native-orientation) to get / listen orientation, and [react-native-extra-dimensions-android](https://github.com/jaysoo/react-native-extra-dimensions-android) to resolved status bar problem for Android.

```js
import React, { Component, PropTypes } from "react";
import { Platform, Dimensions } from "react-native";
import LayoutProvider, { getLayout } from 'react-native-layout-provider';
import Orientation from 'react-native-orientation';
import ExtraDimensions from 'react-native-extra-dimensions-android';

let statusBarHeight = 0;
if (Platform.OS === 'android') {
  try {
    statusBarHeight = ExtraDimensions.get('STATUS_BAR_HEIGHT');
  } catch(e) {}
}

class App extends Component {
  state = {
    orientation: Orientation.getInitialOrientation(),
  };

  _orientationDidChange = orientation => {
    this.setState({ orientation });
  };

  componentDidMount() {
    Orientation.addOrientationListener(this._orientationDidChange);
  }

  componentWillUnmount() {
    Orientation.removeOrientationListener(this._orientationDidChange);
  }

  render() {
    const { height } = Dimensions.get('window');
    return (
      <LayoutProvider
        label="Default"
        height={height - statusBarHeight} // Resolved statusBar problem for Android
        portrait={this.state.orientation === 'PORTRAIT'}
      >
        ...
      </LayoutProvider>
    );
  }
}
```

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/react-native-layout-provider · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
