@amazon-devices/expo-linear-gradient 2.0.1758683737

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.
Files changed (69) hide show
  1. package/.eslintrc.js +2 -0
  2. package/CHANGELOG.md +209 -0
  3. package/CMakeLists.txt +52 -0
  4. package/LICENSE +7 -0
  5. package/README.kepler.md +69 -0
  6. package/README.md +41 -0
  7. package/THIRD_PARTY_NOTICES.txt +24 -0
  8. package/android/build.gradle +107 -0
  9. package/android/src/main/AndroidManifest.xml +3 -0
  10. package/android/src/main/java/expo/modules/lineargradient/LinearGradientModule.kt +36 -0
  11. package/android/src/main/java/expo/modules/lineargradient/LinearGradientView.java +117 -0
  12. package/build/LinearGradient.d.ts +60 -0
  13. package/build/LinearGradient.d.ts.map +1 -0
  14. package/build/LinearGradient.js +31 -0
  15. package/build/LinearGradient.js.map +1 -0
  16. package/build/NativeLinearGradient.android.d.ts +4 -0
  17. package/build/NativeLinearGradient.android.d.ts.map +1 -0
  18. package/build/NativeLinearGradient.android.js +27 -0
  19. package/build/NativeLinearGradient.android.js.map +1 -0
  20. package/build/NativeLinearGradient.d.ts +4 -0
  21. package/build/NativeLinearGradient.d.ts.map +1 -0
  22. package/build/NativeLinearGradient.ios.d.ts +5 -0
  23. package/build/NativeLinearGradient.ios.d.ts.map +1 -0
  24. package/build/NativeLinearGradient.ios.js +4 -0
  25. package/build/NativeLinearGradient.ios.js.map +1 -0
  26. package/build/NativeLinearGradient.js +10 -0
  27. package/build/NativeLinearGradient.js.map +1 -0
  28. package/build/NativeLinearGradient.kepler.d.ts +23 -0
  29. package/build/NativeLinearGradient.kepler.d.ts.map +1 -0
  30. package/build/NativeLinearGradient.kepler.js +23 -0
  31. package/build/NativeLinearGradient.kepler.js.map +1 -0
  32. package/build/NativeLinearGradient.types.d.ts +11 -0
  33. package/build/NativeLinearGradient.types.d.ts.map +1 -0
  34. package/build/NativeLinearGradient.types.js +2 -0
  35. package/build/NativeLinearGradient.types.js.map +1 -0
  36. package/build/NativeLinearGradient.web.d.ts +8 -0
  37. package/build/NativeLinearGradient.web.d.ts.map +1 -0
  38. package/build/NativeLinearGradient.web.js +79 -0
  39. package/build/NativeLinearGradient.web.js.map +1 -0
  40. package/build/normalizeColor.d.ts +2 -0
  41. package/build/normalizeColor.d.ts.map +1 -0
  42. package/build/normalizeColor.js +29 -0
  43. package/build/normalizeColor.js.map +1 -0
  44. package/expo-module.config.json +10 -0
  45. package/ios/ExpoLinearGradient.podspec +32 -0
  46. package/ios/LinearGradientLayer.swift +107 -0
  47. package/ios/LinearGradientModule.swift +28 -0
  48. package/ios/LinearGradientView.swift +14 -0
  49. package/kepler/ExpoLinearGradientDrawableLayer.cpp +85 -0
  50. package/kepler/ExpoLinearGradientDrawableLayer.h +81 -0
  51. package/kepler/ExpoLinearGradientView.cpp +92 -0
  52. package/kepler/ExpoLinearGradientView.h +37 -0
  53. package/kepler/ExpoLinearGradientViewManager.cpp +28 -0
  54. package/kepler/ExpoLinearGradientViewManager.h +19 -0
  55. package/kepler/KeplerInit.cpp +28 -0
  56. package/kepler/test/ExpoLinearGradientDrawableLayer.cpp +134 -0
  57. package/kepler/test/ExpoLinearGradientView.cpp +86 -0
  58. package/kepler/test/ExpoLinearGradientViewManager.cpp +37 -0
  59. package/package.json +1 -0
  60. package/react-native.config.js +21 -0
  61. package/src/LinearGradient.tsx +103 -0
  62. package/src/NativeLinearGradient.android.tsx +49 -0
  63. package/src/NativeLinearGradient.ios.tsx +10 -0
  64. package/src/NativeLinearGradient.kepler.tsx +36 -0
  65. package/src/NativeLinearGradient.tsx +12 -0
  66. package/src/NativeLinearGradient.types.ts +21 -0
  67. package/src/NativeLinearGradient.web.tsx +115 -0
  68. package/src/normalizeColor.ts +32 -0
  69. package/tsconfig.json +9 -0
