Skip to content

Instantly share code, notes, and snippets.

@KevinVR
Created July 25, 2021 18:31
Show Gist options
  • Save KevinVR/ab3009c3f83ac6334be25c2e3fda0623 to your computer and use it in GitHub Desktop.
Save KevinVR/ab3009c3f83ac6334be25c2e3fda0623 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
const LightBox = ({ children, src, alt, Wrapper = 'div', zIndex = 100 }) => {
const [isOpen, setIsOpen] = useState(false);
const toggleIsOpen = () => {
setIsOpen(!isOpen);
};
return (
<Wrapper onClick={toggleIsOpen}>
{children}
{isOpen ?
<div onClick={toggleIsOpen} style={{
position: 'fixed',
top: '0',
left: '0',
height: '100vh',
width: '100vw',
backgroundColor: 'rgba(0,0,0,0.7)',
cursor: 'pointer',
zIndex
}}>
<img src={src}
alt={alt}
style={{
height: 'auto',
width: '100%'
}}
/>
</div>
: null}
</Wrapper>
);
};
export default LightBox;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment