forked from RangerMauve/react-native-responsive-stylesheet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
132 lines (100 loc) · 2.95 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
var ReactNative = require("react-native");
var StyleSheet = ReactNative.StyleSheet;
var Dimensions = ReactNative.Dimensions;
var VALID_direction = [
"min-width",
"max-width",
"min-height",
"max-height",
"min-direction",
"max-direction"
];
module.exports = {
create: create,
createSized: createSized,
createOriented: createOriented,
};
// Pass-through
function create(styles) {
return StyleSheet.create(styles);
}
// Create sheet based on size increments for a certain direction
function createSized(direction, map) {
if(VALID_direction.indexOf(direction) === -1)
throw new TypeError("direction must equal " + VALID_direction.join(" or "));
var sizes = Object.keys(map).sort();
var stylesheets = sizes.reduce(function(result, size){
var stylesheet = map[size];
result[size] = StyleSheet.create(stylesheet);
return result;
}, {});
var styleNames = sizes.reduce(function(names, size){
return names.concat(Object.keys(map[size]));
}, []);
return styleNames.reduce(function(sheet, styleName){
if(styleName in sheet) return sheet;
Object.defineProperty(sheet, styleName, {
get: getStyles
});
function getStyles() {
var valid = validSizes(direction, sizes);
return valid.reduce(function(styles, size){
var stylesheet = stylesheets[size];
var style = stylesheet[styleName];
if(!style) return styles;
return styles.concat(style);
}, []);
}
return sheet;
}, {});
}
// Creates sheet based on landscape or portrait orientation
function createOriented(map) {
var landscapeStyles = map.landscape || {};
var portraitStyles = map.portrait || {};
var landscapeStylesheet = StyleSheet.create(landscapeStyles);
var portraitStylesheet = StyleSheet.create(portraitStyles);
var stylesheets = {
landscape: landscapeStylesheet,
portrait: portraitStylesheet
};
var styleNames = Object.keys(landscapeStyles).concat(Object.keys(portraitStyles));
return styleNames.reduce(function(sheet, styleName){
if(styleName in sheet) return sheet;
Object.defineProperty(sheet, styleName, {
get: getStyle
});
function getStyle() {
var orientation = getOrientation();
return stylesheets[orientation][styleName];
}
return sheet;
}, {});
}
function validSizes(direction, sizes) {
var dimensions = Dimensions.get("window");
var width = dimensions.width;
var height = dimensions.height;
var dimensionName = direction.slice(4);
var dimension;
if(dimensionName === "direction")
dimension = width <= height ? width : height
else
dimension = dimensions[dimensionName];
var directionName = direction.slice(0, 3);
var greater = directionName === "min";
var styles = sizes.filter(function (size) {
var parsed = parseInt(size, 10);
return greater ? (
dimension >= parsed
) : (
dimension <= parsed
);
});
return greater ? styles : styles.reverse();
}
function getOrientation(){
var dimensions = Dimensions.get("window");
return (dimensions.width > dimensions.height) ? "landscape" : "portrait";
}