-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
121 lines (101 loc) · 4.87 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
'use strict';
var postcss = require('postcss');
module.exports = postcss.plugin('postcssResponsiveFont', function() {
function getUnit(x) {
return x.replace(/[0-9.]+/g, '');
}
function getNumber(x) {
return x.replace(/[a-zA-Z]+/g, '');
}
function pxtoem(pixels){
return (pixels/16);
}
function emtopx(pixels){
return (pixels*16);
}
return function (root) {
root.walkRules(function (rule) {
rule.walkDecls(function (decl) {
var prop = decl.prop;
var value = decl.value;
if (prop.indexOf('font-size-responsive') !== -1) {
var values = postcss.list.space(value);
var className = rule.selector;
var minFontSize = values[0];
var maxFontSize = values[1];
var minViewportSize = values[2];
var maxViewportSize = values[3];
/*
Assure valid declaration inputs.
*/
if (getUnit(minFontSize) !== getUnit(maxFontSize)) {
throw rule.error('min/max unit types must match for font sizes.');
}
if (getUnit(minViewportSize) !== getUnit(maxViewportSize)) {
throw rule.error('min/max unit types must match for media queries.');
}
if (getUnit(minFontSize) === null || getUnit(maxFontSize) === null ||
getUnit(minViewportSize) === null || getUnit(maxViewportSize) === null) {
throw rule.error('must declare a unit value');
}
if ((getUnit(minFontSize) === "em") && (getUnit(minViewportSize) === "px")) {
minViewportSize = pxtoem(getNumber(minViewportSize))+"em";
maxViewportSize = pxtoem(getNumber(maxViewportSize))+"em";
}
if ((getUnit(minFontSize) === "px") && (getUnit(minViewportSize) === "em")) {
minViewportSize = emtopx(getNumber(minViewportSize))+"px";
maxViewportSize = emtopx(getNumber(maxViewportSize))+"px";
}
/*
Create rule for MIN font size.
*/
var declMin = postcss.decl({
prop: 'font-size',
value: minFontSize
});
rule.append(declMin);
/*
Create rule for RESPONSIVE(vw-based) font size.
*/
rule.responsiveViewport = postcss.atRule({
name: 'media',
params: '(min-width:' + minViewportSize +
') and (max-width:' + maxViewportSize + ')'
});
rule.responsiveViewport.append({
selector: className
}).walkRules(function (selector) {
selector.append({
prop: 'font-size',
value: 'calc(' + minFontSize + ' + (' + getNumber(maxFontSize) +
' - ' + getNumber(minFontSize) + ') * ( (100vw - ' +
minViewportSize + ') / ( ' + getNumber(maxViewportSize) +
' - ' + getNumber(minViewportSize) + ')))'
});
});
root.insertAfter(rule, rule.responsiveViewport);
/*
Create rule for MAX font size.
*/
rule.largeViewport = postcss.atRule({
name: 'media',
params: '(min-width:' + minViewportSize + ')'
});
rule.largeViewport.append({
selector: className
}).walkRules(function (selector) {
selector.append({
prop: 'font-size',
value: maxFontSize
});
});
root.insertAfter(rule, rule.largeViewport);
/*
Clear original declaration.
*/
decl.remove();
}
});
});
};
});