Skip to content

Commit 4cf4905

Browse files
committed
feat: support livewire components
1 parent 7a53dc5 commit 4cf4905

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
"larastan/larastan": "^2.9.1",
2626
"orchestra/testbench": "^9",
2727
"laravel/pint": "^1.14",
28-
"rector/rector": "^1.0"
28+
"rector/rector": "^1.0",
29+
"livewire/livewire": "^3.4"
30+
},
31+
"suggest": {
32+
"laravel/livewire": "Required for Livewire layout support."
2933
},
3034
"autoload": {
3135
"psr-4": {

src/Html/Layout.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Fluent;
99
use Illuminate\Support\Traits\Macroable;
1010
use InvalidArgumentException;
11+
use Livewire\Livewire;
1112
use Throwable;
1213
use Yajra\DataTables\Html\Enums\LayoutPosition;
1314

@@ -118,6 +119,27 @@ public function addView(
118119
return $this;
119120
}
120121

122+
/**
123+
* @param class-string $component
124+
*
125+
* @throws Throwable
126+
*/
127+
public function addLivewire(
128+
string $component,
129+
LayoutPosition $layoutPosition,
130+
?int $order = null
131+
): static {
132+
$html = json_encode(Livewire::mount($component));
133+
134+
if ($html === false) {
135+
throw new InvalidArgumentException("Cannot render Livewire component [$component] to json.");
136+
}
137+
138+
$this->attributes[$layoutPosition->withOrder($order)] = $this->renderCustomElement($html, false);
139+
140+
return $this;
141+
}
142+
121143
private function renderCustomElement(string $element, bool $asJsSelector = true): string
122144
{
123145
$html = $asJsSelector ? "$('{$element}').html()" : $element;

0 commit comments

Comments
 (0)