Skip to content

Commit

Permalink
fix: do not lose dashboard widget focus in shadow root (#8304)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki authored Dec 10, 2024
1 parent 27422d6 commit ceded83
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/dashboard/src/vaadin-dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class Dashboard extends DashboardLayoutMixin(ElementMixin(ThemableMixin(PolylitM
// Update the wrapper style
this.__updateWrapper(wrapper, item);

if (!wrapper.contains(document.activeElement)) {
if (wrapper !== focusedWrapper) {
if (previousWrapper) {
// Append the wrapper after the previous one if it's not already there
if (wrapper.previousElementSibling !== previousWrapper) {
Expand Down
19 changes: 19 additions & 0 deletions packages/dashboard/test/dashboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,25 @@ describe('dashboard', () => {
expect(document.activeElement).to.equal(getElementFromCell(dashboard, 0, 0)!);
});

it('should not lose focus when removing items inside shadow root', async () => {
// Move the dashboard to a shadow root
const wrapper = fixtureSync('<div></div>');
const shadow = wrapper.attachShadow({ mode: 'open' });
shadow.appendChild(dashboard);
await nextFrame();

// Focus the second widget
const secondWidget = dashboard.querySelectorAll('vaadin-dashboard-widget')[1];
secondWidget.focus();

// Remove the first widget
dashboard.items = [dashboard.items[1]];
await nextFrame();

// Expect the second widget to remain focused
expect(shadow.activeElement).to.equal(secondWidget);
});

it('should not lose focus when reassigning section items', async () => {
dashboard.items = [{ title: 'Section', items: [{ id: '0' }] }, { id: '1' }];
await nextFrame();
Expand Down

0 comments on commit ceded83

Please sign in to comment.