Downloading an image that was loaded via the Item content
attribute
#1076
-
I've got a gallery of images, but i don't know the dimensions of the images in advance, and I also want to apply some custom styling to an image container that I'll be displaying each image in. So I used the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Yes, you're right. Download button works only with images, that were rendered via photoswipe ( Item's I can assume, that you have some knowledge about which image is opened right now in Photoswipe via your custom component (let it be CustomImage component), that you pass to Item ( const customDownloadButton = {
name: 'custom-download-button',
ariaLabel: 'Download',
order: 9,
isButton: true,
tagName: 'a',
appendTo: 'bar',
html: {
isCustomSVG: true,
inner: '<path d="M20.5 14.3 17.1 18V10h-2.2v7.9l-3.4-3.6L10 16l6 6.1 6-6.1ZM23 23H9v2h14Z" id="pswp__icn-custom-download"/>',
outlineID: 'pswp__icn-custom-download',
},
onInit: (el, pswpInstance) => {
el.setAttribute('download', '')
el.setAttribute('target', '_blank')
el.setAttribute('rel', 'noopener')
instance.on('change', () => {
const customImageSrc = ... // here you need to set src of currently opened image
const downloadButton = el as HTMLAnchorElement
downloadButton.href = customImageSrc
})
},
};
<Gallery uiElements=[customDownloadButton]>
<Item content={<CustomImage />} />
...
</Gallery> |
Beta Was this translation helpful? Give feedback.
Yes, you're right.
Download button works only with images, that were rendered via photoswipe (
<Item original="img.jpg" />
).Item's
content
prop was implemented to provide users with an opportunity to open in Photoswipe content, different from simple image - Google maps, for example. That's why download button works only with 'native' Photoswipe images.I can assume, that you have some knowledge about which image is opened right now in Photoswipe via your custom component (let it be CustomImage component), that you pass to Item (
<Item content={<CustomImage} />
). If you have such knowledge, you can implement your own download button: