Skip to content

Commit 28d0dd1

Browse files
committed
fix the menu, preserve action identity when lazy items reorder
1 parent d8daf98 commit 28d0dd1

4 files changed

Lines changed: 102 additions & 29 deletions

File tree

‎package-lock.json‎

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

‎src/components/menu/Menu.test.ts‎

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ describe('solid-ui-menu syncItems', () => {
2828
await menu.updateComplete
2929

3030
// Children should be assigned slot attributes
31-
expect(child1.getAttribute('slot')).toBe('menu-item-0')
32-
expect(child2.getAttribute('slot')).toBe('menu-item-1')
31+
expect(child1.getAttribute('slot')).toBe('menu-item-index-0')
32+
expect(child2.getAttribute('slot')).toBe('menu-item-index-1')
3333

3434
// Trigger should keep its slot
3535
const trigger = menu.querySelector('[slot="trigger"]')
@@ -49,7 +49,7 @@ describe('solid-ui-menu syncItems', () => {
4949
await menu.updateComplete
5050

5151
// First sync assigned slots
52-
expect(child1.getAttribute('slot')).toBe('menu-item-0')
52+
expect(child1.getAttribute('slot')).toBe('menu-item-index-0')
5353

5454
// Trigger a re-sync by adding another child (MutationObserver)
5555
const child2 = document.createElement('div')
@@ -58,8 +58,8 @@ describe('solid-ui-menu syncItems', () => {
5858
await menu.updateComplete
5959

6060
// Both children should still have valid slot assignments
61-
expect(child1.getAttribute('slot')).toBe('menu-item-0')
62-
expect(child2.getAttribute('slot')).toBe('menu-item-1')
61+
expect(child1.getAttribute('slot')).toBe('menu-item-index-0')
62+
expect(child2.getAttribute('slot')).toBe('menu-item-index-1')
6363

6464
document.body.removeChild(menu)
6565
})
@@ -73,12 +73,60 @@ describe('solid-ui-menu syncItems', () => {
7373
menu.appendChild(child)
7474
await menu.updateComplete
7575

76-
expect(child.getAttribute('slot')).toBe('menu-item-0')
76+
expect(child.getAttribute('slot')).toBe('menu-item-index-0')
7777

7878
// Trigger should not have been re-assigned
7979
const trigger = menu.querySelector('[slot="trigger"]')
8080
expect(trigger?.getAttribute('slot')).toBe('trigger')
8181

8282
document.body.removeChild(menu)
8383
})
84+
85+
it('keeps keyed and positional slots distinct', async () => {
86+
const menu = createMenu()
87+
88+
const keyedChild = document.createElement('div')
89+
keyedChild.dataset.menuKey = '1'
90+
menu.appendChild(keyedChild)
91+
92+
const positionalChild = document.createElement('div')
93+
menu.appendChild(positionalChild)
94+
95+
document.body.appendChild(menu)
96+
await menu.updateComplete
97+
98+
expect(keyedChild.getAttribute('slot')).toBe('menu-item-key-1')
99+
expect(positionalChild.getAttribute('slot')).toBe('menu-item-index-1')
100+
101+
document.body.removeChild(menu)
102+
})
103+
104+
it('moves keyed dropdown items instead of recreating them when actions reorder', async () => {
105+
const menu = createMenu()
106+
const first = document.createElement('div')
107+
first.dataset.menuKey = 'first'
108+
const second = document.createElement('div')
109+
second.dataset.menuKey = 'second'
110+
menu.append(first, second)
111+
112+
document.body.appendChild(menu)
113+
await menu.updateComplete
114+
115+
const firstWrapper = Array.from(menu.shadowRoot?.querySelectorAll('wa-dropdown-item') || [])
116+
.find(item => item.children[0]?.getAttribute('name') === 'menu-item-key-first')
117+
const secondWrapper = Array.from(menu.shadowRoot?.querySelectorAll('wa-dropdown-item') || [])
118+
.find(item => item.children[0]?.getAttribute('name') === 'menu-item-key-second')
119+
expect(firstWrapper).toBeTruthy()
120+
expect(secondWrapper).toBeTruthy()
121+
122+
menu.insertBefore(second, first)
123+
await new Promise(resolve => setTimeout(resolve, 0))
124+
await menu.updateComplete
125+
126+
const reorderedWrappers = Array.from(menu.shadowRoot?.querySelectorAll('wa-dropdown-item') || [])
127+
expect(reorderedWrappers[0]).toBe(secondWrapper)
128+
expect(reorderedWrappers[1]).toBe(firstWrapper)
129+
130+
document.body.removeChild(menu)
131+
})
84132
})

‎src/components/menu/Menu.ts‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { customElement, WebComponent } from '@/lib/components'
22
import { html } from 'lit'
33
import { property, query, state } from 'lit/decorators.js'
4+
import { repeat } from 'lit/directives/repeat.js'
45
import type WaDropdown from '@awesome.me/webawesome/dist/components/dropdown/dropdown.js'
56
import type WaDropdownItem from '@awesome.me/webawesome/dist/components/dropdown-item/dropdown-item.js'
67
import type { WaDropdownSelectEvent } from '@awesome.me/webawesome'
@@ -26,6 +27,7 @@ export default class Menu extends WebComponent {
2627
@state()
2728
private accessor items: { slot: string; }[] = []
2829

30+
private slotToRestoreFocus: string | undefined
2931
private observer: MutationObserver = new MutationObserver(() => this.syncItems())
3032

3133
connectedCallback () {
@@ -50,25 +52,33 @@ export default class Menu extends WebComponent {
5052
>
5153
<slot name="trigger" slot="trigger"></slot>
5254
53-
${this.items.map(
54-
(item) =>
55-
html`<wa-dropdown-item @click=${this.onItemClick}>
56-
<slot name=${item.slot}></slot>
57-
</wa-dropdown-item>`
55+
${repeat(
56+
this.items,
57+
item => item.slot,
58+
item => html`<wa-dropdown-item @click=${this.onItemClick}>
59+
<slot name=${item.slot}></slot>
60+
</wa-dropdown-item>`
5861
)}
5962
</wa-dropdown>
6063
`
6164
}
6265

6366
private syncItems (): void {
67+
const activeElement = this.shadowRoot?.activeElement as WaDropdownItem | null
68+
const activeSlot = activeElement?.children[0]?.getAttribute('name')
69+
if (activeSlot) this.slotToRestoreFocus = activeSlot
70+
6471
// Exclude only the trigger element; include all other children
6572
// regardless of whether they already have a slot assigned.
6673
const items = Array.from(this.children).filter(
6774
(child) => child.getAttribute('slot') !== 'trigger'
6875
)
6976

7077
this.items = items.map((item, index) => {
71-
const slotName = `menu-item-${index}`
78+
const key = item.getAttribute('data-menu-key')
79+
const slotName = key
80+
? `menu-item-key-${encodeURIComponent(key)}`
81+
: `menu-item-index-${index}`
7282

7383
if (item.getAttribute('slot') !== slotName) {
7484
item.setAttribute('slot', slotName)
@@ -78,6 +88,16 @@ export default class Menu extends WebComponent {
7888
})
7989
}
8090

91+
protected updated (): void {
92+
if (!this.slotToRestoreFocus) return
93+
94+
const slotToRestore = this.slotToRestoreFocus
95+
this.slotToRestoreFocus = undefined
96+
const item = Array.from(this.shadowRoot?.querySelectorAll<WaDropdownItem>('wa-dropdown-item') || [])
97+
.find(candidate => candidate.children[0]?.getAttribute('name') === slotToRestore)
98+
item?.focus({ preventScroll: true })
99+
}
100+
81101
private onItemClick (event: Event) {
82102
const waItem = event.currentTarget as WaDropdownItem
83103

‎src/components/resource-actions-menu/ResourceActionsMenu.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { customElement, WebComponent } from '@/lib/components'
22
import { property } from 'lit/decorators.js'
33
import { html, nothing } from 'lit'
4+
import { keyed } from 'lit/directives/keyed.js'
45
import '@/components/button'
56
import '@/components/menu'
67
import '@/components/menu-item'
@@ -13,6 +14,7 @@ import styles from './ResourceActionsMenu.styles.css'
1314
import { isContainerSubject } from '@/utils/podUtils'
1415

1516
type ResourceActionMenuItem = {
17+
key?: string
1618
label: string
1719
icon?: HTMLElement | string
1820
action?: (event: Event) => void
@@ -68,14 +70,17 @@ export default class ResourceActionsMenu extends WebComponent {
6870
<icon-lucide-ellipsis-vertical slot="icon" class="ellipsisIcon"></icon-lucide-ellipsis-vertical>
6971
<span class="sr-only">More options</span>
7072
</solid-ui-button>
71-
${actionableMenuItems.map(item => html`
72-
<solid-ui-menu-item @solid-ui-select=${(event: Event) => item.action(event)}>
73+
${actionableMenuItems.map(item => keyed(item.key ?? item.label, html`
74+
<solid-ui-menu-item
75+
data-menu-key=${item.key ?? nothing}
76+
@solid-ui-select=${(event: Event) => item.action(event)}
77+
>
7378
${typeof item.icon === 'string'
7479
? html`<img slot="left-icon" src=${item.icon} alt="">`
7580
: item.icon ?? html`<icon-lucide-file slot="left-icon" aria-hidden="true"></icon-lucide-file>`}
7681
${item.label}
7782
</solid-ui-menu-item>
78-
`)}
83+
`))}
7984
${canEdit
8085
? html`
8186
<solid-ui-menu-item @solid-ui-select=${this.handleEditingClick}>

0 commit comments

Comments
 (0)