Autofocus flux:editor in modal #1483
-
Hey guys! Did anyone manage to get focus / autofocus working on the Flux Editor? Im also mounting the editor with data and that works fine, but I want it to put the focus in the beginning of the editor, not after the mounted data - since im using the flux editor for replying to messages etc. Here is my current setup (without any of the code I tried to get focus working):
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
@LuciusAB Have you tried a little Alpine magic? This works for me: <?php
use Flux\Flux;
use Livewire\Volt\Component;
new class extends Component
{
public string $content = 'Testing...';
};
?>
<div x-init="$nextTick(() => { $refs.my-editor.editor.commands.focus('start') })">
<flux:input label="Test Input" />
<flux:editor x-ref="my-editor" wire:model="content" label="Test Editor" />
</div> |
Beta Was this translation helpful? Give feedback.
-
and if you want the cursor at the end, just change it to See: https://tiptap.dev/docs/editor/api/commands/selection/focus#use-the-focus-command |
Beta Was this translation helpful? Give feedback.
@LuciusAB Another method would be to use Alpine's
x-intersect
(https://alpinejs.dev/plugins/intersect) to trigger a piece of code when the modal becomes visible (a nod to @ju5t for the great idea! see: #760 (comment)) - I think I prefer this method for it's cleaner/simpler code: