An open API service indexing awesome lists of open source software.

https://github.com/ziad-saab/mobx-matchmedia

A MobX observable for window.matchMedia
https://github.com/ziad-saab/mobx-matchmedia

matchmedia-api media-queries mobx react

Last synced: about 2 months ago
JSON representation

A MobX observable for window.matchMedia

Awesome Lists containing this project

README

        

# `mobx-matchmedia`
A [MobX](https://mobx.js.org/README.html) observable for [`window.matchMedia`](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia)

## Install

```sh
yarn add mobx-matchmedia
# or
npm i mobx-matchmedia
```

## Simple Usage

```js
import { autorun } from "mobx";
import { matchMedia } from "mobx-matchmedia";

autorun(() => {
if (matchMedia("(prefers-color-scheme: dark)")) {
console.log('Your OS is in dark mode');
} else {
console.log('Your OS is in light mode');
}
});
```

## React Usage

```js
import React from "react";
import { observer } from "mobx-react-lite";
import { matchMedia } from "mobx-matchmedia";

const MyComponent = observer(() => (


Your OS is in
{' '}
{matchMedia('(prefers-color-scheme: dark)') ? 'dark' : 'light'}
{' '}
mode

));
```