-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (138 loc) · 4.12 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// NOTE: media queries does not work with the combo
// - min-width, max-width, min-height, max-height
// - lower than equal, greater than equal
const DESCRIPTORS = [
'min-width',
'max-width',
'min-height',
'max-height'
];
const OPERATORS = [
'>=',
'<='
];
module.exports = ({
units = {
vh: 'cqh',
vw: 'cqw'
},
containerEl = 'body',
modifierAttr = '',
debug = false,
transform = null
} = {}) => {
const conditionalSelector = `:where(${containerEl}[${modifierAttr}])`;
const conditionalNotSelector = `:where(${containerEl}:not([${modifierAttr}]))`;
const processed = Symbol('processed');
const unitConvertIgnoredRules = [ 'media', 'container' ];
return {
postcssPlugin: 'postcss-viewport-to-container-toggle',
Rule(rule, { Rule }) {
const declsToCopy = [];
if (rule[processed]) {
return;
}
if (
unitConvertIgnoredRules.includes(rule.name) ||
unitConvertIgnoredRules.includes(rule.parent.name)
) {
rule[processed] = true;
return;
}
rule.walkDecls(decl => {
if (decl[processed]) {
return;
}
let value = decl.value;
if (Object.keys(units).every(unit => !value.includes(unit))) {
return;
}
for (const [ unitToConvert, newUnit ] of Object.entries(units)) {
value = value.replaceAll(unitToConvert, newUnit);
}
const clonedDeclWithContainerQueryUnits = decl.clone({ value });
declsToCopy.push(clonedDeclWithContainerQueryUnits);
decl[processed] = true;
});
if (!declsToCopy.length) {
return;
}
const prefixedRule = new Rule({
selector: `${conditionalSelector} ${rule.selector}`,
nodes: declsToCopy,
source: rule.source
});
rule.parent.insertAfter(rule, prefixedRule);
rule[processed] = true;
},
AtRule: {
media(atRule, { Rule }) {
if (atRule[processed]) {
return;
}
if (
atRule.params.includes('print') &&
!atRule.params.includes('all') &&
!atRule.params.includes('screen')
) {
atRule[processed] = true;
return;
}
// Container query
const containerAtRule = atRule.clone({
name: 'container',
params: convertToContainerQuery(atRule.params, {
debug,
transform
})
.replaceAll(/(only\s*)?(all|screen|print)(,)?(\s)*(and\s*)?/g, '')
});
containerAtRule.walkDecls(decl => {
if (decl[processed]) {
return;
}
if (Object.keys(units).every(unit => !decl.value.includes(unit))) {
return;
}
for (const [ unitToConvert, newUnit ] of Object.entries(units)) {
decl.value = decl.value.replaceAll(unitToConvert, newUnit);
}
});
// Media query
// Only applied for conditionalNotSelector
atRule.walkRules(rule => {
if (rule[processed]) {
return;
}
const newRule = rule.clone({
selectors: rule.selectors.map(selector => {
if (selector.startsWith(containerEl)) {
return selector.replace(containerEl, conditionalNotSelector);
}
return `${conditionalNotSelector} ${selector}`;
})
});
rule.replaceWith(newRule);
rule[processed] = true;
newRule[processed] = true;
});
atRule.parent.insertAfter(atRule, containerAtRule);
atRule[processed] = true;
}
}
};
};
module.exports.postcss = true;
function convertToContainerQuery (mediaFeature, { debug, transform }) {
const containerFeature = typeof transform === 'function'
? transform(mediaFeature)
: mediaFeature;
if (
debug &&
DESCRIPTORS.some(descriptor => containerFeature.includes(descriptor)) &&
OPERATORS.some(operator => containerFeature.includes(operator))
) {
console.warn('[postcss-viewport-to-container-toggle] Unsupported media query', containerFeature);
}
return containerFeature;
};