Confirm Before Exit
A common pattern in product tours is to ask the user for confirmation before they exit the tour. Driver.js supports this through the onDestroyStarted hook, and Laravel Driver.js provides a convenient helper to generate the confirm dialog pattern.

The onDestroyStarted Hook
The onDestroyStarted hook is special: if defined, calling destroy() will trigger this hook and NOT actually destroy the driver. You must call driver.destroy() again (inside the hook) to actually destroy it. This enables "confirm before exit" patterns.
Method Signature
DriverJs::onDestroyStarted(string $callback);Manual Example
use RealRashid\LaravelDriverJs\Facades\DriverJs;
DriverJs::tour('onboarding')
->onDestroyStarted('function(element, step, opts) { if (confirm("Are you sure you want to exit the tour?")) { opts.driver.destroy(); } }')
->step('#header', 'Welcome')
->step('#nav', 'Navigation')
->render();Using the StepHooks Helper
Laravel Driver.js provides a StepHooks helper class with a built-in confirm-before-destroy pattern:
use RealRashid\LaravelDriverJs\Support\StepHooks;
DriverJs::tour('onboarding')
->onDestroyStarted(StepHooks::confirmBeforeDestroy())
->step('#header', 'Welcome')
->step('#nav', 'Navigation')
->render();Custom Confirmation Message
You can pass a custom message to the confirmBeforeDestroy() method:
use RealRashid\LaravelDriverJs\Support\StepHooks;
DriverJs::tour('onboarding')
->onDestroyStarted(StepHooks::confirmBeforeDestroy('Are you sure? The tour will help you get started!'))
->step('#header', 'Welcome')
->step('#nav', 'Navigation')
->render();How It Works
When the user tries to close the tour (by clicking the overlay, pressing Escape, or clicking the Close button):
- Driver.js calls the
onDestroyStartedhook instead of destroying the tour. - The hook displays a browser
confirm()dialog with the specified message. - If the user clicks "OK", the hook calls
opts.driver.destroy()which actually destroys the tour. - If the user clicks "Cancel", the tour continues as if nothing happened.
Custom Exit Handler
For more complex exit handling (like showing a custom modal instead of the browser's confirm() dialog), you can write your own JavaScript function:
DriverJs::tour('onboarding')
->onDestroyStarted('customExitHandler')
->step('#header', 'Welcome')
->render();// In your JavaScript
function customExitHandler(element, step, opts) {
// Show a custom modal
Swal.fire({
title: 'Exit Tour?',
text: 'Are you sure you want to exit? You can always restart it later.',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, exit',
cancelButtonText: 'Continue tour'
}).then((result) => {
if (result.isConfirmed) {
opts.driver.destroy();
}
});
}That's it! You're now equipped to add confirm-before-exit behavior to your tours using Laravel Driver.js.