Featherlight - Documentation

Introduction

What is Featherlight?

Featherlight is a lightweight and highly customizable JavaScript library designed to create elegant and responsive lightbox-style image galleries and content viewers. It focuses on simplicity and performance, minimizing the impact on your website’s loading time and resources. Featherlight is ideal for showcasing images, videos, or any other HTML content in a clean and unobtrusive modal window. It’s easy to integrate into existing projects and requires minimal coding.

Key Features and Benefits

Getting Started: Installation and Setup

Featherlight can be included in your project via several methods:

  1. Download: Download the latest version of Featherlight from https://noelboss.github.io/featherlight/. Include the featherlight.min.js and featherlight.min.css files in your project’s <head> section.

  2. CDN: Use a CDN link to include Featherlight in your project. You can find suitable CDN links on sites like jsDelivr or cdnjs.:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/featherlight/1.7.13/featherlight.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/featherlight/1.7.13/featherlight.min.js"></script>
  1. NPM: If you’re using a package manager like npm, install Featherlight using:
npm install featherlight

Remember to include the CSS file in your HTML <head> and the JS file before the closing </body> tag for proper functionality.

Basic Usage Example

This example shows how to create a simple image lightbox:

<!DOCTYPE html>
<html>
<head>
  <title>Featherlight Example</title>
  <link rel="stylesheet" href="featherlight.min.css">
</head>
<body>

<a href="image1.jpg" data-featherlight="image1.jpg">Image 1</a>
<a href="image2.jpg" data-featherlight="image2.jpg">Image 2</a>

<script src="featherlight.min.js"></script>
</body>
</html>

Replace "image1.jpg" and "image2.jpg" with the actual paths to your images. Featherlight automatically detects the href attribute and opens the link in a lightbox. You can customize this further using various options and parameters detailed in the advanced usage section.

Core Functionality

Opening a Lightbox

Featherlight offers several ways to open a lightbox:

1. Using HTML links: The simplest method is to wrap your content links with <a> tags and add the data-featherlight attribute. The value of this attribute can be a URL, a selector, or a function returning the content to be displayed in the lightbox.

<a href="image.jpg" data-featherlight="image.jpg">View Image</a>
<a href="#my-content" data-featherlight="#my-content">View Content</a>
<a href="#" data-featherlight="function(){return '<h1>Hello!</h1>';}">View Custom HTML</a>

2. Programmatically: You can open a lightbox programmatically using JavaScript:

Featherlight.open('image.jpg');
Featherlight.open('#my-content');
Featherlight.open(function(){ return '<h1>Hello!</h1>'; });

// or with options
Featherlight.open('image.jpg', {
    afterOpen: function() { console.log('Lightbox opened!'); }
});

Replace "image.jpg" and "#my-content" with your actual content. The function example demonstrates creating dynamic content within the lightbox. For elements, make sure they exist on the page before the script that opens the lightbox is executed.

Closing a Lightbox

Featherlight provides several methods to close the lightbox:

Featherlight.close();

This will close the currently open lightbox.

Event Handling

Featherlight offers several events you can listen for and use to trigger custom actions:

Here’s how to listen for these events:

Featherlight.open('image.jpg', {
    afterOpen: function() { console.log('Lightbox opened!'); },
    beforeClose: function() {
        if (confirm("Are you sure you want to close?")) {
            return true; // Allow closing
        } else {
            return false; // Prevent closing
        }
    },
    close: function() { console.log('Lightbox closed!'); }
});

// alternative using jQuery:

$('a[data-featherlight]').featherlight({
  afterOpen: function() { console.log('Lightbox opened!'); }
});

Remember to replace "image.jpg" with your actual content.

Accessibility Features

Featherlight incorporates several features to enhance accessibility:

These features ensure that Featherlight is usable by a wider audience, including users who rely on assistive technologies. Always test your implementation with assistive technologies to ensure optimal accessibility.

Customization Options

Styling the Lightbox

Featherlight’s appearance is easily customizable through CSS. The library uses a well-structured CSS architecture, making it straightforward to override default styles. The main CSS class for the lightbox is featherlight. You can target specific elements within the lightbox using classes like .featherlight-content, .featherlight-close, and .featherlight-inner.

For example, to change the background color of the lightbox:

.featherlight {
  background-color: #f0f0f0; /* Change to your desired color */
}

To customize the close button:

.featherlight-close {
  color: red; /* Change the color of the close button */
  font-size: 20px; /* Adjust the size */
}

Remember to include your custom CSS after the Featherlight CSS file to override the default styles.

Customizing Content

You can customize the content displayed within the lightbox in several ways:

<a href="image.jpg" data-featherlight="image.jpg" data-my-custom-attribute="My custom value">View Image</a>

Access this data attribute using Javascript within the lightbox’s callbacks (see Using Callbacks section).

Featherlight.open(function() {
  return '<div><h1>Dynamic Content</h1><p>This content is generated dynamically.</p></div>';
});
<a href="http://example.com" data-featherlight-iframe="true">View External Content</a>

This will open the link within an iframe inside the lightbox. Note that the data-featherlight-iframe attribute is used to indicate iframe content.

Adding Custom Controls

You can add custom controls to your lightbox using Javascript and manipulating the DOM. After the lightbox opens (using the afterOpen callback), you can append your custom elements to the lightbox container.

Featherlight.open('image.jpg', {
  afterOpen: function() {
    const customButton = document.createElement('button');
    customButton.textContent = 'My Custom Button';
    customButton.addEventListener('click', function() {
      // Add your custom functionality here
      alert('Custom button clicked!');
    });
    this.$instance.find('.featherlight-inner').append(customButton);
  }
});

This code adds a button to the lightbox after it’s opened. Replace this example with your own custom HTML and functionality.

Configuring Options

Featherlight provides several options that you can configure to customize its behavior:

To use these options, pass them as an object to Featherlight.open() or as a parameter to $.featherlight().

Featherlight.open('image.jpg', {
  closeOnEscape: false,
  afterOpen: function() { console.log('Lightbox opened!'); }
});

Using Callbacks

Callbacks allow you to execute custom code at specific points in the lightbox lifecycle. Featherlight provides the afterOpen, beforeClose, beforeOpen, close, and load callbacks.

These callbacks receive the lightbox instance as this allowing access to methods and properties. For example, this.$instance refers to the lightbox’s jQuery element. You can use this to manipulate the lightbox’s DOM elements. See the examples within the “Event Handling” and “Adding Custom Controls” sections above. Use callbacks to integrate Featherlight seamlessly into your application’s workflow.

Advanced Usage

Multiple Lightboxes

Featherlight supports opening multiple lightboxes simultaneously. Each lightbox instance is independent, allowing for flexible use cases. Simply call Featherlight.open() multiple times, each with its own content. Featherlight manages the instances internally, ensuring proper behavior even when overlapping. The close() method closes only the most recently opened lightbox unless specified otherwise. Consider using unique identifiers or namespaces to easily manage multiple lightboxes within a complex application.

Integrating with Other Libraries

Featherlight is designed to be compatible with many JavaScript libraries. However, potential conflicts may arise due to overlapping functionality (such as event handling or DOM manipulation). When integrating with other libraries, ensure that you properly load the scripts in the correct order and consider using namespaces to avoid naming collisions. Test your integration thoroughly to identify and resolve any conflicts that may occur. If using jQuery, Featherlight’s jQuery plugin offers a seamless integration, simplifying the setup and usage.

Handling Errors and Edge Cases

While Featherlight is robust, certain situations may require specific handling:

Performance Optimization

For optimal performance, especially when dealing with many images or large content:

Building Custom Lightbox Plugins

Featherlight’s architecture is designed to be extensible. You can create custom plugins to add new features or modify existing behavior. This typically involves extending the core functionality using the provided callbacks and adding your own custom logic. You would then register this plugin, making it available to use throughout your application. Create a separate JavaScript file for your plugin to maintain a clear and organized structure. Keep your plugin’s code concise and well-documented for easy maintenance and future use. Thoroughly test your custom plugin to ensure it functions correctly and integrates seamlessly with the rest of Featherlight.

API Reference

Featherlight Constructor

The Featherlight constructor is used to initialize a new lightbox instance. While generally not called directly (as it’s typically handled internally by the library), understanding its purpose is beneficial for advanced usage and custom plugin development. The constructor takes an optional configuration object as an argument which will be passed through to the Featherlight.open method if called directly from the constructor. For standard use cases, direct instantiation isn’t necessary; instead, utilize the Featherlight.open() method or the jQuery plugin.

Methods

Featherlight provides several methods for manipulating lightbox instances:

Events

Featherlight triggers several custom events throughout its lifecycle. These events are useful for adding custom functionality and extending the library’s behavior. These events are detailed in the “Core Functionality” and “Customization Options” sections, but are summarized here:

These events are accessible via the on method on the instance (e.g., featherlightInstance.on('afterOpen', myFunction)), or via the options object when calling Featherlight.open.

Options

Featherlight offers a range of options for customizing its behavior. These options can be passed as a second argument to Featherlight.open(). Key options include:

A full example of using options:

Featherlight.open('#my-content', {
  closeOnEscape: false,
  afterOpen: function() { console.log('Lightbox opened!'); },
  variant: 'dark'
});

Remember to consult the latest Featherlight documentation for the most up-to-date list of options and their descriptions.

Troubleshooting

Common Issues and Solutions

Debugging Tips

Frequently Asked Questions (FAQ)

Remember to check the official Featherlight documentation and GitHub repository for the most current information and solutions to any issues you encounter.

Contributing

We welcome contributions to Featherlight! Whether you’re reporting bugs, suggesting features, or submitting code changes, your involvement is valuable. Please follow these guidelines to ensure a smooth and efficient contribution process.

Reporting Bugs

When reporting a bug, please provide the following information:

Suggesting Features

We encourage you to suggest new features for Featherlight. When suggesting a feature, please provide:

Submitting Pull Requests

If you’re submitting a pull request (PR), please follow these guidelines:

We will review your pull request and provide feedback.

Coding Style Guide

Featherlight uses a consistent coding style to maintain readability and maintainability. Please adhere to the following guidelines when contributing code:

Please ensure your code follows these guidelines before submitting a pull request. Consistency in code style makes it easier for others to review and understand your contributions. Refer to existing code in Featherlight’s source as a guide for style.