PHP code example of andisiahaan / livewire-dialog

1. Go to this page and download the library: Download andisiahaan/livewire-dialog library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andisiahaan / livewire-dialog example snippets




namespace App\Livewire;

use AndiSiahaan\Dialog\Components\BaseDialog;
use AndiSiahaan\Dialog\Support\DialogOptions;

class EditUser extends BaseDialog
{
    public $user;

    public function mount($userId)
    {
        $this->user = User::findOrFail($userId);
    }

    public function dialogOptions(): DialogOptions
    {
        return DialogOptions::make()
            ->size('lg')
            ->dismissOnClickOutside(false);
    }

    public function save()
    {
        $this->user->save();
        $this->dismiss();
    }

    public function render()
    {
        return view('livewire.edit-user');
    }
}

use AndiSiahaan\Dialog\Dialog;

// Modal
Dialog::modal('edit-user', ['userId' => $id]);

// Sheet (slides from bottom)
Dialog::sheet('select-option');

// Drawer (slides from side)
Dialog::drawer('navigation-menu', [], 'left');

// Alert
Dialog::alert('Success!', 'User has been saved.');

// Confirm
Dialog::confirm('Delete?', 'Are you sure you want to delete this user?');

use AndiSiahaan\Dialog\Concerns\InteractsWithDialog;

class UserList extends Component
{
    use InteractsWithDialog;

    public function editUser($id)
    {
        $this->openDialog('edit-user', ['userId' => $id]);
    }

    public function showSheet()
    {
        $this->openSheet('select-option');
    }

    public function deleteUser($id)
    {
        $this->confirmAction('Delete this user?', 'confirmDelete', ['userId' => $id]);
    }
}

dialog('edit-user', ['userId' => $id]);
dialog_modal('edit-user', ['userId' => $id]);
dialog_sheet('select-option');
dialog_drawer('sidebar', [], 'left');
dialog_alert('Success!', 'Data saved.');
dialog_confirm('Delete?', 'Are you sure?');

public function save()
{
    $this->user->save();
    $this->dismiss();
}

// Force close all dialogs
public function cancel()
{
    $this->forceAll()->dismiss();
}

// Skip previous dialog
public function deleteAndGoBack()
{
    $this->skipPrevious()->dismiss();
}

// Dismiss with events
public function save()
{
    $this->dismissWithEvents([
        UserList::class => 'refreshList',
    ]);
}

public function dialogOptions(): DialogOptions
{
    return DialogOptions::make()
        ->size('lg')                      // sm, md, lg, xl, 2xl-7xl, full
        ->position('center')              // center, top, bottom, left, right
        ->animation('scale')              // none, fade, scale, slide-up/down/left/right
        ->dismissible()                   // Enable dismiss
        ->dismissOnEscape(true)           // Close on Escape key
        ->dismissOnClickOutside(false)    // Close on backdrop click
        ->escapeClosesAll(true)           // Escape closes all dialogs
        ->destroyOnDismiss(true)          // Destroy component on close
        ->persistent();                   // Cannot be dismissed at all
}

'presets' => [
    'confirmation' => [
        'size' => 'sm',
        'animation' => 'scale',
        'dismiss_on_click_outside' => false,
    ],
    'fullscreen' => [
        'size' => 'full',
        'animation' => 'fade',
    ],
],

public function dialogOptions(): DialogOptions
{
    return DialogOptions::make()->preset('confirmation');
}

// Called when dialog is opening (return false to prevent)
protected function onOpening(): bool
{
    return true;
}

// Called when dialog is closing (return false to prevent)
protected function onClosing(): bool
{
    if ($this->formIsDirty) {
        return false; // Prevent closing
    }
    return true;
}

// Called after dialog has closed
protected function onClosed(): void
{
    // Cleanup logic
}
javascript
export default {
    content: [
        './vendor/andisiahaan/livewire-dialog/resources/views/**/*.blade.php',
        // ... your other paths
    ],
    safelist: [
        {
            pattern: /max-w-(sm|md|lg|xl|2xl|3xl|4xl|5xl|6xl|7xl)/,
            variants: ['sm', 'md', 'lg', 'xl', '2xl']
        }
    ],
}
bash
php artisan vendor:publish --tag=dialog-config
bash
php artisan vendor:publish --tag=dialog-views