@artsy/palette-mobile 19.16.0 → 19.17.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/elements/Avatar/Avatar.d.ts +2 -1
- package/dist/elements/Avatar/Avatar.js +2 -2
- package/dist/elements/Image/Image.js +3 -12
- package/dist/elements/Image/__tests__/getImageURL.tests.d.ts +1 -0
- package/dist/elements/Image/__tests__/getImageURL.tests.js +30 -0
- package/dist/elements/Image/helpers/getImageURL.d.ts +14 -0
- package/dist/elements/Image/helpers/getImageURL.js +25 -0
- package/dist/utils/createGeminiUrl.d.ts +1 -0
- package/dist/utils/createGeminiUrl.js +4 -1
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@ export interface AvatarProps extends ImgHTMLAttributes<any> {
|
|
|
7
7
|
/** The size of the Avatar */
|
|
8
8
|
size?: AvatarSize;
|
|
9
9
|
src?: string;
|
|
10
|
+
performResize?: boolean;
|
|
10
11
|
}
|
|
11
12
|
/** A circular Avatar component containing an image or initials */
|
|
12
|
-
export declare const Avatar: ({ src, initials, size, blurhash }: AvatarProps) => import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
export declare const Avatar: ({ src, initials, size, blurhash, performResize, }: AvatarProps) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -26,11 +26,11 @@ const VARIANTS = {
|
|
|
26
26
|
},
|
|
27
27
|
};
|
|
28
28
|
/** A circular Avatar component containing an image or initials */
|
|
29
|
-
export const Avatar = ({ src, initials, size = DEFAULT_SIZE, blurhash }) => {
|
|
29
|
+
export const Avatar = ({ src, initials, size = DEFAULT_SIZE, blurhash, performResize = true, }) => {
|
|
30
30
|
const color = useColor();
|
|
31
31
|
const { diameter, textSize } = VARIANTS[size];
|
|
32
32
|
if (src) {
|
|
33
|
-
return (_jsx(MotiView, { entering: FadeIn, children: _jsx(Box, { width: diameter, height: diameter, borderRadius: diameter / 2, overflow: "hidden", borderColor: color("mono0"), borderWidth: 1, children: _jsx(Image, { blurhash: blurhash, resizeMode: "cover", src: src, height: diameter, width: diameter, accessibilityLabel: "AvatarImage", style: {
|
|
33
|
+
return (_jsx(MotiView, { entering: FadeIn, children: _jsx(Box, { width: diameter, height: diameter, borderRadius: diameter / 2, overflow: "hidden", borderColor: color("mono0"), borderWidth: 1, children: _jsx(Image, { blurhash: blurhash, resizeMode: "cover", src: src, height: diameter, width: diameter, accessibilityLabel: "AvatarImage", performResize: performResize, style: {
|
|
34
34
|
width: diameter,
|
|
35
35
|
height: diameter,
|
|
36
36
|
} }) }) }));
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import FastImage from "@d11/react-native-fast-image";
|
|
3
3
|
import { memo, useRef } from "react";
|
|
4
|
-
import {
|
|
4
|
+
import { View, Animated } from "react-native";
|
|
5
5
|
import { Blurhash } from "react-native-blurhash";
|
|
6
|
-
import {
|
|
6
|
+
import { getImageURL } from "./helpers/getImageURL";
|
|
7
7
|
import { useColor } from "../../utils/hooks";
|
|
8
8
|
import { useScreenDimensions } from "../../utils/hooks/useScreenDimensions";
|
|
9
9
|
import { Flex } from "../Flex";
|
|
@@ -22,15 +22,6 @@ export const Image = memo(({ aspectRatio, width, height, performResize = true, s
|
|
|
22
22
|
if (showLoadingState) {
|
|
23
23
|
return (_jsx(ImageSkeleton, { dimensions: dimensions, blurhash: blurhash, style: { position: "absolute" } }));
|
|
24
24
|
}
|
|
25
|
-
let uri = src;
|
|
26
|
-
if (performResize) {
|
|
27
|
-
uri = createGeminiUrl({
|
|
28
|
-
imageURL: src,
|
|
29
|
-
width: PixelRatio.getPixelSizeForLayoutSize(dimensions.width),
|
|
30
|
-
height: PixelRatio.getPixelSizeForLayoutSize(dimensions.height),
|
|
31
|
-
resizeMode: geminiResizeMode,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
25
|
return (_jsxs(Flex, { position: "relative", ...flexProps, style: { ...dimensions }, children: [_jsx(View, { style: [dimensions, { position: "absolute" }], children: _jsx(ImageSkeleton, { dimensions: dimensions, blurhash: blurhash, style: { position: "absolute" } }) }), _jsx(FastImage, { style: [
|
|
35
26
|
dimensions,
|
|
36
27
|
style,
|
|
@@ -39,7 +30,7 @@ export const Image = memo(({ aspectRatio, width, height, performResize = true, s
|
|
|
39
30
|
{ backgroundColor: blurhash ? "transparent" : color("mono30") },
|
|
40
31
|
], resizeMode: resizeMode, onLoadEnd: onLoadEnd, source: {
|
|
41
32
|
priority: FastImage.priority.normal,
|
|
42
|
-
uri,
|
|
33
|
+
uri: getImageURL({ src, dimensions, geminiResizeMode, performResize }),
|
|
43
34
|
} })] }));
|
|
44
35
|
});
|
|
45
36
|
const useImageDimensions = (props) => {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { getImageURL } from "../helpers/getImageURL";
|
|
2
|
+
describe("getImageURL", () => {
|
|
3
|
+
it("should return the original url if performResize is true", () => {
|
|
4
|
+
const url = getImageURL({
|
|
5
|
+
src: "https://example.com/image.jpg",
|
|
6
|
+
dimensions: { width: 100, height: 100 },
|
|
7
|
+
geminiResizeMode: "fill",
|
|
8
|
+
performResize: true,
|
|
9
|
+
});
|
|
10
|
+
expect(url).toBe("https://d7hftxdivxxvm.cloudfront.net/?height=200&quality=80&resize_to=fill&src=https%3A%2F%2Fexample.com%2Fimage.jpg&width=200");
|
|
11
|
+
});
|
|
12
|
+
it("should return the original url if performResize is true and the url is already resized", () => {
|
|
13
|
+
const url = getImageURL({
|
|
14
|
+
src: "https://d7hftxdivxxvm.cloudfront.net/?height=100&quality=80&resize_to=fill&src=https%3A%2F%2Fexample.com%2Fimage.jpg&width=100",
|
|
15
|
+
dimensions: { width: 100, height: 100 },
|
|
16
|
+
geminiResizeMode: "fill",
|
|
17
|
+
performResize: true,
|
|
18
|
+
});
|
|
19
|
+
expect(url).toBe("https://d7hftxdivxxvm.cloudfront.net/?height=100&quality=80&resize_to=fill&src=https%3A%2F%2Fexample.com%2Fimage.jpg&width=100");
|
|
20
|
+
});
|
|
21
|
+
it("should return the original url if performResize is false", () => {
|
|
22
|
+
const url = getImageURL({
|
|
23
|
+
src: "https://example.com/image.jpg",
|
|
24
|
+
dimensions: { width: 100, height: 100 },
|
|
25
|
+
geminiResizeMode: "fill",
|
|
26
|
+
performResize: false,
|
|
27
|
+
});
|
|
28
|
+
expect(url).toBe("https://example.com/image.jpg");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { GeminiResizeMode } from "../../../utils/createGeminiUrl";
|
|
2
|
+
/**
|
|
3
|
+
* This method retuns a valid image url to be used within an Image Component
|
|
4
|
+
* It handles the case of resizing the image on the fly using Gemini
|
|
5
|
+
*/
|
|
6
|
+
export declare const getImageURL: ({ src, dimensions, geminiResizeMode, performResize, }: {
|
|
7
|
+
src: string;
|
|
8
|
+
dimensions: {
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
};
|
|
12
|
+
geminiResizeMode: GeminiResizeMode;
|
|
13
|
+
performResize: boolean;
|
|
14
|
+
}) => string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PixelRatio } from "react-native";
|
|
2
|
+
import { createGeminiUrl, imageAlreadyResized, } from "../../../utils/createGeminiUrl";
|
|
3
|
+
/**
|
|
4
|
+
* This method retuns a valid image url to be used within an Image Component
|
|
5
|
+
* It handles the case of resizing the image on the fly using Gemini
|
|
6
|
+
*/
|
|
7
|
+
export const getImageURL = ({ src, dimensions, geminiResizeMode, performResize, }) => {
|
|
8
|
+
let uri = src;
|
|
9
|
+
if (performResize) {
|
|
10
|
+
if (!imageAlreadyResized(src)) {
|
|
11
|
+
uri = createGeminiUrl({
|
|
12
|
+
imageURL: src,
|
|
13
|
+
width: PixelRatio.getPixelSizeForLayoutSize(dimensions.width),
|
|
14
|
+
height: PixelRatio.getPixelSizeForLayoutSize(dimensions.height),
|
|
15
|
+
resizeMode: geminiResizeMode,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
if (__DEV__) {
|
|
20
|
+
console.warn("You are resizing a gemini url that is already resized. Pass performResize={false} or do not use a resized url");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return uri;
|
|
25
|
+
};
|
|
@@ -3,8 +3,11 @@ const geminiHosts = [
|
|
|
3
3
|
"https://d7hftxdivxxvm.cloudfront.net",
|
|
4
4
|
"https://d196wkiy8qx2u5.cloudfront.net",
|
|
5
5
|
];
|
|
6
|
+
export const imageAlreadyResized = (imageURL) => {
|
|
7
|
+
return geminiHosts.some((host) => imageURL.includes(host));
|
|
8
|
+
};
|
|
6
9
|
export function createGeminiUrl({ imageURL, width, height, geminiHost = "d7hftxdivxxvm.cloudfront.net", imageQuality = 80, resizeMode = "fill", }) {
|
|
7
|
-
if (
|
|
10
|
+
if (imageAlreadyResized(imageURL)) {
|
|
8
11
|
console.error("Image: `performResize` on self referential url. Avoid resizing gemini urls. Pass performResize={false} to fix this.", { imageURL });
|
|
9
12
|
}
|
|
10
13
|
const src = encodeURIComponent(imageURL);
|