@lumx/react 3.7.6-alpha.1 → 3.7.6-alpha.3

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/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.1",
11
- "@lumx/icons": "^3.7.6-alpha.1",
10
+ "@lumx/core": "^3.7.6-alpha.3",
11
+ "@lumx/icons": "^3.7.6-alpha.3",
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.1"
115
+ "version": "3.7.6-alpha.3"
116
116
  }
@@ -119,15 +119,17 @@ export const MultipleImagesWithZoom = {
119
119
  export const WithButtonTrigger = {
120
120
  decorators: [
121
121
  (Story: any, { args }: any) => {
122
- const { getTriggerProps, imageLightboxProps } = ImageLightbox.useImageLightbox([
123
- { image: IMAGES.portrait1s200, alt: 'Image 1' },
124
- { image: IMAGES.landscape1s200, alt: 'Image 2' },
125
- ]);
122
+ const { getTriggerProps, imageLightboxProps } = ImageLightbox.useImageLightbox({
123
+ images: [
124
+ { image: IMAGES.portrait1s200, alt: 'Image 1' },
125
+ { image: IMAGES.landscape1s200, alt: 'Image 2' },
126
+ ],
127
+ });
126
128
  return (
127
129
  <>
128
130
  <Story args={{ ...args, ...imageLightboxProps }} />
129
- <Button {...(getTriggerProps(0) as any)}>Image 1</Button>
130
- <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>
131
133
  </>
132
134
  );
133
135
  },
@@ -143,14 +145,14 @@ export const WithMosaicTrigger = {
143
145
  args: { ...SLIDESHOW_PROPS, ...ZOOM_PROPS },
144
146
  decorators: [
145
147
  (Story: any, { args }: any) => {
146
- const { getTriggerProps, imageLightboxProps } = ImageLightbox.useImageLightbox(MULTIPLE_IMAGES);
148
+ const { getTriggerProps, imageLightboxProps } = ImageLightbox.useImageLightbox({ images: MULTIPLE_IMAGES });
147
149
  return (
148
150
  <>
149
151
  <Story args={{ ...args, ...imageLightboxProps }} />
150
152
  <Mosaic
151
153
  thumbnails={MULTIPLE_IMAGES.map((image, index) => ({
152
154
  ...image,
153
- ...getTriggerProps(index),
155
+ ...getTriggerProps({ activeImageIndex: index }),
154
156
  }))}
155
157
  />
156
158
  </>
@@ -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
  *
@@ -23,24 +25,33 @@ const EMPTY_PROPS: ManagedProps = { isOpen: false, images: [], parentElement: Re
23
25
  * - Associate a trigger with an image to display on open
24
26
  * - Automatically provide a view transition between an image trigger and the displayed image on open & close
25
27
  *
26
- * @param images Images to display in the image lightbox
28
+ * @param initialProps Images to display in the image lightbox
27
29
  */
28
- export function useImageLightbox(images: ImageLightboxProps['images'] = []): {
30
+ export function useImageLightbox<P extends Partial<ImageLightboxProps>>(
31
+ initialProps: P,
32
+ ): {
29
33
  /**
30
34
  * Generates trigger props
31
35
  * @param index Provide an index to choose which image to display when the image lightbox opens.
32
36
  * */
33
- getTriggerProps: (index?: number) => { onClick: React.MouseEventHandler; ref: React.Ref<any> };
37
+ getTriggerProps: (options: TriggerOptions) => { onClick: React.MouseEventHandler; ref: React.Ref<any> };
34
38
  /** Props to forward to the ImageLightbox */
35
- imageLightboxProps: ManagedProps;
39
+ imageLightboxProps: P & ManagedProps;
36
40
  } {
41
+ const { images = [], ...otherProps } = initialProps;
42
+
43
+ const basePropsRef = React.useRef(EMPTY_PROPS as P & ManagedProps);
44
+ React.useEffect(() => {
45
+ basePropsRef.current = { ...EMPTY_PROPS, ...otherProps } as P & ManagedProps;
46
+ }, [otherProps]);
47
+
37
48
  const imagesPropsRef = React.useRef(images);
38
49
  React.useEffect(() => {
39
50
  imagesPropsRef.current = images.map((props) => ({ imgRef: React.createRef(), ...props }));
40
51
  }, [images]);
41
52
 
42
53
  const currentImageRef = React.useRef<HTMLImageElement>(null);
43
- const [imageLightboxProps, setImageLightboxProps] = React.useState<ManagedProps>(EMPTY_PROPS);
54
+ const [imageLightboxProps, setImageLightboxProps] = React.useState<P & ManagedProps>(basePropsRef.current);
44
55
 
45
56
  const getTriggerProps = React.useMemo(() => {
46
57
  const triggerImageRefs: Record<number, React.RefObject<HTMLImageElement>> = {};
@@ -57,25 +68,24 @@ export function useImageLightbox(images: ImageLightboxProps['images'] = []): {
57
68
  await startViewTransition({
58
69
  changes() {
59
70
  // Close lightbox with reset empty props
60
- setImageLightboxProps(({ parentElement }) => ({ ...EMPTY_PROPS, parentElement }));
71
+ setImageLightboxProps(({ parentElement }) => ({ ...basePropsRef.current, parentElement }));
61
72
  },
62
73
  // Morph from the image in lightbox to the image in trigger
63
74
  viewTransitionName: {
64
75
  source: currentImageRef,
65
- //source: imageIsVisible ? currentImageRef : null,
66
76
  target: triggerImageRefs[currentIndex],
67
77
  name: CLASSNAME,
68
78
  },
69
79
  });
70
80
  }
71
81
 
72
- async function openLightbox(triggerElement: HTMLElement, index?: number) {
82
+ async function openLightbox(triggerElement: HTMLElement, { activeImageIndex }: TriggerOptions = {}) {
73
83
  // If we find an image inside the trigger, animate it in transition with the opening image
74
- const triggerImage = triggerImageRefs[index as any]?.current || findImage(triggerElement);
84
+ const triggerImage = triggerImageRefs[activeImageIndex as any]?.current || findImage(triggerElement);
75
85
 
76
86
  // Inject the trigger image size as a fallback for better loading state
77
87
  const imagesWithFallbackSize = imagesPropsRef.current.map((image, idx) => {
78
- if (triggerImage && idx === index && !image.imgProps?.width && !image.imgProps?.height) {
88
+ if (triggerImage && idx === activeImageIndex && !image.imgProps?.width && !image.imgProps?.height) {
79
89
  const imgProps = {
80
90
  ...image.imgProps,
81
91
  height: triggerImage.naturalHeight,
@@ -90,12 +100,13 @@ export function useImageLightbox(images: ImageLightboxProps['images'] = []): {
90
100
  changes: () => {
91
101
  // Open lightbox with setup props
92
102
  setImageLightboxProps({
103
+ ...basePropsRef.current,
93
104
  activeImageRef: currentImageRef,
94
105
  parentElement: { current: triggerElement },
95
106
  isOpen: true,
96
107
  onClose: closeLightbox,
97
108
  images: imagesWithFallbackSize,
98
- activeImageIndex: index || 0,
109
+ activeImageIndex: activeImageIndex || 0,
99
110
  });
100
111
  },
101
112
  // Morph from the image in trigger to the image in lightbox
@@ -107,13 +118,14 @@ export function useImageLightbox(images: ImageLightboxProps['images'] = []): {
107
118
  });
108
119
  }
109
120
 
110
- return memoize((index?: number) => ({
121
+ return memoize((options?: TriggerOptions) => ({
111
122
  ref(element: HTMLElement | null) {
112
123
  const triggerImage = findImage(element);
113
- if (index !== undefined && triggerImage) triggerImageRefs[index] = { current: triggerImage };
124
+ if (options?.activeImageIndex !== undefined && triggerImage)
125
+ triggerImageRefs[options.activeImageIndex] = { current: triggerImage };
114
126
  },
115
127
  onClick(e: React.MouseEvent) {
116
- openLightbox(e.target as HTMLElement, index);
128
+ openLightbox(e.target as HTMLElement, options);
117
129
  },
118
130
  }));
119
131
  }, []);