react-native-css 0.0.0-nightly.0acc293 → 0.0.0-nightly.126d37e
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/README.md +5 -5
- package/dist/commonjs/compiler/compiler.js +49 -1
- package/dist/commonjs/compiler/compiler.js.map +1 -1
- package/dist/commonjs/compiler/declarations.js +51 -0
- package/dist/commonjs/compiler/declarations.js.map +1 -1
- package/dist/commonjs/compiler/lightningcss-loader.js +12 -2
- package/dist/commonjs/compiler/lightningcss-loader.js.map +1 -1
- package/dist/commonjs/compiler/stylesheet.js +5 -0
- package/dist/commonjs/compiler/stylesheet.js.map +1 -1
- package/dist/commonjs/compiler/supports.js +2 -2
- package/dist/commonjs/compiler/supports.js.map +1 -1
- package/dist/commonjs/components/SafeAreaProvider.js +13 -0
- package/dist/commonjs/components/SafeAreaProvider.js.map +1 -0
- package/dist/commonjs/native/react/useNativeCss.js +11 -5
- package/dist/commonjs/native/react/useNativeCss.js.map +1 -1
- package/dist/commonjs/native/styles/functions/color-mix.js +50 -0
- package/dist/commonjs/native/styles/functions/color-mix.js.map +1 -0
- package/dist/commonjs/native/styles/functions/index.js +11 -0
- package/dist/commonjs/native/styles/functions/index.js.map +1 -1
- package/dist/commonjs/native/styles/index.js +247 -19
- package/dist/commonjs/native/styles/index.js.map +1 -1
- package/dist/commonjs/native/styles/shorthands/_handler.js +10 -0
- package/dist/commonjs/native/styles/shorthands/_handler.js.map +1 -1
- package/dist/commonjs/native/styles/shorthands/box-shadow.js +45 -12
- package/dist/commonjs/native/styles/shorthands/box-shadow.js.map +1 -1
- package/dist/commonjs/web/api.js +1 -1
- package/dist/commonjs/web/api.js.map +1 -1
- package/dist/module/compiler/compiler.js +50 -2
- package/dist/module/compiler/compiler.js.map +1 -1
- package/dist/module/compiler/declarations.js +50 -0
- package/dist/module/compiler/declarations.js.map +1 -1
- package/dist/module/compiler/lightningcss-loader.js +12 -2
- package/dist/module/compiler/lightningcss-loader.js.map +1 -1
- package/dist/module/compiler/stylesheet.js +5 -0
- package/dist/module/compiler/stylesheet.js.map +1 -1
- package/dist/module/compiler/supports.js +2 -2
- package/dist/module/compiler/supports.js.map +1 -1
- package/dist/module/components/SafeAreaProvider.js +4 -0
- package/dist/module/components/SafeAreaProvider.js.map +1 -0
- package/dist/module/native/react/useNativeCss.js +11 -5
- package/dist/module/native/react/useNativeCss.js.map +1 -1
- package/dist/module/native/styles/functions/color-mix.js +45 -0
- package/dist/module/native/styles/functions/color-mix.js.map +1 -0
- package/dist/module/native/styles/functions/index.js +1 -0
- package/dist/module/native/styles/functions/index.js.map +1 -1
- package/dist/module/native/styles/index.js +248 -19
- package/dist/module/native/styles/index.js.map +1 -1
- package/dist/module/native/styles/shorthands/_handler.js +11 -1
- package/dist/module/native/styles/shorthands/_handler.js.map +1 -1
- package/dist/module/native/styles/shorthands/box-shadow.js +46 -12
- package/dist/module/native/styles/shorthands/box-shadow.js.map +1 -1
- package/dist/module/web/api.js +1 -1
- package/dist/module/web/api.js.map +1 -1
- package/dist/typescript/commonjs/.config/eslint.config.d.mts +1 -1
- package/dist/typescript/commonjs/example/src/App.d.ts.map +1 -1
- package/dist/typescript/commonjs/src/compiler/compiler.d.ts.map +1 -1
- package/dist/typescript/commonjs/src/compiler/declarations.d.ts +1 -0
- package/dist/typescript/commonjs/src/compiler/declarations.d.ts.map +1 -1
- package/dist/typescript/commonjs/src/compiler/lightningcss-loader.d.ts.map +1 -1
- package/dist/typescript/commonjs/src/compiler/stylesheet.d.ts +1 -0
- package/dist/typescript/commonjs/src/compiler/stylesheet.d.ts.map +1 -1
- package/dist/typescript/commonjs/src/components/SafeAreaProvider.d.ts +2 -0
- package/dist/typescript/commonjs/src/components/SafeAreaProvider.d.ts.map +1 -0
- package/dist/typescript/commonjs/src/native/react/useNativeCss.d.ts.map +1 -1
- package/dist/typescript/commonjs/src/native/styles/functions/color-mix.d.ts +3 -0
- package/dist/typescript/commonjs/src/native/styles/functions/color-mix.d.ts.map +1 -0
- package/dist/typescript/commonjs/src/native/styles/functions/index.d.ts +1 -0
- package/dist/typescript/commonjs/src/native/styles/functions/index.d.ts.map +1 -1
- package/dist/typescript/commonjs/src/native/styles/index.d.ts.map +1 -1
- package/dist/typescript/commonjs/src/native/styles/shorthands/_handler.d.ts.map +1 -1
- package/dist/typescript/commonjs/src/native/styles/shorthands/box-shadow.d.ts.map +1 -1
- package/dist/typescript/module/.config/eslint.config.d.mts +1 -1
- package/dist/typescript/module/example/src/App.d.ts.map +1 -1
- package/dist/typescript/module/src/compiler/compiler.d.ts.map +1 -1
- package/dist/typescript/module/src/compiler/declarations.d.ts +1 -0
- package/dist/typescript/module/src/compiler/declarations.d.ts.map +1 -1
- package/dist/typescript/module/src/compiler/lightningcss-loader.d.ts.map +1 -1
- package/dist/typescript/module/src/compiler/stylesheet.d.ts +1 -0
- package/dist/typescript/module/src/compiler/stylesheet.d.ts.map +1 -1
- package/dist/typescript/module/src/components/SafeAreaProvider.d.ts +2 -0
- package/dist/typescript/module/src/components/SafeAreaProvider.d.ts.map +1 -0
- package/dist/typescript/module/src/native/react/useNativeCss.d.ts.map +1 -1
- package/dist/typescript/module/src/native/styles/functions/color-mix.d.ts +3 -0
- package/dist/typescript/module/src/native/styles/functions/color-mix.d.ts.map +1 -0
- package/dist/typescript/module/src/native/styles/functions/index.d.ts +1 -0
- package/dist/typescript/module/src/native/styles/functions/index.d.ts.map +1 -1
- package/dist/typescript/module/src/native/styles/index.d.ts.map +1 -1
- package/dist/typescript/module/src/native/styles/shorthands/_handler.d.ts.map +1 -1
- package/dist/typescript/module/src/native/styles/shorthands/box-shadow.d.ts.map +1 -1
- package/package.json +17 -3
- package/src/compiler/compiler.ts +76 -2
- package/src/compiler/declarations.ts +96 -0
- package/src/compiler/lightningcss-loader.ts +20 -1
- package/src/compiler/stylesheet.ts +6 -0
- package/src/compiler/supports.ts +2 -2
- package/src/components/SafeAreaProvider.tsx +1 -0
- package/src/native/react/useNativeCss.ts +13 -6
- package/src/native/styles/functions/color-mix.ts +68 -0
- package/src/native/styles/functions/index.ts +1 -0
- package/src/native/styles/index.ts +302 -26
- package/src/native/styles/shorthands/_handler.ts +14 -1
- package/src/native/styles/shorthands/box-shadow.ts +50 -15
- package/src/web/api.tsx +1 -1
- package/types.d.ts +3 -0
package/src/compiler/compiler.ts
CHANGED
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
type MediaQuery as CSSMediaQuery,
|
|
8
8
|
type CustomAtRules,
|
|
9
9
|
type MediaRule,
|
|
10
|
+
type ParsedComponent,
|
|
11
|
+
type PropertyRule,
|
|
10
12
|
type Rule,
|
|
11
13
|
type Visitor,
|
|
12
14
|
} from "lightningcss";
|
|
@@ -15,11 +17,19 @@ import { maybeMutateReactNativeOptions, parsePropAtRule } from "./atRules";
|
|
|
15
17
|
import type {
|
|
16
18
|
CompilerOptions,
|
|
17
19
|
ContainerQuery,
|
|
20
|
+
StyleDescriptor,
|
|
18
21
|
StyleRuleMapping,
|
|
19
22
|
UniqueVarInfo,
|
|
20
23
|
} from "./compiler.types";
|
|
21
24
|
import { parseContainerCondition } from "./container-query";
|
|
22
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
parseAngle,
|
|
27
|
+
parseColor,
|
|
28
|
+
parseDeclaration,
|
|
29
|
+
parseLength,
|
|
30
|
+
reduceParseUnparsed,
|
|
31
|
+
round,
|
|
32
|
+
} from "./declarations";
|
|
23
33
|
import { inlineVariables } from "./inline-variables";
|
|
24
34
|
import { extractKeyFrames } from "./keyframes";
|
|
25
35
|
import { lightningcssLoader } from "./lightningcss-loader";
|
|
@@ -276,13 +286,15 @@ function extractRule(
|
|
|
276
286
|
}
|
|
277
287
|
}
|
|
278
288
|
break;
|
|
289
|
+
case "property":
|
|
290
|
+
extractPropertyRule(rule.value, builder);
|
|
291
|
+
break;
|
|
279
292
|
case "custom":
|
|
280
293
|
case "font-face":
|
|
281
294
|
case "font-palette-values":
|
|
282
295
|
case "font-feature-values":
|
|
283
296
|
case "namespace":
|
|
284
297
|
case "layer-statement":
|
|
285
|
-
case "property":
|
|
286
298
|
case "view-transition":
|
|
287
299
|
case "ignored":
|
|
288
300
|
case "unknown":
|
|
@@ -377,3 +389,65 @@ function extractContainer(
|
|
|
377
389
|
extractRule(rule, builder, mapping);
|
|
378
390
|
}
|
|
379
391
|
}
|
|
392
|
+
|
|
393
|
+
function extractPropertyRule(
|
|
394
|
+
propertyRule: PropertyRule,
|
|
395
|
+
builder: StylesheetBuilder,
|
|
396
|
+
) {
|
|
397
|
+
const { initialValue, name } = propertyRule;
|
|
398
|
+
|
|
399
|
+
if (initialValue == null) {
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const varName = name.startsWith("--") ? name.slice(2) : name;
|
|
404
|
+
const value = parsePropertyInitialValue(initialValue, builder);
|
|
405
|
+
|
|
406
|
+
if (value !== undefined) {
|
|
407
|
+
builder.addRootVariable(varName, value);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function parsePropertyInitialValue(
|
|
412
|
+
component: ParsedComponent,
|
|
413
|
+
builder: StylesheetBuilder,
|
|
414
|
+
): StyleDescriptor {
|
|
415
|
+
switch (component.type) {
|
|
416
|
+
case "length":
|
|
417
|
+
return parseLength(component.value, builder);
|
|
418
|
+
case "number":
|
|
419
|
+
case "integer":
|
|
420
|
+
return round(component.value);
|
|
421
|
+
case "percentage":
|
|
422
|
+
return `${round(component.value * 100)}%`;
|
|
423
|
+
case "color":
|
|
424
|
+
return parseColor(component.value, builder);
|
|
425
|
+
case "angle":
|
|
426
|
+
return parseAngle(component.value, builder);
|
|
427
|
+
case "length-percentage":
|
|
428
|
+
return parseLength(component.value, builder);
|
|
429
|
+
case "token-list":
|
|
430
|
+
return reduceParseUnparsed(
|
|
431
|
+
component.value,
|
|
432
|
+
builder,
|
|
433
|
+
"@property",
|
|
434
|
+
false,
|
|
435
|
+
);
|
|
436
|
+
case "custom-ident":
|
|
437
|
+
case "literal":
|
|
438
|
+
return component.value;
|
|
439
|
+
case "repeated": {
|
|
440
|
+
const results = component.value.components
|
|
441
|
+
.map((c) => parsePropertyInitialValue(c, builder))
|
|
442
|
+
.filter(
|
|
443
|
+
(v): v is NonNullable<StyleDescriptor> => v !== undefined,
|
|
444
|
+
);
|
|
445
|
+
// Unwrap single-child repeated values so downstream consumers get a
|
|
446
|
+
// scalar instead of a 1-element array. For example, `<length>+` with
|
|
447
|
+
// initial-value `10px` should produce the same shape as `<length>`.
|
|
448
|
+
return results.length === 1 ? results[0] : results;
|
|
449
|
+
}
|
|
450
|
+
default:
|
|
451
|
+
return undefined;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
@@ -1228,6 +1228,8 @@ export function parseUnparsed(
|
|
|
1228
1228
|
builder,
|
|
1229
1229
|
property,
|
|
1230
1230
|
);
|
|
1231
|
+
case "color-mix":
|
|
1232
|
+
return parseColorMix(tokenOrValue.value.arguments, builder, property);
|
|
1231
1233
|
default: {
|
|
1232
1234
|
builder.addWarning("value", `${tokenOrValue.value.name}()`);
|
|
1233
1235
|
return;
|
|
@@ -2633,6 +2635,100 @@ export function parseCalcFn(
|
|
|
2633
2635
|
return;
|
|
2634
2636
|
}
|
|
2635
2637
|
|
|
2638
|
+
export function parseColorMix(
|
|
2639
|
+
tokens: TokenOrValue[],
|
|
2640
|
+
builder: StylesheetBuilder,
|
|
2641
|
+
property: string,
|
|
2642
|
+
): StyleDescriptor {
|
|
2643
|
+
const [inToken, whitespace, colorSpace, comma, ...rest] = tokens;
|
|
2644
|
+
if (
|
|
2645
|
+
typeof inToken !== "object" ||
|
|
2646
|
+
inToken.type !== "token" ||
|
|
2647
|
+
inToken.value.type !== "ident" ||
|
|
2648
|
+
inToken.value.value !== "in"
|
|
2649
|
+
) {
|
|
2650
|
+
return;
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2653
|
+
if (
|
|
2654
|
+
typeof whitespace !== "object" ||
|
|
2655
|
+
whitespace.type !== "token" ||
|
|
2656
|
+
whitespace.value.type !== "white-space"
|
|
2657
|
+
) {
|
|
2658
|
+
return;
|
|
2659
|
+
}
|
|
2660
|
+
|
|
2661
|
+
if (
|
|
2662
|
+
typeof comma !== "object" ||
|
|
2663
|
+
comma.type !== "token" ||
|
|
2664
|
+
comma.value.type !== "comma"
|
|
2665
|
+
) {
|
|
2666
|
+
return;
|
|
2667
|
+
}
|
|
2668
|
+
|
|
2669
|
+
const colorSpaceArg = parseUnparsed(colorSpace, builder, property);
|
|
2670
|
+
if (typeof colorSpaceArg !== "string") {
|
|
2671
|
+
return;
|
|
2672
|
+
}
|
|
2673
|
+
|
|
2674
|
+
let nextToken = rest.shift();
|
|
2675
|
+
|
|
2676
|
+
const leftColorArg = parseUnparsed(nextToken, builder, property);
|
|
2677
|
+
|
|
2678
|
+
if (!leftColorArg) {
|
|
2679
|
+
return;
|
|
2680
|
+
}
|
|
2681
|
+
|
|
2682
|
+
nextToken = rest.shift();
|
|
2683
|
+
|
|
2684
|
+
let leftColorPercentage: StyleDescriptor | undefined;
|
|
2685
|
+
if (nextToken?.type !== "token" || nextToken.value.type !== "comma") {
|
|
2686
|
+
leftColorPercentage = parseUnparsed(nextToken, builder, property);
|
|
2687
|
+
nextToken = rest.shift();
|
|
2688
|
+
}
|
|
2689
|
+
|
|
2690
|
+
if (
|
|
2691
|
+
typeof nextToken !== "object" ||
|
|
2692
|
+
nextToken.type !== "token" ||
|
|
2693
|
+
nextToken.value.type !== "comma"
|
|
2694
|
+
) {
|
|
2695
|
+
return;
|
|
2696
|
+
}
|
|
2697
|
+
|
|
2698
|
+
nextToken = rest.shift();
|
|
2699
|
+
|
|
2700
|
+
const rightColorArg = parseUnparsed(nextToken, builder, property);
|
|
2701
|
+
|
|
2702
|
+
if (rightColorArg === "transparent") {
|
|
2703
|
+
// Ignore the rest, treat as single color with alpha
|
|
2704
|
+
return [{}, "colorMix", [colorSpaceArg, leftColorArg, leftColorPercentage]];
|
|
2705
|
+
}
|
|
2706
|
+
|
|
2707
|
+
nextToken = rest.shift();
|
|
2708
|
+
let rightColorPercentage: StyleDescriptor | undefined;
|
|
2709
|
+
if (nextToken?.type !== "token" || nextToken.value.type !== "comma") {
|
|
2710
|
+
rightColorPercentage = parseUnparsed(nextToken, builder, property);
|
|
2711
|
+
nextToken = rest.shift();
|
|
2712
|
+
}
|
|
2713
|
+
|
|
2714
|
+
// We should have expired all tokens now
|
|
2715
|
+
if (nextToken) {
|
|
2716
|
+
return;
|
|
2717
|
+
}
|
|
2718
|
+
|
|
2719
|
+
return [
|
|
2720
|
+
{},
|
|
2721
|
+
"colorMix",
|
|
2722
|
+
[
|
|
2723
|
+
colorSpaceArg,
|
|
2724
|
+
leftColorArg,
|
|
2725
|
+
leftColorPercentage,
|
|
2726
|
+
rightColorArg,
|
|
2727
|
+
rightColorPercentage,
|
|
2728
|
+
],
|
|
2729
|
+
];
|
|
2730
|
+
}
|
|
2731
|
+
|
|
2636
2732
|
export function parseCalcArguments(
|
|
2637
2733
|
[...args]: TokenOrValue[],
|
|
2638
2734
|
builder: StylesheetBuilder,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
1
2
|
export function lightningcssLoader() {
|
|
2
3
|
let lightningcssPath: string | undefined;
|
|
3
4
|
|
|
@@ -28,11 +29,29 @@ export function lightningcssLoader() {
|
|
|
28
29
|
);
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
32
32
|
const { transform: lightningcss, Features } = require(
|
|
33
33
|
lightningcssPath,
|
|
34
34
|
) as typeof import("lightningcss");
|
|
35
35
|
|
|
36
|
+
try {
|
|
37
|
+
const lightningcssPackageJSONPath = require.resolve("../../package.json", {
|
|
38
|
+
paths: [lightningcssPath],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const packageJSON = require(lightningcssPackageJSONPath) as Record<
|
|
42
|
+
string,
|
|
43
|
+
unknown
|
|
44
|
+
>;
|
|
45
|
+
|
|
46
|
+
if (packageJSON.version === "1.30.2") {
|
|
47
|
+
throw new Error(
|
|
48
|
+
"[react-native-css] lightningcss version 1.30.2 has a critical bug that breaks compilation. Please pin the version of lightningcss to 1.30.1; or try upgrading.",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
} catch {
|
|
52
|
+
// Intentionally left empty
|
|
53
|
+
}
|
|
54
|
+
|
|
36
55
|
return {
|
|
37
56
|
lightningcss,
|
|
38
57
|
Features,
|
|
@@ -577,6 +577,12 @@ export class StylesheetBuilder {
|
|
|
577
577
|
this.ruleTemplate.cq.push(query);
|
|
578
578
|
}
|
|
579
579
|
|
|
580
|
+
addRootVariable(name: string, value: StyleDescriptor) {
|
|
581
|
+
this.shared.rootVariables ??= {};
|
|
582
|
+
this.shared.rootVariables[name] ??= [];
|
|
583
|
+
this.shared.rootVariables[name].push([value]);
|
|
584
|
+
}
|
|
585
|
+
|
|
580
586
|
newAnimationFrames(name: string) {
|
|
581
587
|
this.shared.animations ??= {};
|
|
582
588
|
|
package/src/compiler/supports.ts
CHANGED
|
@@ -21,6 +21,6 @@ export function supportsConditionValid(condition: SupportsCondition): boolean {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
const declarations: Record<string, string[]> = {
|
|
24
|
-
//
|
|
25
|
-
|
|
24
|
+
// Special text used by TailwindCSS. We should probably change this to all color-mix
|
|
25
|
+
color: ["color-mix(in lab, red, red)"],
|
|
26
26
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SafeAreaProvider } from "react-native-css/components/react-native-safe-area-context";
|
|
@@ -172,29 +172,36 @@ export function useNativeCss(
|
|
|
172
172
|
export function mappingToConfig(mapping: StyledConfiguration<any>) {
|
|
173
173
|
return Object.entries(mapping).flatMap(([key, value]): Config => {
|
|
174
174
|
if (value === true) {
|
|
175
|
-
return {
|
|
175
|
+
return {
|
|
176
|
+
source: key,
|
|
177
|
+
target: key,
|
|
178
|
+
};
|
|
176
179
|
} else if (value === false) {
|
|
177
180
|
return { source: key, target: false };
|
|
178
181
|
} else if (typeof value === "string") {
|
|
179
182
|
return { source: key, target: value.split(".") };
|
|
180
183
|
} else if (typeof value === "object") {
|
|
184
|
+
const nativeStyleMapping = value.nativeStyleMapping as
|
|
185
|
+
| Record<string, string>
|
|
186
|
+
| undefined;
|
|
187
|
+
|
|
181
188
|
if (Array.isArray(value)) {
|
|
182
|
-
return { source: key, target: value };
|
|
189
|
+
return { source: key, target: value, nativeStyleMapping };
|
|
183
190
|
}
|
|
184
191
|
|
|
185
192
|
if ("target" in value) {
|
|
186
193
|
if (value.target === false) {
|
|
187
|
-
return { source: key, target: false };
|
|
194
|
+
return { source: key, target: false, nativeStyleMapping };
|
|
188
195
|
} else if (typeof value.target === "string") {
|
|
189
196
|
const target = value.target.split(".");
|
|
190
197
|
|
|
191
198
|
if (target.length === 1) {
|
|
192
|
-
return { source: key, target: target[0]
|
|
199
|
+
return { source: key, target: target[0]!, nativeStyleMapping };
|
|
193
200
|
} else {
|
|
194
|
-
return { source: key, target };
|
|
201
|
+
return { source: key, target, nativeStyleMapping };
|
|
195
202
|
}
|
|
196
203
|
} else if (Array.isArray(value.target)) {
|
|
197
|
-
return { source: key, target: value.target };
|
|
204
|
+
return { source: key, target: value.target, nativeStyleMapping };
|
|
198
205
|
}
|
|
199
206
|
}
|
|
200
207
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
2
|
+
import type { PlainColorObject } from "colorjs.io";
|
|
3
|
+
import {
|
|
4
|
+
ColorSpace,
|
|
5
|
+
to as convert,
|
|
6
|
+
mix,
|
|
7
|
+
OKLab,
|
|
8
|
+
P3,
|
|
9
|
+
parse,
|
|
10
|
+
sRGB,
|
|
11
|
+
type ColorConstructor,
|
|
12
|
+
} from "colorjs.io/fn";
|
|
13
|
+
|
|
14
|
+
import type { StyleFunctionResolver } from "../resolve";
|
|
15
|
+
|
|
16
|
+
ColorSpace.register(sRGB);
|
|
17
|
+
ColorSpace.register(P3);
|
|
18
|
+
ColorSpace.register(OKLab);
|
|
19
|
+
|
|
20
|
+
export const colorMix: StyleFunctionResolver = (resolveValue, value) => {
|
|
21
|
+
const args = resolveValue(value[2]);
|
|
22
|
+
|
|
23
|
+
if (!Array.isArray(args) || args.length < 3) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const space = args.shift();
|
|
29
|
+
|
|
30
|
+
let left: ColorConstructor | PlainColorObject = parse(
|
|
31
|
+
args.shift() as string,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
let next = args.shift();
|
|
35
|
+
|
|
36
|
+
if (typeof next === "string" && next.endsWith("%")) {
|
|
37
|
+
left.alpha = parseFloat(next) / 100;
|
|
38
|
+
next = args.shift();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (next === undefined) {
|
|
42
|
+
if (left.spaceId !== "srgb") {
|
|
43
|
+
left = convert(left, "srgb");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return `rgba(${(left.coords[0] ?? 0) * 255}, ${(left.coords[1] ?? 0) * 255}, ${(left.coords[2] ?? 0) * 255}, ${left.alpha})`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (typeof next !== "string") {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const right = parse(next);
|
|
53
|
+
|
|
54
|
+
next = args.shift();
|
|
55
|
+
if (next && typeof next === "string" && next.endsWith("%")) {
|
|
56
|
+
right.alpha = parseFloat(next) / 100;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const result = mix(left, right, {
|
|
60
|
+
space,
|
|
61
|
+
outputSpace: "srgb",
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return `rgba(${(result.coords[0] ?? 0) * 255}, ${(result.coords[1] ?? 0) * 255}, ${(result.coords[2] ?? 0) * 255}, ${result.alpha})`;
|
|
65
|
+
} catch {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
};
|