-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
components/commmon: Gallery component improvements (#1708)
* components/commmon: Gallery component improvements - Improved logic to render the Gallery Component more efficiently - Expanded the size of the images, when FullScreen is toggled - Added a button to minimize fullscreen slider * common/Gallery: Fix component crash when child is a single image In some situations, only a single image is uploaded, and in that case child.props.children is an object rather, than an array to map it over. * common/ImageSlider: Hide backdrop on FullScreenImageSlider Avoid minimizing gallery due to accidental touches
- Loading branch information
1 parent
852fdd7
commit 04c5d2d
Showing
6 changed files
with
160 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React, { useMemo, useRef, useState } from 'react' | ||
import { FullScreenImageSlider } from './ImageSlider' | ||
import { ImageSlider } from 'components/common/campaign-file/roles' | ||
|
||
type ChildComponentProps = { | ||
children: JSX.Element[] | JSX.Element | ||
images: ImageSlider[] | ||
} | ||
|
||
/** | ||
* Gallery component which allows images to expand whenever clicked. | ||
* @param {React.JSX.Element | React.JSX.Element[]} children Single React Element , or array of React Elements. | ||
* @param {ImageSlider[]} images List of images to show when gallery is expanded | ||
* @returns | ||
*/ | ||
export default function Gallery({ children, images }: ChildComponentProps) { | ||
const initialImage = useRef<number>(0) | ||
const [toggleFullScreeSlider, setToggleFullScreenSlider] = useState(false) | ||
|
||
const onOpenFullScreenSlider = (index: number) => { | ||
setToggleFullScreenSlider(true) | ||
initialImage.current = index | ||
} | ||
|
||
const onCloseFullScreenSlider = () => { | ||
setToggleFullScreenSlider(false) | ||
initialImage.current = 0 | ||
} | ||
|
||
const NestedChild = useMemo(() => { | ||
const childrenCount = React.Children.count(children) | ||
|
||
//if childrenCount > 1, assume list of thumbnail images are shown | ||
if (childrenCount > 1) { | ||
return React.Children.map(children, (child, index) => { | ||
return React.cloneElement(child, { | ||
onClick: () => onOpenFullScreenSlider(index), | ||
style: { cursor: 'pointer' }, | ||
}) | ||
}) | ||
} | ||
|
||
//if childrenCount === 1, assume the child is a component such as slider or gallery grid, and the list of images are nested. | ||
return React.Children.map(children, (child) => { | ||
//In some situations the child could be a single JSX.Element containing an image, thus child.props.children is an object rather than array | ||
if (images.length === 1) { | ||
return React.cloneElement(child, { | ||
style: { cursor: 'pointer' }, | ||
onClick: () => onOpenFullScreenSlider(0), | ||
}) | ||
} | ||
|
||
const modifiedNestedChild = child.props.children.map((elem: JSX.Element, index: number) => { | ||
return React.cloneElement(elem, { | ||
onClick: () => onOpenFullScreenSlider(index), | ||
}) | ||
}) | ||
|
||
return React.cloneElement(child, { style: { cursor: 'pointer' } }, modifiedNestedChild) | ||
}) | ||
}, []) | ||
|
||
return ( | ||
<> | ||
{NestedChild} | ||
{toggleFullScreeSlider && ( | ||
<FullScreenImageSlider | ||
sliderImages={images} | ||
onOpen={toggleFullScreeSlider} | ||
onClose={onCloseFullScreenSlider} | ||
initialImage={initialImage} | ||
/> | ||
)} | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.