Created
September 28, 2021 14:35
-
-
Save fotrino/2c2dfb35a581612da0de68299150d5da to your computer and use it in GitHub Desktop.
@mentions with Alpine.js, Livewire & Tributeq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<x-mention wire:model="body" :mentionables="$mentionables" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Livewire; | |
use Livewire\Component; | |
use App\Models\User; | |
class CreateComment extends Component | |
{ | |
public $mentionables; | |
public $body; | |
public function mount() | |
{ | |
$this->mentionables = User::all() | |
->map(function ($user) { | |
return [ | |
'key' => $user->name, | |
'value' => $user->username, | |
]; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@props([ | |
'mentionables' => [] | |
]) | |
<div | |
x-data | |
x-init=' | |
()=> { | |
let tribute = new Tribute({ | |
trigger: "@", | |
values: @json($mentionables) | |
}); | |
tribute.attach($refs.textarea); | |
} | |
' | |
wire:ignore | |
> | |
<textarea x-ref="textarea" {{ $attributes->merge(['class' => 'form-control']) }}></textarea> | |
</div> | |
@once | |
@push('styles') | |
<link href="https://unpkg.com/browse/[email protected]/dist/tribute.css" rel="stylesheet" /> | |
@endpush | |
@push('scripts') | |
<script src="https://unpkg.com/browse/[email protected]/dist/tribute.min.js"></script> | |
@endpush | |
@endonce |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to implement this?