@colorye/react-native-css 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,254 @@
1
+ function flattenLayersAndSupports(cssStr) {
2
+ let result = "";
3
+ let i = 0;
4
+ const len = cssStr.length;
5
+
6
+ while (i < len) {
7
+ // Skip comments
8
+ if (cssStr[i] === "/" && cssStr[i + 1] === "*") {
9
+ const commentEnd = cssStr.indexOf("*/", i + 2);
10
+ if (commentEnd === -1) {
11
+ result += cssStr.substring(i);
12
+ break;
13
+ }
14
+ result += cssStr.substring(i, commentEnd + 2);
15
+ i = commentEnd + 2;
16
+ continue;
17
+ }
18
+
19
+ // Skip strings
20
+ if (cssStr[i] === '"' || cssStr[i] === "'") {
21
+ const char = cssStr[i];
22
+ let strEnd = i + 1;
23
+ while (strEnd < len) {
24
+ if (cssStr[strEnd] === "\\") {
25
+ strEnd += 2;
26
+ } else if (cssStr[strEnd] === char) {
27
+ strEnd++;
28
+ break;
29
+ } else {
30
+ strEnd++;
31
+ }
32
+ }
33
+ result += cssStr.substring(i, strEnd);
34
+ i = strEnd;
35
+ continue;
36
+ }
37
+
38
+ // Check for @layer or @supports
39
+ if (cssStr[i] === "@") {
40
+ const remaining = cssStr.substring(i);
41
+ const layerMatch = remaining.match(/^@layer\s+[^{]+\{/i);
42
+ const supportsMatch = remaining.match(/^@supports\s+[^{]+\{/i);
43
+
44
+ if (layerMatch || supportsMatch) {
45
+ const match = layerMatch || supportsMatch;
46
+ const matchStr = match[0];
47
+ let braceCount = 1;
48
+ let j = i + matchStr.length;
49
+ let innerContent = "";
50
+
51
+ while (j < len && braceCount > 0) {
52
+ if (cssStr[j] === "/" && cssStr[j + 1] === "*") {
53
+ const commentEnd = cssStr.indexOf("*/", j + 2);
54
+ if (commentEnd === -1) {
55
+ innerContent += cssStr.substring(j);
56
+ j = len;
57
+ break;
58
+ }
59
+ innerContent += cssStr.substring(j, commentEnd + 2);
60
+ j = commentEnd + 2;
61
+ continue;
62
+ }
63
+
64
+ if (cssStr[j] === '"' || cssStr[j] === "'") {
65
+ const char = cssStr[j];
66
+ let strEnd = j + 1;
67
+ while (strEnd < len) {
68
+ if (cssStr[strEnd] === "\\") {
69
+ strEnd += 2;
70
+ } else if (cssStr[strEnd] === char) {
71
+ strEnd++;
72
+ break;
73
+ } else {
74
+ strEnd++;
75
+ }
76
+ }
77
+ innerContent += cssStr.substring(j, strEnd);
78
+ j = strEnd;
79
+ continue;
80
+ }
81
+
82
+ if (cssStr[j] === "{") {
83
+ braceCount++;
84
+ } else if (cssStr[j] === "}") {
85
+ braceCount--;
86
+ }
87
+
88
+ if (braceCount > 0) {
89
+ innerContent += cssStr[j];
90
+ }
91
+ j++;
92
+ }
93
+
94
+ const flattenedInner = flattenLayersAndSupports(innerContent);
95
+ result += flattenedInner;
96
+ i = j;
97
+ continue;
98
+ }
99
+ }
100
+
101
+ result += cssStr[i];
102
+ i++;
103
+ }
104
+
105
+ return result;
106
+ }
107
+
108
+ function convertRangeMediaQueries(cssStr) {
109
+ let result = cssStr.replace(
110
+ /\(\s*width\s*>=\s*([^)]+)\)/gi,
111
+ "(min-width: $1)",
112
+ );
113
+ result = result.replace(/\(\s*width\s*>\s*([^)]+)\)/gi, "(min-width: $1)");
114
+ result = result.replace(/\(\s*width\s*<=\s*([^)]+)\)/gi, "(max-width: $1)");
115
+ result = result.replace(/\(\s*width\s*<\s*([^)]+)\)/gi, "(max-width: $1)");
116
+ return result;
117
+ }
118
+
119
+ function simplifyDoubleSelectors(cssStr) {
120
+ return cssStr.replace(/(\.[a-z0-9_\\\-:]+)\1/gi, "$1");
121
+ }
122
+
123
+ function linearToSrgb(c) {
124
+ return c > 0.0031308 ? 1.055 * Math.pow(c, 1 / 2.4) - 0.055 : 12.92 * c;
125
+ }
126
+
127
+ function oklchToRgb(l, c, h) {
128
+ const hRad = (h * Math.PI) / 180;
129
+ const a = c * Math.cos(hRad);
130
+ const b = c * Math.sin(hRad);
131
+
132
+ const l_ = l + 0.3963377774 * a + 0.2158037573 * b;
133
+ const m_ = l - 0.1055613458 * a - 0.0638541728 * b;
134
+ const s_ = l - 0.0894841775 * a - 1.291485548 * b;
135
+
136
+ const l3 = l_ * l_ * l_;
137
+ const m3 = m_ * m_ * m_;
138
+ const s3 = s_ * s_ * s_;
139
+
140
+ let r = +4.0767416621 * l3 - 3.3077115913 * m3 + 0.2309699292 * s3;
141
+ let g = -1.2684380046 * l3 + 2.6097574011 * m3 - 0.3413193965 * s3;
142
+ let b_rgb = -0.0041960863 * l3 - 0.7034186147 * m3 + 1.707614701 * s3;
143
+
144
+ r = linearToSrgb(r);
145
+ g = linearToSrgb(g);
146
+ b_rgb = linearToSrgb(b_rgb);
147
+
148
+ r = Math.max(0, Math.min(255, Math.round(r * 255)));
149
+ g = Math.max(0, Math.min(255, Math.round(g * 255)));
150
+ b_rgb = Math.max(0, Math.min(255, Math.round(b_rgb * 255)));
151
+
152
+ return { r, g, b: b_rgb };
153
+ }
154
+
155
+ function convertOklchToRgbOrHex(cssStr) {
156
+ const oklchRegex =
157
+ /oklch\(\s*([\d.]+%?)\s+([\d.]+)\s+([\d.]+)(?:\s*\/\s*([\d.]+%?))?\s*\)/gi;
158
+
159
+ return cssStr.replace(oklchRegex, (match, lStr, cStr, hStr, aStr) => {
160
+ let l = parseFloat(lStr);
161
+ if (lStr.includes("%")) {
162
+ l = l / 100;
163
+ }
164
+ const c = parseFloat(cStr);
165
+ const h = parseFloat(hStr);
166
+
167
+ const { r, g, b } = oklchToRgb(l, c, h);
168
+
169
+ if (aStr) {
170
+ let a = parseFloat(aStr);
171
+ if (aStr.includes("%")) {
172
+ a = a / 100;
173
+ }
174
+ return `rgba(${r}, ${g}, ${b}, ${a})`;
175
+ }
176
+
177
+ return "#" + [r, g, b].map((x) => x.toString(16).padStart(2, "0")).join("");
178
+ });
179
+ }
180
+
181
+ function removeContainerClass(cssStr) {
182
+ const index = cssStr.indexOf(".container {");
183
+ if (index === -1) return cssStr;
184
+
185
+ let braceCount = 1;
186
+ let j = index + ".container {".length;
187
+ while (j < cssStr.length && braceCount > 0) {
188
+ if (cssStr[j] === "{") {
189
+ braceCount++;
190
+ } else if (cssStr[j] === "}") {
191
+ braceCount--;
192
+ }
193
+ j++;
194
+ }
195
+
196
+ return cssStr.substring(0, index) + cssStr.substring(j);
197
+ }
198
+
199
+ function extractPropertiesAndInjectToRoot(cssStr) {
200
+ const properties = {};
201
+ const propertyRegex = /@property\s+(--[a-zA-Z0-9_-]+)\s*\{([\s\S]*?)\}/g;
202
+ let match;
203
+ while ((match = propertyRegex.exec(cssStr)) !== null) {
204
+ const name = match[1];
205
+ const body = match[2];
206
+ const valMatch = body.match(/initial-value:\s*([^;]+);/);
207
+ if (valMatch) {
208
+ properties[name] = valMatch[1].trim();
209
+ }
210
+ }
211
+
212
+ const rootRegex = /(:root|:root\s*,\s*:host)\s*\{/g;
213
+ const rootMatch = rootRegex.exec(cssStr);
214
+ if (rootMatch) {
215
+ const insertIndex = rootMatch.index + rootMatch[0].length;
216
+ let injectedProps = "\n";
217
+ for (const [name, val] of Object.entries(properties)) {
218
+ injectedProps += ` ${name}: ${val};\n`;
219
+ }
220
+ return cssStr.substring(0, insertIndex) + injectedProps + cssStr.substring(insertIndex);
221
+ }
222
+
223
+ return cssStr;
224
+ }
225
+
226
+ function flattenNestedAmpersandRules(cssStr) {
227
+ let current = cssStr;
228
+ let prev;
229
+
230
+ const nestedRuleRe = /([^{}@][^{]*?)\s*\{\s*(&[^{]*?)\s*\{([^{}]*)\}\s*\}/g;
231
+
232
+ do {
233
+ prev = current;
234
+ current = current.replace(nestedRuleRe, (_match, parentSelector, childSelector, declarations) => {
235
+ const parent = parentSelector.trim();
236
+ const child = childSelector.trim();
237
+ const merged = child.replace(/&/g, parent);
238
+ return `${merged} {${declarations}}`;
239
+ });
240
+ } while (current !== prev);
241
+
242
+ return current;
243
+ }
244
+
245
+ export function preprocessTailwindCss(css) {
246
+ let preprocessedCss = extractPropertiesAndInjectToRoot(css);
247
+ preprocessedCss = flattenLayersAndSupports(preprocessedCss);
248
+ preprocessedCss = flattenNestedAmpersandRules(preprocessedCss);
249
+ preprocessedCss = removeContainerClass(preprocessedCss);
250
+ preprocessedCss = simplifyDoubleSelectors(preprocessedCss);
251
+ preprocessedCss = convertOklchToRgbOrHex(preprocessedCss);
252
+ preprocessedCss = convertRangeMediaQueries(preprocessedCss);
253
+ return preprocessedCss;
254
+ }
@@ -0,0 +1,3 @@
1
+ export function camelize(str) {
2
+ return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase());
3
+ }