Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit efe70d9

Browse files
authored
feat(decorator): Add responsive decorator (#14)
1 parent 747f9bb commit efe70d9

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

README.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,42 @@ export default class Login extends Component {
6666
| condition | boolean | Abritrary boolean value that must be true for the media query to pass. |
6767

6868

69-
### ResponsiveComponent
69+
### Responsive Annotation
7070

71-
`ResponsiveComponents` extends `React.Component` and add the window dimensions to the state of the component.
71+
You can use es7 annotation in order to listen for dimension changes in a React component.
7272

7373
```jsx
7474
import React from "react";
75-
import {ResponsiveComponent} from "react-native-responsive-ui";
75+
import {responsive} from "react-native-responsive-ui";
7676

77-
export default class Debug extends ResponsiveComponent {
77+
@responsive
78+
export default class Debug extends React.Component {
79+
render() {
80+
const {width, height} = this.state.window;
81+
console.log(`New window dimensions: ${width}x${height}`);
82+
return null;
83+
}
84+
}
85+
```
86+
87+
Or without the decorator syntax:
88+
89+
```jsx
90+
import React from "react";
91+
import {responsive} from "react-native-responsive-ui";
92+
93+
class Debug extends React.Component {
7894
render() {
7995
const {width, height} = this.state.window;
8096
console.log(`New window dimensions: ${width}x${height}`);
8197
return null;
8298
}
8399
}
100+
101+
export default responsive(Debug);
84102
```
85103

104+
86105
### ResponsiveStyleSheet
87106

88107
`ResponsiveStyleSheet` returns a stylesheet given multiple media queries.
@@ -143,3 +162,21 @@ import {Device, MediaQuerySelector} from "react-native-responsive-ui";
143162
const {width, height} = Device.dimensions.window;
144163
MediaQuerySelector.query({ orientation: "portrait", minHeight: 450 }, width, height)
145164
```
165+
166+
167+
### ResponsiveComponent
168+
169+
`ResponsiveComponents` extends `React.Component` and add the window dimensions to the state of the component.
170+
171+
```jsx
172+
import React from "react";
173+
import {ResponsiveComponent} from "react-native-responsive-ui";
174+
175+
export default class Debug extends ResponsiveComponent {
176+
render() {
177+
const {width, height} = this.state.window;
178+
console.log(`New window dimensions: ${width}x${height}`);
179+
return null;
180+
}
181+
}
182+
```

example/login/Login.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// @flow
22
import React from "react";
33
import {View, StyleSheet} from "react-native";
4-
import {MediaQuery, ResponsiveStyleSheet, ResponsiveComponent} from "../../lib";
4+
import {MediaQuery, ResponsiveStyleSheet, responsive} from "../../lib";
55

66
import {BackgroundImage, Images, Button} from "../components";
77
import Mark from "./Mark";
88
import Field from "./Field";
99

10-
export default class Login extends ResponsiveComponent {
10+
@responsive
11+
export default class Login extends React.Component {
1112
render(): React$Element<*> {
1213
// const {navigation} = this.props;
1314
const style = this.getStyle();

lib/Responsive.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @flow
2+
import React from "react";
3+
import ResponsiveComponent from "./ResponsiveComponent";
4+
5+
export default function Responsive(WrapperComponent: ReactClass<*>): ReactClass<*> {
6+
return class ResponsiveComponentWrapper extends ResponsiveComponent {
7+
render(): React$Element<*> {
8+
return <WrapperComponent window={this.state.window} {...this.props} />;
9+
}
10+
}
11+
}

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// @flow
22
import ResponsiveComponent from "./ResponsiveComponent";
3+
import responsive from "./Responsive";
34
import {MediaQuery} from "./MediaQuery";
45
import ResponsiveStyleSheet from "./ResponsiveStyleSheet";
56

67
export {
78
MediaQuery,
89
ResponsiveComponent,
9-
ResponsiveStyleSheet
10+
ResponsiveStyleSheet,
11+
responsive
1012
}

0 commit comments

Comments
 (0)