@lumx/react 2.1.9-alpha-thumbnail13 → 2.1.9-alpha-thumbnail17

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,19 +50,15 @@ 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));
65
- }
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);
60
+ } // Compute CSS properties to apply the focus point.
61
+
66
62
 
67
63
  var useFocusPointStyle = function useFocusPointStyle(_ref, element, isLoaded) {
68
64
  var image = _ref.image,
@@ -73,7 +69,7 @@ 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 () {
77
73
  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
74
  if (typeof width === 'number' && typeof height === 'number') return {
79
75
  width: width,
@@ -84,18 +80,25 @@ var useFocusPointStyle = function useFocusPointStyle(_ref, element, isLoaded) {
84
80
  height: element.naturalHeight
85
81
  };
86
82
  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.
83
+ }, [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.
84
+
85
+ var containerWidth = element === null || element === void 0 ? void 0 : element.offsetWidth;
86
+ var containerHeight = element === null || element === void 0 ? void 0 : element.offsetHeight; // Compute focus point CSS style.
88
87
 
89
88
  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);
89
+ if (!image || aspectRatio === AspectRatio.original || !(focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x) && !(focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y)) return {};
90
+
91
+ if (containerWidth && containerHeight && imageSize) {
92
+ var heightScale = imageSize.height / containerHeight;
93
+ var widthScale = imageSize.width / containerWidth;
94
+ var scale = Math.min(widthScale, heightScale); // Focus Y relative to the top (instead of the center)
95
+
96
+ var focusPointFromTop = Math.abs(((focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y) || 0) - 1) / 2;
97
+ var y = shiftPosition(scale, focusPointFromTop, imageSize.height, containerHeight); // Focus X relative to the left (instead of the center)
98
+
99
+ var focusPointFromLeft = Math.abs(((focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x) || 0) + 1) / 2;
100
+ var x = shiftPosition(scale, focusPointFromLeft, imageSize.width, containerWidth);
101
+ console.debug('x, y', aspectRatio, x, y);
99
102
  return {
100
103
  objectPosition: "".concat(x, "% ").concat(y, "%")
101
104
  };
@@ -105,7 +108,8 @@ var useFocusPointStyle = function useFocusPointStyle(_ref, element, isLoaded) {
105
108
  return {
106
109
  visibility: 'hidden'
107
110
  };
108
- }, [aspectRatio, element, focusPoint, naturalSize]);
111
+ }, // Update when the image changes (url or size), when the ratio changes, when the container size changes and when the focus point changes.
112
+ [image, imageSize, aspectRatio, containerHeight, containerWidth, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.x, focusPoint === null || focusPoint === void 0 ? void 0 : focusPoint.y]);
109
113
  };
110
114
 
111
115
  /**
@@ -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,QA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AAK,IAAA,SAAS,YAAKA,SAAL;AAAd,KACI,wCACQlB,QADR;AAEI,IAAA,KAAK,qBACEA,QADF,aACEA,QADF,uBACEA,QAAQ,CAAEoE,KADZ,MAEEhB,eAFF,MAGEH,eAHF,CAFT;AAOI,IAAA,GAAG,EAAEoB,SAAS,CAACvB,aAAD,EAAgBT,UAAhB,CAPlB;AAQI,IAAA,SAAS,EAAE2B,UAAU,WAAI9C,SAAJ,cAAwBqB,SAAS,cAAOrB,SAAP,wBAAjC,CARzB;AASI,IAAA,WAAW,EAAEiB,WATjB;AAUI,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, useMemo } 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\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 = 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 // Get container size.\n const containerWidth = element?.offsetWidth;\n const containerHeight = element?.offsetHeight;\n\n // Compute focus point CSS style.\n return useMemo(\n () => {\n if (!image || aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) return {};\n if (containerWidth && containerHeight && imageSize) {\n const heightScale = imageSize.height / containerHeight;\n const widthScale = imageSize.width / containerWidth;\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, containerHeight);\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, containerWidth);\n\n console.debug('x, y', aspectRatio, x, y);\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 },\n // Update when the image changes (url or size), when the ratio changes, when the container size changes and when the focus point changes.\n [image, imageSize, aspectRatio, containerHeight, containerWidth, focusPoint?.x, focusPoint?.y],\n );\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","containerWidth","offsetWidth","containerHeight","offsetHeight","heightScale","widthScale","focusPointFromTop","abs","focusPointFromLeft","console","debug","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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AAUI,IAAA,GAAG,EAAEzC,KAVT;AAWI,IAAA,GAAG,EAAEsC,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;;;;"}
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-thumbnail13",
11
- "@lumx/icons": "^2.1.9-alpha-thumbnail13",
10
+ "@lumx/core": "^2.1.9-alpha-thumbnail17",
11
+ "@lumx/icons": "^2.1.9-alpha-thumbnail17",
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-thumbnail13",
124
- "gitHead": "18619b44fecc3b26cbbed5ff531b538b4d7ca550"
123
+ "version": "2.1.9-alpha-thumbnail17",
124
+ "gitHead": "2daf2a861216bfae2000004360185c50f683a155"
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
@@ -2,54 +2,57 @@ import { CSSProperties, useMemo } 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
+ // Compute CSS properties to apply the focus point.
27
15
  export const useFocusPointStyle = (
28
16
  { image, aspectRatio, focusPoint, imgProps: { width, height } = {} }: ThumbnailProps,
29
17
  element: HTMLImageElement | undefined,
30
18
  isLoaded: boolean,
31
19
  ): CSSProperties => {
32
20
  // Get natural image size from imgProps or img element.
33
- const naturalSize = useMemo(() => {
21
+ const imageSize = useMemo(() => {
34
22
  if (!image || aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) return undefined;
35
23
  if (typeof width === 'number' && typeof height === 'number') return { width, height };
36
24
  if (element && isLoaded) return { width: element.naturalWidth, height: element.naturalHeight };
37
25
  return undefined;
38
26
  }, [aspectRatio, element, focusPoint?.x, focusPoint?.y, height, image, isLoaded, width]);
39
27
 
28
+ // Get container size.
29
+ const containerWidth = element?.offsetWidth;
30
+ const containerHeight = element?.offsetHeight;
31
+
40
32
  // 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}%` };
51
- }
52
- // Focus point can't be computed yet => We hide the image until it can.
53
- return { visibility: 'hidden' };
54
- }, [aspectRatio, element, focusPoint, naturalSize]);
33
+ return useMemo(
34
+ () => {
35
+ if (!image || aspectRatio === AspectRatio.original || (!focusPoint?.x && !focusPoint?.y)) return {};
36
+ if (containerWidth && containerHeight && imageSize) {
37
+ const heightScale = imageSize.height / containerHeight;
38
+ const widthScale = imageSize.width / containerWidth;
39
+ const scale = Math.min(widthScale, heightScale);
40
+
41
+ // Focus Y relative to the top (instead of the center)
42
+ const focusPointFromTop = Math.abs((focusPoint?.y || 0) - 1) / 2;
43
+ const y = shiftPosition(scale, focusPointFromTop, imageSize.height, containerHeight);
44
+
45
+ // Focus X relative to the left (instead of the center)
46
+ const focusPointFromLeft = Math.abs((focusPoint?.x || 0) + 1) / 2;
47
+ const x = shiftPosition(scale, focusPointFromLeft, imageSize.width, containerWidth);
48
+
49
+ console.debug('x, y', aspectRatio, x, y);
50
+ return { objectPosition: `${x}% ${y}%` };
51
+ }
52
+ // Focus point can't be computed yet => We hide the image until it can.
53
+ return { visibility: 'hidden' };
54
+ },
55
+ // Update when the image changes (url or size), when the ratio changes, when the container size changes and when the focus point changes.
56
+ [image, imageSize, aspectRatio, containerHeight, containerWidth, focusPoint?.x, focusPoint?.y],
57
+ );
55
58
  };