@lumx/react 3.7.6-alpha.2 → 3.7.6-alpha.4
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/index.d.ts +8 -7
- package/index.js +27 -20
- package/index.js.map +1 -1
- package/package.json +3 -3
- package/src/components/image-lightbox/ImageLightbox.stories.tsx +3 -3
- package/src/components/image-lightbox/internal/ImageSlide.tsx +12 -5
- package/src/components/image-lightbox/internal/ImageSlideshow.tsx +17 -6
- package/src/components/image-lightbox/types.ts +9 -11
- package/src/components/image-lightbox/useImageLightbox.tsx +11 -9
- package/src/hooks/useElementSizeDependentOfWindowSize.ts +2 -2
package/package.json
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@juggle/resize-observer": "^3.2.0",
|
|
10
|
-
"@lumx/core": "^3.7.6-alpha.
|
|
11
|
-
"@lumx/icons": "^3.7.6-alpha.
|
|
10
|
+
"@lumx/core": "^3.7.6-alpha.4",
|
|
11
|
+
"@lumx/icons": "^3.7.6-alpha.4",
|
|
12
12
|
"@popperjs/core": "^2.5.4",
|
|
13
13
|
"body-scroll-lock": "^3.1.5",
|
|
14
14
|
"classnames": "^2.3.2",
|
|
@@ -112,5 +112,5 @@
|
|
|
112
112
|
"build:storybook": "storybook build"
|
|
113
113
|
},
|
|
114
114
|
"sideEffects": false,
|
|
115
|
-
"version": "3.7.6-alpha.
|
|
115
|
+
"version": "3.7.6-alpha.4"
|
|
116
116
|
}
|
|
@@ -128,8 +128,8 @@ export const WithButtonTrigger = {
|
|
|
128
128
|
return (
|
|
129
129
|
<>
|
|
130
130
|
<Story args={{ ...args, ...imageLightboxProps }} />
|
|
131
|
-
<Button {...(getTriggerProps(0) as any)}>Image 1</Button>
|
|
132
|
-
<Button {...(getTriggerProps(1) as any)}>Image 2</Button>
|
|
131
|
+
<Button {...(getTriggerProps({ activeImageIndex: 0 }) as any)}>Image 1</Button>
|
|
132
|
+
<Button {...(getTriggerProps({ activeImageIndex: 1 }) as any)}>Image 2</Button>
|
|
133
133
|
</>
|
|
134
134
|
);
|
|
135
135
|
},
|
|
@@ -152,7 +152,7 @@ export const WithMosaicTrigger = {
|
|
|
152
152
|
<Mosaic
|
|
153
153
|
thumbnails={MULTIPLE_IMAGES.map((image, index) => ({
|
|
154
154
|
...image,
|
|
155
|
-
...getTriggerProps(index),
|
|
155
|
+
...getTriggerProps({ activeImageIndex: index }),
|
|
156
156
|
}))}
|
|
157
157
|
/>
|
|
158
158
|
</>
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
|
+
import isEqual from 'lodash/isEqual';
|
|
3
4
|
import { SlideshowItem, Thumbnail } from '@lumx/react';
|
|
4
5
|
import { mergeRefs } from '@lumx/react/utils/mergeRefs';
|
|
5
6
|
import { useElementSizeDependentOfWindowSize } from '@lumx/react/hooks/useElementSizeDependentOfWindowSize';
|
|
6
7
|
import { useImageSize } from '@lumx/react/hooks/useImageSize';
|
|
7
|
-
|
|
8
8
|
import { getPrefersReducedMotion } from '@lumx/react/utils/getPrefersReducedMotion';
|
|
9
|
+
|
|
9
10
|
import { CLASSNAME } from '../constants';
|
|
10
11
|
import { usePointerZoom } from './usePointerZoom';
|
|
11
12
|
import { useAnimateScroll } from './useAnimateScroll';
|
|
12
|
-
import type {
|
|
13
|
+
import type { ImageProps } from '../types';
|
|
13
14
|
|
|
14
|
-
export interface ImageSlideProps
|
|
15
|
+
export interface ImageSlideProps {
|
|
16
|
+
image: ImageProps;
|
|
15
17
|
isActive?: boolean;
|
|
16
18
|
scale?: number;
|
|
17
19
|
onScaleChange?: (value: number) => void;
|
|
@@ -19,7 +21,12 @@ export interface ImageSlideProps extends InheritedThumbnailProps {
|
|
|
19
21
|
|
|
20
22
|
/** Internal image slide component for ImageLightbox */
|
|
21
23
|
export const ImageSlide = React.memo((props: ImageSlideProps) => {
|
|
22
|
-
const {
|
|
24
|
+
const {
|
|
25
|
+
isActive,
|
|
26
|
+
scale,
|
|
27
|
+
onScaleChange,
|
|
28
|
+
image: { image, imgRef: propImgRef, imgProps, alt },
|
|
29
|
+
} = props;
|
|
23
30
|
|
|
24
31
|
// Get scroll area size
|
|
25
32
|
const scrollAreaRef = React.useRef<HTMLDivElement>(null);
|
|
@@ -96,4 +103,4 @@ export const ImageSlide = React.memo((props: ImageSlideProps) => {
|
|
|
96
103
|
/>
|
|
97
104
|
</SlideshowItem>
|
|
98
105
|
);
|
|
99
|
-
});
|
|
106
|
+
}, isEqual);
|
|
@@ -4,12 +4,13 @@ import { mdiMagnifyMinusOutline, mdiMagnifyPlusOutline } from '@lumx/icons';
|
|
|
4
4
|
import { FlexBox, IconButton, Slides, SlideshowControls } from '@lumx/react';
|
|
5
5
|
import { mergeRefs } from '@lumx/react/utils/mergeRefs';
|
|
6
6
|
|
|
7
|
+
import memoize from 'lodash/memoize';
|
|
7
8
|
import { ImageCaption } from '../../image-block/ImageCaption';
|
|
8
9
|
import { CLASSNAME } from '../constants';
|
|
9
|
-
import type { ImagesProps, InheritedSlideShowProps,
|
|
10
|
+
import type { ImagesProps, InheritedSlideShowProps, ZoomButtonProps } from '../types';
|
|
10
11
|
import { ImageSlide } from './ImageSlide';
|
|
11
12
|
|
|
12
|
-
export interface ImageSlideshowProps extends InheritedSlideShowProps,
|
|
13
|
+
export interface ImageSlideshowProps extends InheritedSlideShowProps, ZoomButtonProps, ImagesProps {
|
|
13
14
|
currentPaginationItemRef?: React.Ref<HTMLButtonElement>;
|
|
14
15
|
footerRef?: React.Ref<HTMLDivElement>;
|
|
15
16
|
}
|
|
@@ -110,6 +111,14 @@ export const ImageSlideshow: React.FC<ImageSlideshowProps> = ({
|
|
|
110
111
|
</>
|
|
111
112
|
);
|
|
112
113
|
|
|
114
|
+
const getImgRef = React.useMemo(
|
|
115
|
+
() =>
|
|
116
|
+
memoize((index: number) =>
|
|
117
|
+
mergeRefs(images?.[index].imgRef, index === activeImageIndex ? activeImageRef : undefined),
|
|
118
|
+
),
|
|
119
|
+
[images, activeImageIndex, activeImageRef],
|
|
120
|
+
);
|
|
121
|
+
|
|
113
122
|
return (
|
|
114
123
|
<>
|
|
115
124
|
<Slides
|
|
@@ -122,15 +131,17 @@ export const ImageSlideshow: React.FC<ImageSlideshowProps> = ({
|
|
|
122
131
|
slidesId={slideshowSlidesId}
|
|
123
132
|
toggleAutoPlay={toggleAutoPlay}
|
|
124
133
|
>
|
|
125
|
-
{images.map(({ image, imgRef, ...
|
|
134
|
+
{images.map(({ image, imgRef, ...imageProps }, index) => {
|
|
126
135
|
const isActive = index === activeIndex;
|
|
127
136
|
return (
|
|
128
137
|
<ImageSlide
|
|
129
|
-
{...props}
|
|
130
138
|
isActive={isActive}
|
|
131
139
|
key={image}
|
|
132
|
-
|
|
133
|
-
|
|
140
|
+
image={{
|
|
141
|
+
...imageProps,
|
|
142
|
+
image,
|
|
143
|
+
imgRef: getImgRef(index),
|
|
144
|
+
}}
|
|
134
145
|
scale={isActive ? scale : undefined}
|
|
135
146
|
onScaleChange={onScaleChange}
|
|
136
147
|
/>
|
|
@@ -4,14 +4,12 @@ import type { IconButtonProps, LightboxProps, SlideshowProps, ThumbnailProps } f
|
|
|
4
4
|
import type { HasClassName } from '@lumx/react/utils/type';
|
|
5
5
|
import type { ImageCaptionMetadata } from '@lumx/react/components/image-block/ImageCaption';
|
|
6
6
|
|
|
7
|
-
export type InheritedSlideShowProps = Pick<
|
|
8
|
-
SlideshowProps,
|
|
9
|
-
'activeIndex' | 'slideshowControlsProps' | 'slideGroupLabel'
|
|
10
|
-
>;
|
|
7
|
+
export type InheritedSlideShowProps = Pick<SlideshowProps, 'slideshowControlsProps' | 'slideGroupLabel'>;
|
|
11
8
|
|
|
12
|
-
export interface
|
|
13
|
-
/** */
|
|
9
|
+
export interface ZoomButtonProps {
|
|
10
|
+
/** Zoom in button props */
|
|
14
11
|
zoomInButtonProps?: IconButtonProps;
|
|
12
|
+
/** Zoom out button props */
|
|
15
13
|
zoomOutButtonProps?: IconButtonProps;
|
|
16
14
|
}
|
|
17
15
|
|
|
@@ -30,9 +28,10 @@ export interface ImagesProps {
|
|
|
30
28
|
activeImageRef?: React.Ref<HTMLImageElement>;
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
export type InheritedLightboxProps = Pick<
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
export type InheritedLightboxProps = Pick<
|
|
32
|
+
LightboxProps,
|
|
33
|
+
'isOpen' | 'parentElement' | 'onClose' | 'closeButtonProps' | 'aria-label' | 'aria-labelledby'
|
|
34
|
+
>;
|
|
36
35
|
|
|
37
36
|
export type ForwardedProps = React.ComponentPropsWithoutRef<'div'>;
|
|
38
37
|
|
|
@@ -41,9 +40,8 @@ export type ForwardedProps = React.ComponentPropsWithoutRef<'div'>;
|
|
|
41
40
|
*/
|
|
42
41
|
export interface ImageLightboxProps
|
|
43
42
|
extends HasClassName,
|
|
44
|
-
|
|
43
|
+
ZoomButtonProps,
|
|
45
44
|
ImagesProps,
|
|
46
45
|
InheritedSlideShowProps,
|
|
47
46
|
InheritedLightboxProps,
|
|
48
|
-
InheritedAriaAttributes,
|
|
49
47
|
ForwardedProps {}
|
|
@@ -16,6 +16,8 @@ type ManagedProps = Pick<
|
|
|
16
16
|
|
|
17
17
|
const EMPTY_PROPS: ManagedProps = { isOpen: false, images: [], parentElement: React.createRef() };
|
|
18
18
|
|
|
19
|
+
type TriggerOptions = Pick<ImageLightboxProps, 'activeImageIndex'>;
|
|
20
|
+
|
|
19
21
|
/**
|
|
20
22
|
* Set up an ImageLightbox with images and triggers.
|
|
21
23
|
*
|
|
@@ -32,7 +34,7 @@ export function useImageLightbox<P extends Partial<ImageLightboxProps>>(
|
|
|
32
34
|
* Generates trigger props
|
|
33
35
|
* @param index Provide an index to choose which image to display when the image lightbox opens.
|
|
34
36
|
* */
|
|
35
|
-
getTriggerProps: (
|
|
37
|
+
getTriggerProps: (options?: TriggerOptions) => { onClick: React.MouseEventHandler; ref: React.Ref<any> };
|
|
36
38
|
/** Props to forward to the ImageLightbox */
|
|
37
39
|
imageLightboxProps: P & ManagedProps;
|
|
38
40
|
} {
|
|
@@ -71,20 +73,19 @@ export function useImageLightbox<P extends Partial<ImageLightboxProps>>(
|
|
|
71
73
|
// Morph from the image in lightbox to the image in trigger
|
|
72
74
|
viewTransitionName: {
|
|
73
75
|
source: currentImageRef,
|
|
74
|
-
//source: imageIsVisible ? currentImageRef : null,
|
|
75
76
|
target: triggerImageRefs[currentIndex],
|
|
76
77
|
name: CLASSNAME,
|
|
77
78
|
},
|
|
78
79
|
});
|
|
79
80
|
}
|
|
80
81
|
|
|
81
|
-
async function openLightbox(triggerElement: HTMLElement,
|
|
82
|
+
async function openLightbox(triggerElement: HTMLElement, { activeImageIndex }: TriggerOptions = {}) {
|
|
82
83
|
// If we find an image inside the trigger, animate it in transition with the opening image
|
|
83
|
-
const triggerImage = triggerImageRefs[
|
|
84
|
+
const triggerImage = triggerImageRefs[activeImageIndex as any]?.current || findImage(triggerElement);
|
|
84
85
|
|
|
85
86
|
// Inject the trigger image size as a fallback for better loading state
|
|
86
87
|
const imagesWithFallbackSize = imagesPropsRef.current.map((image, idx) => {
|
|
87
|
-
if (triggerImage && idx ===
|
|
88
|
+
if (triggerImage && idx === activeImageIndex && !image.imgProps?.width && !image.imgProps?.height) {
|
|
88
89
|
const imgProps = {
|
|
89
90
|
...image.imgProps,
|
|
90
91
|
height: triggerImage.naturalHeight,
|
|
@@ -105,7 +106,7 @@ export function useImageLightbox<P extends Partial<ImageLightboxProps>>(
|
|
|
105
106
|
isOpen: true,
|
|
106
107
|
onClose: closeLightbox,
|
|
107
108
|
images: imagesWithFallbackSize,
|
|
108
|
-
activeImageIndex:
|
|
109
|
+
activeImageIndex: activeImageIndex || 0,
|
|
109
110
|
});
|
|
110
111
|
},
|
|
111
112
|
// Morph from the image in trigger to the image in lightbox
|
|
@@ -117,13 +118,14 @@ export function useImageLightbox<P extends Partial<ImageLightboxProps>>(
|
|
|
117
118
|
});
|
|
118
119
|
}
|
|
119
120
|
|
|
120
|
-
return memoize((
|
|
121
|
+
return memoize((options?: TriggerOptions) => ({
|
|
121
122
|
ref(element: HTMLElement | null) {
|
|
122
123
|
const triggerImage = findImage(element);
|
|
123
|
-
if (
|
|
124
|
+
if (options?.activeImageIndex !== undefined && triggerImage)
|
|
125
|
+
triggerImageRefs[options.activeImageIndex] = { current: triggerImage };
|
|
124
126
|
},
|
|
125
127
|
onClick(e: React.MouseEvent) {
|
|
126
|
-
openLightbox(e.target as HTMLElement,
|
|
128
|
+
openLightbox(e.target as HTMLElement, options);
|
|
127
129
|
},
|
|
128
130
|
}));
|
|
129
131
|
}, []);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
|
-
import { RectSize } from '@lumx/react/utils/type';
|
|
4
3
|
import throttle from 'lodash/throttle';
|
|
4
|
+
import { RectSize } from '@lumx/react/utils/type';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Observe element size (only works if it's size depends on the window size).
|
|
@@ -18,7 +18,7 @@ export function useElementSizeDependentOfWindowSize(
|
|
|
18
18
|
const updateSize = React.useMemo(
|
|
19
19
|
() =>
|
|
20
20
|
throttle(() => {
|
|
21
|
-
const newSize = elementRef
|
|
21
|
+
const newSize = elementRef.current?.getBoundingClientRect();
|
|
22
22
|
if (newSize) setSize(newSize);
|
|
23
23
|
}, 10),
|
|
24
24
|
[elementRef],
|