-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature - Show images in a LightBox when clicked #1473
base: main
Are you sure you want to change the base?
Changes from 9 commits
38d2693
4df0870
dbcd077
06962e3
6beb298
3e50101
3fce1cb
eb8f30e
ec3085e
cb86d5a
52a3817
ebdb069
01afa18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,3 +60,5 @@ dist-ssr | |
.coverage | ||
|
||
backend/README.md | ||
|
||
myenv | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,85 @@ | ||
import { useState } from 'react'; | ||
|
||
import Skeleton from '@mui/material/Skeleton'; | ||
import Lightbox from 'yet-another-react-lightbox'; | ||
import 'yet-another-react-lightbox/styles.css'; | ||
import Zoom from 'yet-another-react-lightbox/plugins/zoom'; | ||
import Download from 'yet-another-react-lightbox/plugins/download'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a lot of dependencies to import, increasing binary size even if the feature is not turned on. How much larger is the output bundle -- and could we somehow make the lightbox optional in the frontend? Perhaps we can set up stuff so it's a separate chunk, only importing the chunk in the browser if and when the lightbox feature is enabled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ill look into it. |
||
|
||
import { type IImageElement } from 'client-types/'; | ||
import { type IImageElement, useConfig } from '@chainlit/react-client'; | ||
|
||
import { FrameElement } from './Frame'; | ||
|
||
interface Props { | ||
element: IImageElement; | ||
} | ||
|
||
const handleImageClick = (name: string, src: string) => { | ||
const width = window.innerWidth / 2; | ||
const height = window.innerHeight / 2; | ||
const left = window.innerWidth / 4; | ||
const top = window.innerHeight / 4; | ||
|
||
const newWindow = window.open( | ||
'', | ||
'_blank', | ||
`width=${width},height=${height},left=${left},top=${top}` | ||
); | ||
if (newWindow) { | ||
newWindow.document.write(` | ||
<html> | ||
<head> | ||
<title>${name}</title> | ||
<link rel="icon" href="/favicon"> | ||
<style> | ||
body { | ||
margin: 0; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
background-color: rgba(0, 0, 0, 0.8); | ||
} | ||
img { | ||
max-width: 100%; | ||
max-height: calc(100% - 50px); | ||
} | ||
a { | ||
margin: 10px 0; | ||
color: white; | ||
text-decoration: none; | ||
font-size: 15px; | ||
background-color: rgba(255, 255, 255, 0.2); | ||
padding: 8px 12px; | ||
border-radius: 5px; | ||
} | ||
a:hover { | ||
background-color: rgba(255, 255, 255, 0.4); | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<img src="${src}" alt="${name}" /> | ||
<a href="${src}" download="${name}">Download</a> | ||
</body> | ||
</html> | ||
`); | ||
newWindow.document.close(); | ||
} | ||
}; | ||
|
||
const ImageElement = ({ element }: Props) => { | ||
const [loading, setLoading] = useState(true); | ||
const [lightboxOpen, setLightboxOpen] = useState(false); | ||
const config = useConfig(); | ||
|
||
if (!element.url) { | ||
return null; | ||
} | ||
|
||
const enableLightbox = config.config?.features.image_lightbox && element.display === 'inline'; | ||
|
||
const handleImageClick = () => { | ||
if (enableLightbox) { | ||
setLightboxOpen(true); | ||
} | ||
}; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like how this lightbox code is much more minimal. However, if I read this well, with the lightbox disabled the old image click behaviour is entirely removed. That's not the way forward; developers and users expect what worked before to keep working towards the future and not suddenly disappear. So let's truly make lightbox optional and make sure that we retain the exact legacy behaviour unless the lightbox is enabled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with this, but since the original feature was not released at the time of making this, i figured that it could be removed all together. Ill fix that. |
||
return ( | ||
<FrameElement> | ||
{loading && <Skeleton variant="rectangular" width="100%" height={200} />} | ||
<img | ||
className={`${element.display}-image`} | ||
src={element.url} | ||
onLoad={() => setLoading(false)} | ||
onClick={() => { | ||
if (element.display === 'inline') { | ||
const name = `${element.name}.png`; | ||
handleImageClick(name, element.url!); | ||
} | ||
}} | ||
onClick={handleImageClick} | ||
style={{ | ||
objectFit: 'cover', | ||
maxWidth: '100%', | ||
margin: 'auto', | ||
height: 'auto', | ||
display: 'block', | ||
cursor: element.display === 'inline' ? 'pointer' : 'default' | ||
cursor: enableLightbox ? 'pointer' : 'default' | ||
}} | ||
alt={element.name} | ||
loading="lazy" | ||
/> | ||
{enableLightbox && ( | ||
<Lightbox | ||
open={lightboxOpen} | ||
close={() => setLightboxOpen(false)} | ||
slides={[{ src: element.url }]} | ||
carousel={{ finite: true }} | ||
render={{ buttonPrev: () => null, buttonNext: () => null }} | ||
plugins={[Zoom, Download]} | ||
zoom={{ | ||
maxZoomPixelRatio: 5, | ||
zoomInMultiplier: 2, | ||
}} | ||
download={{ | ||
download: async ({ slide }) => { | ||
try { | ||
const response = await fetch(slide.src, { mode: 'cors' }); | ||
const blob = await response.blob(); | ||
const url = window.URL.createObjectURL(blob); | ||
const link = document.createElement('a'); | ||
link.href = url; | ||
link.download = element.name || 'image'; | ||
link.click(); | ||
window.URL.revokeObjectURL(url); | ||
} catch (error) { | ||
console.error('Failed to download image:', error); | ||
} | ||
}, | ||
}} | ||
/> | ||
)} | ||
</FrameElement> | ||
); | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,4 @@ | |
"micromatch@<4.0.8": ">=4.0.8" | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing trailing newline. You might want to check your text editor's config! |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is specific to your dev environment and should not be in this PR!
Protip: update gitignore in your system's git config/home directory!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅 Agree, had to swap to my private hardware, and forgot to check that the global git config was set up.