Skip to content

Commit 5712bcd

Browse files
authored
Mentions allow custom loading message (#601)
* Add custom loading message to mentions * update readme
1 parent 6429307 commit 5712bcd

File tree

7 files changed

+76
-46
lines changed

7 files changed

+76
-46
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,9 @@ TiptapEditor::make(name: 'content')
645645
// Set a custom placeholder message. Note: if you set a placeholder, then it will ONLY show suggestions when the query is not empty.
646646
->mentionItemsPlaceholder("Search for users...")
647647

648+
// Set a custom loading message. This will be displayed instead of a loading spinner.
649+
->mentionItemsLoading("Loading...")
650+
648651
// Customize how many mention items should be shown at once, 8 by default. Is nullable and only works with static suggestions.
649652
->maxMentionItems()
650653

resources/dist/filament-tiptap-editor.js

Lines changed: 41 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/extensions/Mention/Mention.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export const CustomMention = Mention.extend({
156156
props,
157157
this.options.emptyMentionItemsMessage,
158158
this.options.mentionItemsPlaceholder,
159+
this.options.mentionItemsLoading,
159160
_query
160161
)
161162
if (!props.clientRect) {

resources/js/extensions/Mention/get-content.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
export default (props, emptyMentionItemsMessage, mentionItemsPlaceholder, initialQuery) => {
1+
export default (props, emptyMentionItemsMessage, mentionItemsPlaceholder, mentionItemsLoading, initialQuery) => {
22
Alpine.data('suggestions', () => ({
33
items: props.items,
44
query: initialQuery,
55
selectedIndex: 0,
66
emptyMentionItemsMessage: emptyMentionItemsMessage,
77
mentionItemsPlaceholder: mentionItemsPlaceholder,
8+
mentionItemsLoading: mentionItemsLoading,
89
isLoading: !mentionItemsPlaceholder,
910
init() {
1011
this.$watch('items', () => {
@@ -55,14 +56,16 @@ export default (props, emptyMentionItemsMessage, mentionItemsPlaceholder, initia
5556
}));
5657
return `
5758
<div class="mention-dropdown" x-data=suggestions x-bind="rootEvents">
58-
5959
<!-- Loading spinner -->
60-
<template x-if="isLoading && !(mentionItemsPlaceholder && !query.length)">
61-
<div class="px-2 py-1">
60+
<template x-if="isLoading && ! (mentionItemsPlaceholder && !query.length)">
61+
<div>
62+
<div class="px-2 py-1" x-show="!mentionItemsLoading">
6263
<svg class="animate-spin size-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
63-
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
64-
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
65-
</svg>
64+
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
65+
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
66+
</svg>
67+
</div>
68+
<p x-text="mentionItemsLoading"></p>
6669
</div>
6770
</template>
6871

resources/js/plugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export default function tiptap({
168168
mentionItems = null,
169169
emptyMentionItemsMessage = '',
170170
mentionItemsPlaceholder = null,
171+
mentionItemsLoading = null,
171172
maxMentionItems = null,
172173
mentionTrigger = '@',
173174
livewireId,
@@ -227,6 +228,7 @@ export default function tiptap({
227228
mentionItems,
228229
emptyMentionItemsMessage,
229230
mentionItemsPlaceholder,
231+
mentionItemsLoading,
230232
maxMentionItems,
231233
mentionTrigger,
232234
livewireId,

resources/views/tiptap-editor.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
$mentionItems = $getMentionItems();
1616
$emptyMentionItemsMessage = $getEmptyMentionItemsMessage();
1717
$mentionItemsPlaceholder = $getMentionItemsPlaceholder();
18+
$mentionItemsLoading = $getMentionItemsLoading();
1819
$getMentionItemsUsingEnabled = $getMentionItemsUsingEnabled();
1920
$maxMentionItems = $getMaxMentionItems();
2021
$mentionTrigger = $getMentionTrigger();
@@ -63,6 +64,7 @@ class="relative z-0 tiptap-wrapper rounded-md bg-white dark:bg-gray-900 focus-wi
6364
mentionItems: @js($mentionItems),
6465
emptyMentionItemsMessage: @js($emptyMentionItemsMessage),
6566
mentionItemsPlaceholder: @js($mentionItemsPlaceholder),
67+
mentionItemsLoading: @js($mentionItemsLoading),
6668
maxMentionItems: @js($maxMentionItems),
6769
mentionTrigger: @js($mentionTrigger),
6870
livewireId: @js($this->getId()),

src/Concerns/HasMentions.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ trait HasMentions
1515

1616
protected string | Closure | null $mentionItemsPlaceholder = null;
1717

18+
protected string | Closure | null $mentionItemsLoading = null;
19+
1820
protected int | Closure | null $maxMentionItems = 8;
1921

2022
protected ?Closure $getMentionItemsUsing = null;
@@ -139,6 +141,21 @@ public function getMentionItemsPlaceholder(): ?string
139141
return $this->evaluate($this->mentionItemsPlaceholder);
140142
}
141143

144+
/**
145+
* Set the message to display in the loading suggestions state when the trigger character is typed and input is provided.
146+
*/
147+
public function mentionItemsLoading(string | Closure | null $message): static
148+
{
149+
$this->mentionItemsLoading = $message;
150+
151+
return $this;
152+
}
153+
154+
public function getMentionItemsLoading(): ?string
155+
{
156+
return $this->evaluate($this->mentionItemsLoading);
157+
}
158+
142159
public function getMentionItemsUsing(?Closure $callback): static
143160
{
144161
$this->getMentionItemsUsing = $callback;

0 commit comments

Comments
 (0)