Git Product home page Git Product logo

vue-my-photos's Introduction

Simple Image Lightbox for Vue.js

Upgraded to support Vue 3 and still no dependencies required!

Inspired by vue-pure-lightbox, however I needed a framework that allowed for a gallery of thumbnails as well as filtering functionality.

Vue Compatibility

Versions >= 3.0.0 are built for Vue 3.

If your project uses Vue 2, use vue-my-photos-1.1.1

Demo

Live demo available on Codepen

Installation

Via NPM:

npm i vue-my-photos --save

Via CDN:

<!-- In <body>, after Vue import -->
<script src="https://unpkg.com/vue-my-photos/dist/lightbox.min.js"></script>

Setup

In your App:

<script>
    import Lightbox from "@/lightbox.vue";
    // ...
    export default defineComponent({
        name: "VueMyPhotosDemo",
        components: {
            Lightbox,
        },
        // ...
    })
</script>

Usage

Simply initiate a lightbox component with the lightbox tag:

<lightbox 
    id="myLightbox"
    ref="mylightbox"                            * Now Optional
    :images="images"
    :current-image-name="currentImageName"
    @on-lightbox-close="onLightboxClose"
    :filter="galleryFilter"                     * Optional
    :directory="thumbnailDir"                   * Optional
    :timeout-duration=5000                      * Optional
    :close-on-backdrop-click="true"             * Optional
    @on-lightbox-change="onLightboxChange"      * Optional
></lightbox>

Expose the appropriate data for the template:

data() {
    return {
        thumbnailDir: "/.../.../",
        images: imageList,
        galleryFilter: "all",
        currentImageName: ""
    };
},

Each thumbnail in the gallery registers a click event, passing the name of the photo:

<img @click="showLightbox('img.jpg')" src="..." alt="..." title="..." />

And add the showLightbox and onLightboxClose methods to your vue page (these can be named however you like):

showLightbox(imageName: string) {
    this.currentImageName = imageName;
},
onLightboxClose(imageName: string) {
    this.currentImageName = imageName;
},

To update which images show within the lightbox, update the filter string like so:

updateFilter(filterName) {
    this.galleryFilter = filterName;
}

A Note On v3 Updates

Previously, the lightbox was shown by accessing the component via the $refs object and calling the show method directly:

showLightbox: function(imageName) {
    this.$refs.mylightbox.show(imageName);
}

This approach can still be done (and in Vue 3 using Ref() within the setup method), however, in an effort to decouple the Lightbox Component from its parent Component, the new recommended approach is detailed above using the currentImageName prop. This is a reactive property that will trigger the lightbox to display whenever its value is changed. A method that listens to the on-lightbox-close event must also be implemented in order to reset the value of currentImageName (Otherwise, if the user tries to open the lightbox with the same image twice in a row, currentImageName won't change and the lightbox won't open).

Properties

Property Type Value
images (Required) array Array of objects with image data (example below)
currentImageName (Required) string Should initially be an empty string, then updated later to trigger lightbox
filter (Optional - Default: "all") string String to filter on specific images (Ex: "nature")
directory (Optional - Default: "") string Path to location of images (Ex: "/src/assets/")
timeoutDuration (Optional - Default: 3000) integer duration in ms of key/mouse inactivity before caption disappears
closeOnBackdropClick (Optional - Default: false) boolean toggle whether or not to close lightbox when clicking outside of image

Events

Event Description
onLightboxClose(imageName: string) Fired every time the lightbox is closed. Must implement a method to update currentImageName with the value passed by this event
onLightboxChange(newImage: { name: string, alt: string, filter: string, id: string } Fired every time the user advances the lightbox to the next or previous image.

Example of images array:

var images = [{'name':'mountains.jpg', 'alt':'The Dolomites', 'filter':'nature', 'id':'image1' },
              {'name':'bird.jpg', 'alt':'It is a bird', 'filter':'animals', 'id':'image2' }];

Note:

  • 'name' value should include the file extension
  • 'alt' is optional
  • 'filter' is optional if you never pass or try to update the filter value on the lightbox component
  • 'id' is optional, but useful as a key if you're displaying the images in a gallery using the v-for iterator

Recommended additional modules

disable-scroll or similar module to prevent the user from scrolling while the lightbox is visible.

vue-fontawesome if you want to replace/re-style the svg icons for left/right arrows and close icon.

vue-my-photos's People

Contributors

am283721 avatar aminerman avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vue-my-photos's Issues

Close on backdrop click

Would be nice to have a boolean option to close the lightbox if the user clicks on the black backdrop.

After filtering it opens allways the 1st image, even if clicked on the 3rd...

Hi,

I have set your component in a project and it looks really great!

Meanwhile, I have a problem:
I've got plenty of images that I filter by an id associated to each one. Several images have the same id.
On the first click after filtering, if for instance I get 4 images with the same 'id', and I click on the 3rd, the gallery opens but with the first one and not the 3rd I clicked on. After closing the image with 'Esc', if I click again on the 3rd then the 3rd image opens correctly.
This happens always after filtering and first click, following clicks to open the gallery opens the right image, but the first click always opens the 1st image, independently of which image is clicked.

I have set a console.log(imageName) just before the this.$refs.lightbox.show(imageName) and it prints the correct image that should be opened (the 3rd if I clicked the 3rd for instance), but it shows another image... ¿?¿?¿?

Have someone met this problem?

Regards,

Adopt "teleport" in Vue 3

Vue 3 will introduce the built in <teleport> component. This will be useful for lightboxes, modals, alerts, dialogs, toast messages, and other similar components.

Custom nav arrows

Is there some options to set custom arrow? like:
navigation: { navPrev: '.prevel', navNext: '.nextel' }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.