Respect's avatar

Why search input inside custom slot not working when typing , if moved out the custom slot will working

  • hello Why search input inside custom slot not working when typing , if moved out the custom slot will working
    // index page code & app-admin.blade.php layout code below

// app-admin.blade.php code

0 likes
2 replies
imranbru's avatar
imranbru
Best Answer
Level 4

This happens because of how Livewire scopes its DOM tree. When you push the search input into <x-slot name="rightSection">, Blade extracts that code and renders it outside of your Livewire component's root <div> in the app-admin.blade.php layout. Livewire only tracks and hydrates wire:model bindings that live inside its root element (which is what gets rendered in {{ $slot }}). Because the named slot is injected elsewhere in the DOM, Livewire's JavaScript simply can't see it.

Keep the input physically inside your Livewire component's default slot, but "teleport" it visually to the header.

Add an ID to the target container in your app-admin.blade.php layout:

<div id="right-section-container" class="flex justify-center items-center gap-2">
    {{ $rightSection ?? '' }}
</div>

In your Livewire component, remove the search input from the <x-slot> and use <template x-teleport="..."> inside your main default slot:

<div>
    <x-slot name="title">...</x-slot>
    <x-slot name="breadcrumbs">...</x-slot>
    <x-slot name="rightSection">
        <x-buttons.create href="#" x-data="{}" x-on:click="$dispatch('open-modal', { name: 'create-job-title' })" :name="__('app.create_job_title')" />
        <x-dropdowns.per-page />
    </x-slot>

    <template x-teleport="#right-section-container">
        <x-inputs.search wire:model.live="search" />
    </template>

    </div>

This keeps the input inside Livewire's component state/scope while rendering it exactly where you want in the layout UI.

1 like
Respect's avatar

which method is better for preformance because my app is erp system 1 - using x-teleport 2 - using $disptach event for example

// on search input 
x-on:input="$dispatch('search-input-change ',{value : $event.target.value})"
// in traits

    #[On('search-input-change')]
    public function onSearchInputChange($value)
    {
        $this->search = $value;

            $this->resetPage();
    }


Please or to participate in this conversation.