Skip to content

Commit

Permalink
[FEATURE] Add option to load the lightbox empty if no elements are av…
Browse files Browse the repository at this point in the history
…ailable

(e.g. when elements are loaded via Ajax)
  • Loading branch information
deoostfrees committed Apr 7, 2024
1 parent b727c94 commit 9a180fc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ The following options are available:

```js
{
// Load the lightbox empty if no elements are available
// (e.g. when elements are loaded via Ajax)
loadEmpty: false,

// Selector for elements that trigger Parvus
selector: '.lightbox',

Expand Down
6 changes: 6 additions & 0 deletions dist/js/parvus.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function Parvus(userOptions) {
const mergeOptions = userOptions => {
// Default options
const DEFAULT_OPTIONS = {
loadEmpty: false,
selector: '.lightbox',
gallerySelector: null,
captions: true,
Expand Down Expand Up @@ -1403,6 +1404,11 @@ function Parvus(userOptions) {
const init = () => {
// Merge user options into defaults
config = mergeOptions(userOptions);

// Check if the lightbox should be loaded empty or if there are elements for the lightbox.
if (!config.loadEmpty && !document.querySelectorAll(config.selector).length) {
return;
}
reducedMotionCheck();

// Check if the lightbox already exists
Expand Down
2 changes: 1 addition & 1 deletion dist/js/parvus.esm.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions dist/js/parvus.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
const mergeOptions = userOptions => {
// Default options
const DEFAULT_OPTIONS = {
loadEmpty: false,
selector: '.lightbox',
gallerySelector: null,
captions: true,
Expand Down Expand Up @@ -1409,6 +1410,11 @@
const init = () => {
// Merge user options into defaults
config = mergeOptions(userOptions);

// Check if the lightbox should be loaded empty or if there are elements for the lightbox.
if (!config.loadEmpty && !document.querySelectorAll(config.selector).length) {
return;
}
reducedMotionCheck();

// Check if the lightbox already exists
Expand Down
2 changes: 1 addition & 1 deletion dist/js/parvus.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/js/parvus.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default function Parvus (userOptions) {
const mergeOptions = (userOptions) => {
// Default options
const DEFAULT_OPTIONS = {
loadEmpty: false,
selector: '.lightbox',
gallerySelector: null,
captions: true,
Expand Down Expand Up @@ -1496,6 +1497,11 @@ export default function Parvus (userOptions) {
// Merge user options into defaults
config = mergeOptions(userOptions)

// Check if the lightbox should be loaded empty or if there are elements for the lightbox.
if (!config.loadEmpty && !document.querySelectorAll(config.selector).length) {
return
}

reducedMotionCheck()

// Check if the lightbox already exists
Expand Down

0 comments on commit 9a180fc

Please sign in to comment.