|
| 1 | +<p align="center"> |
| 2 | +<a href="https://github.com/livewire-ui/modal/actions"><img src="https://github.com/livewire-ui/modal/workflows/PHPUnit/badge.svg" alt="Build Status"></a> |
| 3 | +<a href="https://packagist.org/packages/livewire-ui/modal"><img src="https://img.shields.io/packagist/dt/livewire-ui/modal" alt="Total Downloads"></a> |
| 4 | +<a href="https://packagist.org/packages/livewire-ui/modal"><img src="https://img.shields.io/packagist/v/livewire-ui/modal" alt="Latest Stable Version"></a> |
| 5 | +<a href="https://packagist.org/packages/livewire-ui/modal"><img src="https://img.shields.io/packagist/l/livewire-ui/modal" alt="License"></a> |
| 6 | +</p> |
| 7 | + |
| 8 | +## About LivewireUI Modal |
| 9 | +LivewireUI Modal is a Livewire component that provides you with a modal that supports multiple child modals while maintaining state. |
| 10 | + |
| 11 | +## Installation |
| 12 | +To get started, require the package via Composer: |
| 13 | + |
| 14 | +``` |
| 15 | +composer require livewire-ui/modal |
| 16 | +``` |
| 17 | + |
| 18 | +## Livewire directive |
| 19 | +Add the Livewire directive `@livewire('livewire-ui-modal')` and also the Javascript `@livewireUIScripts` directive to your template. |
| 20 | +```html |
| 21 | +<html> |
| 22 | +<body> |
| 23 | + <!-- content --> |
| 24 | + |
| 25 | + @livewire('livewire-ui-modal') |
| 26 | + @livewireUIScripts |
| 27 | +</body> |
| 28 | +</html> |
| 29 | +``` |
| 30 | + |
| 31 | +Next you will need to publish the required scripts with the following command: |
| 32 | +```shell |
| 33 | +php artisan vendor:publish --tag=livewire-ui:public |
| 34 | +``` |
| 35 | + |
| 36 | +## Alpine |
| 37 | +Livewire UI requires [Alpine](https://github.com/alpinejs/alpine). You can use the official CDN to quickly include Alpine: |
| 38 | + |
| 39 | +```html |
| 40 | +< script src= "https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></ script> |
| 41 | +``` |
| 42 | + |
| 43 | +## TailwindCSS |
| 44 | +The base modal is made with TailwindCSS. If you use a different CSS framework I recommend that you publish the modal template and change the markup to include the required classes for your CSS framework. |
| 45 | +```shell |
| 46 | +php artisan vendor:publish --tag=livewire-ui:views |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +## Creating a modal |
| 51 | +You can run `php artisan make:livewire EditUser` to make the initial Livewire component. Open your component class and make sure it extends the `ModalComponent` class: |
| 52 | + |
| 53 | +```php |
| 54 | +<?php |
| 55 | + |
| 56 | +namespace App\Http\Livewire; |
| 57 | + |
| 58 | +use LivewireUI\Modal\ModalComponent; |
| 59 | + |
| 60 | +class EditUser extends ModalComponent |
| 61 | +{ |
| 62 | + public function render() |
| 63 | + { |
| 64 | + return view('livewire.edit-user'); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Opening a modal |
| 70 | +To open a modal you will need to emit an event. To open the `EditUser` modal for example: |
| 71 | + |
| 72 | +```html |
| 73 | +<!-- Outside of any Livewire component --> |
| 74 | +<button onclick="Livewire.emit('openModal', 'edit-user')">Edit User</button> |
| 75 | + |
| 76 | +<!-- Inside existing Livewire component --> |
| 77 | +<button wire:click="$emit('openModal', 'edit-user')">Edit User</button> |
| 78 | +``` |
| 79 | + |
| 80 | +## Passing parameters |
| 81 | +To open the `EditUser` modal for a specific user we can pass the user id: |
| 82 | + |
| 83 | +```html |
| 84 | +<!-- Outside of any Livewire component --> |
| 85 | +<button onclick="Livewire.emit('openModal', 'edit-user', @json(['user' => $user->id]))">Edit User</button> |
| 86 | + |
| 87 | +<!-- Inside existing Livewire component --> |
| 88 | +<button wire:click="$emit('openModal', 'edit-user', @json(['user' => $user->id])">Edit User</button> |
| 89 | +``` |
| 90 | + |
| 91 | +The parameters are passed to the `mount` method on the modal component: |
| 92 | + |
| 93 | +```php |
| 94 | +<?php |
| 95 | + |
| 96 | +namespace App\Http\Livewire; |
| 97 | + |
| 98 | +use App\Models\User; |
| 99 | +use LivewireUI\Modal\ModalComponent; |
| 100 | + |
| 101 | +class EditUser extends ModalComponent |
| 102 | +{ |
| 103 | + public User $user; |
| 104 | + |
| 105 | + public function mount(User $user) { |
| 106 | + $this->user = $user; |
| 107 | + } |
| 108 | + |
| 109 | + public function render() |
| 110 | + { |
| 111 | + return view('livewire.edit-user'); |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Opening a child modal |
| 117 | +From an existing modal you can use the exact same event and a child modal will be created: |
| 118 | + |
| 119 | +```html |
| 120 | +<!-- Edit User Modal --> |
| 121 | + |
| 122 | +<!-- Edit Form --> |
| 123 | + |
| 124 | +<button wire:click="$emit('openModal', 'delete-user', @json(['user' => $user->id])">Delete User</button> |
| 125 | +``` |
| 126 | + |
| 127 | +## Closing a (child) modal |
| 128 | +If for example a user clicks the 'Delete' button which will open a confirm dialog, you can cancel the deletion and return to the edit user modal by emitting the `closeModal` event. This will open the previous modal. If there is no previous modal the entire modal component is closed and the state will be reset. |
| 129 | +```html |
| 130 | +<button wire:click="$emit('closeModal')">No, do not delete</button> |
| 131 | +``` |
| 132 | + |
| 133 | +You can also close a modal from within your modal component class: |
| 134 | + |
| 135 | +```php |
| 136 | +<?php |
| 137 | + |
| 138 | +namespace App\Http\Livewire; |
| 139 | + |
| 140 | +use App\Models\User; |
| 141 | +use LivewireUI\Modal\ModalComponent; |
| 142 | + |
| 143 | +class EditUser extends ModalComponent |
| 144 | +{ |
| 145 | + public User $user; |
| 146 | + |
| 147 | + public function mount(User $user) { |
| 148 | + $this->user = $user; |
| 149 | + } |
| 150 | + |
| 151 | + public function update() |
| 152 | + { |
| 153 | + $this->user->update($data); |
| 154 | + |
| 155 | + $this->closeModal(); |
| 156 | + } |
| 157 | + |
| 158 | + public function render() |
| 159 | + { |
| 160 | + return view('livewire.edit-user'); |
| 161 | + } |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +If you don't want to go to the previous modal but close the entire modal component you can use the `forceClose` method: |
| 166 | + |
| 167 | +```php |
| 168 | +public function update() |
| 169 | +{ |
| 170 | + $this->user->update($data); |
| 171 | + |
| 172 | + $this->forceClose()->closeModal(); |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +Often you will want to update other Livewire components when changes have been made. For example, the user overview when a user is updated. You can use the `closeModalWithEvents` method to achieve this. |
| 177 | + |
| 178 | +```php |
| 179 | +public function update() |
| 180 | +{ |
| 181 | + $this->user->update($data); |
| 182 | + |
| 183 | + $this->closeModalWithEvents([ |
| 184 | + UserOverview::getName() => 'userModified', |
| 185 | + ]); |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +It's also possible to add parameters to your events: |
| 190 | + |
| 191 | +```php |
| 192 | +public function update() |
| 193 | +{ |
| 194 | + $this->user->update($data); |
| 195 | + |
| 196 | + $this->closeModalWithEvents([ |
| 197 | + UserOverview::getName() => ['userModified', [$this->user->id], |
| 198 | + ]); |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +## Skipping previous modals |
| 203 | +In some cases you might want to skip previous modals. For example: |
| 204 | +1. Team overview modal |
| 205 | +2. -> Edit Team |
| 206 | +3. -> Delete Team |
| 207 | + |
| 208 | +In this case, when a team is deleted, you don't want to go back to step 2 but go back to the overview. |
| 209 | +You can use the `skipPreviousModal` method to achieve this. By default it will skip the previous modal. If you want to skip more you can pass the number of modals to skip `skipPreviousModals(2)`. |
| 210 | + |
| 211 | +```php |
| 212 | +<?php |
| 213 | + |
| 214 | +namespace App\Http\Livewire; |
| 215 | + |
| 216 | +use App\Models\Team; |
| 217 | +use LivewireUI\Modal\ModalComponent; |
| 218 | + |
| 219 | +class DeleteTeam extends ModalComponent |
| 220 | +{ |
| 221 | + public Team $team; |
| 222 | + |
| 223 | + public function mount(Team $team) { |
| 224 | + $this->team = $team; |
| 225 | + } |
| 226 | + |
| 227 | + public function delete() |
| 228 | + { |
| 229 | + $this->team->delete(); |
| 230 | + |
| 231 | + $this->skipPreviousModal()->closeModalWithEvents([ |
| 232 | + TeamOverview::getName() => 'teamDeleted' |
| 233 | + ]); |
| 234 | + } |
| 235 | + |
| 236 | + public function render() |
| 237 | + { |
| 238 | + return view('livewire.delete-team'); |
| 239 | + } |
| 240 | +} |
| 241 | +``` |
| 242 | + |
| 243 | +## Credits |
| 244 | +- [Philo Hermans](https://github.com/philoNL) |
| 245 | +- [All Contributors](../../contributors) |
| 246 | + |
| 247 | +## License |
| 248 | +Livewire UI is open-sourced software licensed under the [MIT license](LICENSE.md). |
0 commit comments