@cssxio/react-native 0.3.4 → 0.5.0

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.
package/dist/babel.cjs CHANGED
@@ -24,8 +24,114 @@ __export(babel_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(babel_exports);
26
26
 
27
- // src/index.ts
27
+ // src/create.ts
28
+ var import_compiler2 = require("@cssxio/compiler");
29
+
30
+ // src/compile-candidates.ts
28
31
  var import_compiler = require("@cssxio/compiler");
32
+
33
+ // src/oklch-to-srgb-hex.ts
34
+ function oklchToSrgbHex(value) {
35
+ const match = /^oklch\(([\d.]+)%(?:\s|_)+([\d.]+)(?:\s|_)+([\d.]+)\)$/.exec(value);
36
+ if (!match) {
37
+ return null;
38
+ }
39
+ const lightness = Number(match[1]) / 100;
40
+ const chroma = Number(match[2]);
41
+ const hue = Number(match[3]) * Math.PI / 180;
42
+ const a = chroma * Math.cos(hue);
43
+ const b = chroma * Math.sin(hue);
44
+ const l = (lightness + 0.3963377774 * a + 0.2158037574 * b) ** 3;
45
+ const m = (lightness - 0.1055613458 * a - 0.0638541728 * b) ** 3;
46
+ const s = (lightness - 0.0894841775 * a - 1.291485548 * b) ** 3;
47
+ const channels = [
48
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
49
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
50
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s
51
+ ];
52
+ return `#${channels.map((channel) => {
53
+ const bounded = Math.max(0, Math.min(1, channel));
54
+ const srgb = bounded <= 31308e-7 ? 12.92 * bounded : 1.055 * bounded ** (1 / 2.4) - 0.055;
55
+ return Math.round(srgb * 255).toString(16).padStart(2, "0");
56
+ }).join("")}`;
57
+ }
58
+
59
+ // src/native-values.ts
60
+ function nativeValue(value, candidate) {
61
+ const important = value.endsWith(" !important") ? value.slice(0, -11) : value;
62
+ const calculation = /^calc\((-?[\d.]+)rem \* (-?[\d.]+)\)$/.exec(important);
63
+ if (calculation) {
64
+ return Number(calculation[1]) * 16 * Number(calculation[2]);
65
+ }
66
+ const rem = /^(-?[\d.]+)rem$/.exec(important);
67
+ if (rem) {
68
+ return Number(rem[1]) * 16;
69
+ }
70
+ const pixels = /^(-?[\d.]+)px$/.exec(important);
71
+ if (pixels) {
72
+ return Number(pixels[1]);
73
+ }
74
+ if (/^-?[\d.]+$/.test(important)) {
75
+ return Number(important);
76
+ }
77
+ if (/^(?:#|rgb|rgba|hsl|hsla|hwb|transparent|black|white)/.test(important) || important.endsWith("%")) {
78
+ return important;
79
+ }
80
+ const oklch = oklchToSrgbHex(important);
81
+ if (oklch) {
82
+ return oklch;
83
+ }
84
+ if (important.startsWith("var(") || important.startsWith("color-mix(") || important.startsWith("oklch(")) {
85
+ throw new Error(`CSSX React Native requires "${candidate}" to resolve to a native color or value at build time.`);
86
+ }
87
+ return important;
88
+ }
89
+
90
+ // src/append-custom-transform.ts
91
+ function appendCustomTransform(style, property, value, candidate) {
92
+ const transformProperty = {
93
+ "--cssx-rotate": "rotate",
94
+ "--cssx-scale-x": "scaleX",
95
+ "--cssx-scale-y": "scaleY",
96
+ "--cssx-translate-x": "translateX",
97
+ "--cssx-translate-y": "translateY"
98
+ };
99
+ const nativeProperty = transformProperty[property];
100
+ if (!nativeProperty) {
101
+ return false;
102
+ }
103
+ style.transform = [
104
+ ...style.transform ?? [],
105
+ { [nativeProperty]: nativeValue(value, candidate) }
106
+ ];
107
+ return true;
108
+ }
109
+
110
+ // src/append-transform.ts
111
+ function appendTransform(style, property, value) {
112
+ if ((property === "translate" || property === "rotate" || property === "scale") && value.includes("var(")) {
113
+ return true;
114
+ }
115
+ let transforms;
116
+ if (property === "translate") {
117
+ const x = value.split(" ", 1).join("");
118
+ const [y = "0"] = value.split(" ").slice(1);
119
+ transforms = [{ translateX: nativeValue(x, "translate") }, { translateY: nativeValue(y, "translate") }];
120
+ } else if (property === "rotate") {
121
+ transforms = [{ rotate: value }];
122
+ } else if (property === "scale") {
123
+ const x = value.split(" ", 1).join("");
124
+ const [y = x] = value.split(" ").slice(1);
125
+ transforms = [{ scaleX: Number(x) }, { scaleY: Number(y) }];
126
+ }
127
+ if (!transforms) {
128
+ return false;
129
+ }
130
+ style.transform = [...style.transform ?? [], ...transforms];
131
+ return true;
132
+ }
133
+
134
+ // src/native-properties.ts
29
135
  var propertyNames = {
30
136
  "align-content": "alignContent",
31
137
  "align-items": "alignItems",
@@ -98,42 +204,8 @@ var logicalProperties = {
98
204
  "padding-block": ["paddingTop", "paddingBottom"],
99
205
  "padding-inline": ["paddingLeft", "paddingRight"]
100
206
  };
101
- function create(styles, options = {}) {
102
- const records = (0, import_compiler.compileStyleRecords)(styles, { theme: options.theme });
103
- const theme = (0, import_compiler.parseTheme)(options.theme);
104
- return Object.fromEntries(
105
- Object.entries(records.candidates).map(([name, candidates]) => [
106
- name,
107
- { $$cssx: 3, style: compileCandidates(candidates, theme, options.platform) }
108
- ])
109
- );
110
- }
111
- function compileCandidates(candidates, theme, platform) {
112
- const style = {};
113
- for (const source of candidates) {
114
- const candidate = platformCandidate(source, platform);
115
- if (candidate === null) {
116
- continue;
117
- }
118
- const recipe = (0, import_compiler.describeUtilityRecipe)(candidate, theme);
119
- for (const atom of recipe.atoms) {
120
- for (const declaration of atom) {
121
- appendDeclaration(style, candidate, declaration);
122
- }
123
- }
124
- }
125
- return style;
126
- }
127
- function platformCandidate(source, platform) {
128
- const match = /^(ios|android):(.*)$/.exec(source);
129
- if (match) {
130
- return match[1] === platform ? match[2] : null;
131
- }
132
- if (!source.startsWith("[") && source.includes(":")) {
133
- throw new Error(`CSSX React Native cannot represent the variant in "${source}".`);
134
- }
135
- return source;
136
- }
207
+
208
+ // src/append-declaration.ts
137
209
  function appendDeclaration(style, candidate, declaration) {
138
210
  if (declaration.selectorSuffix || declaration.atRule) {
139
211
  throw new Error(`CSSX React Native cannot represent browser-only utility "${candidate}".`);
@@ -165,128 +237,51 @@ function appendDeclaration(style, candidate, declaration) {
165
237
  if (property === "overflow" && value !== "visible" && value !== "hidden") {
166
238
  throw new Error(`CSSX React Native cannot represent overflow value "${String(value)}" from "${candidate}".`);
167
239
  }
168
- if (property === "flex" && typeof value === "string") {
169
- style[property] = Number.parseFloat(value);
170
- return;
171
- }
172
- style[property] = value;
173
- }
174
- function appendTransform(style, property, value) {
175
- if ((property === "translate" || property === "rotate" || property === "scale") && value.includes("var(")) {
176
- return true;
177
- }
178
- let transforms;
179
- if (property === "translate") {
180
- const [x = "0", y = "0"] = value.split(" ");
181
- transforms = [{ translateX: nativeValue(x, "translate") }, { translateY: nativeValue(y, "translate") }];
182
- } else if (property === "rotate") {
183
- transforms = [{ rotate: value }];
184
- } else if (property === "scale") {
185
- const [x = "1", y = x] = value.split(" ");
186
- transforms = [{ scaleX: Number(x) }, { scaleY: Number(y) }];
187
- }
188
- if (!transforms) {
189
- return false;
190
- }
191
- style.transform = [...style.transform ?? [], ...transforms];
192
- return true;
240
+ style[property] = property === "flex" && typeof value === "string" ? Number.parseFloat(value) : value;
193
241
  }
194
- function appendCustomTransform(style, property, value, candidate) {
195
- const transformProperty = {
196
- "--cssx-rotate": "rotate",
197
- "--cssx-scale-x": "scaleX",
198
- "--cssx-scale-y": "scaleY",
199
- "--cssx-translate-x": "translateX",
200
- "--cssx-translate-y": "translateY"
201
- };
202
- const nativeProperty = transformProperty[property];
203
- if (!nativeProperty) {
204
- return false;
205
- }
206
- const native = nativeValue(value, candidate);
207
- style.transform = [
208
- ...style.transform ?? [],
209
- { [nativeProperty]: native }
210
- ];
211
- return true;
212
- }
213
- function nativeValue(value, candidate) {
214
- const important = value.endsWith(" !important") ? value.slice(0, -11) : value;
215
- const calculation = /^calc\((-?[\d.]+)rem \* (-?[\d.]+)\)$/.exec(important);
216
- if (calculation) {
217
- return Number(calculation[1]) * 16 * Number(calculation[2]);
218
- }
219
- const rem = /^(-?[\d.]+)rem$/.exec(important);
220
- if (rem) {
221
- return Number(rem[1]) * 16;
222
- }
223
- const pixels = /^(-?[\d.]+)px$/.exec(important);
224
- if (pixels) {
225
- return Number(pixels[1]);
226
- }
227
- if (/^-?[\d.]+$/.test(important)) {
228
- return Number(important);
229
- }
230
- if (/^(?:#|rgb|rgba|hsl|hsla|hwb|transparent|black|white)/.test(important) || important.endsWith("%")) {
231
- return important;
232
- }
233
- const oklch = oklchToSrgbHex(important);
234
- if (oklch) {
235
- return oklch;
236
- }
237
- if (important.startsWith("var(") || important.startsWith("color-mix(") || important.startsWith("oklch(")) {
238
- throw new Error(`CSSX React Native requires "${candidate}" to resolve to a native color or value at build time.`);
242
+
243
+ // src/platform-candidate.ts
244
+ function platformCandidate(source, platform) {
245
+ const match = /^(ios|android):(.*)$/.exec(source);
246
+ if (match) {
247
+ return match[1] === platform ? match[2] : null;
239
248
  }
240
- return important;
241
- }
242
- function oklchToSrgbHex(value) {
243
- const match = /^oklch\(([\d.]+)%(?:\s|_)+([\d.]+)(?:\s|_)+([\d.]+)\)$/.exec(value);
244
- if (!match) {
245
- return null;
249
+ if (!source.startsWith("[") && source.includes(":")) {
250
+ throw new Error(`CSSX React Native cannot represent the variant in "${source}".`);
246
251
  }
247
- const lightness = Number(match[1]) / 100;
248
- const chroma = Number(match[2]);
249
- const hue = Number(match[3]) * Math.PI / 180;
250
- const a = chroma * Math.cos(hue);
251
- const b = chroma * Math.sin(hue);
252
- const l = (lightness + 0.3963377774 * a + 0.2158037573 * b) ** 3;
253
- const m = (lightness - 0.1055613458 * a - 0.0638541728 * b) ** 3;
254
- const s = (lightness - 0.0894841775 * a - 1.291485548 * b) ** 3;
255
- const channels = [
256
- 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
257
- -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
258
- -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s
259
- ];
260
- const hex = channels.map((channel) => {
261
- const bounded = Math.max(0, Math.min(1, channel));
262
- const srgb = bounded <= 31308e-7 ? 12.92 * bounded : 1.055 * bounded ** (1 / 2.4) - 0.055;
263
- return Math.round(srgb * 255).toString(16).padStart(2, "0");
264
- }).join("");
265
- return `#${hex}`;
252
+ return source;
266
253
  }
267
254
 
268
- // src/babel.ts
269
- function cssxReactNativeBabelPlugin(api, options = {}) {
270
- api.assertVersion(7);
271
- const types = api.types;
272
- const importSource = options.importSource ?? "@cssxio/react-native";
273
- return {
274
- name: "@cssxio/react-native/babel",
275
- visitor: {
276
- CallExpression(path) {
277
- if (!isCreateCall(path, importSource, types)) {
278
- return;
279
- }
280
- const argument = path.node.arguments[0];
281
- if (path.node.arguments.length !== 1 || !types.isObjectExpression(argument)) {
282
- throw path.buildCodeFrameError("cssx.create() expects one object literal argument.");
283
- }
284
- const input = readStyleMap(argument, types, path);
285
- path.replaceWith(types.valueToNode(create(input, options)));
255
+ // src/compile-candidates.ts
256
+ function compileCandidates(candidates, theme, platform) {
257
+ const style = {};
258
+ for (const source of candidates) {
259
+ const candidate = platformCandidate(source, platform);
260
+ if (candidate === null) {
261
+ continue;
262
+ }
263
+ for (const atom of (0, import_compiler.describeUtilityRecipe)(candidate, theme).atoms) {
264
+ for (const declaration of atom) {
265
+ appendDeclaration(style, candidate, declaration);
286
266
  }
287
267
  }
288
- };
268
+ }
269
+ return style;
270
+ }
271
+
272
+ // src/create.ts
273
+ function create(styles, options = {}) {
274
+ const records = (0, import_compiler2.compileStyleRecords)(styles, { theme: options.theme });
275
+ const theme = (0, import_compiler2.parseTheme)(options.theme);
276
+ return Object.fromEntries(
277
+ Object.entries(records.candidates).map(([name, candidates]) => [
278
+ name,
279
+ { $$cssx: 3, style: compileCandidates(candidates, theme, options.platform) }
280
+ ])
281
+ );
289
282
  }
283
+
284
+ // src/is-create-call.ts
290
285
  function isCreateCall(path, importSource, types) {
291
286
  if (!types.isIdentifier(path.node.callee)) {
292
287
  return false;
@@ -297,6 +292,8 @@ function isCreateCall(path, importSource, types) {
297
292
  binding?.path.isImportSpecifier() && types.isIdentifier(binding.path.node.imported, { name: "create" }) && types.isImportDeclaration(declaration) && declaration.source.value === importSource
298
293
  );
299
294
  }
295
+
296
+ // src/read-style-map.ts
300
297
  function readStyleMap(object, types, path) {
301
298
  const input = {};
302
299
  for (const property of object.properties) {
@@ -314,3 +311,26 @@ function readStyleMap(object, types, path) {
314
311
  }
315
312
  return input;
316
313
  }
314
+
315
+ // src/babel.ts
316
+ function cssxReactNativeBabelPlugin(api, options = {}) {
317
+ api.assertVersion(7);
318
+ const types = api.types;
319
+ const importSource = options.importSource ?? "@cssxio/react-native";
320
+ return {
321
+ name: "@cssxio/react-native/babel",
322
+ visitor: {
323
+ CallExpression(path) {
324
+ if (!isCreateCall(path, importSource, types)) {
325
+ return;
326
+ }
327
+ const argument = path.node.arguments[0];
328
+ if (path.node.arguments.length !== 1 || !types.isObjectExpression(argument)) {
329
+ throw path.buildCodeFrameError("cssx.create() expects one object literal argument.");
330
+ }
331
+ const input = readStyleMap(argument, types, path);
332
+ path.replaceWith(types.valueToNode(create(input, options)));
333
+ }
334
+ }
335
+ };
336
+ }
package/dist/babel.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { PluginObj, PluginPass } from '@babel/core';
2
2
  import * as BabelTypes from '@babel/types';
3
- import { NativeCompilerOptions } from './index.cjs';
3
+ import { N as NativeCompilerOptions } from './native-types-Cv9_KYf7.cjs';
4
4
 
5
5
  interface NativeBabelOptions extends NativeCompilerOptions {
6
6
  readonly importSource?: string;
package/dist/babel.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { PluginObj, PluginPass } from '@babel/core';
2
2
  import * as BabelTypes from '@babel/types';
3
- import { NativeCompilerOptions } from './index.js';
3
+ import { N as NativeCompilerOptions } from './native-types-Cv9_KYf7.js';
4
4
 
5
5
  interface NativeBabelOptions extends NativeCompilerOptions {
6
6
  readonly importSource?: string;
package/dist/babel.js CHANGED
@@ -1,29 +1,8 @@
1
1
  import {
2
2
  create
3
- } from "./chunk-7REDTOKV.js";
3
+ } from "./chunk-5ETB3X4A.js";
4
4
 
5
- // src/babel.ts
6
- function cssxReactNativeBabelPlugin(api, options = {}) {
7
- api.assertVersion(7);
8
- const types = api.types;
9
- const importSource = options.importSource ?? "@cssxio/react-native";
10
- return {
11
- name: "@cssxio/react-native/babel",
12
- visitor: {
13
- CallExpression(path) {
14
- if (!isCreateCall(path, importSource, types)) {
15
- return;
16
- }
17
- const argument = path.node.arguments[0];
18
- if (path.node.arguments.length !== 1 || !types.isObjectExpression(argument)) {
19
- throw path.buildCodeFrameError("cssx.create() expects one object literal argument.");
20
- }
21
- const input = readStyleMap(argument, types, path);
22
- path.replaceWith(types.valueToNode(create(input, options)));
23
- }
24
- }
25
- };
26
- }
5
+ // src/is-create-call.ts
27
6
  function isCreateCall(path, importSource, types) {
28
7
  if (!types.isIdentifier(path.node.callee)) {
29
8
  return false;
@@ -34,6 +13,8 @@ function isCreateCall(path, importSource, types) {
34
13
  binding?.path.isImportSpecifier() && types.isIdentifier(binding.path.node.imported, { name: "create" }) && types.isImportDeclaration(declaration) && declaration.source.value === importSource
35
14
  );
36
15
  }
16
+
17
+ // src/read-style-map.ts
37
18
  function readStyleMap(object, types, path) {
38
19
  const input = {};
39
20
  for (const property of object.properties) {
@@ -51,6 +32,29 @@ function readStyleMap(object, types, path) {
51
32
  }
52
33
  return input;
53
34
  }
35
+
36
+ // src/babel.ts
37
+ function cssxReactNativeBabelPlugin(api, options = {}) {
38
+ api.assertVersion(7);
39
+ const types = api.types;
40
+ const importSource = options.importSource ?? "@cssxio/react-native";
41
+ return {
42
+ name: "@cssxio/react-native/babel",
43
+ visitor: {
44
+ CallExpression(path) {
45
+ if (!isCreateCall(path, importSource, types)) {
46
+ return;
47
+ }
48
+ const argument = path.node.arguments[0];
49
+ if (path.node.arguments.length !== 1 || !types.isObjectExpression(argument)) {
50
+ throw path.buildCodeFrameError("cssx.create() expects one object literal argument.");
51
+ }
52
+ const input = readStyleMap(argument, types, path);
53
+ path.replaceWith(types.valueToNode(create(input, options)));
54
+ }
55
+ }
56
+ };
57
+ }
54
58
  export {
55
59
  cssxReactNativeBabelPlugin as default
56
60
  };