Skip to content

Commit 9cdc8dd

Browse files
committed
refactor(aria/menu): use write in afterRenderEffect when setting focus and use computed for patterns
1 parent cd56cbc commit 9cdc8dd

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

src/aria/menu/menu-bar.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class MenuBar<V> {
104104
readonly _pattern: MenuBarPattern<V>;
105105

106106
/** The menu items as a writable signal. */
107-
private readonly _itemPatterns = signal<any[]>([]);
107+
private readonly _itemPatterns = computed(() => this._items().map(i => i._pattern));
108108

109109
/** A callback function triggered when a menu item is selected. */
110110
readonly itemSelected = output<V>();
@@ -123,10 +123,6 @@ export class MenuBar<V> {
123123
element: computed(() => this._elementRef.nativeElement),
124124
});
125125

126-
afterRenderEffect(() => {
127-
this._itemPatterns.set(this._items().map(i => i._pattern));
128-
});
129-
130126
afterRenderEffect(() => {
131127
this._pattern.setDefaultStateEffect();
132128
});

src/aria/menu/menu.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,13 @@ export class Menu<V> {
176176
// Focuses an active menu item when the menu becomes visible. This is needed to
177177
// properly restore focus to the active item when returning to a menu, and to
178178
// focus the first item when navigating into a submenu with hover.
179-
afterRenderEffect(() => {
180-
if (this._pattern.visible()) {
181-
const activeItem = untracked(() => this._pattern.inputs.activeItem());
182-
this._pattern.listBehavior.goto(activeItem!);
183-
}
179+
afterRenderEffect({
180+
write: () => {
181+
if (this.visible()) {
182+
const activeItem = untracked(() => this._pattern.inputs.activeItem());
183+
this._pattern.listBehavior.goto(activeItem!);
184+
}
185+
},
184186
});
185187

186188
afterRenderEffect(() => {

0 commit comments

Comments
 (0)