-
Notifications
You must be signed in to change notification settings - Fork 92
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
- 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
- Loading branch information
1 parent
ae3f223
commit f9a0ec0
Showing
6 changed files
with
149 additions
and
115 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,67 @@ | ||
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) => { | ||
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.