Skip to content

Commit 01ea690

Browse files
committed
Livewire v3 compatibility
1 parent a952aa7 commit 01ea690

12 files changed

Lines changed: 62 additions & 94 deletions

File tree

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
php-versions: [ '7.4', '8.0' ]
15+
php-versions: [ '8.1' ]
1616
phpunit-versions: [ 'latest' ]
1717

1818
name: PHP ${{ matrix.php-versions }}

README.md

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
<a href="https://packagist.org/packages/wire-elements/modal"><img src="https://img.shields.io/packagist/l/wire-elements/modal" alt="License"></a>
77
</p>
88

9+
## BETA for Livewire v3
10+
This is the readme for the BETA version of the package for Livewire v3. **If you are looking for the readme for the stable version for Livewire v2 [click here](https://github.com/wire-elements/modal/tree/1.0.0).**
11+
12+
913
## About Wire Elements Modal
1014
Wire Elements Modal is a Livewire component that provides you with a modal that supports multiple child modals while maintaining state.
1115

@@ -18,7 +22,7 @@ Click the image above to read a full article on using the Wire Elements modal pa
1822
To get started, require the package via Composer:
1923

2024
```
21-
composer require wire-elements/modal
25+
composer require wire-elements/modal:^2.0@beta
2226
```
2327

2428
## Livewire directive
@@ -33,17 +37,6 @@ Add the Livewire directive `@livewire('livewire-ui-modal')` directive to your te
3337
</html>
3438
```
3539

36-
## Alpine
37-
Livewire Elements Modal requires [Alpine](https://github.com/alpinejs/alpine) and the plugin[Focus](https://alpinejs.dev/plugins/focus). You can use the official CDN to quickly include Alpine:
38-
39-
```html
40-
<!-- Alpine v3 -->
41-
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
42-
43-
<!-- Focus plugin -->
44-
<script defer src="https://unpkg.com/@alpinejs/focus@3.x.x/dist/cdn.min.js"></script>
45-
```
46-
4740
## TailwindCSS
4841
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.
4942
```shell
@@ -75,30 +68,30 @@ To open a modal you will need to emit an event. To open the `EditUser` modal for
7568

7669
```html
7770
<!-- Outside of any Livewire component -->
78-
<button onclick="Livewire.emit('openModal', 'edit-user')">Edit User</button>
71+
<button onclick="Livewire.$dispatch('openModal', {component: 'edit-user'})">Edit User</button>
7972

8073
<!-- Inside existing Livewire component -->
81-
<button wire:click="$emit('openModal', 'edit-user')">Edit User</button>
74+
<button wire:click="$dispatch('openModal', {component: 'edit-user'})">Edit User</button>
8275

8376
<!-- Taking namespace into account for component Admin/Actions/EditUser -->
84-
<button wire:click="$emit('openModal', 'admin.actions.edit-user')">Edit User</button>
77+
<button wire:click="$dispatch('openModal', {component: 'admin.actions.edit-user'})">Edit User</button>
8578
```
8679

8780
## Passing parameters
8881
To open the `EditUser` modal for a specific user we can pass the user id:
8982

9083
```html
9184
<!-- Outside of any Livewire component -->
92-
<button onclick="Livewire.emit('openModal', 'edit-user', {{ json_encode(['user' => $user->id]) }})">Edit User</button>
85+
<button onclick="Livewire.emit('openModal', {component: 'edit-user', parameters: {user: $user->id}})">Edit User</button>
9386

9487
<!-- Inside existing Livewire component -->
95-
<button wire:click="$emit('openModal', 'edit-user', {{ json_encode(['user' => $user->id]) }})">Edit User</button>
88+
<button wire:click="$emit('openModal', {component: 'edit-user', parameters: {user: $user->id}})">Edit User</button>
9689

9790
<!-- If you use a different primaryKey (e.g. email), adjust accordingly -->
98-
<button wire:click="$emit('openModal', 'edit-user', {{ json_encode(['user' => $user->email]) }})">Edit User</button>
91+
<button wire:click="$emit('openModal', {component: 'edit-user', parameters: {user: $user->email}})">Edit User</button>
9992

10093
<!-- Example of passing multiple parameters -->
101-
<button wire:click="$emit('openModal', 'edit-user', {{ json_encode([$user->id, $isAdmin]) }})">Edit User</button>
94+
<button wire:click="$emit('openModal', {component: 'edit-user', parameters: {user: $user->id, advancedMode: true}})">Edit User</button>
10295
```
10396

10497
The parameters are injected into the modal component and the model will be automatically fetched from the database if the type is defined:
@@ -140,7 +133,7 @@ From an existing modal you can use the exact same event and a child modal will b
140133

141134
<!-- Edit Form -->
142135

143-
<button wire:click='$emit("openModal", "delete-user", {{ json_encode(["user" => $user->id]) }})'>Delete User</button>
136+
<button wire:click='$emit("openModal"{component: 'delete-user', parameters: {user: $user->id}})'>Delete User</button>
144137
```
145138

146139
## Closing a (child) modal

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
],
1616
"require-dev": {
17-
"orchestra/testbench": "^6.15",
17+
"orchestra/testbench": "^8.5",
1818
"phpunit/phpunit": "^9.5"
1919
},
2020
"scripts": {
@@ -30,7 +30,7 @@
3030
},
3131
"require": {
3232
"php": "^7.4|^8.0",
33-
"livewire/livewire": "^2.0",
33+
"livewire/livewire": "^3.0",
3434
"spatie/laravel-package-tools": "^1.9"
3535
},
3636
"autoload": {

public/modal.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/modal.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@ window.LivewireUIModal = () => {
3232

3333
if (this.getActiveComponentModalAttribute('dispatchCloseEvent') === true) {
3434
const componentName = this.$wire.get('components')[this.activeComponent].name;
35-
Livewire.emit('modalClosed', componentName);
35+
Livewire.dispatch('modalClosed', componentName);
3636
}
3737

3838
if (this.getActiveComponentModalAttribute('destroyOnClose') === true) {
39-
Livewire.emit('destroyComponent', this.activeComponent);
39+
Livewire.dispatch('destroyComponent', this.activeComponent);
4040
}
4141

4242
if (skipPreviousModals > 0) {
4343
for (var i = 0; i < skipPreviousModals; i++) {
4444
if (destroySkipped) {
4545
const id = this.componentHistory[this.componentHistory.length - 1];
46-
Livewire.emit('destroyComponent', id);
46+
Livewire.dispatch('destroyComponent', {id: id});
4747
}
4848
this.componentHistory.pop();
4949
}
5050
}
5151

5252
const id = this.componentHistory.pop();
5353

54-
if (id && force === false) {
54+
if (id && !force) {
5555
if (id) {
5656
this.setActiveModalComponent(id, true);
5757
} else {
@@ -140,11 +140,11 @@ window.LivewireUIModal = () => {
140140
init() {
141141
this.modalWidth = this.getActiveComponentModalAttribute('maxWidthClass');
142142

143-
Livewire.on('closeModal', (force = false, skipPreviousModals = 0, destroySkipped = false) => {
144-
this.closeModal(force, skipPreviousModals, destroySkipped);
143+
Livewire.on('closeModal', (data) => {
144+
this.closeModal(data?.force ?? false, data?.skipPreviousModals ?? 0, data?.destroySkipped ?? false);
145145
});
146146

147-
Livewire.on('activeModalComponentChanged', (id) => {
147+
Livewire.on('activeModalComponentChanged', ({id}) => {
148148
this.setActiveModalComponent(id);
149149
});
150150
}

resources/views/modal.blade.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
<div
1010
x-data="LivewireUIModal()"
11-
x-init="init()"
1211
x-on:close.stop="setShowPropertyTo(false)"
1312
x-on:keydown.escape.window="closeModalOnEscape()"
1413
x-show="show"

0 commit comments

Comments
 (0)