@applicaster/zapp-react-native-utils 16.0.0-rc.22 → 16.0.0-rc.23

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.
@@ -1,7 +1,7 @@
1
1
  import * as R from "ramda";
2
2
  import { dayjs } from "../dateUtils";
3
- import validateColor from "validate-color";
4
3
 
4
+ import { isValidColor } from "@applicaster/zapp-react-native-utils/colorUtils";
5
5
  import { transformColorCode as fixColorHexCode } from "@applicaster/zapp-react-native-utils/transform";
6
6
  import {
7
7
  capitalize,
@@ -468,15 +468,13 @@ export const getColorFromData = ({
468
468
  data,
469
469
  valueFromLayout,
470
470
  }: GetColorFromData): string => {
471
- // Temporary hack to fix color validation when alpha is floating point number
472
- // https://github.com/dreamyguy/validate-color/issues/44
473
- if (validateColor(valueFromLayout.replace(".00", ""))) {
471
+ if (isValidColor(valueFromLayout)) {
474
472
  return valueFromLayout;
475
473
  }
476
474
 
477
475
  const pathValue = R.path(valueFromLayout.split("."), data);
478
476
 
479
- if (pathValue && validateColor(pathValue)) {
477
+ if (pathValue && isValidColor(pathValue)) {
480
478
  return pathValue;
481
479
  }
482
480
 
@@ -0,0 +1,70 @@
1
+ import { isValidColor } from "..";
2
+
3
+ describe("isValidColor", () => {
4
+ it("returns true for valid hex colors", () => {
5
+ const validHexColors = ["#fff", "#ffffff", "#FF0000", "#000", "#abc123"];
6
+
7
+ expect.assertions(validHexColors.length);
8
+
9
+ validHexColors.forEach((color) => {
10
+ expect(isValidColor(color)).toBe(true);
11
+ });
12
+ });
13
+
14
+ it("returns true for valid rgb and rgba colors", () => {
15
+ const validRgbColors = [
16
+ "rgb(255,0,0)",
17
+ "rgb(255, 0, 0)",
18
+ "rgba(255,0,0,0.5)",
19
+ "rgba(255, 0, 0, 0.5)",
20
+ ];
21
+
22
+ expect.assertions(validRgbColors.length);
23
+
24
+ validRgbColors.forEach((color) => {
25
+ expect(isValidColor(color)).toBe(true);
26
+ });
27
+ });
28
+
29
+ it("returns true for valid named and special CSS colors", () => {
30
+ const validNamedColors = ["red", "transparent", "currentColor", "inherit"];
31
+
32
+ expect.assertions(validNamedColors.length);
33
+
34
+ validNamedColors.forEach((color) => {
35
+ expect(isValidColor(color)).toBe(true);
36
+ });
37
+ });
38
+
39
+ it("returns true for valid hsl colors", () => {
40
+ expect(isValidColor("hsl(0, 100%, 50%)")).toBe(true);
41
+ });
42
+
43
+ it("returns false for invalid color strings", () => {
44
+ const invalidColors = [
45
+ "invalid",
46
+ "",
47
+ "#gggggg",
48
+ "#fff.00",
49
+ "unset",
50
+ undefined,
51
+ null,
52
+ ];
53
+
54
+ expect.assertions(invalidColors.length);
55
+
56
+ invalidColors.forEach((color) => {
57
+ expect(isValidColor(color)).toBe(false);
58
+ });
59
+ });
60
+
61
+ it("returns true for valid rgb and rgba colors", () => {
62
+ const validRgbColors = ["rgba(239,239,239,1.0)"];
63
+
64
+ expect.assertions(validRgbColors.length);
65
+
66
+ validRgbColors.forEach((color) => {
67
+ expect(isValidColor(color)).toBe(true);
68
+ });
69
+ });
70
+ });
@@ -0,0 +1,34 @@
1
+ import validateColor from "validate-color";
2
+ import { isNil } from "@applicaster/zapp-react-native-utils/utils";
3
+ import { isString } from "@applicaster/zapp-react-native-utils/stringUtils";
4
+
5
+ const normalizeRgbaAlpha = (color: string): string => {
6
+ return color.replace(
7
+ /^rgba\(([^)]+),\s*(\d+(?:\.\d+)?)\s*\)$/i,
8
+ (_, rgb, alpha) => {
9
+ const alphaValue = parseFloat(alpha);
10
+
11
+ if (!Number.isFinite(alphaValue) || !Number.isInteger(alphaValue)) {
12
+ return color;
13
+ }
14
+
15
+ return `rgba(${rgb},${alphaValue})`;
16
+ }
17
+ );
18
+ };
19
+
20
+ export const isValidColor = (color: string): boolean => {
21
+ if (isNil(color) || !isString(color)) {
22
+ return false;
23
+ }
24
+
25
+ if (validateColor(color)) {
26
+ return true;
27
+ }
28
+
29
+ // validate-color rejects integer alpha values written as floats (e.g. 1.0)
30
+ // https://github.com/dreamyguy/validate-color/issues/44
31
+ const normalizedColor = normalizeRgbaAlpha(color);
32
+
33
+ return normalizedColor !== color && validateColor(normalizedColor);
34
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "16.0.0-rc.22",
3
+ "version": "16.0.0-rc.23",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/applicaster/quickbrick#readme",
29
29
  "dependencies": {
30
- "@applicaster/applicaster-types": "16.0.0-rc.22",
30
+ "@applicaster/applicaster-types": "16.0.0-rc.23",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",
@@ -1,10 +1,20 @@
1
1
  import * as React from "react";
2
- import { NativeModules, StyleSheet, View } from "react-native";
2
+ import { NativeModules, View, StyleSheet } from "react-native";
3
3
  import { getXray } from "@applicaster/zapp-react-native-utils/logger";
4
4
 
5
- import { isApplePlatform, isWeb } from "../reactUtils";
5
+ import { isApplePlatform, isWeb, isTV } from "../reactUtils";
6
6
  import { useRivers } from "../reactHooks";
7
7
 
8
+ const isTVPlatform = isTV();
9
+
10
+ const styles = StyleSheet.create({
11
+ container: isTVPlatform
12
+ ? {
13
+ flex: 1,
14
+ }
15
+ : {},
16
+ });
17
+
8
18
  const layoutReducer = (state, { payload }) => {
9
19
  return state.map((item, index, _state) => ({
10
20
  height: index === payload.index ? payload.height : item.height,
@@ -30,12 +40,6 @@ type MeasurementContext = {
30
40
  onLayout: (index: number) => (event) => void;
31
41
  };
32
42
 
33
- const styles = StyleSheet.create({
34
- container: {
35
- flex: 1,
36
- },
37
- });
38
-
39
43
  const { Logger } = getXray();
40
44
 
41
45
  const logger = new Logger("general", "ui");
@@ -152,9 +156,5 @@ export const ScreenLoadingMeasurementsListItemWrapper = ({
152
156
  }: ItemProps) => {
153
157
  const { onLayout } = React.useContext(MeasurementsSettersContext);
154
158
 
155
- return (
156
- <View style={styles.container} onLayout={onLayout?.(index)}>
157
- {children}
158
- </View>
159
- );
159
+ return <View onLayout={onLayout?.(index)}>{children}</View>;
160
160
  };