|
1 | | -# react-native-responsive-ui |
| 1 | +# React Native Reponsive UI |
2 | 2 |
|
3 | | -Responsive UIs for React Native |
| 3 | +[](https://circleci.com/gh/wcandillon/react-native-responsive-ui) |
| 4 | +[](https://badge.fury.io/js/react-native-responsive-ui) |
| 5 | + |
| 6 | + |
| 7 | +Building responsive UIs in React Native. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +An example is available via expo [here](https://expo.io/@wcandillon/react-native-responsive-ui). |
4 | 12 |
|
5 | 13 | ## Installation |
6 | 14 |
|
7 | | -```sh |
8 | | -npm install react-native-responsive-ui |
| 15 | +```bash |
| 16 | +npm install react-native-responsive-ui --save |
9 | 17 | ``` |
10 | 18 |
|
11 | 19 | ## Usage |
12 | 20 |
|
13 | | -```js |
14 | | -import ResponsiveUi from "react-native-responsive-ui"; |
| 21 | +The `MediaQuery` component renders its children only if the query evaluates to true (see list of properties below). |
| 22 | +This component listens to changes in the window dimensions. |
| 23 | +In the example below, we render the `Logo` component if the window's height has a minimum size of `450dp` and if the device orientation is in portrait mode (height is larger than width). |
| 24 | + |
| 25 | +### Media Queries |
| 26 | + |
| 27 | +```jsx |
| 28 | +// @flow |
| 29 | +import React, {Component} from "react"; |
| 30 | +import {View} from "react-native"; |
| 31 | +import {MediaQuery} from "react-native-responsive-ui"; |
| 32 | + |
| 33 | +export default class Login extends Component { |
| 34 | + render(): React$Element<*> { |
| 35 | + return <View> |
| 36 | + <MediaQuery minHeight={450} orientation="portrait"> |
| 37 | + <Logo /> |
| 38 | + </MediaQuery> |
| 39 | + </View>; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +``` |
| 44 | + |
| 45 | +#### Properties |
| 46 | + |
| 47 | +| Name | Type | Description | |
| 48 | +|----------------|--------|--------------------------------------------------------------------------------------| |
| 49 | +| minHeight | dp | Minimum height of the window. | |
| 50 | +| maxHeight | dp | Maximum height of the window. | |
| 51 | +| minWidth | dp | Minimum width of the window. | |
| 52 | +| maxWidth | dp | Maximum width of the window. | |
| 53 | +| minAspectRatio | number | Minimum aspect ration of the window (ratio of horizontal pixels to vertical pixels). | |
| 54 | +| maxAspectRatio | number | Maximum aspect ration of the window (ratio of horizontal pixels to vertical pixels). | |
| 55 | +| minPixelRatio | number | Minimum device pixel density. See [PixelRatio](https://facebook.github.io/react-native/docs/pixelratio.html). | |
| 56 | +| maxPixelRatio | number | Maximum device pixel density. See [PixelRatio](https://facebook.github.io/react-native/docs/pixelratio.html). | |
| 57 | +| orientation | `portrait` or `landspace` | Indicates whether the viewport is in landscape (the display is wider than it is tall) or portrait (the display is square or taller than it is wide) mode. | |
| 58 | +| platform | string | Platform of the device. See [Platform](https://facebook.github.io/react-native/docs/platform-specific-code.html#platform-module). | |
| 59 | +| condition | boolean | Abritrary boolean value that must be true for the media query to pass. | |
15 | 60 |
|
16 | | -// ... |
17 | 61 |
|
18 | | -const result = await ResponsiveUi.multiply(3, 7); |
| 62 | +### useDimensions |
| 63 | + |
| 64 | +```jsx |
| 65 | +import React from "react"; |
| 66 | +import {useDimensions} from "react-native-responsive-ui"; |
| 67 | + |
| 68 | +export default ({ children }) => { |
| 69 | + const {width, height} = useDimensions(); |
| 70 | + console.log(`New window dimensions: ${width}x${height}`); |
| 71 | + return children; |
| 72 | +}; |
19 | 73 | ``` |
20 | 74 |
|
21 | | -## Contributing |
| 75 | +### useStylesheet |
| 76 | + |
| 77 | +```jsx |
| 78 | +import React from "react"; |
| 79 | +import {useStylesheet} from "react-native-responsive-ui"; |
| 80 | + |
| 81 | +export default class Buttons extends ResponsiveComponent { |
| 82 | + render() { |
| 83 | + const style = useStylesheet(staticStyle) |
| 84 | + return <View style={style.btns}> |
| 85 | + <Button label="Login" primary style={style.btn} /> |
| 86 | + <Button label="Sign Up" style={style.btn} /> |
| 87 | + </View>; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +const staticStyle = [ |
| 92 | + { |
| 93 | + query: { orientation: "landscape" }, |
| 94 | + style: { |
| 95 | + btns: { |
| 96 | + flexDirection: "row" |
| 97 | + }, |
| 98 | + btn: { |
| 99 | + flex: 1 |
| 100 | + } |
| 101 | + } |
| 102 | + }, |
| 103 | + { |
| 104 | + query: { orientation: "portrait" }, |
| 105 | + style: { |
| 106 | + btns: { |
| 107 | + alignSelf: "stretch" |
| 108 | + }, |
| 109 | + btn: { |
| 110 | + flex: 0 |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +]; |
| 115 | +``` |
| 116 | + |
| 117 | +### Media Query |
| 118 | + |
| 119 | +`mediaQuery()` evaluates a media query and return true or false. |
| 120 | +See example below. |
| 121 | + |
| 122 | +```jsx |
| 123 | +import {mediaQuery, useDimensions} from "react-native-responsive-ui"; |
| 124 | + |
| 125 | +const {width, height} = useDimensions(); |
| 126 | +mediaQuery({ orientation: "portrait", minHeight: 450 }, width, height) |
| 127 | +``` |
| 128 | + |
| 129 | +### ResponsiveComponent |
| 130 | + |
| 131 | +`ResponsiveComponents` extends `React.Component` and add the window dimensions to the state of the component. |
| 132 | + |
| 133 | +```jsx |
| 134 | +import React from "react"; |
| 135 | +import {ResponsiveComponent} from "react-native-responsive-ui"; |
| 136 | + |
| 137 | +export default class Debug extends ResponsiveComponent { |
| 138 | + render() { |
| 139 | + const {width, height} = this.state.window; |
| 140 | + console.log(`New window dimensions: ${width}x${height}`); |
| 141 | + return null; |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
22 | 145 |
|
23 | | -See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow. |
| 146 | +### getStyleSheet |
24 | 147 |
|
25 | | -## License |
| 148 | +```jsx |
| 149 | +import React from "react"; |
| 150 | +import {ResponsiveComponent, getStyleSheet} from "react-native-responsive-ui"; |
26 | 151 |
|
27 | | -MIT |
| 152 | +export default class Debug extends ResponsiveComponent { |
| 153 | + render() { |
| 154 | + const {width, height} = this.state.window; |
| 155 | + const styles = [ |
| 156 | + { |
| 157 | + query: { minHeight: 500 }, |
| 158 | + style: { container: { backgroundColor: "red" } } |
| 159 | + } |
| 160 | + ]; |
| 161 | + const style = getStyleSheet({width, height}, styles) |
| 162 | + return <View style={style.container} /> |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
0 commit comments