Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tips on DRY-er useage of media queries #4

Merged
merged 1 commit into from
Feb 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,57 @@ With React Native Responsive, you would write:

<br/>

#### Tips for DRY Media Queries
Much like SCSS and other preprocessed libraries, you can create a variable to reuse common queries.

In scss you might do:
```scss
$xsUp: "@media all and (min-width: 320px)";
```

With ES6 you might do:
```js
const IPHONE_7_AND_UP = `@media (min-device-width: 320) and (min-device-height: 720)`;
```

<br />

For further `DRY`-ness, create a seperate file with all of your media query breakpoints and export for use throughout your application.

```js
const IPHONE_WIDTH = 320;
const IPHONE_7_HEIGHT = 720;
export const IPHONE_7_AND_UP = `@media (min-device-width: ${IPHONE_WIDTH) and (min-device-height: ${IPHONE_7_HEIGHT)`;
```

Altogether that would look like:

```js
import {IPHONE_7_AND_UP} from '../styles/breakpoints';

...

const styles = MediaQueryStyleSheet.create(
//Base styles:
{
container: {
flex: 1,
flexDirection: "row",
backgroundColor: "red"
}
},
//Media Queries styles:
{
[IPHONE_7_AND_UP]: {
container: {
flexDirection: "column"
}
}
}
);
```
<br />

### Demonstration Application

If you want an overview of this library, it's interesting to try the demonstration code located inside `./example` folder.
Expand Down