Skip to content

Commit

Permalink
fix(select)!: don't reflect selected attribute
Browse files Browse the repository at this point in the history
Why? This is needed for selects in forms, which uses the `selected` attribute to indicate default selected values when resetting.

BREAKING CHANGE: `option.selected` no longer reflects. Set the attribute instead if relying on the attribute for styles/queries.

PiperOrigin-RevId: 565212026
  • Loading branch information
asyncLiz authored and copybara-github committed Sep 14, 2023
1 parent 6cca5d6 commit 573caae
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
6 changes: 2 additions & 4 deletions menu/internal/menuitem/_menu-item.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@
);
}

.list-item {
:host([selected]) & {
background-color: var(--_selected-container-color);
}
.list-item.selected {
background-color: var(--_selected-container-color);
}

// Set the ripple opacity to 0 if there is a submenu that is hovered.
Expand Down
2 changes: 1 addition & 1 deletion menu/internal/menuitem/forced-colors-styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// Show double border only when selected, and the current list item does not
// have a focus ring on it.
:host([selected]) .list-item:not(.has-focus-ring)::before {
.list-item.selected:not(.has-focus-ring)::before {
content: '';
position: absolute;
inset: 0;
Expand Down
8 changes: 6 additions & 2 deletions menu/internal/submenuitem/sub-menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ export class SubMenuItem extends MenuItemEl {
}

protected override getRenderClasses() {
return {...super.getRenderClasses(), 'submenu-hover': this.submenuHover};
return {
...super.getRenderClasses(),
'submenu-hover': this.submenuHover,
selected: this.selected
};
}

/**
Expand Down Expand Up @@ -370,4 +374,4 @@ function callIfEventNotBubbledThroughMenu(
// invoke the callback because we have not run into a submenu.
await callback(event);
};
}
}
9 changes: 8 additions & 1 deletion select/internal/selectoption/select-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class SelectOptionEl extends MenuItemEl implements SelectOption {
/**
* Whether or not this option is selected.
*/
@property({type: Boolean, reflect: true}) selected = false;
@property({type: Boolean}) selected = false;

override readonly type: ListItemRole = 'option';

Expand Down Expand Up @@ -65,4 +65,11 @@ export class SelectOptionEl extends MenuItemEl implements SelectOption {
}
}
}

protected override getRenderClasses() {
return {
...super.getRenderClasses(),
selected: this.selected,
};
}
}

0 comments on commit 573caae

Please sign in to comment.