@lumx/react 3.7.6-alpha.0 → 3.7.6-alpha.2

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.0",
11
- "@lumx/icons": "^3.7.6-alpha.0",
10
+ "@lumx/core": "^3.7.6-alpha.2",
11
+ "@lumx/icons": "^3.7.6-alpha.2",
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.0"
115
+ "version": "3.7.6-alpha.2"
116
116
  }
@@ -119,10 +119,12 @@ 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 }} />
@@ -143,7 +145,7 @@ 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 }} />
@@ -23,24 +23,33 @@ const EMPTY_PROPS: ManagedProps = { isOpen: false, images: [], parentElement: Re
23
23
  * - Associate a trigger with an image to display on open
24
24
  * - Automatically provide a view transition between an image trigger and the displayed image on open & close
25
25
  *
26
- * @param images Images to display in the image lightbox
26
+ * @param initialProps Images to display in the image lightbox
27
27
  */
28
- export function useImageLightbox(images: ImageLightboxProps['images'] = []): {
28
+ export function useImageLightbox<P extends Partial<ImageLightboxProps>>(
29
+ initialProps: P,
30
+ ): {
29
31
  /**
30
32
  * Generates trigger props
31
33
  * @param index Provide an index to choose which image to display when the image lightbox opens.
32
34
  * */
33
35
  getTriggerProps: (index?: number) => { onClick: React.MouseEventHandler; ref: React.Ref<any> };
34
36
  /** Props to forward to the ImageLightbox */
35
- imageLightboxProps: ManagedProps;
37
+ imageLightboxProps: P & ManagedProps;
36
38
  } {
39
+ const { images = [], ...otherProps } = initialProps;
40
+
41
+ const basePropsRef = React.useRef(EMPTY_PROPS as P & ManagedProps);
42
+ React.useEffect(() => {
43
+ basePropsRef.current = { ...EMPTY_PROPS, ...otherProps } as P & ManagedProps;
44
+ }, [otherProps]);
45
+
37
46
  const imagesPropsRef = React.useRef(images);
38
47
  React.useEffect(() => {
39
48
  imagesPropsRef.current = images.map((props) => ({ imgRef: React.createRef(), ...props }));
40
49
  }, [images]);
41
50
 
42
51
  const currentImageRef = React.useRef<HTMLImageElement>(null);
43
- const [imageLightboxProps, setImageLightboxProps] = React.useState<ManagedProps>(EMPTY_PROPS);
52
+ const [imageLightboxProps, setImageLightboxProps] = React.useState<P & ManagedProps>(basePropsRef.current);
44
53
 
45
54
  const getTriggerProps = React.useMemo(() => {
46
55
  const triggerImageRefs: Record<number, React.RefObject<HTMLImageElement>> = {};
@@ -57,7 +66,7 @@ export function useImageLightbox(images: ImageLightboxProps['images'] = []): {
57
66
  await startViewTransition({
58
67
  changes() {
59
68
  // Close lightbox with reset empty props
60
- setImageLightboxProps(({ parentElement }) => ({ ...EMPTY_PROPS, parentElement }));
69
+ setImageLightboxProps(({ parentElement }) => ({ ...basePropsRef.current, parentElement }));
61
70
  },
62
71
  // Morph from the image in lightbox to the image in trigger
63
72
  viewTransitionName: {
@@ -90,6 +99,7 @@ export function useImageLightbox(images: ImageLightboxProps['images'] = []): {
90
99
  changes: () => {
91
100
  // Open lightbox with setup props
92
101
  setImageLightboxProps({
102
+ ...basePropsRef.current,
93
103
  activeImageRef: currentImageRef,
94
104
  parentElement: { current: triggerElement },
95
105
  isOpen: true,