Skip to content

Commit 4e0cf4c

Browse files
authored
Close popup when checked item is clicked.
We rely on change to commit the click, but change event won't happen if the item is already checked so shortcutting in that case to commit directly and close the menu.
1 parent 03d0c3d commit 4e0cf4c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ function shouldCommit(details: Element, menu: DetailsMenuElement, event: Event)
174174

175175
if (event.type === 'click') {
176176
const menuitem = target.closest('[role="menuitem"], [role="menuitemradio"]')
177-
const onlyCommitOnChangeEvent = menuitem && menuitem.tagName === 'LABEL' && menuitem.querySelector('input')
177+
const input = menuitem?.querySelector('input')
178+
// An input inside a label will be committed as a change event (we assume it's a radio input),
179+
// unless the input is already checked, so we need to commit on click (to close the popup)
180+
const onlyCommitOnChangeEvent = menuitem?.tagName === 'LABEL' && input && !input.checked
178181
if (menuitem && !onlyCommitOnChangeEvent) {
179182
commit(menuitem, details)
180183
}

0 commit comments

Comments
 (0)