Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vue-silentbox

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-silentbox

<source media="(prefers-color-scheme: dark)" srcset="https://silentbox.rocek.dev/images/sb3-dark.png">

  • 3.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5K
increased by25.08%
Maintainers
1
Weekly downloads
 
Created
Source

SilentBox logo.

Github Stars Github Issues License


A third version of the lightweight lightbox inspired component for Vue.js with local video support and more features coming. If you're interested, see demo.



Supported formats and services

  • All image formats that can be displayed in browser
  • Local video files with following extensions .mp4, .ogg, .webm, .mov, .flv, .wmv, .mkv
  • YouTube, Vimeo and Twitch embed videos with autoplay

Installation

Npm

npm install --save vue-silentbox

Import the plugin into Vue:

import { createApp } from 'vue'
import VueSilentbox from 'vue-silentbox'
import 'vue-silentbox/dist/style.css'

createApp({
    // your app props ...
})
.use(VueSilentbox)
.mount('#root')

How to use?

Define an array of images in the data object of your Vue instance or component.

import { createApp } from 'vue'
import VueSilentbox from 'vue-silentbox'
import 'vue-silentbox/dist/style.css'

createApp({
    data: {
        images: [
            {
                src: 'images/image001.jpg',
                srcSet: '/images/image001-640.jpg 640w,/images/image001-1280.jpg 1280w,/images/image001-1920.jpg 1920w',
                description: 'Sunken dreams II. by Arbebuk',
            },
            {
                src: 'images/image002.jpg',
                srcSet: '/images/image002-640.jpg 640w,/images/image002-1280.jpg 1280w,/images/image003-1920.jpg 1920w',
                description: 'Tunnel View Sunrise by Porbital',
            }
        ]
    },
})
.use(VueSilentbox)
.mount('#root')

Then in the template you use a silent-box component to display the gallery.

<silent-box :gallery="images"><!-- your additional content --></silent-box>

Or you can show a single image by just renaming the property.

<silent-box :image="images[0]"><!-- your additional content --></silent-box>

Custom activators

In case you don't like the default image previews that SilentBox provides, you can set your own activators - text, button or even a video! SilentBox provides a named slot for this - silentbox-item. The slot provides variable called silentboxItem which provides you access to all properties you set on image object.

<!--
In this example only alt text is displayed as an activator.
-->
<silent-box :gallery="images">
    <h2 class="tw-text-3xl tw-font-bold tw-mb-2">Gallery</h2>
    <p class="tw-font-light tw-mb-3">
        Items could be merged into groups that make galleries.
    </p>
    <template v-slot:silentbox-item="{ silentboxItem }">
        <p>{{ silentboxItem.alt  }}</p>
    </template>
</silent-box>

Plugin options

You can set the following options to the plugin to change the behaviour of the SilentBox or display additional information.

NameRequiredTypeDefaultDescription
downloadButtonLabelnostringDownloadLabel used in the download button.
createApp({
    // your app props ...
})
.use(VueSilentbox, {
    downloadButtonLabel: 'Download'
})
.mount('#root')

Image object attributes

You can set the following attributes to the image object to change the behaviour of the SilentBox or display additional information. On the other hand, if you're lazy, only the src attribute is required.

AttributerequiredtypeDescription
srcyesstringmedia source, it could be an image, video or a YouTube / Vimeo embed link
srcSetnostringsrcSet to make use of responsive images
thumbnailnostringimage used for thumbnail
thumbnailHeightnostringheight of the thumbnail in px
thumbnailWidthnostringwidth of the thumbnail in px
descriptionnostringshort description below image (doesn't work below videos yet)
altnostringalt description for images
autoplaynoboolto autoplay youtube / Vimeo video
downloadnobool/stringLink to download the file. When set to a boolean value (true/false), the src is used as the download link.
controlsnobooldoes not work for Vimeo, setting false will hide video controls

These attributes can change the gallery element behaviour.

AttributerequiredtypeDescription
gallerynoarraylist of image objects that will be displayed in the gallery
imagenoobjectimage object that will be displayed in the gallery
lazy-loadingnoboolwhether images should be lazy loaded
preview-countnonumbernumber of images that should be displayed in the gallery

Events

SilentBox also fires several events that can be further used in your Vue.js application. Each event has a payload that contains the item object which holds information about the currently displayed item.

Event nameWhen is event fired
silentbox-overlay-openedwhen the overlay is opened
silentbox-overlay-hiddenwhen the overlay is closed (button or ESC key)
silentbox-overlay-next-item-displayedwhen the user moves to the next picture (arrow or key)
silentbox-overlay-prev-item-displayedwhen the user moves to the previous picture (arrow or key)

Open overlay programatically

SilentBox provides two options how to open the overlay programatically. If you need to open an existing gallery, the best option is to use the ref attribute and then call the method openOverlay on the $refs object in your method. See example:

<silent-box ref="silentbox" :gallery="images"></silent-box>

and then the method openOverlay can be called from your method:

// ...
methods: {
    // the index parameter is optional, however it should be set if you're opening
    // the overlay on different position than the beginning of the gallery
    openOverlayProgramaticallyWithContext (item, index = 0) {
        this.$refs.silentbox.openOverlay(item, index)
    }
}
// ...

However, in case you just want to open an item without any context, it might be a better choice to call the global open method that SilentBox provides.

// ...
methods: {
    openOverlayProgramaticallyWithoutContext (item) {
        this.$silentbox.open(item)
    }
}
// ...

Upgrading from 2.x to 3.x?

Almost all APIs remained same except two things:

  • Vue3 always transforms <img /> width and height to numbers, thus properties thumbnailWidth and thumbnailHeight should always be a number
  • event fired when user navigates to previous image was renamed to silentbox-overlay-prev-item-displayed from silentbox-overlay-previous-item-displayed

Upgrading from 0.1?

Version 2 brought many breaking changes. There are no more two separate components to display a single image or gallery. So, change all your silentbox-group and silentbox-single components to silent-box. The source of images must be an array of objects or a single object with previously mentioned attributes.


Contributions

All contributions are welcomed, however give me some time to review your requests. Please, use emoji in your commits, so it is easier to identify what your commits do. There are several emoji guides on the internet. Please stick with mine which is inspired by Atom contributing guidelines, see emoji in commits.

Keywords

FAQs

Package last updated on 12 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc