-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (53 loc) · 1.14 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { Dimensions, PixelRatio } from 'react-native'
class Units {
constructor() {
this.scale = 1
this.grid = {
cols: 3,
padding: 10,
spacing: 10
}
this.update()
}
update = () => {
this.window = Dimensions.get('window')
this.pixelRatio = PixelRatio.get()
this.setGrid()
}
setScale = (scale) => {
this.scale = scale
}
setGrid = (o) => {
this.grid = {
...this.grid, ...o
}
const w = this.window.width - (this.grid.padding*2) + this.grid.spacing
this.grid.colWidth = (w/this.grid.cols)
}
px = (x=1) => {
return x/this.pixelRatio
}
su = (x=1) => {
return x*this.scale
}
vh = (x=1) => {
return x*this.window.height*0.01
}
vw = (x=1) => {
return x*this.window.width*0.01
}
gr = (x=1) => {
return (x*this.grid.colWidth)-this.grid.spacing
}
gs = (x=1) => {
return x*this.grid.spacing
}
gp = (x=1) => {
return x*this.grid.padding
}
}
const units = new Units()
module.exports = units;
['vw', 'vh', 'px', 'su', 'gr', 'gs', 'gp', 'update', 'setScale', 'setGrid'].forEach(o => {
module.exports[o] = units[o];
})