Ekko Lightbox - Documentation

Introduction

What is Ekko Lightbox?

Ekko Lightbox is a lightweight, responsive, and customizable JavaScript library designed to create beautiful and user-friendly image and video lightboxes. It’s built for ease of use and integration, allowing developers to quickly add a robust lightbox functionality to their websites and applications with minimal code. Ekko Lightbox supports various media types, including images, iframes, and even custom content, offering a flexible solution for diverse content presentation needs.

Key Features and Benefits

Installation and Setup

Ekko Lightbox can be easily installed via npm or a CDN.

Using npm:

npm install ekko-lightbox

Then, import it into your project:

import EkkoLightbox from 'ekko-lightbox';

Using a CDN (e.g., jsDelivr):

Include the following script tag in your HTML <head>:

<script src="https://cdn.jsdelivr.net/npm/ekko-lightbox@5.3.0/dist/ekko-lightbox.min.js"></script>

Remember to include the appropriate CSS file as well (either from npm or CDN):

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ekko-lightbox@5.3.0/dist/ekko-lightbox.css">

After including the necessary files, initialize Ekko Lightbox:

$(document).ready(function(){
    $(document).on('click', '[data-toggle="lightbox"]', function(event) {
        event.preventDefault();
        $(this).ekkoLightbox();
    });
});

Basic Usage Example

This example shows how to create a simple image lightbox:

<!DOCTYPE html>
<html>
<head>
    <title>Ekko Lightbox Example</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ekko-lightbox@5.3.0/dist/ekko-lightbox.css">
</head>
<body>

<a href="image1.jpg" data-toggle="lightbox" data-gallery="gallery1" data-title="Image 1">
    <img src="thumbnail1.jpg" alt="Image 1">
</a>
<a href="image2.jpg" data-toggle="lightbox" data-gallery="gallery1" data-title="Image 2">
    <img src="thumbnail2.jpg" alt="Image 2">
</a>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ekko-lightbox@5.3.0/dist/ekko-lightbox.min.js"></script>
<script>
$(document).ready(function(){
    $(document).on('click', '[data-toggle="lightbox"]', function(event) {
        event.preventDefault();
        $(this).ekkoLightbox();
    });
});
</script>

</body>
</html>

Remember to replace "image1.jpg", "thumbnail1.jpg", etc. with the actual paths to your images. The data-gallery attribute allows grouping images into galleries for sequential navigation. The data-title attribute sets the caption for the image. Ensure you have jQuery included, as Ekko Lightbox relies on it.

Core Functionality

Opening the Lightbox

Ekko Lightbox is primarily initiated through the use of the data-toggle="lightbox" attribute on HTML elements (typically <a> tags for images). When a user clicks on an element with this attribute, the lightbox will open, displaying the content specified in the element’s href attribute. For example:

<a href="image.jpg" data-toggle="lightbox" data-title="My Image">View Image</a>

Alternatively, you can programmatically open the lightbox using jQuery:

$('#myElement').ekkoLightbox();

Replace #myElement with the appropriate jQuery selector for your element.

Closing the Lightbox

The lightbox can be closed by clicking the close button (usually an “X” in the top-right corner), clicking outside the lightbox overlay, or pressing the Escape key. Programmatically, you can close the lightbox using:

$.ekkoLightbox.close();

This will close any currently open lightbox instance.

Image Display Options

Ekko Lightbox offers several options for displaying images within the lightbox. These options can be set using data attributes on the link element or through JavaScript configuration. Key options include:

Video Display Options

Ekko Lightbox supports video display primarily through iframes. To display a video, use the href attribute to point to the URL of the video embed code (e.g., a YouTube or Vimeo embed link). For example:

<a href="https://www.youtube.com/embed/yourVideoID" data-toggle="lightbox" data-type="iframe">Watch Video</a>

Note the data-type="iframe" attribute, which is crucial for Ekko Lightbox to correctly handle the iframe content. The lightbox will handle resizing and displaying the video appropriately within its confines. Consider adding options for autoplay, controls, etc., directly within the embed code from your video hosting platform.