@@ -0,0 +1,103 @@
1
+ import * as React from 'react';
2
+ import { Platform, processColor, ViewProps } from 'react-native';
3
+
4
+ import NativeLinearGradient from './NativeLinearGradient';
5
+ import { NativeLinearGradientPoint } from './NativeLinearGradient.types';
6
+
7
+ // @needsAudit
8
+ /**
9
+ * An object `{ x: number; y: number }` or array `[x, y]` that represents the point
10
+ * at which the gradient starts or ends, as a fraction of the overall size of the gradient ranging
11
+ * from `0` to `1`, inclusive.
12
+ */
13
+ export type LinearGradientPoint =
14
+ | {
15
+ /**
16
+ * A number ranging from `0` to `1`, representing the position of gradient transformation.
17
+ */
18
+ x: number;
19
+ /**
20
+ * A number ranging from `0` to `1`, representing the position of gradient transformation.
21
+ */
22
+ y: number;
23
+ }
24
+ | NativeLinearGradientPoint;
25
+
26
+ // @needsAudit
27
+ export type LinearGradientProps = ViewProps & {
28
+ /**
29
+ * An array of colors that represent stops in the gradient. At least two colors are required
30
+ * (for a single-color background, use the `style.backgroundColor` prop on a `View` component).
31
+ */
32
+ colors: string[];
33
+ /**
34
+ * An array that contains `number`s ranging from `0` to `1`, inclusive, and is the same length as the `colors` property.
35
+ * Each number indicates a color-stop location where each respective color should be located.
36
+ * If not specified, the colors will be distributed evenly across the gradient.
37
+ *
38
+ * For example, `[0.5, 0.8]` would render:
39
+ * - the first color, solid, from the beginning of the gradient view to 50% through (the middle);
40
+ * - a gradient from the first color to the second from the 50% point to the 80% point; and
41
+ * - the second color, solid, from the 80% point to the end of the gradient view.
42
+ *
43
+ * > The color-stop locations must be ascending from least to greatest.
44
+ * @default []
45
+ */
46
+ locations?: number[] | null;
47
+ /**
48
+ * For example, `{ x: 0.1, y: 0.2 }` means that the gradient will start `10%` from the left and `20%` from the top.
49
+ *
50
+ * **On web**, this only changes the angle of the gradient because CSS gradients don't support changing the starting position.
51
+ * @default { x: 0.5, y: 0.0 }
52
+ */
53
+ start?: LinearGradientPoint | null;
54
+ /**
55
+ * For example, `{ x: 0.1, y: 0.2 }` means that the gradient will end `10%` from the left and `20%` from the bottom.
56
+ *
57
+ * **On web**, this only changes the angle of the gradient because CSS gradients don't support changing the end position.
58
+ * @default { x: 0.5, y: 1.0 }
59
+ */
60
+ end?: LinearGradientPoint | null;
61
+ };
62
+
63
+ /**
64
+ * Renders a native view that transitions between multiple colors in a linear direction.
65
+ */
66
+ export class LinearGradient extends React.Component<LinearGradientProps> {
67
+ render() {
68
+ const { colors, locations, start, end, ...props } = this.props;
69
+ let resolvedLocations = locations;
70
+ if (locations && colors.length !== locations.length) {
71
+ console.warn('LinearGradient colors and locations props should be arrays of the same length');
72
+ resolvedLocations = locations.slice(0, colors.length);
73
+ }
74
+
75
+ return (
76
+ <NativeLinearGradient
77
+ {...props}
78
+ colors={Platform.select({
79
+ web: colors as any,
80
+ default: colors.map(processColor),
81
+ })}
82
+ locations={resolvedLocations}
83
+ startPoint={_normalizePoint(start)}
84
+ endPoint={_normalizePoint(end)}
85
+ />
86
+ );
87
+ }
88
+ }
89
+
90
+ function _normalizePoint(
91
+ point: LinearGradientPoint | null | undefined
92
+ ): NativeLinearGradientPoint | undefined {
93
+ if (!point) {
94
+ return undefined;
95
+ }
96
+
97
+ if (Array.isArray(point) && point.length !== 2) {
98
+ console.warn('start and end props for LinearGradient must be of the format [x,y] or {x, y}');
99
+ return undefined;
100
+ }
101
+
102
+ return Array.isArray(point) ? point : [point.x, point.y];
103
+ }
@@ -0,0 +1,49 @@
1
+ import { requireNativeViewManager } from 'expo-modules-core';
2
+ import * as React from 'react';
3
+ import { StyleSheet, View } from 'react-native';
4
+
5
+ import { NativeLinearGradientProps } from './NativeLinearGradient.types';
6
+
7
+ export default function NativeLinearGradient({
8
+ colors,
9
+ locations,
10
+ startPoint,
11
+ endPoint,
12
+ children,
13
+ style,
14
+ ...props
15
+ }: NativeLinearGradientProps): React.ReactElement {
16
+ // TODO: revisit whether we need to inherit the container's borderRadius since this issue has
17
+ // been resolved: https://github.com/facebook/react-native/issues/3198
18
+ const flatStyle = StyleSheet.flatten(style) ?? {};
19
+ const borderRadius = flatStyle.borderRadius ?? 0;
20
+
21
+ // This is the format from:
22
+ // https://developer.android.com/reference/android/graphics/Path.html#addRoundRect(android.graphics.RectF,%20float[],%20android.graphics.Path.Direction)
23
+ const borderRadiiPerCorner = [
24
+ flatStyle.borderTopLeftRadius ?? borderRadius,
25
+ flatStyle.borderTopLeftRadius ?? borderRadius,
26
+ flatStyle.borderTopRightRadius ?? borderRadius,
27
+ flatStyle.borderTopRightRadius ?? borderRadius,
28
+ flatStyle.borderBottomRightRadius ?? borderRadius,
29
+ flatStyle.borderBottomRightRadius ?? borderRadius,
30
+ flatStyle.borderBottomLeftRadius ?? borderRadius,
31
+ flatStyle.borderBottomLeftRadius ?? borderRadius,
32
+ ];
33
+
34
+ return (
35
+ <View {...props} style={style}>
36
+ <BaseNativeLinearGradient
37
+ style={StyleSheet.absoluteFill}
38
+ colors={colors}
39
+ startPoint={startPoint}
40
+ endPoint={endPoint}
41
+ locations={locations}
42
+ borderRadii={borderRadiiPerCorner}
43
+ />
44
+ {children}
45
+ </View>
46
+ );
47
+ }
48
+
49
+ const BaseNativeLinearGradient = requireNativeViewManager('ExpoLinearGradient');
@@ -0,0 +1,10 @@
1
+ import { requireNativeViewManager } from 'expo-modules-core';
2
+ import * as React from 'react';
3
+
4
+ import { NativeLinearGradientProps } from './NativeLinearGradient.types';
5
+
6
+ const NativeLinearGradient = requireNativeViewManager(
7
+ 'ExpoLinearGradient'
8
+ ) as React.FC<NativeLinearGradientProps>;
9
+
10
+ export default NativeLinearGradient;
@@ -0,0 +1,36 @@
1
+ import React from 'react'
2
+ import { ViewProps, ColorValue } from 'react-native';
3
+ // @ts-ignore
4
+ import register from 'react-native/Libraries/Utilities/registerGeneratedViewConfig';
5
+ import {
6
+ HostComponent,
7
+ } from 'react-native';
8
+
9
+ interface NativeProps extends ViewProps {
10
+ colors: (number | ColorValue)[];
11
+ startPoint?: { x: number; y: number };
12
+ endPoint?: { x: number; y: number };
13
+ locations?: number[];
14
+ }
15
+
16
+ const NativeComponent = 'ExpoLinearGradient' as unknown as HostComponent<NativeProps>;
17
+
18
+ register(NativeComponent, {
19
+ uiViewClassName: NativeComponent,
20
+ validAttributes: {
21
+ colors: true,
22
+ startPoint: true,
23
+ endPoint: true,
24
+ locations: true,
25
+ },
26
+ });
27
+
28
+ /**
29
+ * This responsive component displays native React View that transitions between multiple colours along the respective dimensions.
30
+ * You can pass different colors, angles, locations etc. to communicate different visuals of gradient.
31
+ * @param props The properties passed to this component for rendering
32
+ * @constructor
33
+ */
34
+ export default function (props: NativeProps) {
35
+ return <NativeComponent {...props} />;
36
+ }
@@ -0,0 +1,12 @@
1
+ import * as React from 'react';
2
+ import { View } from 'react-native';
3
+
4
+ import { NativeLinearGradientProps } from './NativeLinearGradient.types';
5
+
6
+ // This is a shim view for platforms that aren't supported by Expo.
7
+ // The component and prop types should match all of the other platform variations.
8
+ export default function NativeLinearGradient(props: NativeLinearGradientProps): React.ReactElement {
9
+ const { colors, locations, startPoint, endPoint, ...viewProps } = props;
10
+ console.warn('LinearGradient is not available on this platform');
11
+ return <View {...(viewProps as any)} />;
12
+ }
@@ -0,0 +1,21 @@
1
+ import { PropsWithChildren } from 'react';
2
+ import { ViewProps } from 'react-native';
3
+
4
+ export type NativeLinearGradientProps = ViewProps &
5
+ PropsWithChildren<{
6
+ colors: number[];
7
+ locations?: number[] | null;
8
+ startPoint?: NativeLinearGradientPoint | null;
9
+ endPoint?: NativeLinearGradientPoint | null;
10
+ }>;
11
+
12
+ export type getLinearGradientBackgroundImage = (
13
+ colors: number[],
14
+ width?: number,
15
+ height?: number,
16
+ locations?: number[] | null,
17
+ startPoint?: NativeLinearGradientPoint | null,
18
+ endPoint?: NativeLinearGradientPoint | null
19
+ ) => string;
20
+
21
+ export type NativeLinearGradientPoint = [number, number];
@@ -0,0 +1,115 @@
1
+ import * as React from 'react';
2
+ import { View } from 'react-native';
3
+
4
+ import { NativeLinearGradientPoint, NativeLinearGradientProps } from './NativeLinearGradient.types';
5
+ import { normalizeColor } from './normalizeColor';
6
+
7
+ export default function NativeLinearGradient({
8
+ colors,
9
+ locations,
10
+ startPoint,
11
+ endPoint,
12
+ ...props
13
+ }: NativeLinearGradientProps): React.ReactElement {
14
+ const [{ height, width }, setLayout] = React.useState({
15
+ height: 1,
16
+ width: 1,
17
+ });
18
+
19
+ // TODO(Bacon): In the future we could consider adding `backgroundRepeat: "no-repeat"`. For more
20
+ // browser support.
21
+ const linearGradientBackgroundImage = React.useMemo(() => {
22
+ return getLinearGradientBackgroundImage(colors, locations, startPoint, endPoint, width, height);
23
+ }, [colors, locations, startPoint, endPoint, width, height]);
24
+
25
+ return (
26
+ <View
27
+ {...props}
28
+ style={[
29
+ props.style,
30
+ // @ts-ignore: [ts] Property 'backgroundImage' does not exist on type 'ViewStyle'.
31
+ { backgroundImage: linearGradientBackgroundImage },
32
+ ]}
33
+ onLayout={(event) => {
34
+ const { width, height } = event.nativeEvent.layout;
35
+
36
+ setLayout((oldLayout) => {
37
+ // don't set new layout state unless the layout has actually changed
38
+ if (width !== oldLayout.width || height !== oldLayout.height) {
39
+ return { height, width };
40
+ }
41
+
42
+ return oldLayout;
43
+ });
44
+
45
+ if (props.onLayout) {
46
+ props.onLayout(event);
47
+ }
48
+ }}
49
+ />
50
+ );
51
+ }
52
+
53
+ /**
54
+ * Extracted to a separate function in order to be able to test logic independently.
55
+ */
56
+ export function getLinearGradientBackgroundImage(
57
+ colors: number[] | string[],
58
+ locations?: number[] | null,
59
+ startPoint?: NativeLinearGradientPoint | null,
60
+ endPoint?: NativeLinearGradientPoint | null,
61
+ width: number = 1,
62
+ height: number = 1
63
+ ) {
64
+ const gradientColors = calculateGradientColors(colors, locations);
65
+ const angle = calculatePseudoAngle(width, height, startPoint, endPoint);
66
+ return `linear-gradient(${angle}deg, ${gradientColors.join(', ')})`;
67
+ }
68
+
69
+ function calculatePseudoAngle(
70
+ width: number,
71
+ height: number,
72
+ startPoint?: NativeLinearGradientPoint | null,
73
+ endPoint?: NativeLinearGradientPoint | null
74
+ ) {
75
+ const getControlPoints = (): NativeLinearGradientPoint[] => {
76
+ let correctedStartPoint: NativeLinearGradientPoint = [0, 0];
77
+ if (Array.isArray(startPoint)) {
78
+ correctedStartPoint = [
79
+ startPoint[0] != null ? startPoint[0] : 0.0,
80
+ startPoint[1] != null ? startPoint[1] : 0.0,
81
+ ];
82
+ }
83
+ let correctedEndPoint: NativeLinearGradientPoint = [0.0, 1.0];
84
+ if (Array.isArray(endPoint)) {
85
+ correctedEndPoint = [
86
+ endPoint[0] != null ? endPoint[0] : 0.0,
87
+ endPoint[1] != null ? endPoint[1] : 1.0,
88
+ ];
89
+ }
90
+ return [correctedStartPoint, correctedEndPoint];
91
+ };
92
+
93
+ const [start, end] = getControlPoints();
94
+ start[0] *= width;
95
+ end[0] *= width;
96
+ start[1] *= height;
97
+ end[1] *= height;
98
+ const py = end[1] - start[1];
99
+ const px = end[0] - start[0];
100
+
101
+ return 90 + (Math.atan2(py, px) * 180) / Math.PI;
102
+ }
103
+
104
+ function calculateGradientColors(colors: number[] | string[], locations?: number[] | null) {
105
+ return colors.map((color: number | string, index: number): string | void => {
106
+ const output = normalizeColor(color);
107
+ if (locations && locations[index]) {
108
+ const location = Math.max(0, Math.min(1, locations[index]));
109
+ // Convert 0...1 to 0...100
110
+ const percentage = location * 100;
111
+ return `${output} ${percentage}%`;
112
+ }
113
+ return output;
114
+ });
115
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Expo.
3
+ * Copyright (c) Nicolas Gallagher.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import { processColor } from 'react-native';
9
+
10
+ const isWebColor = (color: string): boolean =>
11
+ color === 'currentcolor' ||
12
+ color === 'currentColor' ||
13
+ color === 'inherit' ||
14
+ color.indexOf('var(') === 0;
15
+
16
+ export function normalizeColor(color?: number | string, opacity: number = 1): void | string {
17
+ if (color == null) return;
18
+
19
+ if (typeof color === 'string' && isWebColor(color)) {
20
+ return color;
21
+ }
22
+
23
+ const colorInt = processColor(color) as number | undefined;
24
+ if (colorInt != null) {
25
+ const r = (colorInt >> 16) & 255;
26
+ const g = (colorInt >> 8) & 255;
27
+ const b = colorInt & 255;
28
+ const a = ((colorInt >> 24) & 255) / 255;
29
+ const alpha = (a * opacity).toFixed(2);
30
+ return `rgba(${r},${g},${b},${alpha})`;
31
+ }
32
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ // @generated by expo-module-scripts
2
+ {
3
+ "extends": "expo-module-scripts/tsconfig.base",
4
+ "compilerOptions": {
5
+ "outDir": "./build"
6
+ },
7
+ "include": ["./src"],
8
+ "exclude": ["**/__mocks__/*", "**/__tests__/*"]
9
+ }