@lumx/react 2.1.9-alpha-thumbnail14 → 2.1.9-alpha-thumbnail18

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.
@@ -50,20 +50,16 @@ function useImageLoad(imageURL, imgRef) {
50
50
  return state;
51
51
  }
52
52
 
53
- function shiftPosition(scale, containerSize, imageSize, focusSize, isVertical) {
54
- if (!focusSize) return 50;
55
- var focusFactor = (focusSize + 1) / 2;
56
- var scaledSize = Math.floor(imageSize / scale);
57
- var focus = Math.floor(focusFactor * scaledSize);
58
- if (isVertical) focus = scaledSize - focus;
59
- var containerCenter = Math.floor(containerSize / 2);
60
- var focusOffset = focus - containerCenter;
61
- var remainder = scaledSize - focus;
62
- if (remainder < containerCenter) focusOffset -= containerCenter - remainder;
63
- if (focusOffset < 0) return 0;
64
- return Math.min(100, Math.floor(focusOffset * 100 / containerSize));
53
+ // Calculate shift to center the focus point in the container.
54
+ function shiftPosition(scale, focusPoint, imageSize, containerSize) {
55
+ var scaledSize = imageSize / scale;
56
+ var scaledFocusHeight = focusPoint * scaledSize;
57
+ var startFocus = scaledFocusHeight - containerSize / 2;
58
+ var shift = startFocus / (scaledSize - containerSize);
59
+ return Math.floor(Math.max(Math.min(shift, 1), 0) * 100);
65
60
  }
66
61
 
62
+ // Compute CSS properties to apply the focus point.
67
63
  var useFocusPointStyle = function useFocusPointStyle(_ref, element, isLoaded) {
68
64
  var image = _ref.image,
69
65
  aspectRatio = _ref.aspectRatio,
@@ -73,7 +69,8 @@ var useFocusPointStyle = function useFocusPointStyle(_ref, element, isLoaded) {
73
69
  var width = _ref$imgProps.width,
74
70
  height = _ref$imgProps.height;
75
71
  // Get natural image size from imgProps or img element.
76
- var naturalSize = useMemo(function () {
72
+ var imageSize = useMemo(function () {
73
+ // Focus point is not applicable => exit early
77
74
  if (!image || aspectRatio === AspectRatio.original || !(focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x) && !(focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y)) return undefined;
78
75
  if (typeof width === 'number' && typeof height === 'number') return {
79
76
  width: width,
@@ -84,28 +81,74 @@ var useFocusPointStyle = function useFocusPointStyle(_ref, element, isLoaded) {
84
81
  height: element.naturalHeight
85
82
  };
86
83
  return undefined;
87
- }, [aspectRatio, element, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y, height, image, isLoaded, width]); // Compute focus point CSS style.
88
-
89
- return useMemo(function () {
90
- if (aspectRatio === AspectRatio.original || !(focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x) && !(focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y)) return {};
91
-
92
- if (element && naturalSize) {
93
- var actualWidth = element.offsetWidth;
94
- var actualHeight = element.offsetHeight;
95
- var heightScale = actualHeight / naturalSize.height;
96
- var widthScale = actualWidth / naturalSize.width;
97
- var x = shiftPosition(heightScale, actualWidth, naturalSize.width, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x);
98
- var y = shiftPosition(widthScale, actualHeight, naturalSize.height, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y, true);
99
- return {
100
- objectPosition: "".concat(x, "% ").concat(y, "%")
101
- };
102
- } // Focus point can't be computed yet => We hide the image until it can.
84
+ }, [aspectRatio, element, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y, height, image, isLoaded, width]); // Get container size (dependant on imageSize).
85
+
86
+ var _useState = useState(undefined),
87
+ _useState2 = _slicedToArray(_useState, 2),
88
+ containerSize = _useState2[0],
89
+ setContainerSize = _useState2[1];
103
90
 
91
+ useEffect(function updateContainerSize() {
92
+ var cWidth = element === null || element === void 0 ? void 0 : element.offsetWidth;
93
+ var cHeight = element === null || element === void 0 ? void 0 : element.offsetHeight;
104
94
 
105
- return {
106
- visibility: 'hidden'
107
- };
108
- }, [aspectRatio, element, focusPoint, naturalSize]);
95
+ if (cWidth && cHeight) {
96
+ // Update only if needed.
97
+ setContainerSize(function (oldContainerSize) {
98
+ return (oldContainerSize === null || oldContainerSize === void 0 ? void 0 : oldContainerSize.width) === cWidth && oldContainerSize.height === cHeight ? oldContainerSize : {
99
+ width: cWidth,
100
+ height: cHeight
101
+ };
102
+ });
103
+ } else if (imageSize) {
104
+ // Wait for a render (in case the container size is dependent on the image size).
105
+ requestAnimationFrame(updateContainerSize);
106
+ }
107
+ }, [element === null || element === void 0 ? void 0 : element.offsetHeight, element === null || element === void 0 ? void 0 : element.offsetWidth, imageSize]); // Compute style.
108
+
109
+ var _useState3 = useState({}),
110
+ _useState4 = _slicedToArray(_useState3, 2),
111
+ style = _useState4[0],
112
+ setStyle = _useState4[1];
113
+
114
+ useEffect(function () {
115
+ // Focus point is not applicable => exit early
116
+ if (!image || aspectRatio === AspectRatio.original || !(focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x) && !(focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y)) {
117
+ return;
118
+ }
119
+
120
+ if (!element || !imageSize) {
121
+ // Focus point can be computed but now right now (image size unknown).
122
+ setStyle({
123
+ visibility: 'hidden'
124
+ });
125
+ return;
126
+ }
127
+
128
+ if (!containerSize) {
129
+ // Missing container size abort focus point compute.
130
+ setStyle({});
131
+ return;
132
+ }
133
+
134
+ var heightScale = imageSize.height / containerSize.height;
135
+ var widthScale = imageSize.width / containerSize.width;
136
+ var scale = Math.min(widthScale, heightScale); // Focus Y relative to the top (instead of the center)
137
+
138
+ var focusPointFromTop = Math.abs(((focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y) || 0) - 1) / 2;
139
+ var y = shiftPosition(scale, focusPointFromTop, imageSize.height, containerSize.height); // Focus X relative to the left (instead of the center)
140
+
141
+ var focusPointFromLeft = Math.abs(((focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x) || 0) + 1) / 2;
142
+ var x = shiftPosition(scale, focusPointFromLeft, imageSize.width, containerSize.width);
143
+ var objectPosition = "".concat(x, "% ").concat(y, "%"); // Update only if needed.
144
+
145
+ setStyle(function (oldStyle) {
146
+ return oldStyle.objectPosition === objectPosition ? oldStyle : {
147
+ objectPosition: objectPosition
148
+ };
149
+ });
150
+ }, [aspectRatio, containerSize, element, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y, image, imageSize]);
151
+ return style;
109
152
  };
110
153
 
111
154
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"Thumbnail2.js","sources":["../../../src/components/thumbnail/useImageLoad.ts","../../../src/components/thumbnail/useFocusPointStyle.tsx","../../../src/components/thumbnail/Thumbnail.tsx"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nexport type LoadingState = 'isLoading' | 'isLoaded' | 'hasError';\n\nfunction getState(img: HTMLImageElement | null | undefined, event?: Event) {\n // Error event occurred or image loaded empty.\n if (event?.type === 'error' || (img?.complete && (img?.naturalWidth === 0 || img?.naturalHeight === 0))) {\n return 'hasError';\n }\n // Image is undefined or incomplete.\n if (!img || !img.complete) {\n return 'isLoading';\n }\n // Else loaded.\n return 'isLoaded';\n}\n\nexport function useImageLoad(imageURL: string, imgRef?: HTMLImageElement): LoadingState {\n const [state, setState] = useState<LoadingState>(getState(imgRef));\n\n // Update state when changing image URL or DOM reference.\n useEffect(() => {\n setState(getState(imgRef));\n }, [imageURL, imgRef]);\n\n // Listen to `load` and `error` event on image\n useEffect(() => {\n const img = imgRef;\n if (!img) return undefined;\n const update = (event?: Event) => setState(getState(img, event));\n img.addEventListener('load', update);\n img.addEventListener('error', update);\n return () => {\n img.removeEventListener('load', update);\n img.removeEventListener('error', update);\n };\n }, [imgRef, imgRef?.src]);\n\n return state;\n}\n","import { CSSProperties, useMemo } from 'react';\nimport { AspectRatio } from '@lumx/react/components';\nimport { ThumbnailProps } from '@lumx/react/components/thumbnail/Thumbnail';\n\nfunction shiftPosition(\n scale: number,\n containerSize: number,\n imageSize: number,\n focusSize: number | undefined,\n isVertical?: boolean,\n) {\n if (!focusSize) return 50;\n const focusFactor = (focusSize + 1) / 2;\n const scaledSize = Math.floor(imageSize / scale);\n let focus = Math.floor(focusFactor * scaledSize);\n if (isVertical) focus = scaledSize - focus;\n\n const containerCenter = Math.floor(containerSize / 2);\n let focusOffset = focus - containerCenter;\n const remainder = scaledSize - focus;\n if (remainder < containerCenter) focusOffset -= containerCenter - remainder;\n if (focusOffset < 0) return 0;\n\n return Math.min(100, Math.floor((focusOffset * 100) / containerSize));\n}\n\nexport const useFocusPointStyle = (\n { image, aspectRatio, focusPoint, imgProps: { width, height } = {} }: ThumbnailProps,\n element: HTMLImageElement | undefined,\n isLoaded: boolean,\n): CSSProperties => {\n // Get natural image size from imgProps or img element.\n const naturalSize = useMemo(() => {\n if (!image || aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) return undefined;\n if (typeof width === 'number' && typeof height === 'number') return { width, height };\n if (element && isLoaded) return { width: element.naturalWidth, height: element.naturalHeight };\n return undefined;\n }, [aspectRatio, element, focusPoint?.x, focusPoint?.y, height, image, isLoaded, width]);\n\n // Compute focus point CSS style.\n return useMemo(() => {\n if (aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) return {};\n if (element && naturalSize) {\n const actualWidth = element.offsetWidth;\n const actualHeight = element.offsetHeight;\n const heightScale = actualHeight / naturalSize.height;\n const widthScale = actualWidth / naturalSize.width;\n const x = shiftPosition(heightScale, actualWidth, naturalSize.width, focusPoint?.x);\n const y = shiftPosition(widthScale, actualHeight, naturalSize.height, focusPoint?.y, true);\n return { objectPosition: `${x}% ${y}%` };\n }\n // Focus point can't be computed yet => We hide the image until it can.\n return { visibility: 'hidden' };\n }, [aspectRatio, element, focusPoint, naturalSize]);\n};\n","import React, {\n CSSProperties,\n forwardRef,\n ImgHTMLAttributes,\n KeyboardEventHandler,\n MouseEventHandler,\n ReactElement,\n ReactNode,\n Ref,\n useState,\n} from 'react';\nimport classNames from 'classnames';\n\nimport { AspectRatio, HorizontalAlignment, Icon, Size, Theme } from '@lumx/react';\n\nimport { Comp, GenericProps, getRootClassName, handleBasicClasses } from '@lumx/react/utils';\n\nimport { mdiImageBroken } from '@lumx/icons';\nimport { mergeRefs } from '@lumx/react/utils/mergeRefs';\nimport { useImageLoad } from '@lumx/react/components/thumbnail/useImageLoad';\nimport { useFocusPointStyle } from '@lumx/react/components/thumbnail/useFocusPointStyle';\nimport { FocusPoint, ThumbnailSize, ThumbnailVariant } from './types';\n\ntype ImgHTMLProps = ImgHTMLAttributes<HTMLImageElement>;\n\n/**\n * Defines the props of the component.\n */\nexport interface ThumbnailProps extends GenericProps {\n /** Alignment of the thumbnail in it's parent (requires flex parent). */\n align?: HorizontalAlignment;\n /** Image alternative text. */\n alt: string;\n /** Image aspect ratio. */\n aspectRatio?: AspectRatio;\n /** Badge. */\n badge?: ReactElement;\n /** Image cross origin resource policy. */\n crossOrigin?: ImgHTMLProps['crossOrigin'];\n /** Fallback icon (SVG path) or react node when image fails to load. */\n fallback?: string | ReactNode;\n /** Whether the thumbnail should fill it's parent size (requires flex parent) or not. */\n fillHeight?: boolean;\n /** Apply relative vertical and horizontal shift (from -1 to 1) on the image position inside the thumbnail. */\n focusPoint?: FocusPoint;\n /** Image URL. */\n image: string;\n /** Props to inject into the native <img> element. */\n imgProps?: ImgHTMLProps;\n /** Reference to the native <img> element. */\n imgRef?: Ref<HTMLImageElement>;\n /** Set to true to force the display of the loading skeleton. */\n isLoading?: boolean;\n /** Size variant of the component. */\n size?: ThumbnailSize;\n /** Image loading mode. */\n loading?: ImgHTMLProps['loading'];\n /** On click callback. */\n onClick?: MouseEventHandler<HTMLDivElement>;\n /** On key press callback. */\n onKeyPress?: KeyboardEventHandler<HTMLDivElement>;\n /** Theme adapting the component to light or dark background. */\n theme?: Theme;\n /** Variant of the component. */\n variant?: ThumbnailVariant;\n /** Props to pass to the link wrapping the thumbnail. */\n linkProps?: React.DetailedHTMLProps<React.AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>;\n /** Custom react component for the link (can be used to inject react router Link). */\n linkAs?: 'a' | any;\n}\n\n/**\n * Component display name.\n */\nconst COMPONENT_NAME = 'Thumbnail';\n\n/**\n * Component default class name and class prefix.\n */\nconst CLASSNAME = getRootClassName(COMPONENT_NAME);\n\n/**\n * Component default props.\n */\nconst DEFAULT_PROPS: Partial<ThumbnailProps> = {\n fallback: mdiImageBroken,\n loading: 'lazy',\n theme: Theme.light,\n};\n\n/**\n * Thumbnail component.\n *\n * @param props Component props.\n * @param ref Component ref.\n * @return React element.\n */\nexport const Thumbnail: Comp<ThumbnailProps> = forwardRef((props, ref) => {\n const {\n align,\n alt,\n aspectRatio = AspectRatio.original,\n badge,\n className,\n crossOrigin,\n fallback,\n fillHeight,\n focusPoint,\n image,\n imgProps,\n imgRef: propImgRef,\n isLoading: isLoadingProp,\n loading,\n size,\n theme,\n variant,\n linkProps,\n linkAs,\n ...forwardedProps\n } = props;\n const [imgElement, setImgElement] = useState<HTMLImageElement>();\n\n // Image loading state.\n const loadingState = useImageLoad(image, imgElement);\n const isLoaded = loadingState === 'isLoaded';\n const isLoading = isLoadingProp || loadingState === 'isLoading';\n const hasError = loadingState === 'hasError';\n\n // Focus point.\n const focusPointStyle = useFocusPointStyle(props, imgElement, isLoaded);\n\n const hasIconErrorFallback = hasError && typeof fallback === 'string';\n const hasCustomErrorFallback = hasError && !hasIconErrorFallback;\n const imageErrorStyle: CSSProperties = {};\n if (hasIconErrorFallback) {\n // Keep the image layout on icon fallback.\n imageErrorStyle.visibility = 'hidden';\n } else if (hasCustomErrorFallback) {\n // Remove the image on custom fallback.\n imageErrorStyle.display = 'none';\n }\n\n const isLink = Boolean(linkProps?.href || linkAs);\n const isButton = !!forwardedProps.onClick;\n const isClickable = isButton || isLink;\n\n let Wrapper: any = 'div';\n const wrapperProps = { ...forwardedProps };\n if (isLink) {\n Wrapper = linkAs || 'a';\n Object.assign(wrapperProps, linkProps);\n } else if (isButton) {\n Wrapper = 'button';\n }\n\n return (\n <Wrapper\n {...wrapperProps}\n ref={ref}\n className={classNames(\n linkProps?.className,\n className,\n handleBasicClasses({\n align,\n aspectRatio,\n prefix: CLASSNAME,\n size,\n theme,\n variant,\n isClickable,\n hasError,\n hasIconErrorFallback,\n hasCustomErrorFallback,\n isLoading,\n hasBadge: !!badge,\n }),\n fillHeight && `${CLASSNAME}--fill-height`,\n )}\n >\n <div className={`${CLASSNAME}__background`}>\n <img\n {...imgProps}\n style={{\n ...imgProps?.style,\n ...imageErrorStyle,\n ...focusPointStyle,\n }}\n ref={mergeRefs(setImgElement, propImgRef)}\n className={classNames(`${CLASSNAME}__image`, isLoading && `${CLASSNAME}__image--is-loading`)}\n crossOrigin={crossOrigin}\n src={image}\n alt={alt}\n loading={loading}\n />\n {!isLoading && hasError && (\n <div className={`${CLASSNAME}__fallback`}>\n {hasIconErrorFallback ? (\n <Icon icon={fallback as string} size={Size.xxs} theme={theme} />\n ) : (\n fallback\n )}\n </div>\n )}\n </div>\n {badge &&\n React.cloneElement(badge, { className: classNames(`${CLASSNAME}__badge`, badge.props.className) })}\n </Wrapper>\n );\n});\nThumbnail.displayName = COMPONENT_NAME;\nThumbnail.className = CLASSNAME;\nThumbnail.defaultProps = DEFAULT_PROPS;\n"],"names":["getState","img","event","type","complete","naturalWidth","naturalHeight","useImageLoad","imageURL","imgRef","useState","state","setState","useEffect","undefined","update","addEventListener","removeEventListener","src","shiftPosition","scale","containerSize","imageSize","focusSize","isVertical","focusFactor","scaledSize","Math","floor","focus","containerCenter","focusOffset","remainder","min","useFocusPointStyle","element","isLoaded","image","aspectRatio","focusPoint","imgProps","width","height","naturalSize","useMemo","AspectRatio","original","x","y","actualWidth","offsetWidth","actualHeight","offsetHeight","heightScale","widthScale","objectPosition","visibility","COMPONENT_NAME","CLASSNAME","getRootClassName","DEFAULT_PROPS","fallback","mdiImageBroken","loading","theme","Theme","light","Thumbnail","forwardRef","props","ref","align","alt","badge","className","crossOrigin","fillHeight","propImgRef","isLoadingProp","isLoading","size","variant","linkProps","linkAs","forwardedProps","imgElement","setImgElement","loadingState","hasError","focusPointStyle","hasIconErrorFallback","hasCustomErrorFallback","imageErrorStyle","display","isLink","Boolean","href","isButton","onClick","isClickable","Wrapper","wrapperProps","Object","assign","classNames","handleBasicClasses","prefix","hasBadge","style","mergeRefs","Size","xxs","React","cloneElement","displayName","defaultProps"],"mappings":";;;;;;;;AAIA,SAASA,QAAT,CAAkBC,GAAlB,EAA4DC,KAA5D,EAA2E;AACvE;AACA,MAAI,CAAAA,KAAK,SAAL,IAAAA,KAAK,WAAL,YAAAA,KAAK,CAAEC,IAAP,MAAgB,OAAhB,IAA4B,CAAAF,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEG,QAAL,MAAkB,CAAAH,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEI,YAAL,MAAsB,CAAtB,IAA2B,CAAAJ,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEK,aAAL,MAAuB,CAApE,CAAhC,EAAyG;AACrG,WAAO,UAAP;AACH,GAJsE;;;AAMvE,MAAI,CAACL,GAAD,IAAQ,CAACA,GAAG,CAACG,QAAjB,EAA2B;AACvB,WAAO,WAAP;AACH,GARsE;;;AAUvE,SAAO,UAAP;AACH;;AAEM,SAASG,YAAT,CAAsBC,QAAtB,EAAwCC,MAAxC,EAAiF;AAAA,kBAC1DC,QAAQ,CAAeV,QAAQ,CAACS,MAAD,CAAvB,CADkD;AAAA;AAAA,MAC7EE,KAD6E;AAAA,MACtEC,QADsE;;;AAIpFC,EAAAA,SAAS,CAAC,YAAM;AACZD,IAAAA,QAAQ,CAACZ,QAAQ,CAACS,MAAD,CAAT,CAAR;AACH,GAFQ,EAEN,CAACD,QAAD,EAAWC,MAAX,CAFM,CAAT,CAJoF;;AASpFI,EAAAA,SAAS,CAAC,YAAM;AACZ,QAAMZ,GAAG,GAAG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I,IAAA,GAAG,EAAEtC,KAVT;AAWI,IAAA,GAAG,EAAEmC,GAXT;AAYI,IAAA,OAAO,EAAET;AAZb,KADJ,EAeK,CAACgB,SAAD,IAAcS,QAAd,IACG;AAAK,IAAA,SAAS,YAAK9B,SAAL;AAAd,KACKgC,oBAAoB,GACjB,oBAAC,IAAD;AAAM,IAAA,IAAI,EAAE7B,QAAZ;AAAgC,IAAA,IAAI,EAAEiD,IAAI,CAACC,GAA3C;AAAgD,IAAA,KAAK,EAAE/C;AAAvD,IADiB,GAGjBH,QAJR,CAhBR,CAvBJ,EAgDKY,KAAK,IACFuC,KAAK,CAACC,YAAN,CAAmBxC,KAAnB,EAA0B;AAAEC,IAAAA,SAAS,EAAE8B,UAAU,WAAI9C,SAAJ,cAAwBe,KAAK,CAACJ,KAAN,CAAYK,SAApC;AAAvB,GAA1B,CAjDR,CADJ;AAqDH,CA/GwD;AAgHzDP,SAAS,CAAC+C,WAAV,GAAwBzD,cAAxB;AACAU,SAAS,CAACO,SAAV,GAAsBhB,SAAtB;AACAS,SAAS,CAACgD,YAAV,GAAyBvD,aAAzB;;;;"}
1
+ {"version":3,"file":"Thumbnail2.js","sources":["../../../src/components/thumbnail/useImageLoad.ts","../../../src/components/thumbnail/useFocusPointStyle.tsx","../../../src/components/thumbnail/Thumbnail.tsx"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nexport type LoadingState = 'isLoading' | 'isLoaded' | 'hasError';\n\nfunction getState(img: HTMLImageElement | null | undefined, event?: Event) {\n // Error event occurred or image loaded empty.\n if (event?.type === 'error' || (img?.complete && (img?.naturalWidth === 0 || img?.naturalHeight === 0))) {\n return 'hasError';\n }\n // Image is undefined or incomplete.\n if (!img || !img.complete) {\n return 'isLoading';\n }\n // Else loaded.\n return 'isLoaded';\n}\n\nexport function useImageLoad(imageURL: string, imgRef?: HTMLImageElement): LoadingState {\n const [state, setState] = useState<LoadingState>(getState(imgRef));\n\n // Update state when changing image URL or DOM reference.\n useEffect(() => {\n setState(getState(imgRef));\n }, [imageURL, imgRef]);\n\n // Listen to `load` and `error` event on image\n useEffect(() => {\n const img = imgRef;\n if (!img) return undefined;\n const update = (event?: Event) => setState(getState(img, event));\n img.addEventListener('load', update);\n img.addEventListener('error', update);\n return () => {\n img.removeEventListener('load', update);\n img.removeEventListener('error', update);\n };\n }, [imgRef, imgRef?.src]);\n\n return state;\n}\n","import { CSSProperties, useEffect, useMemo, useState } from 'react';\nimport { AspectRatio } from '@lumx/react/components';\nimport { ThumbnailProps } from '@lumx/react/components/thumbnail/Thumbnail';\n\n// Calculate shift to center the focus point in the container.\nfunction shiftPosition(scale: number, focusPoint: number, imageSize: number, containerSize: number) {\n const scaledSize = imageSize / scale;\n const scaledFocusHeight = focusPoint * scaledSize;\n const startFocus = scaledFocusHeight - containerSize / 2;\n const shift = startFocus / (scaledSize - containerSize);\n return Math.floor(Math.max(Math.min(shift, 1), 0) * 100);\n}\n\ntype Size = { width: number; height: number };\n\n// Compute CSS properties to apply the focus point.\nexport const useFocusPointStyle = (\n { image, aspectRatio, focusPoint, imgProps: { width, height } = {} }: ThumbnailProps,\n element: HTMLImageElement | undefined,\n isLoaded: boolean,\n): CSSProperties => {\n // Get natural image size from imgProps or img element.\n const imageSize: Size | undefined = useMemo(() => {\n // Focus point is not applicable => exit early\n if (!image || aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) return undefined;\n if (typeof width === 'number' && typeof height === 'number') return { width, height };\n if (element && isLoaded) return { width: element.naturalWidth, height: element.naturalHeight };\n return undefined;\n }, [aspectRatio, element, focusPoint?.x, focusPoint?.y, height, image, isLoaded, width]);\n\n // Get container size (dependant on imageSize).\n const [containerSize, setContainerSize] = useState<Size | undefined>(undefined);\n useEffect(\n function updateContainerSize() {\n const cWidth = element?.offsetWidth;\n const cHeight = element?.offsetHeight;\n if (cWidth && cHeight) {\n // Update only if needed.\n setContainerSize((oldContainerSize) =>\n oldContainerSize?.width === cWidth && oldContainerSize.height === cHeight\n ? oldContainerSize\n : { width: cWidth, height: cHeight },\n );\n } else if (imageSize) {\n // Wait for a render (in case the container size is dependent on the image size).\n requestAnimationFrame(updateContainerSize);\n }\n },\n [element?.offsetHeight, element?.offsetWidth, imageSize],\n );\n\n // Compute style.\n const [style, setStyle] = useState<CSSProperties>({});\n useEffect(() => {\n // Focus point is not applicable => exit early\n if (!image || aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) {\n return;\n }\n if (!element || !imageSize) {\n // Focus point can be computed but now right now (image size unknown).\n setStyle({ visibility: 'hidden' });\n return;\n }\n if (!containerSize) {\n // Missing container size abort focus point compute.\n setStyle({});\n return;\n }\n\n const heightScale = imageSize.height / containerSize.height;\n const widthScale = imageSize.width / containerSize.width;\n const scale = Math.min(widthScale, heightScale);\n\n // Focus Y relative to the top (instead of the center)\n const focusPointFromTop = Math.abs((focusPoint?.y || 0) - 1) / 2;\n const y = shiftPosition(scale, focusPointFromTop, imageSize.height, containerSize.height);\n\n // Focus X relative to the left (instead of the center)\n const focusPointFromLeft = Math.abs((focusPoint?.x || 0) + 1) / 2;\n const x = shiftPosition(scale, focusPointFromLeft, imageSize.width, containerSize.width);\n\n const objectPosition = `${x}% ${y}%`;\n\n // Update only if needed.\n setStyle((oldStyle) => (oldStyle.objectPosition === objectPosition ? oldStyle : { objectPosition }));\n }, [aspectRatio, containerSize, element, focusPoint?.x, focusPoint?.y, image, imageSize]);\n\n return style;\n};\n","import React, {\n CSSProperties,\n forwardRef,\n ImgHTMLAttributes,\n KeyboardEventHandler,\n MouseEventHandler,\n ReactElement,\n ReactNode,\n Ref,\n useState,\n} from 'react';\nimport classNames from 'classnames';\n\nimport { AspectRatio, HorizontalAlignment, Icon, Size, Theme } from '@lumx/react';\n\nimport { Comp, GenericProps, getRootClassName, handleBasicClasses } from '@lumx/react/utils';\n\nimport { mdiImageBroken } from '@lumx/icons';\nimport { mergeRefs } from '@lumx/react/utils/mergeRefs';\nimport { useImageLoad } from '@lumx/react/components/thumbnail/useImageLoad';\nimport { useFocusPointStyle } from '@lumx/react/components/thumbnail/useFocusPointStyle';\nimport { FocusPoint, ThumbnailSize, ThumbnailVariant } from './types';\n\ntype ImgHTMLProps = ImgHTMLAttributes<HTMLImageElement>;\n\n/**\n * Defines the props of the component.\n */\nexport interface ThumbnailProps extends GenericProps {\n /** Alignment of the thumbnail in it's parent (requires flex parent). */\n align?: HorizontalAlignment;\n /** Image alternative text. */\n alt: string;\n /** Image aspect ratio. */\n aspectRatio?: AspectRatio;\n /** Badge. */\n badge?: ReactElement;\n /** Image cross origin resource policy. */\n crossOrigin?: ImgHTMLProps['crossOrigin'];\n /** Fallback icon (SVG path) or react node when image fails to load. */\n fallback?: string | ReactNode;\n /** Whether the thumbnail should fill it's parent size (requires flex parent) or not. */\n fillHeight?: boolean;\n /** Apply relative vertical and horizontal shift (from -1 to 1) on the image position inside the thumbnail. */\n focusPoint?: FocusPoint;\n /** Image URL. */\n image: string;\n /** Props to inject into the native <img> element. */\n imgProps?: ImgHTMLProps;\n /** Reference to the native <img> element. */\n imgRef?: Ref<HTMLImageElement>;\n /** Set to true to force the display of the loading skeleton. */\n isLoading?: boolean;\n /** Size variant of the component. */\n size?: ThumbnailSize;\n /** Image loading mode. */\n loading?: ImgHTMLProps['loading'];\n /** On click callback. */\n onClick?: MouseEventHandler<HTMLDivElement>;\n /** On key press callback. */\n onKeyPress?: KeyboardEventHandler<HTMLDivElement>;\n /** Theme adapting the component to light or dark background. */\n theme?: Theme;\n /** Variant of the component. */\n variant?: ThumbnailVariant;\n /** Props to pass to the link wrapping the thumbnail. */\n linkProps?: React.DetailedHTMLProps<React.AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>;\n /** Custom react component for the link (can be used to inject react router Link). */\n linkAs?: 'a' | any;\n}\n\n/**\n * Component display name.\n */\nconst COMPONENT_NAME = 'Thumbnail';\n\n/**\n * Component default class name and class prefix.\n */\nconst CLASSNAME = getRootClassName(COMPONENT_NAME);\n\n/**\n * Component default props.\n */\nconst DEFAULT_PROPS: Partial<ThumbnailProps> = {\n fallback: mdiImageBroken,\n loading: 'lazy',\n theme: Theme.light,\n};\n\n/**\n * Thumbnail component.\n *\n * @param props Component props.\n * @param ref Component ref.\n * @return React element.\n */\nexport const Thumbnail: Comp<ThumbnailProps> = forwardRef((props, ref) => {\n const {\n align,\n alt,\n aspectRatio = AspectRatio.original,\n badge,\n className,\n crossOrigin,\n fallback,\n fillHeight,\n focusPoint,\n image,\n imgProps,\n imgRef: propImgRef,\n isLoading: isLoadingProp,\n loading,\n size,\n theme,\n variant,\n linkProps,\n linkAs,\n ...forwardedProps\n } = props;\n const [imgElement, setImgElement] = useState<HTMLImageElement>();\n\n // Image loading state.\n const loadingState = useImageLoad(image, imgElement);\n const isLoaded = loadingState === 'isLoaded';\n const isLoading = isLoadingProp || loadingState === 'isLoading';\n const hasError = loadingState === 'hasError';\n\n // Focus point.\n const focusPointStyle = useFocusPointStyle(props, imgElement, isLoaded);\n\n const hasIconErrorFallback = hasError && typeof fallback === 'string';\n const hasCustomErrorFallback = hasError && !hasIconErrorFallback;\n const imageErrorStyle: CSSProperties = {};\n if (hasIconErrorFallback) {\n // Keep the image layout on icon fallback.\n imageErrorStyle.visibility = 'hidden';\n } else if (hasCustomErrorFallback) {\n // Remove the image on custom fallback.\n imageErrorStyle.display = 'none';\n }\n\n const isLink = Boolean(linkProps?.href || linkAs);\n const isButton = !!forwardedProps.onClick;\n const isClickable = isButton || isLink;\n\n let Wrapper: any = 'div';\n const wrapperProps = { ...forwardedProps };\n if (isLink) {\n Wrapper = linkAs || 'a';\n Object.assign(wrapperProps, linkProps);\n } else if (isButton) {\n Wrapper = 'button';\n }\n\n return (\n <Wrapper\n {...wrapperProps}\n ref={ref}\n className={classNames(\n linkProps?.className,\n className,\n handleBasicClasses({\n align,\n aspectRatio,\n prefix: CLASSNAME,\n size,\n theme,\n variant,\n isClickable,\n hasError,\n hasIconErrorFallback,\n hasCustomErrorFallback,\n isLoading,\n hasBadge: !!badge,\n }),\n fillHeight && `${CLASSNAME}--fill-height`,\n )}\n >\n <div className={`${CLASSNAME}__background`}>\n <img\n {...imgProps}\n style={{\n ...imgProps?.style,\n ...imageErrorStyle,\n ...focusPointStyle,\n }}\n ref={mergeRefs(setImgElement, propImgRef)}\n className={classNames(`${CLASSNAME}__image`, isLoading && `${CLASSNAME}__image--is-loading`)}\n crossOrigin={crossOrigin}\n src={image}\n alt={alt}\n loading={loading}\n />\n {!isLoading && hasError && (\n <div className={`${CLASSNAME}__fallback`}>\n {hasIconErrorFallback ? (\n <Icon icon={fallback as string} size={Size.xxs} theme={theme} />\n ) : (\n fallback\n )}\n </div>\n )}\n </div>\n {badge &&\n React.cloneElement(badge, { className: classNames(`${CLASSNAME}__badge`, badge.props.className) })}\n </Wrapper>\n );\n});\nThumbnail.displayName = COMPONENT_NAME;\nThumbnail.className = CLASSNAME;\nThumbnail.defaultProps = DEFAULT_PROPS;\n"],"names":["getState","img","event","type","complete","naturalWidth","naturalHeight","useImageLoad","imageURL","imgRef","useState","state","setState","useEffect","undefined","update","addEventListener","removeEventListener","src","shiftPosition","scale","focusPoint","imageSize","containerSize","scaledSize","scaledFocusHeight","startFocus","shift","Math","floor","max","min","useFocusPointStyle","element","isLoaded","image","aspectRatio","imgProps","width","height","useMemo","AspectRatio","original","x","y","setContainerSize","updateContainerSize","cWidth","offsetWidth","cHeight","offsetHeight","oldContainerSize","requestAnimationFrame","style","setStyle","visibility","heightScale","widthScale","focusPointFromTop","abs","focusPointFromLeft","objectPosition","oldStyle","COMPONENT_NAME","CLASSNAME","getRootClassName","DEFAULT_PROPS","fallback","mdiImageBroken","loading","theme","Theme","light","Thumbnail","forwardRef","props","ref","align","alt","badge","className","crossOrigin","fillHeight","propImgRef","isLoadingProp","isLoading","size","variant","linkProps","linkAs","forwardedProps","imgElement","setImgElement","loadingState","hasError","focusPointStyle","hasIconErrorFallback","hasCustomErrorFallback","imageErrorStyle","display","isLink","Boolean","href","isButton","onClick","isClickable","Wrapper","wrapperProps","Object","assign","classNames","handleBasicClasses","prefix","hasBadge","mergeRefs","Size","xxs","React","cloneElement","displayName","defaultProps"],"mappings":";;;;;;;;AAIA,SAASA,QAAT,CAAkBC,GAAlB,EAA4DC,KAA5D,EAA2E;AACvE;AACA,MAAI,CAAAA,KAAK,SAAL,IAAAA,KAAK,WAAL,YAAAA,KAAK,CAAEC,IAAP,MAAgB,OAAhB,IAA4B,CAAAF,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEG,QAAL,MAAkB,CAAAH,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEI,YAAL,MAAsB,CAAtB,IAA2B,CAAAJ,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEK,aAAL,MAAuB,CAApE,CAAhC,EAAyG;AACrG,WAAO,UAAP;AACH,GAJsE;;;AAMvE,MAAI,CAACL,GAAD,IAAQ,CAACA,GAAG,CAACG,QAAjB,EAA2B;AACvB,WAAO,WAAP;AACH,GARsE;;;AAUvE,SAAO,UAAP;AACH;;AAEM,SAASG,YAAT,CAAsBC,QAAtB,EAAwCC,MAAxC,EAAiF;AAAA,kBAC1DC,QAAQ,CAAeV,QAAQ,CAACS,MAAD,CAAvB,CADkD;AAAA;AAAA,MAC7EE,KAD6E;AAAA,MACtEC,QADsE;;;AAIpFC,EAAAA,SAAS,CAAC,YAAM;AACZD,IAAAA,QAAQ,CAACZ,QAAQ,CAACS,MAAD,CAAT,CAAR;AACH,GAFQ,EAEN,CAACD,QAAD,EAAWC,MAAX,CAFM,CAAT,CAJoF;;AASpFI,EAAAA,SAAS,CAAC,YAAM;AACZ,QAAMZ,GAAG,GAAGQ,MAAZ;AACA,QAAI,CAACR,GAAL,EAAU,OAAOa,SAAP;;AACV,QAAMC,MAAM,GAAG,SAATA,MAAS,CAACb,KAAD;AAAA,aAAmBU,QAAQ,CAACZ,QAAQ,CAACC,GAAD,EAAMC,KAAN,CAAT,CAA3B;AAA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wBAAjC,CARzB;AASI,IAAA,WAAW,EAAEiB,WATjB;AAUI,IAAA,GAAG,EAAE9C,KAVT;AAWI,IAAA,GAAG,EAAE2C,GAXT;AAYI,IAAA,OAAO,EAAET;AAZb,KADJ,EAeK,CAACgB,SAAD,IAAcS,QAAd,IACG;AAAK,IAAA,SAAS,YAAK9B,SAAL;AAAd,KACKgC,oBAAoB,GACjB,oBAAC,IAAD;AAAM,IAAA,IAAI,EAAE7B,QAAZ;AAAgC,IAAA,IAAI,EAAEgD,IAAI,CAACC,GAA3C;AAAgD,IAAA,KAAK,EAAE9C;AAAvD,IADiB,GAGjBH,QAJR,CAhBR,CAvBJ,EAgDKY,KAAK,IACFsC,KAAK,CAACC,YAAN,CAAmBvC,KAAnB,EAA0B;AAAEC,IAAAA,SAAS,EAAE8B,UAAU,WAAI9C,SAAJ,cAAwBe,KAAK,CAACJ,KAAN,CAAYK,SAApC;AAAvB,GAA1B,CAjDR,CADJ;AAqDH,CA/GwD;AAgHzDP,SAAS,CAAC8C,WAAV,GAAwBxD,cAAxB;AACAU,SAAS,CAACO,SAAV,GAAsBhB,SAAtB;AACAS,SAAS,CAAC+C,YAAV,GAAyBtD,aAAzB;;;;"}
package/package.json CHANGED
@@ -7,8 +7,8 @@
7
7
  },
8
8
  "dependencies": {
9
9
  "@juggle/resize-observer": "^3.2.0",
10
- "@lumx/core": "^2.1.9-alpha-thumbnail14",
11
- "@lumx/icons": "^2.1.9-alpha-thumbnail14",
10
+ "@lumx/core": "^2.1.9-alpha-thumbnail18",
11
+ "@lumx/icons": "^2.1.9-alpha-thumbnail18",
12
12
  "@popperjs/core": "^2.5.4",
13
13
  "body-scroll-lock": "^3.1.5",
14
14
  "classnames": "^2.2.6",
@@ -120,6 +120,6 @@
120
120
  "build:storybook": "cd storybook && ./build"
121
121
  },
122
122
  "sideEffects": false,
123
- "version": "2.1.9-alpha-thumbnail14",
124
- "gitHead": "29022275aab278a3f761e561f98a3ec18bd9ad21"
123
+ "version": "2.1.9-alpha-thumbnail18",
124
+ "gitHead": "3fe7dcafc62926ccfeda5c507b41b4cb9a2fac92"
125
125
  }
@@ -85,16 +85,15 @@ export const WithBadge = () => {
85
85
 
86
86
  export const FocusPoint = () => {
87
87
  const focusPoint = { x: focusKnob('Focus X ', -0.2), y: focusKnob('Focus Y', -0.3) };
88
- const aspectRatio = enumKnob('Aspect ratio', [undefined, ...Object.values(AspectRatio)], AspectRatio.free);
88
+ const aspectRatio = enumKnob('Aspect ratio', [undefined, ...Object.values(AspectRatio)], AspectRatio.wide);
89
89
  const fillHeight = aspectRatio === AspectRatio.free;
90
90
  return (
91
91
  <>
92
92
  <small>Focus point will delay the display of the image if the original image size is not accessible.</small>
93
-
94
93
  <Resizable initialSize={{ height: 200, width: 300 }}>
95
94
  <Thumbnail
96
95
  alt="Image"
97
- image={IMAGES.portrait1}
96
+ image={IMAGES.portrait1s200}
98
97
  aspectRatio={aspectRatio}
99
98
  fillHeight={fillHeight}
100
99
  focusPoint={focusPoint}
@@ -13,11 +13,7 @@ exports[`<Thumbnail> Snapshots and structure should render story 'Clickable' 1`]
13
13
  className="lumx-thumbnail__image lumx-thumbnail__image--is-loading"
14
14
  loading="lazy"
15
15
  src="/demo-assets/landscape1.jpg"
16
- style={
17
- Object {
18
- "objectPosition": undefined,
19
- }
20
- }
16
+ style={Object {}}
21
17
  />
22
18
  </div>
23
19
  </button>
@@ -36,11 +32,7 @@ exports[`<Thumbnail> Snapshots and structure should render story 'ClickableCusto
36
32
  className="lumx-thumbnail__image lumx-thumbnail__image--is-loading"
37
33
  loading="lazy"
38
34
  src="/demo-assets/landscape1.jpg"
39
- style={
40
- Object {
41
- "objectPosition": undefined,
42
- }
43
- }
35
+ style={Object {}}
44
36
  />
45
37
  </div>
46
38
  </CustomLinkComponent>
@@ -59,11 +51,7 @@ exports[`<Thumbnail> Snapshots and structure should render story 'ClickableLink'
59
51
  className="lumx-thumbnail__image lumx-thumbnail__image--is-loading"
60
52
  loading="lazy"
61
53
  src="/demo-assets/landscape1.jpg"
62
- style={
63
- Object {
64
- "objectPosition": undefined,
65
- }
66
- }
54
+ style={Object {}}
67
55
  />
68
56
  </div>
69
57
  </a>
@@ -81,11 +69,7 @@ exports[`<Thumbnail> Snapshots and structure should render story 'Default' 1`] =
81
69
  className="lumx-thumbnail__image lumx-thumbnail__image--is-loading"
82
70
  loading="lazy"
83
71
  src="/demo-assets/landscape1.jpg"
84
- style={
85
- Object {
86
- "objectPosition": undefined,
87
- }
88
- }
72
+ style={Object {}}
89
73
  />
90
74
  </div>
91
75
  </div>
@@ -103,11 +87,7 @@ exports[`<Thumbnail> Snapshots and structure should render story 'WithBadge' 1`]
103
87
  className="lumx-thumbnail__image lumx-thumbnail__image--is-loading"
104
88
  loading="lazy"
105
89
  src="/demo-assets/landscape1.jpg"
106
- style={
107
- Object {
108
- "objectPosition": undefined,
109
- }
110
- }
90
+ style={Object {}}
111
91
  />
112
92
  </div>
113
93
  <Badge
@@ -1,55 +1,89 @@
1
- import { CSSProperties, useMemo } from 'react';
1
+ import { CSSProperties, useEffect, useMemo, useState } from 'react';
2
2
  import { AspectRatio } from '@lumx/react/components';
3
3
  import { ThumbnailProps } from '@lumx/react/components/thumbnail/Thumbnail';
4
4
 
5
- function shiftPosition(
6
- scale: number,
7
- containerSize: number,
8
- imageSize: number,
9
- focusSize: number | undefined,
10
- isVertical?: boolean,
11
- ) {
12
- if (!focusSize) return 50;
13
- const focusFactor = (focusSize + 1) / 2;
14
- const scaledSize = Math.floor(imageSize / scale);
15
- let focus = Math.floor(focusFactor * scaledSize);
16
- if (isVertical) focus = scaledSize - focus;
17
-
18
- const containerCenter = Math.floor(containerSize / 2);
19
- let focusOffset = focus - containerCenter;
20
- const remainder = scaledSize - focus;
21
- if (remainder < containerCenter) focusOffset -= containerCenter - remainder;
22
- if (focusOffset < 0) return 0;
23
-
24
- return Math.min(100, Math.floor((focusOffset * 100) / containerSize));
5
+ // Calculate shift to center the focus point in the container.
6
+ function shiftPosition(scale: number, focusPoint: number, imageSize: number, containerSize: number) {
7
+ const scaledSize = imageSize / scale;
8
+ const scaledFocusHeight = focusPoint * scaledSize;
9
+ const startFocus = scaledFocusHeight - containerSize / 2;
10
+ const shift = startFocus / (scaledSize - containerSize);
11
+ return Math.floor(Math.max(Math.min(shift, 1), 0) * 100);
25
12
  }
26
13
 
14
+ type Size = { width: number; height: number };
15
+
16
+ // Compute CSS properties to apply the focus point.
27
17
  export const useFocusPointStyle = (
28
18
  { image, aspectRatio, focusPoint, imgProps: { width, height } = {} }: ThumbnailProps,
29
19
  element: HTMLImageElement | undefined,
30
20
  isLoaded: boolean,
31
21
  ): CSSProperties => {
32
22
  // Get natural image size from imgProps or img element.
33
- const naturalSize = useMemo(() => {
23
+ const imageSize: Size | undefined = useMemo(() => {
24
+ // Focus point is not applicable => exit early
34
25
  if (!image || aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) return undefined;
35
26
  if (typeof width === 'number' && typeof height === 'number') return { width, height };
36
27
  if (element && isLoaded) return { width: element.naturalWidth, height: element.naturalHeight };
37
28
  return undefined;
38
29
  }, [aspectRatio, element, focusPoint?.x, focusPoint?.y, height, image, isLoaded, width]);
39
30
 
40
- // Compute focus point CSS style.
41
- return useMemo(() => {
42
- if (aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) return {};
43
- if (element && naturalSize) {
44
- const actualWidth = element.offsetWidth;
45
- const actualHeight = element.offsetHeight;
46
- const heightScale = actualHeight / naturalSize.height;
47
- const widthScale = actualWidth / naturalSize.width;
48
- const x = shiftPosition(heightScale, actualWidth, naturalSize.width, focusPoint?.x);
49
- const y = shiftPosition(widthScale, actualHeight, naturalSize.height, focusPoint?.y, true);
50
- return { objectPosition: `${x}% ${y}%` };
31
+ // Get container size (dependant on imageSize).
32
+ const [containerSize, setContainerSize] = useState<Size | undefined>(undefined);
33
+ useEffect(
34
+ function updateContainerSize() {
35
+ const cWidth = element?.offsetWidth;
36
+ const cHeight = element?.offsetHeight;
37
+ if (cWidth && cHeight) {
38
+ // Update only if needed.
39
+ setContainerSize((oldContainerSize) =>
40
+ oldContainerSize?.width === cWidth && oldContainerSize.height === cHeight
41
+ ? oldContainerSize
42
+ : { width: cWidth, height: cHeight },
43
+ );
44
+ } else if (imageSize) {
45
+ // Wait for a render (in case the container size is dependent on the image size).
46
+ requestAnimationFrame(updateContainerSize);
47
+ }
48
+ },
49
+ [element?.offsetHeight, element?.offsetWidth, imageSize],
50
+ );
51
+
52
+ // Compute style.
53
+ const [style, setStyle] = useState<CSSProperties>({});
54
+ useEffect(() => {
55
+ // Focus point is not applicable => exit early
56
+ if (!image || aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) {
57
+ return;
58
+ }
59
+ if (!element || !imageSize) {
60
+ // Focus point can be computed but now right now (image size unknown).
61
+ setStyle({ visibility: 'hidden' });
62
+ return;
51
63
  }
52
- // Focus point can't be computed yet => We hide the image until it can.
53
- return { visibility: 'hidden' };
54
- }, [aspectRatio, element, focusPoint, naturalSize]);
64
+ if (!containerSize) {
65
+ // Missing container size abort focus point compute.
66
+ setStyle({});
67
+ return;
68
+ }
69
+
70
+ const heightScale = imageSize.height / containerSize.height;
71
+ const widthScale = imageSize.width / containerSize.width;
72
+ const scale = Math.min(widthScale, heightScale);
73
+
74
+ // Focus Y relative to the top (instead of the center)
75
+ const focusPointFromTop = Math.abs((focusPoint?.y || 0) - 1) / 2;
76
+ const y = shiftPosition(scale, focusPointFromTop, imageSize.height, containerSize.height);
77
+
78
+ // Focus X relative to the left (instead of the center)
79
+ const focusPointFromLeft = Math.abs((focusPoint?.x || 0) + 1) / 2;
80
+ const x = shiftPosition(scale, focusPointFromLeft, imageSize.width, containerSize.width);
81
+
82
+ const objectPosition = `${x}% ${y}%`;
83
+
84
+ // Update only if needed.
85
+ setStyle((oldStyle) => (oldStyle.objectPosition === objectPosition ? oldStyle : { objectPosition }));
86
+ }, [aspectRatio, containerSize, element, focusPoint?.x, focusPoint?.y, image, imageSize]);
87
+
88
+ return style;
55
89
  };