Skip to content

kristerkari/react-native-css-media-query-processor

Repository files navigation

React Native CSS Media Query processor

NPM version Build Status Build status PRs Welcome

Match style objects containing CSS Media Queries with React Native

This library is based on React Native Extended StyleSheet's CSS Media Queries implementation.

Installation

npm install react-native-css-media-query-processor --save-dev

or

yarn add react-native-css-media-query-processor --dev

Usage

You can use this library together with css-to-react-native-transform to transform a string of CSS containing media queries to an React Native style object.

Notice that there is no syntax validation for CSS media queries. This is done to ensure that the media query matching is as fast as possible. If you want to validate the media queries, you should do that when they are parsed to style objects (that's what css-to-react-native-transform does).

Given that React Native returns the following dimensions:

import { Dimensions } from "react-native";
Dimensions.get("window");
// => { width: 110, height: 100 }

App.css:

.foo {
  color: red;
}

.bar {
  font-size: 1rem;
}

@media (min-width: 50px) and (max-width: 150px) {
  .foo {
    color: blue;
  }
  .bar {
    font-size: 2rem;
  }
}

↓ ↓ ↓ ↓ ↓ ↓

import styles from "./App.css";
import transform from "css-to-react-native-transform";
import { process } from "react-native-css-media-query-processor";

process(transform(styles, { parseMediaQueries: true }));

↓ ↓ ↓ ↓ ↓ ↓

{
  foo: {
    color: "blue"
  },
  bar: {
    fontSize: 32
  }
}

About

Match style objects containing CSS Media Queries with React Native

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •