Looking to enhance your Symfony application's UX with elegant modal dialogs and toast notifications? Meet UX SweetAlert, a Symfony UX bundle that seamlessly integrates SweetAlert2 into your PHP backend and Twig frontend.
โจ Features
- โ Modal alerts (success, error, info, warning, question)
- ๐ฅ Toast notifications with customizable timers and positions
- ๐ก LiveComponent integration for confirm dialogs
- ๐ง Uses Symfony's FlashBag under the hood
- ๐จ Supports light, dark, or auto themes
- ๐ Fluent API for alert customization
โ๏ธ Installation
composer require pentiminax/ux-sweet-alert
If you're using Webpack Encore:
npm install --force
npm run watch
Already using Symfony UX? You're good to go!
๐ Getting Started
Start by injecting the AlertManagerInterface
or ToastManagerInterface
in your controller.
โ Example: Alert Modal
use Pentiminax\UX\SweetAlert\AlertManagerInterface;
public function updateSettings(AlertManagerInterface $alertManager): Response
{
$alertManager->success(
id: 'update-success',
title: 'Update Successful',
text: 'Your settings have been saved.'
);
return $this->redirectToRoute('dashboard');
}
๐ Example: Toast Notification
use Pentiminax\UX\SweetAlert\ToastManagerInterface;
public function profile(ToastManagerInterface $toastManager): Response
{
$toastManager->success(
id: 'profile-updated',
title: 'Profile Updated!',
text: 'Changes saved.',
timer: 3000,
timerProgressBar: true
);
return $this->redirectToRoute('profile');
}
๐๏ธ Customizing Alerts
All alerts return an Alert
object which supports a fluent API:
$alert = $alertManager->info('info-alert', 'Heads up!', 'Just a quick note');
$alert
->withCancelButton()
->withDenyButton()
->theme(Theme::DARK)
->confirmButtonColor('#0d6efd');
๐งช Rendering
To render alerts and toasts, include the Twig helper in your base layout:
{{ ux_sweet_alert_scripts() }}
Make sure your frontend listens for the JavaScript event:
document.addEventListener('ux-sweet-alert:alert:added', event => {
Swal.fire(event.detail);
});
๐งฌ UX Live Component Integration
Need a confirmation popup before taking action? Use the provided ConfirmButton
component:
<twig:SweetAlert:ConfirmButton
title="Are you sure?"
text="This action cannot be undone."
icon="warning"
showCancelButton="true"
callback="onConfirm"
/>
When clicked:
- Fires a LiveComponent event.
- Displays SweetAlert modal.
- Triggers the
onConfirm()
callback if confirmed.
๐ Docs
โค๏ธ Why Use This?
UX SweetAlert gives you an elegant, JavaScript-powered feedback system directly from your Symfony controllers โ no need to manage messy JS state manually.
If you're already using Symfony UX and Stimulus, this is a no-brainer.
๐ฆ GitHub repository : https://github.com/pentiminax/ux-sweet-alert
Top comments (0)