Skip to content

Accordion-item focus behavior inside Accordion #6618

@YonatanKra

Description

@YonatanKra

In Accordion, focusing on the child sets the private property activeItemIndex to the focused item's index.
I've opened a PR to simplify the code (#6616 ), but then noticed some edge cases that could be troubling.
Because it sets the current active index, if I'm in single mode and change the items in the accordion it will open the focused item regardless of the state before the addition.

Here's a unit test that shows the issue:

it('should set focused element as the active item', async function () {
			function triggerAccordionUpdate() {
				const newItem = document.createElement('vwc-accordion-item') as AccordionItem;
				element.appendChild(newItem);
			}

			accordion.expandmode = 'single';
			toggleAccordionItem(accordionItem2);
			const item2ExpandedBeforeFocus = accordionItem2.expanded;
			const item1ExpandedBeforeFocus = accordionItem1.expanded;

			accordionItem1.dispatchEvent(new FocusEvent('focus'));
			triggerAccordionUpdate();

			expect(item1ExpandedBeforeFocus).toBeFalsy();
			expect(item2ExpandedBeforeFocus).toBeTruthy();

			expect(accordionItem1.expanded).toBeTruthy();
			expect(accordionItem2.expanded).toBeFalsy();
		});

The activeItemIndex is used in the focusItem method that's supposed to set the focus to the current active item but it is also used to determine the current expanded item in setItems:

this.activeItemIndex !== index
                            ? (item.expanded = false)
                            : (item.expanded = true);

Is this the wanted behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions