Skip to content

Commit 8c5b989

Browse files
committed
Initial commit
0 parents  commit 8c5b989

22 files changed

+22982
-0
lines changed

.github/workflows/php.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: PHPUnit
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
php-versions: [ '7.4', '8.0' ]
16+
phpunit-versions: [ 'latest' ]
17+
18+
name: PHP ${{ matrix.php-versions }}
19+
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v2
23+
24+
- name: Setup PHP
25+
uses: shivammathur/setup-php@v2
26+
with:
27+
php-version: ${{ matrix.php-versions }}
28+
extensions: mbstring, ctype, fileinfo, openssl, PDO, bcmath, json, tokenizer, xml
29+
tools: phpunit:${{ matrix.phpunit-versions }}
30+
coverage: none
31+
32+
- name: Install dependencies
33+
if: steps.composer-cache.outputs.cache-hit != 'true'
34+
run: |
35+
composer update --prefer-dist --no-interaction --no-suggest
36+
37+
- name: Run test suite
38+
run: composer run-script test

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
/vendor
3+
.idea
4+
.DS_Store
5+
composer.lock

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright © 2021 Philo Hermans and contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
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).

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "livewire-ui/modal",
3+
"description": "Laravel Livewire UI component",
4+
"require-dev": {
5+
"orchestra/testbench": "^6.15",
6+
"phpunit/phpunit": "^9.5"
7+
},
8+
"scripts": {
9+
"test": "vendor/bin/phpunit",
10+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
11+
},
12+
"extra": {
13+
"laravel": {
14+
"providers": [
15+
"LivewireUI\\Modal\\LivewireModalServiceProvider"
16+
]
17+
}
18+
},
19+
"require": {
20+
"php": "^7.4|^8.0",
21+
"livewire/livewire": "^2.0",
22+
"livewire-ui/livewire-ui": "^0.1.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"LivewireUI\\Modal\\": "src"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"LivewireUI\\Modal\\Tests\\": "tests"
32+
}
33+
},
34+
"minimum-stability": "dev",
35+
"prefer-stable": true
36+
}

mix-manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"/public/modal.js": "/public/modal.js"
3+
}

0 commit comments

Comments
 (0)