Skip to content
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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

buzzCraft
Copy link

Feature - Image LightBox

Why?

After seeing that this feature was requested back in Nov 2023, and since I couldn't find any implementation of it, I decided to implement it myself.
During my work I discovered #1402 which is a PR that implements a simple popup for images. While this is an improvement over the current situation, I believe that a lightbox is a better solution for this problem.

Added dependencies

  • Yet Another React Lightbox - A lightbox component for React.
    I chose this libary due to its MIT license and that it is actively maintained.

Disclaimer

Im not a frontend developer, so I might have missed some best practices or made some mistakes. Please let me know if you see anything that could be improved.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request frontend Pertains to the frontend. labels Oct 23, 2024
@buzzCraft
Copy link
Author

Simple demo of the feature:

chainlit.webm

Copy link
Collaborator

@dokterbob dokterbob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obviously a nice feature and a proper iteration on the last fixup to image display.

But we should make sure to either drop legacy behaviour entirely (and be careful not to increase our bundle size too much) or not change default behaviour and really make lightbox optional. In the latter case, the js code should only be loaded if the lightbox is enabled. My suggestion would be the latter. Regardless, I would like to see the increase in bundle size, please!

In addition, I would also like to ask for E2E testing both with and without the lightbox enabled. You can prepare the test with it disabled on a fork of main, so you can be really sure that your patch does not break pre-existing UX. Here's some suggestions of where you might want to put that: https://github.com/search?q=repo%3AChainlit%2Fchainlit+path%3A%2F%5Ecypress%5C%2Fe2e%5C%2F%2F++image&type=code

Then, add a lightbox E2E test specifically testing with the lightbox enabled.

If the original contributor needs support, I imagine there's other community members who'd like to help out getting this big UX improvement in!

Lastly a question; does this particular lightbox also work for video?

.gitignore Outdated
@@ -60,3 +60,5 @@ dist-ssr
.coverage

backend/README.md

myenv
Copy link
Collaborator

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!

Copy link
Author

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.

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';
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill look into it.

}
};


Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

@@ -34,4 +34,4 @@
"micromatch@<4.0.8": ">=4.0.8"
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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!

Copy link
Collaborator

@dokterbob dokterbob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the requested changes.

There's still one missing \n -- please take care to set up your editor appropriately (this goes for all code projects).

Other than that, there's one question about the download.

I'd also like to invite community members to play with this and give feedback before merging.

backend/README.md

myenv
backend/README.md
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for dropping the ball on this a bit, but sometimes too much is happening to do community work :(
All the work has been done on my old private laptop, so yeah, my editor was just set up quick and dirty.. Sorry about that.

Im more than happy to hand this over to someone with more time, as im quite busy two more weeks

link.href = url;
link.download = imageName || 'image';
link.click();
window.URL.revokeObjectURL(url);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have a custom fetch() here rather than just download=true?

Download URL's do not require auth tokens, nor will it in the future (we're using cookie auth instead #1521).

Ref: https://yet-another-react-lightbox.com/plugins/download

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this idea of keeping plugins to a minimum, but with the lazy load you suggested, that might not be needed anymore.
And, to be honest, this is my first try on TS ever, so iv been confused.

Spent way to long trying to make E2E tests, before I had to put everything on hold.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty good for a first try. :)

I know, building these tests, especially in the beginning, is quite tedious. But once you get the hang of it, additional tests (per project), go faster. Really appreciate your work on this, thanks!

Now on this issue, you're already using the download plugin. ;)

Just putting the URL there might work. Let me know if you're gonna add that to the PR. With that, I'm happy to merge.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill look into it after work tomorrow and report back. Thanks for the guidance and for all the amazing work you and your team does <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request frontend Pertains to the frontend. size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants