-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
45 lines (38 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Dimensions } from 'react-native'
import mediaQuery from 'css-mediaquery'
import Orientation from 'react-native-orientation-listener'
class NativeMediaQueryList {
_listeners = [];
_query = '';
_orientation = 'PORTRAIT';
constructor(mediaQueryString) {
this._query = mediaQueryString
Orientation.getOrientation(orientation => {
this._notifyListeners({ orientation })
})
Orientation.addListener(e => {
this._notifyListeners(e)
})
}
get matches() {
return mediaQuery.match(this._query, {
type: 'screen',
...Dimensions.get('window'),
})
}
_notifyListeners(e) {
this._orientation = e.orientation
this._listeners.forEach(listener => {
listener(this)
})
}
addListener(listener) {
this._listeners.push(listener)
}
removeListener(listener) {
const index = this._listeners.indexOf(listener)
if (index === -1) return
this._listeners.splice(index)
}
}
export default mediaQueryString => new NativeMediaQueryList(mediaQueryString)