Keep open menus and rich tooltips open through a Livewire render

<x-menu>, <x-fab-menu> and <x-rich-tooltip> gave their popover an id
that is new with every render, and Livewire's morph matches an element
without a wire:key by its id. So every render of the component around
them swapped the popover for a closed copy: an open menu closed, its
listeners stayed behind on the old element (Escape or a press outside
then left aria-expanded="true" on the trigger and focus unreturned), a
keep-open item's wire:click closed its menu when the response came, and
a rich tooltip went on showing the detached bubble, so it never opened
again. <x-carousel>'s row had the same kind of id: after a render it
scrolled without its listeners, and its items stopped re-masking.

The popover, the bubble and the row now carry a wire:key, so the morph
patches them in place and changes the id and anchor name together with
the trigger's, as it already did for everything else. The key goes
through an attribute bag: Livewire compiles a wire:key written in a
template into the key of the loop iteration around it, which would have
given every child component after the menu in a row the same key.

A morph also removes the menu button's ARIA attributes, which only
script writes. menu.js now writes them again after every morph, so the
button of a menu that stays open, and a FAB menu's close look, still
say it is open, and aria-controls names the popover's new id.

A second press on an open menu's button opened it again, render or not:
the popover closes on the press, and the guard against the click that
follows was timed from the toggle event, which is queued and arrives
after that click. It is timed from beforetoggle now.

Browser tests with Livewire probes in Chromium, Firefox and WebKit, and
a render test for the keys and the keys of the child components after.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RHoXZSHc8gGpZjFmA5fPc2
This commit is contained in:
Andreas Reinhold / reini
2026-09-13 18:50:14 +02:00
co-authored by Claude Opus 5
parent ab7317faae
commit 8640d815c8
10 changed files with 405 additions and 21 deletions
@@ -0,0 +1,48 @@
<?php
use Livewire\Component;
use Livewire\Livewire;
/** The child component in each row of `KeyedRowsProbe`. */
class KeyedRowChildProbe extends Component
{
public function render(): string
{
return '<p data-row-child>Row</p>';
}
}
/**
* A list whose rows each hold `$markup`, then a child component. Livewire keys a child from the
* loop around it, and takes a `wire:key` written in any template rendered in the row for the row's
* key: a component that wrote its own that way would key every row's child alike.
*/
class KeyedRowsProbe extends Component
{
public static string $markup = '';
public function render(): string
{
return '<div>@foreach ([1, 2] as $row)<div wire:key="row-{{ $row }}">'.static::$markup.'<livewire:keyed-row-child-probe /></div>@endforeach</div>';
}
}
it('keys what a morph must patch in place, without keying the child components after it', function (string $markup, string $key) {
Livewire::component('keyed-row-child-probe', KeyedRowChildProbe::class);
Livewire::component('keyed-rows-probe', KeyedRowsProbe::class);
KeyedRowsProbe::$markup = $markup;
$html = Livewire::test('keyed-rows-probe')->html();
preg_match_all('/<p\b[^>]*\bwire:key="([^"]+)"[^>]*>Row<\/p>/', $html, $children);
expect(substr_count($html, "wire:key=\"{$key}\""))->toBe(2)
->and($children[1])->toHaveCount(2)
->and(array_unique($children[1]))->toHaveCount(2);
})->with([
'menu' => ['<x-menu label="Row actions"><x-slot:trigger><button>More</button></x-slot:trigger><x-menu-item label="Delete" /></x-menu>', 'material-menu'],
'FAB menu' => ['<x-fab-menu label="New"><x-fab-menu-item label="Upload" /></x-fab-menu>', 'material-fab-menu'],
'rich tooltip' => ['<x-rich-tooltip text="Details"><button>i</button></x-rich-tooltip>', 'material-rich-tooltip'],
'carousel' => ['<x-carousel label="Photos"><x-carousel-item><div class="size-full"></div></x-carousel-item></x-carousel>', 'material-carousel'],
]);