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

Ability to customize gallery items #95

Open
hyperstown opened this issue Aug 4, 2023 · 2 comments
Open

Ability to customize gallery items #95

hyperstown opened this issue Aug 4, 2023 · 2 comments

Comments

@hyperstown
Copy link

Hi. I noticed that currently is quite hard to do any customization on gallery items.
I need to add a button to each image and I thought about two solutions:

Modify 'item-added-to-dom' event so it returns also item.element instead only item.model,
or make a custom method in Item class similar to what photoSwipe did:

photoSwipe.pswp.ui.registerElement({
  name: "test-button",
  ariaLabel: "Add to favs",
  order: 9,
  isButton: true,
  html: '<i class="bi bi-heart" style="text-shadow: 2px 2px 5px black;"></i>',
  onClick: (event, el) => {
    addToFavs(event, el)
  },
});
@sambaptista
Copy link
Member

sambaptista commented Aug 8, 2023

This lib is supposed to achieve your goal, out of the box, but I may be misundertanding your need. I'm not even sure if you're talking about the gallery (list with thumbnails) or the photoswipe zoom part.

I can answer for the gallery itself :

Consider setting the option activable :

new NaturalGallery.Natural(galleryElement, {activable: true}, scrollableElement)

capture 2023-08-08 at 16 14 47

Each item in the gallery gets a button, with CSS class that allows you to customize it.

Then use activate event to react to click (https://ecodev.github.io/natural-gallery-js/docs-api.html#activable) :

gallery.addEventListener('activate', function(ev) {
    console.log(ev.detail); // {model: Object, clickEvent: MouseEvent}
});

Does it answer your question ? or do you mean adding an additionnal button ( = 2 buttons by thumbnail).

@hyperstown
Copy link
Author

Sorry if I wasn't specific enough, I meant gallery (thumbnails). Photoswipe was only an example on how customization could be implemented.
But I think I found a nice workaround.
My problem was that I needed to add a "like" button to every element so I can then filter by favorites.
I managed to accomplish that using 'item-added-to-dom' event and modifying last item of gallery.visibleCollection

gallery.addEventListener('item-added-to-dom', (e) => {
  const item = gallery.visibleCollection[gallery.visibleCollectionLength - 1];
  if(!item._element) {
    console.warn("Item has no element!");
    return; // This shouldn't happen but if that was the case display warning.
  }
  item._element.appendChild(createLikeElement(item.model));
  //console.log("added", item, e);
}); 

The effect looks like this:

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

2 participants