PHP code example of salioudiabate / modal

1. Go to this page and download the library: Download salioudiabate/modal 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/ */

    

salioudiabate / modal example snippets


Modal::open(EditUser::class)->with(['user' => $user])->show();

use Salioudiabate\Modal\ModalComponent;

class EditUser extends ModalComponent
{
    public ?User $user = null;

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

    public function save(): void
    {
        $this->user->save();

        $this->closeModal();
    }

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

use Salioudiabate\Modal\Concerns\InteractsWithModals;

class UserTable extends Component
{
    use InteractsWithModals;

    public function edit(User $user): void
    {
        $this->openModal(EditUser::class, ['user' => $user]);

        // or the fluent form, for anything openModal() can't reach:
        $this->modal(EditUser::class)
            ->with(['user' => $user])
            ->maxWidth('xl')
            ->show();
    }
}

Modal::open(EditUser::class)->with(['user' => $user])->show();

Modal::open(EditUser::class)
    ->with(['user' => $user])       // arguments passed to mount()
    ->maxWidth('xl')                 // xs..5xl, full, or a raw length: '480px', '60ch', '75vw'
    ->slideOver()                    // docks to the edge of the screen instead of centering
    ->closeOnClickAway(false)        // clicking the backdrop does nothing
    ->closeOnEscape(false)           // Escape does nothing
    ->forceCloseOnEscape(false)      // Escape pops back to the previous modal instead of closing the whole stack
    ->dispatchCloseEvent()           // fires "modal-closed" (name: ...) once this modal closes
    ->destroyOnClose()               // drops the component's server-side state on close, instead of keeping it for a possible re-open
    ->forComponent($this)            // explicit dispatch target (usually unnecessary — see above)
    ->show();                        // alias: ->open()

class EditUser extends ModalComponent
{
    public static function modalMaxWidth(): string { return 'xl'; }
    public static function slideOver(): bool { return true; }
    public static function closeOnClickAway(): bool { return false; }
    public static function closeOnEscape(): bool { return true; }
    public static function forceCloseOnEscape(): bool { return true; }
    public static function dispatchCloseEvent(): bool { return false; }
    public static function destroyOnClose(): bool { return false; }
}

$this->closeModal();

// or, dispatching one or more events first (page-wide, or scoped to a component):
$this->closeModalWithEvents([
    'user-updated',                          // page-wide
    'user-table' => 'refresh',                // scoped to the "user-table" component
    'user-table' => ['refresh', ['id' => 1]], // ...with parameters
]);

$this->skipPreviousModals(count: 1, destroy: true)->closeModal();

class EditUser extends ModalComponent
{
    public ?User $user = null; // resolved from the "user" id above

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

// config/modal.php
'dropdown_events' => [
    'opened' => 'select-search-opened',
    'closed' => 'select-search-closed',
],

use Livewire\Livewire;

Livewire::test(EditUser::class, ['user' => $user])
    ->call('save')
    ->assertDispatched('modal-close');

Livewire::test(UserTable::class)
    ->call('edit', $user)
    ->assertDispatched('modal-open', name: EditUser::class, arguments: ['user' => $user->id]);
bash
php artisan vendor:publish --tag=modal-assets
bash
php artisan vendor:publish --tag=modal-config
bash
php artisan make:modal EditUser
js
Modal.setColorScheme('dark'); // 'light' | 'dark' | 'system'
bash
php artisan vendor:publish --tag=modal-views
bash
php artisan vendor:publish --tag=modal-config