You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add support for JavaScript callback options (didOpen, willClose, etc.) (#16)
* Initial plan
* Add support for JavaScript callback options (didOpen, willClose, etc.)
Co-authored-by: limonte <[email protected]>
* Improve callback implementation: address code duplication and add security documentation
Co-authored-by: limonte <[email protected]>
* Add JSON_THROW_ON_ERROR flag for better error handling
Co-authored-by: limonte <[email protected]>
* Fix test workflow to use local package code instead of published version
Co-authored-by: limonte <[email protected]>
* Fix JSON encoding to escape HTML characters for security
Co-authored-by: limonte <[email protected]>
* Use JSON_HEX_TAG flag to escape HTML tags for XSS protection
Co-authored-by: limonte <[email protected]>
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: limonte <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+98-2Lines changed: 98 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -79,6 +79,29 @@ Swal::toastQuestion([
79
79
]);
80
80
```
81
81
82
+
### Using JavaScript Callbacks
83
+
84
+
You can use JavaScript callbacks (like `didOpen`, `willClose`, etc.) by passing them as strings:
85
+
86
+
```php
87
+
// Toast with pause on hover
88
+
Swal::fire([
89
+
'title' => 'Autoclosealert',
90
+
'toast' => true,
91
+
'position' => 'top-end',
92
+
'icon' => 'info',
93
+
'showConfirmButton' => false,
94
+
'timer' => 3000,
95
+
'timerProgressBar' => true,
96
+
'didOpen' => '(toast) => {
97
+
toast.onmouseenter = Swal.stopTimer;
98
+
toast.onmouseleave = Swal.resumeTimer;
99
+
}',
100
+
]);
101
+
```
102
+
103
+
For more details on using callbacks, see [FAQ #4](#4-what-are-the-limitations).
104
+
82
105

83
106
84
107
## Livewire Components
@@ -162,6 +185,25 @@ $this->swalToastQuestion([
162
185
]);
163
186
```
164
187
188
+
### Using JavaScript Callbacks in Livewire
189
+
190
+
Just like in Laravel controllers, you can use JavaScript callbacks in Livewire components:
191
+
192
+
```php
193
+
$this->swalFire([
194
+
'title' => 'Processing...',
195
+
'toast' => true,
196
+
'position' => 'top-end',
197
+
'icon' => 'info',
198
+
'timer' => 3000,
199
+
'timerProgressBar' => true,
200
+
'didOpen' => '(toast) => {
201
+
toast.onmouseenter = Swal.stopTimer;
202
+
toast.onmouseleave = Swal.resumeTimer;
203
+
}',
204
+
]);
205
+
```
206
+
165
207
## Inertia.js
166
208
167
209
You can use `Swal::fire()` or any of the available helper methods in your Inertia.js controllers to show popups after navigation:
@@ -330,5 +372,59 @@ This package uses a smart loading strategy for the SweetAlert2 library:
330
372
331
373
## 4. What are the limitations?
332
374
333
-
SweetAlert2 is a JavaScript package and some of its options are JS callbacks. It's not possible to use them in the `Swal::fire()` or `$this->swalFire()` methods.
334
-
If you need to use JS callbacks, you have to go to JS and use the SweetAlert2 API directly.
375
+
SweetAlert2 is a JavaScript package and some of its options are JS callbacks. While you can pass JavaScript callback functions as strings in the `Swal::fire()` or `$this->swalFire()` methods, keep in mind:
376
+
377
+
1.**Callbacks must be passed as strings**: Write your JavaScript function as a string. For example:
0 commit comments