contao-components/colorbox
Contao integration for the jQuery Colorbox lightbox. Adds easy modal image/content overlays with configurable settings for galleries and links, fitting neatly into Contao sites to provide a simple, responsive lightbox experience.
Installation
composer require contao-components/colorbox
Ensure the package is registered in config/app.php under providers (if not auto-discovered).
Basic Usage Include the Colorbox CSS/JS in your Laravel app:
@vite(['resources/css/colorbox.css', 'resources/js/colorbox.js'])
Or via CDN in a blade template:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/colorbox/1.6.4/colorbox.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/colorbox/1.6.4/jquery.colorbox-min.js"></script>
First Use Case Trigger Colorbox on a link:
<a href="path/to/image.jpg" class="colorbox" title="My Image">Open Image</a>
Initialize via JavaScript:
$(document).ready(function() {
$(".colorbox").colorbox({maxWidth:"80%", maxHeight:"80%"});
});
Dynamic Content Loading Use Laravel Blade + AJAX to fetch content dynamically:
$(".dynamic-link").colorbox({
href: "/api/content/" + id,
onLoad: function() {
$.get("/api/content/" + id, function(data) {
$(".colorbox").html(data);
});
}
});
Integration with Laravel Collections Loop through a collection and generate Colorbox links:
@foreach($images as $image)
<a href="{{ asset('storage/' . $image->path) }}"
class="colorbox"
title="{{ $image->title }}">
{{ $image->thumbnail }}
</a>
@endforeach
Form Handling Inside Colorbox
Use onClosed to process form submissions:
$(".colorbox-form").colorbox({
onClosed: function() {
$.post("/submit-form", $("#form-id").serialize(), function(response) {
alert("Submitted!");
});
}
});
public function boot()
{
Colorbox::macro('laravelInit', function() {
return $this->js('$(document).ready(function(){ $(".colorbox").colorbox({maxWidth:"90%"}); });');
});
}
@colorbox directive for reusable markup.jQuery Dependency Ensure jQuery is loaded before Colorbox:
<!-- Wrong order -->
<script src="colorbox.js"></script>
<script src="jquery.js"></script>
<!-- Correct -->
<script src="jquery.js"></script>
<script src="colorbox.js"></script>
CSS Conflicts Colorbox may override Bootstrap/modal styles. Override with:
.colorbox, #colorbox, .colorbox-loaded { z-index: 9999 !important; }
Lazy Loading Issues If using lazy-loaded images, ensure Colorbox triggers after images load:
$(".colorbox").colorbox({
onOpen: function() {
$(".lazy-image").trigger("click"); // Force load
}
});
overflow: hidden or z-index.Custom Templates Override default templates by extending the package’s JS:
$.colorbox.settings.template = '<div class="my-custom-colorbox">{content}</div>';
Laravel Events
Hook into colorbox.init via a custom event listener:
Event::listen('colorbox.init', function() {
// Post-init logic (e.g., analytics)
});
API Integration Use Laravel’s HTTP client to fetch remote content:
$(".remote-link").colorbox({
href: function() {
return "{{ route('api.colorbox.content') }}?id=" + $(this).data('id');
}
});
How can I help you explore Laravel packages today?