Installation:
composer require superbyte23/sileo-livewire
npm install sileo react react-dom @vitejs/plugin-react
Update vite.config.js to include the React plugin.
Publish Assets:
php artisan vendor:publish --tag=sileo-livewire-js
Add the toaster to your root layout:
<livewire:sileo-toaster position="top-right" />
First Toast:
Use the SileoToastable trait in a Livewire component:
use Superbyte23\SileoLivewire\SileoToastable;
class MyComponent extends Component {
use SileoToastable;
public function showToast() {
$this->toast('Success!', 'This is a test toast.', 'success');
}
}
SileoToastable trait methods in src/SileoToastable.php.resources/js/sileo-bridge.js for emitted events and payload structure.Component Integration:
SileoToastable trait in any Livewire component to enable toasts.public function submitForm() {
if ($this->validate()) {
$this->save();
$this->toast('Saved!', 'Your changes have been saved.', 'success');
} else {
$this->toast('Error!', 'Please fix the errors below.', 'error');
}
}
Dynamic Toasts:
$this->toast('User Created', "User {$this->user->name} added successfully.", 'info');
Custom Toast Types:
success, error, info, warning) by modifying the JavaScript bridge or publishing custom assets.Positioning:
position prop in the SileoToaster component:
<livewire:sileo-toaster position="bottom-left" />
Form Submission:
public function save() {
try {
$this->validate();
$this->performSave();
$this->toast('Success', 'Data saved.', 'success');
} catch (\Exception $e) {
$this->toast('Error', $e->getMessage(), 'error');
}
}
Async Operations:
public function upload() {
$this->toast('Uploading...', 'Please wait.', 'info');
$this->dispatch('upload-started');
// Handle upload logic...
}
Global Toasts:
SileoToastable trait or using a facade/service.Vite Optimization:
sileo-bridge.js is imported in your main JS file (e.g., resources/js/app.js):
import './sileo-bridge';
Styling:
.sileo-toast {
--sileo-bg: #2d3748;
--sileo-text: #ffffff;
}
Testing:
SileoToastable trait in PHPUnit tests:
$component->expectsToast('Success', 'Test', 'success');
JavaScript Bridge Missing:
php artisan vendor:publish --tag=sileo-livewire-js) will result in no toasts appearing.sileo-bridge.js is imported.React/Vite Mismatch:
@vitejs/plugin-react is included in vite.config.js.react and react-dom are installed via npm.Livewire Component Not Found:
<livewire:sileo-toaster /> fails, ensure:
SileoToaster component is registered (automatically done via the package).config/livewire.php.Toast Not Showing:
SileoToastable trait or JS bridge issues).console.log in sileo-bridge.js to verify events are emitted.Event Listener Issues:
sileo-bridge.js is correctly set up:
window.addEventListener('toast', (e) => {
console.log('Toast event:', e.detail); // Debug payload
// ... rest of the logic
});
Payload Structure:
$this->dispatch('toast', [
'title' => 'Title',
'message' => 'Message',
'type' => 'success',
]);
Custom Toast Types:
// In sileo-bridge.js
const types = {
success: { bg: '#48bb78', icon: '✓' },
error: { bg: '#f56565', icon: '✗' },
custom: { bg: '#6366f1', icon: '🎨' }, // Add custom type
};
Auto-Dismissal:
duration parameter (in milliseconds):
$this->toast('Info', 'This will disappear in 3 seconds.', 'info', 3000);
Dark Mode Support:
dark prop to the SileoToaster:
<livewire:sileo-toaster position="top-right" dark="{{ request()->wantsDarkMode() }}" />
Performance:
Localization:
__() function:
$this->toast('Success', __('messages.saved_successfully'), 'success');
How can I help you explore Laravel packages today?