Content Loading and Display

Beyond images and videos, Ekko Lightbox allows you to display custom content. This is primarily done using the data-remote attribute. This attribute specifies a URL from which the lightbox will load content dynamically. The content at the specified URL should be valid HTML that will be rendered within the lightbox.

<a href="#" data-toggle="lightbox" data-remote="content.html" data-type="ajax">Load Content</a>

In this example, the lightbox will load the content of content.html via an AJAX request. The data-type="ajax" attribute tells Ekko Lightbox to handle this as an AJAX request. You can also directly specify HTML content without a remote URL by using the data-html attribute.

Note: Always ensure that the content loaded via data-remote or data-html is appropriately formatted and safe for inclusion in the lightbox. Consider sanitizing any user-supplied content to prevent XSS vulnerabilities.

Customization

Styling the Lightbox

Ekko Lightbox’s appearance can be customized extensively using CSS. The library provides a set of CSS classes that can be targeted to modify various aspects of the lightbox’s design. The main container for the lightbox is given the class ekko-lightbox. Within this container, you’ll find classes for the overlay, image container, caption, navigation controls, and more. Consult the Ekko Lightbox CSS file for a complete list of classes and their functionality. You can override these styles in your own CSS file to match your website’s design. For example, to change the background color of the overlay:

.ekko-lightbox .modal-backdrop {
  background-color: rgba(0, 0, 0, 0.8); /* Adjust opacity as needed */
}

Remember to include your custom CSS after the Ekko Lightbox CSS file to ensure your styles override the defaults.

Customizing Navigation Controls

While Ekko Lightbox provides default navigation controls (previous/next buttons and a close button), you can customize their appearance and behavior through CSS or, to a lesser extent, JavaScript. CSS allows you to modify the styling of the buttons, changing their size, color, shape, and position. More complex interactions might require JavaScript modifications, but this is generally not recommended unless absolutely necessary as it can lead to conflicts with updates to the library.

Adding Custom Content

Beyond images, iframes, and remotely loaded content, you can incorporate custom HTML content into the lightbox. This is achieved through the data-html attribute. The value of data-html should contain the HTML code to be displayed. For example:

<a href="#" data-toggle="lightbox" data-html="<div><h1>My Custom Content</h1><p>This is a paragraph of text.</p></div>">Show Custom Content</a>

This will display a div containing a heading and a paragraph within the lightbox. Remember to sanitize any dynamically generated HTML to prevent security vulnerabilities.

Responsive Design

Ekko Lightbox is inherently responsive. It adapts to different screen sizes and devices without requiring specific responsive CSS adjustments. However, you might need to fine-tune the CSS to ensure optimal appearance and behavior across various screen sizes and orientations. Consider using media queries to apply specific styles for different screen widths. For example:

@media (max-width: 768px) {
  .ekko-lightbox .modal-dialog {
    max-width: 90%; /* Adjust as needed */
  }
}

This code adjusts the maximum width of the lightbox on screens smaller than 768 pixels.

Accessibility Considerations

To enhance the accessibility of your Ekko Lightbox implementation:

By following these guidelines, you can create an accessible and inclusive lightbox experience for all users.

Advanced Usage

Programmatic Control

Beyond the basic data attributes, Ekko Lightbox offers robust programmatic control through its JavaScript API. This allows for more complex integration and customization. Key methods include:

Example of programmatic opening:

$.ekkoLightbox.open({
    href: 'image.jpg',
    title: 'My Image',
    gallery: 'myGallery'
});

Event Handling

Ekko Lightbox triggers several events throughout its lifecycle, allowing you to hook into various stages of the lightbox functionality. These events can be listened for using jQuery’s on() method. Important events include:

Example of handling the shown.ekkoLightbox event:

$(document).on('shown.ekkoLightbox', function (e) {
    console.log('Lightbox shown:', e.target);
});

Integration with Other Libraries

Ekko Lightbox can be integrated with various other JavaScript libraries. However, ensure compatibility. Potential conflicts might arise, especially when dealing with other modal or overlay libraries. Thorough testing is crucial when combining Ekko Lightbox with other frameworks or plugins. Properly managing event handling and ensuring no conflicting selectors are used is essential for successful integration.

Handling Errors

Ekko Lightbox provides the error.ekkoLightbox event to handle errors during content loading. This event is triggered if the lightbox fails to load the specified content (e.g., due to a 404 error or invalid URL). You can use this event to display a user-friendly error message or take alternative actions.

$(document).on('error.ekkoLightbox', function (e) {
    alert('Error loading content: ' + e.message);
});

Make sure to handle potential errors gracefully to provide a better user experience.

Performance Optimization

For optimal performance, consider these points:

By following these optimization techniques, you can improve the performance and responsiveness of Ekko Lightbox in your application.

API Reference

EkkoLightbox Constructor

The EkkoLightbox constructor is primarily invoked implicitly through the data-toggle="lightbox" attribute on HTML elements or explicitly via the $.ekkoLightbox() method. It does not accept arguments directly in this way. Configuration options are provided via data attributes on the element or as an options object passed to $.ekkoLightbox(). These options determine the lightbox’s behavior. Refer to the options section below for a detailed list of available settings.

Methods

The EkkoLightbox plugin exposes the following methods via jQuery:

Events

Ekko Lightbox triggers several custom events that can be listened for using jQuery’s on() method. These events provide hooks for extending functionality or customizing behavior:

To listen for these events, use:

$(document).on('shown.ekkoLightbox', function(e) { /* your code */ });

Properties

Ekko Lightbox doesn’t directly expose public properties in the traditional sense. Its state and configuration are largely managed internally. However, information about the currently active lightbox (if any) can be accessed indirectly through inspecting the DOM elements that are created and modified by Ekko Lightbox. For example, you could access the current image URL by inspecting the src attribute of the image element within the lightbox modal. This approach is not recommended for general use, as it relies on the internal implementation details that could change in future versions of the library. It’s best to leverage the provided events and methods for interacting with the lightbox’s behavior and state.

Troubleshooting

Common Issues and Solutions

Debugging Tips

Browser Compatibility

Ekko Lightbox is designed to work across modern browsers. However, older or less common browsers might experience compatibility issues. Thorough testing across various browsers (Chrome, Firefox, Safari, Edge) and versions is recommended.

Frequently Asked Questions (FAQ)

If you have further questions or encounter issues not covered here, please refer to the project’s GitHub repository or community forums for additional support and information.

Contributing

We welcome contributions to Ekko Lightbox! Whether you’re fixing bugs, adding features, or improving the documentation, your help is valuable. Please follow these guidelines to ensure a smooth and efficient contribution process.

Reporting Bugs

When reporting bugs, please provide as much detail as possible to help us understand and reproduce the issue. A clear and concise bug report should include:

Suggesting Features

Feature suggestions are always welcome! When suggesting a new feature, please:

Submitting Pull Requests

  1. Fork the repository: Create a fork of the Ekko Lightbox repository on GitHub.
  2. Create a new branch: Create a new branch for your changes. Use a descriptive branch name that reflects the changes you’re making (e.g., fix-bug-123, feature-new-gallery).
  3. Make your changes: Implement your changes, adhering to the coding style guide (see below).
  4. Test thoroughly: Ensure your changes work correctly and don’t introduce new bugs.
  5. Commit your changes: Commit your changes with clear and concise commit messages.
  6. Push your branch: Push your branch to your forked repository.
  7. Create a pull request: Create a pull request from your branch to the main repository. Include a clear description of your changes and why they are necessary.

Coding Style Guide

To maintain consistency and readability, please follow these coding style guidelines when contributing to Ekko Lightbox:

Before submitting a pull request, ensure your code passes any automated tests and adheres to these guidelines. We appreciate your cooperation in making Ekko Lightbox a well-maintained and high-quality project.