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

Adding a button that triggers a function onClick in Caption (pswp-caption-content) #19

Open
MATTiVOLTARii opened this issue Mar 9, 2023 · 3 comments

Comments

@MATTiVOLTARii
Copy link

Nice Work with PhotoSwipe!
I want to include a button in the caption that executes a function when clicked. Unfortunately, the "onClick" is not being executed.

<div className="pswp-caption-content">                    
                    <button onClick={() => handlerClick()}>Click</button>                      
                      <div>
                        <Text size="xl" weight={700}>
                          Lorem ipsum dolor (1933)
                        </Text>
                      </div>
                      Color photograph 12 x 10
                    </div>

Does anyone have an idea of how it could work?

@dimsemenov
Copy link
Owner

The plugin doesn't do anything special when adding the content, it's just simple innerHTML https://github.com/dimsemenov/photoswipe-dynamic-caption-plugin/blob/main/photoswipe-dynamic-caption-plugin.esm.js#L247

You may try using dynamicCaptionUpdateHTML event:

const lightbox = new PhotoSwipeLightbox({
  gallerySelector: '#gallery',
  childSelector: '',
});

const captionPlugin = new PhotoSwipeDynamicCaption(lightbox, {
  type: 'auto',
});

lightbox.on('dynamicCaptionUpdateHTML', (e) => {
  console.log(e.captionElement, e.slide);
});

lightbox.init();

@MATTiVOLTARii
Copy link
Author

Oh, I see! Thanks! Now I understand. The elements are reset through innerHTML and are no longer connected to the original DOM, which is why my JSX onClick directive is no longer handled. Is there a workaround for that? So that the button works as it is without having to reset it through lightbox.on, or is it possible to reference this button?

@ikabod
Copy link

ikabod commented Jul 27, 2023

You can give your button an id and handle all click events of the document / body.

In the event handler function check if the button was clicked and call the desired function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants